blob: 62e38d3dbcbe37afc5b43a2230b72e7b74976fe6 [file] [log] [blame]
Romain Guyf7f93552010-07-08 19:17:03 -07001/*
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 Guyf7f93552010-07-08 19:17:03 -070020
21#include "PatchCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070022#include "Properties.h"
Romain Guyf7f93552010-07-08 19:17:03 -070023
24namespace android {
25namespace uirenderer {
26
27///////////////////////////////////////////////////////////////////////////////
28// Constructors/destructor
29///////////////////////////////////////////////////////////////////////////////
30
Romain Guy2728f962010-10-08 18:36:15 -070031PatchCache::PatchCache(): mMaxEntries(DEFAULT_PATCH_CACHE_SIZE) {
Romain Guyfb8b7632010-08-23 21:05:08 -070032}
33
Romain Guy2728f962010-10-08 18:36:15 -070034PatchCache::PatchCache(uint32_t maxEntries): mMaxEntries(maxEntries) {
Romain Guyf7f93552010-07-08 19:17:03 -070035}
36
37PatchCache::~PatchCache() {
38 clear();
39}
40
41///////////////////////////////////////////////////////////////////////////////
Romain Guyf7f93552010-07-08 19:17:03 -070042// Caching
43///////////////////////////////////////////////////////////////////////////////
44
Romain Guy13ba0052013-02-15 12:47:26 -080045int PatchCache::PatchDescription::compare(
46 const PatchCache::PatchDescription& lhs, const PatchCache::PatchDescription& rhs) {
47 int deltaInt = lhs.bitmapWidth - rhs.bitmapWidth;
48 if (deltaInt != 0) return deltaInt;
49
50 deltaInt = lhs.bitmapHeight - rhs.bitmapHeight;
51 if (deltaInt != 0) return deltaInt;
52
53 if (lhs.pixelWidth < rhs.pixelWidth) return -1;
Chris Craik9935abc2013-02-15 19:33:43 -080054 if (lhs.pixelWidth > rhs.pixelWidth) return +1;
Romain Guy13ba0052013-02-15 12:47:26 -080055
56 if (lhs.pixelHeight < rhs.pixelHeight) return -1;
Chris Craik9935abc2013-02-15 19:33:43 -080057 if (lhs.pixelHeight > rhs.pixelHeight) return +1;
Romain Guy13ba0052013-02-15 12:47:26 -080058
59 deltaInt = lhs.xCount - rhs.xCount;
60 if (deltaInt != 0) return deltaInt;
61
62 deltaInt = lhs.yCount - rhs.yCount;
63 if (deltaInt != 0) return deltaInt;
64
65 deltaInt = lhs.emptyCount - rhs.emptyCount;
66 if (deltaInt != 0) return deltaInt;
67
68 deltaInt = lhs.colorKey - rhs.colorKey;
69 if (deltaInt != 0) return deltaInt;
70
71 return 0;
72}
73
Romain Guyf7f93552010-07-08 19:17:03 -070074void PatchCache::clear() {
Romain Guy2728f962010-10-08 18:36:15 -070075 size_t count = mCache.size();
Romain Guy4bb94202010-10-12 15:59:26 -070076 for (size_t i = 0; i < count; i++) {
Romain Guy2728f962010-10-08 18:36:15 -070077 delete mCache.valueAt(i);
78 }
Romain Guyf7f93552010-07-08 19:17:03 -070079 mCache.clear();
Romain Guyf7f93552010-07-08 19:17:03 -070080}
81
Romain Guy13ba0052013-02-15 12:47:26 -080082Patch* PatchCache::get(const uint32_t bitmapWidth, const uint32_t bitmapHeight,
Romain Guy2728f962010-10-08 18:36:15 -070083 const float pixelWidth, const float pixelHeight,
Romain Guy4bb94202010-10-12 15:59:26 -070084 const int32_t* xDivs, const int32_t* yDivs, const uint32_t* colors,
85 const uint32_t width, const uint32_t height, const int8_t numColors) {
86
87 int8_t transparentQuads = 0;
88 uint32_t colorKey = 0;
89
90 if (uint8_t(numColors) < sizeof(uint32_t) * 4) {
91 for (int8_t i = 0; i < numColors; i++) {
92 if (colors[i] == 0x0) {
93 transparentQuads++;
94 colorKey |= 0x1 << i;
95 }
96 }
97 }
Romain Guyf7f93552010-07-08 19:17:03 -070098
Romain Guy054dc182010-10-15 17:55:25 -070099 // If the 9patch is made of only transparent quads
Romain Guy03750a02010-10-18 14:06:08 -0700100 if (transparentQuads == int8_t((width + 1) * (height + 1))) {
Romain Guy054dc182010-10-15 17:55:25 -0700101 return NULL;
102 }
103
Romain Guy2728f962010-10-08 18:36:15 -0700104 const PatchDescription description(bitmapWidth, bitmapHeight,
Romain Guy4bb94202010-10-12 15:59:26 -0700105 pixelWidth, pixelHeight, width, height, transparentQuads, colorKey);
Romain Guy2728f962010-10-08 18:36:15 -0700106
107 ssize_t index = mCache.indexOfKey(description);
108 Patch* mesh = NULL;
109 if (index >= 0) {
110 mesh = mCache.valueAt(index);
111 }
112
Romain Guyf7f93552010-07-08 19:17:03 -0700113 if (!mesh) {
Romain Guy4bb94202010-10-12 15:59:26 -0700114 PATCH_LOGD("New patch mesh "
115 "xCount=%d yCount=%d, w=%.2f h=%.2f, bw=%.2f bh=%.2f",
116 width, height, pixelWidth, pixelHeight, bitmapWidth, bitmapHeight);
Romain Guy2728f962010-10-08 18:36:15 -0700117
Romain Guy4bb94202010-10-12 15:59:26 -0700118 mesh = new Patch(width, height, transparentQuads);
Romain Guy6f72beb2010-11-30 12:04:14 -0800119 mesh->updateColorKey(colorKey);
120 mesh->copy(xDivs, yDivs);
121 mesh->updateVertices(bitmapWidth, bitmapHeight, 0.0f, 0.0f, pixelWidth, pixelHeight);
Romain Guy2728f962010-10-08 18:36:15 -0700122
123 if (mCache.size() >= mMaxEntries) {
Romain Guy21b028a2010-10-08 18:43:58 -0700124 delete mCache.valueAt(mCache.size() - 1);
125 mCache.removeItemsAt(mCache.size() - 1, 1);
Romain Guy2728f962010-10-08 18:36:15 -0700126 }
127
128 mCache.add(description, mesh);
Romain Guy5341cea2013-01-09 14:15:58 -0800129 } else if (!mesh->matches(xDivs, yDivs, colorKey, transparentQuads)) {
Romain Guy6f72beb2010-11-30 12:04:14 -0800130 PATCH_LOGD("Patch mesh does not match, refreshing vertices");
131 mesh->updateVertices(bitmapWidth, bitmapHeight, 0.0f, 0.0f, pixelWidth, pixelHeight);
Romain Guyf7f93552010-07-08 19:17:03 -0700132 }
133
134 return mesh;
135}
136
137}; // namespace uirenderer
138}; // namespace android