blob: bd6feb9fc762b4646663f1da8b4b6f5a3c7f0f39 [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
Romain Guy3b748a42013-04-17 18:54:38 -070017#include <utils/JenkinsHash.h>
Romain Guyf7f93552010-07-08 19:17:03 -070018#include <utils/Log.h>
Romain Guyf7f93552010-07-08 19:17:03 -070019
Romain Guy3b748a42013-04-17 18:54:38 -070020#include "Caches.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040021#include "Patch.h"
Romain Guyf7f93552010-07-08 19:17:03 -070022#include "PatchCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070023#include "Properties.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080024#include "renderstate/RenderState.h"
Romain Guyf7f93552010-07-08 19:17:03 -070025
26namespace android {
27namespace uirenderer {
28
29///////////////////////////////////////////////////////////////////////////////
30// Constructors/destructor
31///////////////////////////////////////////////////////////////////////////////
32
Chris Craik96a5c4c2015-01-27 15:46:35 -080033PatchCache::PatchCache(RenderState& renderState)
34 : mRenderState(renderState)
Chris Craik48a8f432016-02-05 15:59:29 -080035 , mMaxSize(Properties::patchCacheSize)
Chris Craik96a5c4c2015-01-27 15:46:35 -080036 , mSize(0)
37 , mCache(LruCache<PatchDescription, Patch*>::kUnlimitedCapacity)
38 , mMeshBuffer(0)
39 , mFreeBlocks(nullptr)
Chris Craik48a8f432016-02-05 15:59:29 -080040 , mGenerationId(0) {}
Romain Guyf7f93552010-07-08 19:17:03 -070041
42PatchCache::~PatchCache() {
43 clear();
44}
45
Chris Craik96a5c4c2015-01-27 15:46:35 -080046void PatchCache::init() {
Romain Guy7d9b1b32013-05-28 14:25:09 -070047 bool created = false;
48 if (!mMeshBuffer) {
49 glGenBuffers(1, &mMeshBuffer);
50 created = true;
51 }
52
Chris Craik96a5c4c2015-01-27 15:46:35 -080053 mRenderState.meshState().bindMeshBuffer(mMeshBuffer);
54 mRenderState.meshState().resetVertexPointers();
Romain Guy3b748a42013-04-17 18:54:38 -070055
Romain Guy7d9b1b32013-05-28 14:25:09 -070056 if (created) {
Romain Guy4c2547f2013-06-11 16:19:24 -070057 createVertexBuffer();
Romain Guy7d9b1b32013-05-28 14:25:09 -070058 }
Romain Guy3b748a42013-04-17 18:54:38 -070059}
60
Romain Guyf7f93552010-07-08 19:17:03 -070061///////////////////////////////////////////////////////////////////////////////
Romain Guyf7f93552010-07-08 19:17:03 -070062// Caching
63///////////////////////////////////////////////////////////////////////////////
64
Romain Guy3b748a42013-04-17 18:54:38 -070065hash_t PatchCache::PatchDescription::hash() const {
66 uint32_t hash = JenkinsHashMix(0, android::hash_type(mPatch));
67 hash = JenkinsHashMix(hash, mBitmapWidth);
68 hash = JenkinsHashMix(hash, mBitmapHeight);
69 hash = JenkinsHashMix(hash, mPixelWidth);
70 hash = JenkinsHashMix(hash, mPixelHeight);
71 return JenkinsHashWhiten(hash);
72}
Romain Guy13ba0052013-02-15 12:47:26 -080073
Romain Guy3b748a42013-04-17 18:54:38 -070074int PatchCache::PatchDescription::compare(const PatchCache::PatchDescription& lhs,
75 const PatchCache::PatchDescription& rhs) {
76 return memcmp(&lhs, &rhs, sizeof(PatchDescription));
Romain Guy13ba0052013-02-15 12:47:26 -080077}
78
Romain Guyf7f93552010-07-08 19:17:03 -070079void PatchCache::clear() {
Romain Guy3b748a42013-04-17 18:54:38 -070080 clearCache();
Romain Guy7d9b1b32013-05-28 14:25:09 -070081
82 if (mMeshBuffer) {
Chris Craik96a5c4c2015-01-27 15:46:35 -080083 mRenderState.meshState().unbindMeshBuffer();
Romain Guy7d9b1b32013-05-28 14:25:09 -070084 glDeleteBuffers(1, &mMeshBuffer);
85 mMeshBuffer = 0;
86 mSize = 0;
87 }
Romain Guy3b748a42013-04-17 18:54:38 -070088}
89
90void PatchCache::clearCache() {
91 LruCache<PatchDescription, Patch*>::Iterator i(mCache);
92 while (i.next()) {
Romain Guy3b748a42013-04-17 18:54:38 -070093 delete i.value();
Romain Guy2728f962010-10-08 18:36:15 -070094 }
Romain Guyf7f93552010-07-08 19:17:03 -070095 mCache.clear();
Romain Guye3b0a012013-06-26 15:45:41 -070096
97 BufferBlock* block = mFreeBlocks;
98 while (block) {
99 BufferBlock* next = block->next;
100 delete block;
101 block = next;
102 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800103 mFreeBlocks = nullptr;
Romain Guye3b0a012013-06-26 15:45:41 -0700104}
105
106void PatchCache::remove(Vector<patch_pair_t>& patchesToRemove, Res_png_9patch* patch) {
107 LruCache<PatchDescription, Patch*>::Iterator i(mCache);
108 while (i.next()) {
109 const PatchDescription& key = i.key();
110 if (key.getPatch() == patch) {
111 patchesToRemove.push(patch_pair_t(&key, i.value()));
112 }
113 }
114}
115
116void PatchCache::removeDeferred(Res_png_9patch* patch) {
117 Mutex::Autolock _l(mLock);
Jens Gulin6056e102014-02-04 17:38:02 +0100118
119 // Assert that patch is not already garbage
120 size_t count = mGarbage.size();
121 for (size_t i = 0; i < count; i++) {
122 if (patch == mGarbage[i]) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800123 patch = nullptr;
Jens Gulin6056e102014-02-04 17:38:02 +0100124 break;
125 }
126 }
Chris Craikd41c4d82015-01-05 15:51:13 -0800127 LOG_ALWAYS_FATAL_IF(patch == nullptr);
Jens Gulin6056e102014-02-04 17:38:02 +0100128
Romain Guye3b0a012013-06-26 15:45:41 -0700129 mGarbage.push(patch);
130}
131
132void PatchCache::clearGarbage() {
133 Vector<patch_pair_t> patchesToRemove;
134
135 { // scope for the mutex
136 Mutex::Autolock _l(mLock);
137 size_t count = mGarbage.size();
138 for (size_t i = 0; i < count; i++) {
Sangkyu Lee36fad8f2014-01-09 14:11:57 +0900139 Res_png_9patch* patch = mGarbage[i];
140 remove(patchesToRemove, patch);
141 // A Res_png_9patch is actually an array of byte that's larger
142 // than sizeof(Res_png_9patch). It must be freed as an array.
143 delete[] (int8_t*) patch;
Romain Guye3b0a012013-06-26 15:45:41 -0700144 }
145 mGarbage.clear();
146 }
147
148 // TODO: We could sort patchesToRemove by offset to merge
149 // adjacent free blocks
150 for (size_t i = 0; i < patchesToRemove.size(); i++) {
151 const patch_pair_t& pair = patchesToRemove[i];
152
Jens Gulin6056e102014-02-04 17:38:02 +0100153 // Release the patch and mark the space in the free list
154 Patch* patch = pair.getSecond();
Chris Craik8820fd12015-03-03 14:20:47 -0800155 BufferBlock* block = new BufferBlock(patch->positionOffset, patch->getSize());
Romain Guye3b0a012013-06-26 15:45:41 -0700156 block->next = mFreeBlocks;
157 mFreeBlocks = block;
158
159 mSize -= patch->getSize();
160
161 mCache.remove(*pair.getFirst());
Jens Gulin6056e102014-02-04 17:38:02 +0100162 delete patch;
Romain Guye3b0a012013-06-26 15:45:41 -0700163 }
164
165#if DEBUG_PATCHES
166 if (patchesToRemove.size() > 0) {
167 dumpFreeBlocks("Removed garbage");
168 }
169#endif
Romain Guyf7f93552010-07-08 19:17:03 -0700170}
171
Romain Guy4c2547f2013-06-11 16:19:24 -0700172void PatchCache::createVertexBuffer() {
Chris Craikd41c4d82015-01-05 15:51:13 -0800173 glBufferData(GL_ARRAY_BUFFER, mMaxSize, nullptr, GL_DYNAMIC_DRAW);
Romain Guy4c2547f2013-06-11 16:19:24 -0700174 mSize = 0;
Romain Guye3b0a012013-06-26 15:45:41 -0700175 mFreeBlocks = new BufferBlock(0, mMaxSize);
Romain Guy4c2547f2013-06-11 16:19:24 -0700176 mGenerationId++;
177}
178
Romain Guye3b0a012013-06-26 15:45:41 -0700179/**
180 * Sets the mesh's offsets and copies its associated vertices into
181 * the mesh buffer (VBO).
182 */
Chris Craik8820fd12015-03-03 14:20:47 -0800183void PatchCache::setupMesh(Patch* newMesh) {
Romain Guye3b0a012013-06-26 15:45:41 -0700184 // This call ensures the VBO exists and that it is bound
Chris Craik96a5c4c2015-01-27 15:46:35 -0800185 init();
Romain Guye3b0a012013-06-26 15:45:41 -0700186
187 // If we're running out of space, let's clear the entire cache
188 uint32_t size = newMesh->getSize();
189 if (mSize + size > mMaxSize) {
190 clearCache();
191 createVertexBuffer();
192 }
193
194 // Find a block where we can fit the mesh
Chris Craikd41c4d82015-01-05 15:51:13 -0800195 BufferBlock* previous = nullptr;
Romain Guye3b0a012013-06-26 15:45:41 -0700196 BufferBlock* block = mFreeBlocks;
197 while (block) {
198 // The mesh fits
199 if (block->size >= size) {
200 break;
201 }
202 previous = block;
203 block = block->next;
204 }
205
206 // We have enough space left in the buffer, but it's
207 // too fragmented, let's clear the cache
208 if (!block) {
209 clearCache();
210 createVertexBuffer();
Chris Craikd41c4d82015-01-05 15:51:13 -0800211 previous = nullptr;
Romain Guye3b0a012013-06-26 15:45:41 -0700212 block = mFreeBlocks;
213 }
214
215 // Copy the 9patch mesh in the VBO
Chris Craik8820fd12015-03-03 14:20:47 -0800216 newMesh->positionOffset = (GLintptr) (block->offset);
217 newMesh->textureOffset = newMesh->positionOffset + kMeshTextureOffset;
218 glBufferSubData(GL_ARRAY_BUFFER, newMesh->positionOffset, size, newMesh->vertices.get());
Romain Guye3b0a012013-06-26 15:45:41 -0700219
220 // Remove the block since we've used it entirely
221 if (block->size == size) {
222 if (previous) {
223 previous->next = block->next;
224 } else {
225 mFreeBlocks = block->next;
226 }
Jens Gulin6056e102014-02-04 17:38:02 +0100227 delete block;
Romain Guye3b0a012013-06-26 15:45:41 -0700228 } else {
229 // Resize the block now that it's occupied
230 block->offset += size;
231 block->size -= size;
232 }
233
234 mSize += size;
235}
236
Chris Craik8820fd12015-03-03 14:20:47 -0800237static const UvMapper sIdentity;
238
Romain Guy3b748a42013-04-17 18:54:38 -0700239const Patch* PatchCache::get(const AssetAtlas::Entry* entry,
240 const uint32_t bitmapWidth, const uint32_t bitmapHeight,
241 const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch) {
Romain Guy4bb94202010-10-12 15:59:26 -0700242
Romain Guy3b748a42013-04-17 18:54:38 -0700243 const PatchDescription description(bitmapWidth, bitmapHeight, pixelWidth, pixelHeight, patch);
244 const Patch* mesh = mCache.get(description);
Romain Guy2728f962010-10-08 18:36:15 -0700245
Romain Guyf7f93552010-07-08 19:17:03 -0700246 if (!mesh) {
Chris Craik8820fd12015-03-03 14:20:47 -0800247 const UvMapper& mapper = entry ? entry->uvMapper : sIdentity;
248 Patch* newMesh = new Patch(bitmapWidth, bitmapHeight,
249 pixelWidth, pixelHeight, mapper, patch);
Romain Guy2728f962010-10-08 18:36:15 -0700250
Chris Craik8820fd12015-03-03 14:20:47 -0800251 if (newMesh->vertices) {
252 setupMesh(newMesh);
Romain Guy3b748a42013-04-17 18:54:38 -0700253 }
254
Romain Guye3b0a012013-06-26 15:45:41 -0700255#if DEBUG_PATCHES
256 dumpFreeBlocks("Adding patch");
257#endif
258
Romain Guy3b748a42013-04-17 18:54:38 -0700259 mCache.put(description, newMesh);
260 return newMesh;
Romain Guyf7f93552010-07-08 19:17:03 -0700261 }
262
263 return mesh;
264}
265
Romain Guye3b0a012013-06-26 15:45:41 -0700266#if DEBUG_PATCHES
267void PatchCache::dumpFreeBlocks(const char* prefix) {
268 String8 dump;
269 BufferBlock* block = mFreeBlocks;
270 while (block) {
Chris Craik8820fd12015-03-03 14:20:47 -0800271 dump.appendFormat("->(%d, %d)", block->positionOffset, block->size);
Romain Guye3b0a012013-06-26 15:45:41 -0700272 block = block->next;
273 }
274 ALOGD("%s: Free blocks%s", prefix, dump.string());
275}
276#endif
277
Romain Guyf7f93552010-07-08 19:17:03 -0700278}; // namespace uirenderer
279}; // namespace android