Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "OpenGLRenderer" |
| 18 | |
| 19 | #include <utils/Log.h> |
| 20 | #include <utils/ResourceTypes.h> |
| 21 | |
| 22 | #include "PatchCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 23 | #include "Properties.h" |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace uirenderer { |
| 27 | |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | // Constructors/destructor |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame^] | 32 | PatchCache::PatchCache(): mMaxEntries(DEFAULT_PATCH_CACHE_SIZE) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame^] | 35 | PatchCache::PatchCache(uint32_t maxEntries): mMaxEntries(maxEntries) { |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | PatchCache::~PatchCache() { |
| 39 | clear(); |
| 40 | } |
| 41 | |
| 42 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 43 | // Caching |
| 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | |
| 46 | void PatchCache::clear() { |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame^] | 47 | size_t count = mCache.size(); |
| 48 | for (int i = 0; i < count; i++) { |
| 49 | delete mCache.valueAt(i); |
| 50 | } |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 51 | mCache.clear(); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame^] | 54 | Patch* PatchCache::get(const float bitmapWidth, const float bitmapHeight, |
| 55 | const float pixelWidth, const float pixelHeight, |
| 56 | const int32_t* xDivs, const int32_t* yDivs, |
| 57 | const uint32_t width, const uint32_t height) { |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 58 | |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame^] | 59 | const PatchDescription description(bitmapWidth, bitmapHeight, |
| 60 | pixelWidth, pixelHeight, width, height); |
| 61 | |
| 62 | ssize_t index = mCache.indexOfKey(description); |
| 63 | Patch* mesh = NULL; |
| 64 | if (index >= 0) { |
| 65 | mesh = mCache.valueAt(index); |
| 66 | } |
| 67 | |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 68 | if (!mesh) { |
| 69 | PATCH_LOGD("Creating new patch mesh, w=%d h=%d", width, height); |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame^] | 70 | |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 71 | mesh = new Patch(width, height); |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame^] | 72 | mesh->updateVertices(bitmapWidth, bitmapHeight, 0.0f, 0.0f, |
| 73 | pixelWidth, pixelHeight, xDivs, yDivs, width, height); |
| 74 | |
| 75 | if (mCache.size() >= mMaxEntries) { |
| 76 | delete mCache.valueAt(0); |
| 77 | mCache.removeItemsAt(0, 1); |
| 78 | } |
| 79 | |
| 80 | mCache.add(description, mesh); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | return mesh; |
| 84 | } |
| 85 | |
| 86 | }; // namespace uirenderer |
| 87 | }; // namespace android |