blob: 387f79acf0ec57e12202523575e8ed6e2d1226c8 [file] [log] [blame]
Romain Guyf7f93552010-07-08 19:17:03 -07001/*
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 Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_PATCH_CACHE_H
18#define ANDROID_HWUI_PATCH_CACHE_H
Romain Guyf7f93552010-07-08 19:17:03 -070019
Romain Guy3b748a42013-04-17 18:54:38 -070020#include <GLES2/gl2.h>
Romain Guy2728f962010-10-08 18:36:15 -070021
Romain Guy3b748a42013-04-17 18:54:38 -070022#include <utils/LruCache.h>
23
24#include <androidfw/ResourceTypes.h>
25
26#include "AssetAtlas.h"
Romain Guyc15008e2010-11-10 11:59:15 -080027#include "Debug.h"
Romain Guye3b0a012013-06-26 15:45:41 -070028#include "utils/Pair.h"
Romain Guyf7f93552010-07-08 19:17:03 -070029
30namespace android {
31namespace uirenderer {
32
Tom Hudson2dc236b2014-10-15 15:46:42 -040033class Patch;
34
Romain Guyf7f93552010-07-08 19:17:03 -070035///////////////////////////////////////////////////////////////////////////////
36// Defines
37///////////////////////////////////////////////////////////////////////////////
38
39// Debug
Romain Guyf7f93552010-07-08 19:17:03 -070040#if DEBUG_PATCHES
Steve Block5baa3a62011-12-20 16:23:08 +000041 #define PATCH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guyf7f93552010-07-08 19:17:03 -070042#else
43 #define PATCH_LOGD(...)
44#endif
45
46///////////////////////////////////////////////////////////////////////////////
47// Cache
48///////////////////////////////////////////////////////////////////////////////
49
Romain Guy3b748a42013-04-17 18:54:38 -070050class Caches;
51
Romain Guy2728f962010-10-08 18:36:15 -070052class PatchCache {
Romain Guyf7f93552010-07-08 19:17:03 -070053public:
Chris Craik96a5c4c2015-01-27 15:46:35 -080054 PatchCache(RenderState& renderState);
Romain Guyf7f93552010-07-08 19:17:03 -070055 ~PatchCache();
Chris Craik96a5c4c2015-01-27 15:46:35 -080056 void init();
Romain Guyf7f93552010-07-08 19:17:03 -070057
Romain Guy3b748a42013-04-17 18:54:38 -070058 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 Guyf7f93552010-07-08 19:17:03 -070061 void clear();
62
Romain Guyc15008e2010-11-10 11:59:15 -080063 uint32_t getSize() const {
Romain Guy3b748a42013-04-17 18:54:38 -070064 return mSize;
Romain Guyc15008e2010-11-10 11:59:15 -080065 }
66
67 uint32_t getMaxSize() const {
Romain Guy3b748a42013-04-17 18:54:38 -070068 return mMaxSize;
69 }
70
71 GLuint getMeshBuffer() const {
72 return mMeshBuffer;
Romain Guyc15008e2010-11-10 11:59:15 -080073 }
74
Romain Guy4c2547f2013-06-11 16:19:24 -070075 uint32_t getGenerationId() const {
76 return mGenerationId;
77 }
78
Romain Guye3b0a012013-06-26 15:45:41 -070079 /**
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 Guy3b748a42013-04-17 18:54:38 -070085
Romain Guye3b0a012013-06-26 15:45:41 -070086 /**
87 * Process deferred removals.
88 */
89 void clearGarbage();
90
91
92private:
Romain Guy6f72beb2010-11-30 12:04:14 -080093 struct PatchDescription {
Chris Craike84a2082014-12-22 14:28:49 -080094 PatchDescription(): mPatch(nullptr), mBitmapWidth(0), mBitmapHeight(0),
Romain Guy3b748a42013-04-17 18:54:38 -070095 mPixelWidth(0), mPixelHeight(0) {
Romain Guy6f72beb2010-11-30 12:04:14 -080096 }
97
Romain Guy13ba0052013-02-15 12:47:26 -080098 PatchDescription(const uint32_t bitmapWidth, const uint32_t bitmapHeight,
Romain Guy3b748a42013-04-17 18:54:38 -070099 const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch):
100 mPatch(patch), mBitmapWidth(bitmapWidth), mBitmapHeight(bitmapHeight),
101 mPixelWidth(pixelWidth), mPixelHeight(pixelHeight) {
Romain Guy6f72beb2010-11-30 12:04:14 -0800102 }
103
Romain Guy3b748a42013-04-17 18:54:38 -0700104 hash_t hash() const;
105
Romain Guye3b0a012013-06-26 15:45:41 -0700106 const Res_png_9patch* getPatch() const { return mPatch; }
107
Romain Guy13ba0052013-02-15 12:47:26 -0800108 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 Guy6f72beb2010-11-30 12:04:14 -0800126 }
127
Romain Guy3b748a42013-04-17 18:54:38 -0700128 friend inline hash_t hash_type(const PatchDescription& entry) {
129 return entry.hash();
130 }
131
Romain Guy6f72beb2010-11-30 12:04:14 -0800132 private:
Romain Guy3b748a42013-04-17 18:54:38 -0700133 const Res_png_9patch* mPatch;
134 uint32_t mBitmapWidth;
135 uint32_t mBitmapHeight;
136 float mPixelWidth;
137 float mPixelHeight;
Romain Guy6f72beb2010-11-30 12:04:14 -0800138
139 }; // struct PatchDescription
140
Romain Guye3b0a012013-06-26 15:45:41 -0700141 /**
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 Craike84a2082014-12-22 14:28:49 -0800149 BufferBlock(uint32_t offset, uint32_t size): offset(offset), size(size), next(nullptr) {
Romain Guye3b0a012013-06-26 15:45:41 -0700150 }
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 Craik8820fd12015-03-03 14:20:47 -0800163 void setupMesh(Patch* newMesh);
Romain Guye3b0a012013-06-26 15:45:41 -0700164
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 Craik96a5c4c2015-01-27 15:46:35 -0800171 RenderState& mRenderState;
Romain Guy3b748a42013-04-17 18:54:38 -0700172 uint32_t mMaxSize;
173 uint32_t mSize;
Romain Guy6f72beb2010-11-30 12:04:14 -0800174
Romain Guy4c2547f2013-06-11 16:19:24 -0700175 LruCache<PatchDescription, Patch*> mCache;
176
Romain Guy3b748a42013-04-17 18:54:38 -0700177 GLuint mMeshBuffer;
Romain Guye3b0a012013-06-26 15:45:41 -0700178 // First available free block inside the mesh buffer
179 BufferBlock* mFreeBlocks;
Romain Guy7d9b1b32013-05-28 14:25:09 -0700180
Romain Guy4c2547f2013-06-11 16:19:24 -0700181 uint32_t mGenerationId;
Romain Guye3b0a012013-06-26 15:45:41 -0700182
183 // Garbage tracking, required to handle GC events on the VM side
184 Vector<Res_png_9patch*> mGarbage;
185 mutable Mutex mLock;
Romain Guyf7f93552010-07-08 19:17:03 -0700186}; // class PatchCache
187
188}; // namespace uirenderer
189}; // namespace android
190
Romain Guy5b3b3522010-10-27 18:57:51 -0700191#endif // ANDROID_HWUI_PATCH_CACHE_H