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