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