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