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 | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_PATCH_CACHE_H |
| 18 | #define ANDROID_HWUI_PATCH_CACHE_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 <GLES2/gl2.h> |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 21 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 22 | #include <utils/LruCache.h> |
| 23 | |
| 24 | #include <androidfw/ResourceTypes.h> |
| 25 | |
| 26 | #include "AssetAtlas.h" |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 27 | #include "Debug.h" |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 28 | #include "utils/Pair.h" |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 33 | class Patch; |
| 34 | |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | // Defines |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
| 39 | // Debug |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 40 | #if DEBUG_PATCHES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 41 | #define PATCH_LOGD(...) ALOGD(__VA_ARGS__) |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 42 | #else |
| 43 | #define PATCH_LOGD(...) |
| 44 | #endif |
| 45 | |
| 46 | /////////////////////////////////////////////////////////////////////////////// |
| 47 | // Cache |
| 48 | /////////////////////////////////////////////////////////////////////////////// |
| 49 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 50 | class Caches; |
| 51 | |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 52 | class PatchCache { |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 53 | public: |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 54 | PatchCache(RenderState& renderState); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 55 | ~PatchCache(); |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 56 | void init(); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 57 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 58 | const Patch* get(const AssetAtlas::Entry* entry, |
| 59 | const uint32_t bitmapWidth, const uint32_t bitmapHeight, |
| 60 | const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 61 | void clear(); |
| 62 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 63 | uint32_t getSize() const { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 64 | return mSize; |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | uint32_t getMaxSize() const { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 68 | return mMaxSize; |
| 69 | } |
| 70 | |
| 71 | GLuint getMeshBuffer() const { |
| 72 | return mMeshBuffer; |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Romain Guy | 4c2547f | 2013-06-11 16:19:24 -0700 | [diff] [blame] | 75 | uint32_t getGenerationId() const { |
| 76 | return mGenerationId; |
| 77 | } |
| 78 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Removes the entries associated with the specified 9-patch. This is meant |
| 81 | * to be called from threads that are not the EGL context thread (GC thread |
| 82 | * on the VM side for instance.) |
| 83 | */ |
| 84 | void removeDeferred(Res_png_9patch* patch); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 85 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Process deferred removals. |
| 88 | */ |
| 89 | void clearGarbage(); |
| 90 | |
| 91 | |
| 92 | private: |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 93 | struct PatchDescription { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 94 | PatchDescription(): mPatch(nullptr), mBitmapWidth(0), mBitmapHeight(0), |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 95 | mPixelWidth(0), mPixelHeight(0) { |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Romain Guy | 13ba005 | 2013-02-15 12:47:26 -0800 | [diff] [blame] | 98 | PatchDescription(const uint32_t bitmapWidth, const uint32_t bitmapHeight, |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 99 | const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch): |
| 100 | mPatch(patch), mBitmapWidth(bitmapWidth), mBitmapHeight(bitmapHeight), |
| 101 | mPixelWidth(pixelWidth), mPixelHeight(pixelHeight) { |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 104 | hash_t hash() const; |
| 105 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 106 | const Res_png_9patch* getPatch() const { return mPatch; } |
| 107 | |
Romain Guy | 13ba005 | 2013-02-15 12:47:26 -0800 | [diff] [blame] | 108 | static int compare(const PatchDescription& lhs, const PatchDescription& rhs); |
| 109 | |
| 110 | bool operator==(const PatchDescription& other) const { |
| 111 | return compare(*this, other) == 0; |
| 112 | } |
| 113 | |
| 114 | bool operator!=(const PatchDescription& other) const { |
| 115 | return compare(*this, other) != 0; |
| 116 | } |
| 117 | |
| 118 | friend inline int strictly_order_type(const PatchDescription& lhs, |
| 119 | const PatchDescription& rhs) { |
| 120 | return PatchDescription::compare(lhs, rhs) < 0; |
| 121 | } |
| 122 | |
| 123 | friend inline int compare_type(const PatchDescription& lhs, |
| 124 | const PatchDescription& rhs) { |
| 125 | return PatchDescription::compare(lhs, rhs); |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 128 | friend inline hash_t hash_type(const PatchDescription& entry) { |
| 129 | return entry.hash(); |
| 130 | } |
| 131 | |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 132 | private: |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 133 | const Res_png_9patch* mPatch; |
| 134 | uint32_t mBitmapWidth; |
| 135 | uint32_t mBitmapHeight; |
| 136 | float mPixelWidth; |
| 137 | float mPixelHeight; |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 138 | |
| 139 | }; // struct PatchDescription |
| 140 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 141 | /** |
| 142 | * A buffer block represents an empty range in the mesh buffer |
| 143 | * that can be used to store vertices. |
| 144 | * |
| 145 | * The patch cache maintains a linked-list of buffer blocks |
| 146 | * to track available regions of memory in the VBO. |
| 147 | */ |
| 148 | struct BufferBlock { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 149 | BufferBlock(uint32_t offset, uint32_t size): offset(offset), size(size), next(nullptr) { |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | uint32_t offset; |
| 153 | uint32_t size; |
| 154 | |
| 155 | BufferBlock* next; |
| 156 | }; // struct BufferBlock |
| 157 | |
| 158 | typedef Pair<const PatchDescription*, Patch*> patch_pair_t; |
| 159 | |
| 160 | void clearCache(); |
| 161 | void createVertexBuffer(); |
| 162 | |
Chris Craik | 8820fd1 | 2015-03-03 14:20:47 -0800 | [diff] [blame] | 163 | void setupMesh(Patch* newMesh); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 164 | |
| 165 | void remove(Vector<patch_pair_t>& patchesToRemove, Res_png_9patch* patch); |
| 166 | |
| 167 | #if DEBUG_PATCHES |
| 168 | void dumpFreeBlocks(const char* prefix); |
| 169 | #endif |
| 170 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 171 | RenderState& mRenderState; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 172 | uint32_t mMaxSize; |
| 173 | uint32_t mSize; |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 174 | |
Romain Guy | 4c2547f | 2013-06-11 16:19:24 -0700 | [diff] [blame] | 175 | LruCache<PatchDescription, Patch*> mCache; |
| 176 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 177 | GLuint mMeshBuffer; |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 178 | // First available free block inside the mesh buffer |
| 179 | BufferBlock* mFreeBlocks; |
Romain Guy | 7d9b1b3 | 2013-05-28 14:25:09 -0700 | [diff] [blame] | 180 | |
Romain Guy | 4c2547f | 2013-06-11 16:19:24 -0700 | [diff] [blame] | 181 | uint32_t mGenerationId; |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 182 | |
| 183 | // Garbage tracking, required to handle GC events on the VM side |
| 184 | Vector<Res_png_9patch*> mGarbage; |
| 185 | mutable Mutex mLock; |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 186 | }; // class PatchCache |
| 187 | |
| 188 | }; // namespace uirenderer |
| 189 | }; // namespace android |
| 190 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 191 | #endif // ANDROID_HWUI_PATCH_CACHE_H |