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> |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 20 | |
| 21 | #include "PatchCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 22 | #include "Properties.h" |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace uirenderer { |
| 26 | |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | // Constructors/destructor |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 31 | PatchCache::PatchCache(): mMaxEntries(DEFAULT_PATCH_CACHE_SIZE) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 34 | PatchCache::PatchCache(uint32_t maxEntries): mMaxEntries(maxEntries) { |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | PatchCache::~PatchCache() { |
| 38 | clear(); |
| 39 | } |
| 40 | |
| 41 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 42 | // Caching |
| 43 | /////////////////////////////////////////////////////////////////////////////// |
| 44 | |
| 45 | void PatchCache::clear() { |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 46 | size_t count = mCache.size(); |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 47 | for (size_t i = 0; i < count; i++) { |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 48 | delete mCache.valueAt(i); |
| 49 | } |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 50 | mCache.clear(); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 53 | Patch* PatchCache::get(const float bitmapWidth, const float bitmapHeight, |
| 54 | const float pixelWidth, const float pixelHeight, |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 55 | const int32_t* xDivs, const int32_t* yDivs, const uint32_t* colors, |
| 56 | const uint32_t width, const uint32_t height, const int8_t numColors) { |
| 57 | |
| 58 | int8_t transparentQuads = 0; |
| 59 | uint32_t colorKey = 0; |
| 60 | |
| 61 | if (uint8_t(numColors) < sizeof(uint32_t) * 4) { |
| 62 | for (int8_t i = 0; i < numColors; i++) { |
| 63 | if (colors[i] == 0x0) { |
| 64 | transparentQuads++; |
| 65 | colorKey |= 0x1 << i; |
| 66 | } |
| 67 | } |
| 68 | } |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 69 | |
Romain Guy | 054dc18 | 2010-10-15 17:55:25 -0700 | [diff] [blame] | 70 | // If the 9patch is made of only transparent quads |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 71 | if (transparentQuads == int8_t((width + 1) * (height + 1))) { |
Romain Guy | 054dc18 | 2010-10-15 17:55:25 -0700 | [diff] [blame] | 72 | return NULL; |
| 73 | } |
| 74 | |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 75 | const PatchDescription description(bitmapWidth, bitmapHeight, |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 76 | pixelWidth, pixelHeight, width, height, transparentQuads, colorKey); |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 77 | |
| 78 | ssize_t index = mCache.indexOfKey(description); |
| 79 | Patch* mesh = NULL; |
| 80 | if (index >= 0) { |
| 81 | mesh = mCache.valueAt(index); |
| 82 | } |
| 83 | |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 84 | if (!mesh) { |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 85 | PATCH_LOGD("New patch mesh " |
| 86 | "xCount=%d yCount=%d, w=%.2f h=%.2f, bw=%.2f bh=%.2f", |
| 87 | width, height, pixelWidth, pixelHeight, bitmapWidth, bitmapHeight); |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 88 | |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 89 | mesh = new Patch(width, height, transparentQuads); |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 90 | mesh->updateColorKey(colorKey); |
| 91 | mesh->copy(xDivs, yDivs); |
| 92 | mesh->updateVertices(bitmapWidth, bitmapHeight, 0.0f, 0.0f, pixelWidth, pixelHeight); |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 93 | |
| 94 | if (mCache.size() >= mMaxEntries) { |
Romain Guy | 21b028a | 2010-10-08 18:43:58 -0700 | [diff] [blame] | 95 | delete mCache.valueAt(mCache.size() - 1); |
| 96 | mCache.removeItemsAt(mCache.size() - 1, 1); |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | mCache.add(description, mesh); |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 100 | } else if (!mesh->matches(xDivs, yDivs, colorKey)) { |
| 101 | PATCH_LOGD("Patch mesh does not match, refreshing vertices"); |
| 102 | mesh->updateVertices(bitmapWidth, bitmapHeight, 0.0f, 0.0f, pixelWidth, pixelHeight); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | return mesh; |
| 106 | } |
| 107 | |
| 108 | }; // namespace uirenderer |
| 109 | }; // namespace android |