1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "OpenGLRenderer"
#include <utils/JenkinsHash.h>
#include <utils/Log.h>
#include "Caches.h"
#include "Patch.h"
#include "PatchCache.h"
#include "Properties.h"
#include "renderstate/RenderState.h"
namespace android {
namespace uirenderer {
///////////////////////////////////////////////////////////////////////////////
// Constructors/destructor
///////////////////////////////////////////////////////////////////////////////
PatchCache::PatchCache(RenderState& renderState)
: mRenderState(renderState)
, mSize(0)
, mCache(LruCache<PatchDescription, Patch*>::kUnlimitedCapacity)
, mMeshBuffer(0)
, mFreeBlocks(nullptr)
, mGenerationId(0) {
char property[PROPERTY_VALUE_MAX];
if (property_get(PROPERTY_PATCH_CACHE_SIZE, property, nullptr) > 0) {
INIT_LOGD(" Setting patch cache size to %skB", property);
mMaxSize = KB(atoi(property));
} else {
INIT_LOGD(" Using default patch cache size of %.2fkB", DEFAULT_PATCH_CACHE_SIZE);
mMaxSize = KB(DEFAULT_PATCH_CACHE_SIZE);
}
}
PatchCache::~PatchCache() {
clear();
}
void PatchCache::init() {
bool created = false;
if (!mMeshBuffer) {
glGenBuffers(1, &mMeshBuffer);
created = true;
}
mRenderState.meshState().bindMeshBuffer(mMeshBuffer);
mRenderState.meshState().resetVertexPointers();
if (created) {
createVertexBuffer();
}
}
///////////////////////////////////////////////////////////////////////////////
// Caching
///////////////////////////////////////////////////////////////////////////////
hash_t PatchCache::PatchDescription::hash() const {
uint32_t hash = JenkinsHashMix(0, android::hash_type(mPatch));
hash = JenkinsHashMix(hash, mBitmapWidth);
hash = JenkinsHashMix(hash, mBitmapHeight);
hash = JenkinsHashMix(hash, mPixelWidth);
hash = JenkinsHashMix(hash, mPixelHeight);
return JenkinsHashWhiten(hash);
}
int PatchCache::PatchDescription::compare(const PatchCache::PatchDescription& lhs,
const PatchCache::PatchDescription& rhs) {
return memcmp(&lhs, &rhs, sizeof(PatchDescription));
}
void PatchCache::clear() {
clearCache();
if (mMeshBuffer) {
mRenderState.meshState().unbindMeshBuffer();
glDeleteBuffers(1, &mMeshBuffer);
mMeshBuffer = 0;
mSize = 0;
}
}
void PatchCache::clearCache() {
LruCache<PatchDescription, Patch*>::Iterator i(mCache);
while (i.next()) {
delete i.value();
}
mCache.clear();
BufferBlock* block = mFreeBlocks;
while (block) {
BufferBlock* next = block->next;
delete block;
block = next;
}
mFreeBlocks = nullptr;
}
void PatchCache::remove(Vector<patch_pair_t>& patchesToRemove, Res_png_9patch* patch) {
LruCache<PatchDescription, Patch*>::Iterator i(mCache);
while (i.next()) {
const PatchDescription& key = i.key();
if (key.getPatch() == patch) {
patchesToRemove.push(patch_pair_t(&key, i.value()));
}
}
}
void PatchCache::removeDeferred(Res_png_9patch* patch) {
Mutex::Autolock _l(mLock);
// Assert that patch is not already garbage
size_t count = mGarbage.size();
for (size_t i = 0; i < count; i++) {
if (patch == mGarbage[i]) {
patch = nullptr;
break;
}
}
LOG_ALWAYS_FATAL_IF(patch == nullptr);
mGarbage.push(patch);
}
void PatchCache::clearGarbage() {
Vector<patch_pair_t> patchesToRemove;
{ // scope for the mutex
Mutex::Autolock _l(mLock);
size_t count = mGarbage.size();
for (size_t i = 0; i < count; i++) {
Res_png_9patch* patch = mGarbage[i];
remove(patchesToRemove, patch);
// A Res_png_9patch is actually an array of byte that's larger
// than sizeof(Res_png_9patch). It must be freed as an array.
delete[] (int8_t*) patch;
}
mGarbage.clear();
}
// TODO: We could sort patchesToRemove by offset to merge
// adjacent free blocks
for (size_t i = 0; i < patchesToRemove.size(); i++) {
const patch_pair_t& pair = patchesToRemove[i];
// Release the patch and mark the space in the free list
Patch* patch = pair.getSecond();
BufferBlock* block = new BufferBlock(patch->offset, patch->getSize());
block->next = mFreeBlocks;
mFreeBlocks = block;
mSize -= patch->getSize();
mCache.remove(*pair.getFirst());
delete patch;
}
#if DEBUG_PATCHES
if (patchesToRemove.size() > 0) {
dumpFreeBlocks("Removed garbage");
}
#endif
}
void PatchCache::createVertexBuffer() {
glBufferData(GL_ARRAY_BUFFER, mMaxSize, nullptr, GL_DYNAMIC_DRAW);
mSize = 0;
mFreeBlocks = new BufferBlock(0, mMaxSize);
mGenerationId++;
}
/**
* Sets the mesh's offsets and copies its associated vertices into
* the mesh buffer (VBO).
*/
void PatchCache::setupMesh(Patch* newMesh, TextureVertex* vertices) {
// This call ensures the VBO exists and that it is bound
init();
// If we're running out of space, let's clear the entire cache
uint32_t size = newMesh->getSize();
if (mSize + size > mMaxSize) {
clearCache();
createVertexBuffer();
}
// Find a block where we can fit the mesh
BufferBlock* previous = nullptr;
BufferBlock* block = mFreeBlocks;
while (block) {
// The mesh fits
if (block->size >= size) {
break;
}
previous = block;
block = block->next;
}
// We have enough space left in the buffer, but it's
// too fragmented, let's clear the cache
if (!block) {
clearCache();
createVertexBuffer();
previous = nullptr;
block = mFreeBlocks;
}
// Copy the 9patch mesh in the VBO
newMesh->offset = (GLintptr) (block->offset);
newMesh->textureOffset = newMesh->offset + kMeshTextureOffset;
glBufferSubData(GL_ARRAY_BUFFER, newMesh->offset, size, vertices);
// Remove the block since we've used it entirely
if (block->size == size) {
if (previous) {
previous->next = block->next;
} else {
mFreeBlocks = block->next;
}
delete block;
} else {
// Resize the block now that it's occupied
block->offset += size;
block->size -= size;
}
mSize += size;
}
const Patch* PatchCache::get(const AssetAtlas::Entry* entry,
const uint32_t bitmapWidth, const uint32_t bitmapHeight,
const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch) {
const PatchDescription description(bitmapWidth, bitmapHeight, pixelWidth, pixelHeight, patch);
const Patch* mesh = mCache.get(description);
if (!mesh) {
Patch* newMesh = new Patch();
TextureVertex* vertices;
if (entry) {
// An atlas entry has a UV mapper
vertices = newMesh->createMesh(bitmapWidth, bitmapHeight,
pixelWidth, pixelHeight, entry->uvMapper, patch);
} else {
vertices = newMesh->createMesh(bitmapWidth, bitmapHeight,
pixelWidth, pixelHeight, patch);
}
if (vertices) {
setupMesh(newMesh, vertices);
}
#if DEBUG_PATCHES
dumpFreeBlocks("Adding patch");
#endif
mCache.put(description, newMesh);
return newMesh;
}
return mesh;
}
#if DEBUG_PATCHES
void PatchCache::dumpFreeBlocks(const char* prefix) {
String8 dump;
BufferBlock* block = mFreeBlocks;
while (block) {
dump.appendFormat("->(%d, %d)", block->offset, block->size);
block = block->next;
}
ALOGD("%s: Free blocks%s", prefix, dump.string());
}
#endif
}; // namespace uirenderer
}; // namespace android
|