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