2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_HWUI_PATCH_CACHE_H
18 #define ANDROID_HWUI_PATCH_CACHE_H
20 #include <GLES2/gl2.h>
22 #include <utils/LruCache.h>
24 #include <androidfw/ResourceTypes.h>
26 #include "AssetAtlas.h"
28 #include "utils/Pair.h"
31 namespace uirenderer {
35 ///////////////////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////////
41 #define PATCH_LOGD(...) ALOGD(__VA_ARGS__)
43 #define PATCH_LOGD(...)
46 ///////////////////////////////////////////////////////////////////////////////
48 ///////////////////////////////////////////////////////////////////////////////
54 PatchCache(RenderState& renderState);
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);
63 uint32_t getSize() const {
67 uint32_t getMaxSize() const {
71 GLuint getMeshBuffer() const {
75 uint32_t getGenerationId() const {
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.)
84 void removeDeferred(Res_png_9patch* patch);
87 * Process deferred removals.
93 struct PatchDescription {
94 PatchDescription(): mPatch(nullptr), mBitmapWidth(0), mBitmapHeight(0),
95 mPixelWidth(0), mPixelHeight(0) {
98 PatchDescription(const uint32_t bitmapWidth, const uint32_t bitmapHeight,
99 const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch):
100 mPatch(patch), mBitmapWidth(bitmapWidth), mBitmapHeight(bitmapHeight),
101 mPixelWidth(pixelWidth), mPixelHeight(pixelHeight) {
106 const Res_png_9patch* getPatch() const { return mPatch; }
108 static int compare(const PatchDescription& lhs, const PatchDescription& rhs);
110 bool operator==(const PatchDescription& other) const {
111 return compare(*this, other) == 0;
114 bool operator!=(const PatchDescription& other) const {
115 return compare(*this, other) != 0;
118 friend inline int strictly_order_type(const PatchDescription& lhs,
119 const PatchDescription& rhs) {
120 return PatchDescription::compare(lhs, rhs) < 0;
123 friend inline int compare_type(const PatchDescription& lhs,
124 const PatchDescription& rhs) {
125 return PatchDescription::compare(lhs, rhs);
128 friend inline hash_t hash_type(const PatchDescription& entry) {
133 const Res_png_9patch* mPatch;
134 uint32_t mBitmapWidth;
135 uint32_t mBitmapHeight;
139 }; // struct PatchDescription
142 * A buffer block represents an empty range in the mesh buffer
143 * that can be used to store vertices.
145 * The patch cache maintains a linked-list of buffer blocks
146 * to track available regions of memory in the VBO.
149 BufferBlock(uint32_t offset, uint32_t size): offset(offset), size(size), next(nullptr) {
156 }; // struct BufferBlock
158 typedef Pair<const PatchDescription*, Patch*> patch_pair_t;
161 void createVertexBuffer();
163 void setupMesh(Patch* newMesh);
165 void remove(Vector<patch_pair_t>& patchesToRemove, Res_png_9patch* patch);
168 void dumpFreeBlocks(const char* prefix);
171 RenderState& mRenderState;
172 const uint32_t mMaxSize;
175 LruCache<PatchDescription, Patch*> mCache;
178 // First available free block inside the mesh buffer
179 BufferBlock* mFreeBlocks;
181 uint32_t mGenerationId;
183 // Garbage tracking, required to handle GC events on the VM side
184 Vector<Res_png_9patch*> mGarbage;
186 }; // class PatchCache
188 }; // namespace uirenderer
189 }; // namespace android
191 #endif // ANDROID_HWUI_PATCH_CACHE_H