blob: aa746c7dfa154c97a622a211583c1df47510ae61 [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
Chris Craik5e00c7c2016-07-06 16:10:09 -070017#pragma once
Romain Guyf7f93552010-07-08 19:17:03 -070018
Romain Guy3b748a42013-04-17 18:54:38 -070019#include <GLES2/gl2.h>
Romain Guy2728f962010-10-08 18:36:15 -070020
Romain Guy3b748a42013-04-17 18:54:38 -070021#include <utils/LruCache.h>
22
23#include <androidfw/ResourceTypes.h>
24
Romain Guyc15008e2010-11-10 11:59:15 -080025#include "Debug.h"
Romain Guye3b0a012013-06-26 15:45:41 -070026#include "utils/Pair.h"
Romain Guyf7f93552010-07-08 19:17:03 -070027
28namespace android {
29namespace uirenderer {
30
Tom Hudson2dc236b2014-10-15 15:46:42 -040031class Patch;
32
Romain Guyf7f93552010-07-08 19:17:03 -070033///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
37// Debug
Romain Guyf7f93552010-07-08 19:17:03 -070038#if DEBUG_PATCHES
Steve Block5baa3a62011-12-20 16:23:08 +000039 #define PATCH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guyf7f93552010-07-08 19:17:03 -070040#else
41 #define PATCH_LOGD(...)
42#endif
43
44///////////////////////////////////////////////////////////////////////////////
45// Cache
46///////////////////////////////////////////////////////////////////////////////
47
Romain Guy3b748a42013-04-17 18:54:38 -070048class Caches;
Chris Craik5e00c7c2016-07-06 16:10:09 -070049class RenderState;
Romain Guy3b748a42013-04-17 18:54:38 -070050
Romain Guy2728f962010-10-08 18:36:15 -070051class PatchCache {
Romain Guyf7f93552010-07-08 19:17:03 -070052public:
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -070053 explicit PatchCache(RenderState& renderState);
Romain Guyf7f93552010-07-08 19:17:03 -070054 ~PatchCache();
55
Romain Guy253f2c22016-09-28 17:34:42 -070056 const Patch* get(const uint32_t bitmapWidth, const uint32_t bitmapHeight,
Romain Guy3b748a42013-04-17 18:54:38 -070057 const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch);
Romain Guyf7f93552010-07-08 19:17:03 -070058 void clear();
59
Romain Guyc15008e2010-11-10 11:59:15 -080060 uint32_t getSize() const {
Romain Guy3b748a42013-04-17 18:54:38 -070061 return mSize;
Romain Guyc15008e2010-11-10 11:59:15 -080062 }
63
64 uint32_t getMaxSize() const {
Romain Guy3b748a42013-04-17 18:54:38 -070065 return mMaxSize;
66 }
67
68 GLuint getMeshBuffer() const {
69 return mMeshBuffer;
Romain Guyc15008e2010-11-10 11:59:15 -080070 }
71
Romain Guye3b0a012013-06-26 15:45:41 -070072 /**
73 * Removes the entries associated with the specified 9-patch. This is meant
74 * to be called from threads that are not the EGL context thread (GC thread
75 * on the VM side for instance.)
76 */
77 void removeDeferred(Res_png_9patch* patch);
Romain Guy3b748a42013-04-17 18:54:38 -070078
Romain Guye3b0a012013-06-26 15:45:41 -070079 /**
80 * Process deferred removals.
81 */
82 void clearGarbage();
83
84
85private:
Romain Guy6f72beb2010-11-30 12:04:14 -080086 struct PatchDescription {
Chris Craike84a2082014-12-22 14:28:49 -080087 PatchDescription(): mPatch(nullptr), mBitmapWidth(0), mBitmapHeight(0),
Romain Guy3b748a42013-04-17 18:54:38 -070088 mPixelWidth(0), mPixelHeight(0) {
Romain Guy6f72beb2010-11-30 12:04:14 -080089 }
90
Romain Guy13ba0052013-02-15 12:47:26 -080091 PatchDescription(const uint32_t bitmapWidth, const uint32_t bitmapHeight,
Romain Guy3b748a42013-04-17 18:54:38 -070092 const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch):
93 mPatch(patch), mBitmapWidth(bitmapWidth), mBitmapHeight(bitmapHeight),
94 mPixelWidth(pixelWidth), mPixelHeight(pixelHeight) {
Romain Guy6f72beb2010-11-30 12:04:14 -080095 }
96
Romain Guy3b748a42013-04-17 18:54:38 -070097 hash_t hash() const;
98
Romain Guye3b0a012013-06-26 15:45:41 -070099 const Res_png_9patch* getPatch() const { return mPatch; }
100
Romain Guy13ba0052013-02-15 12:47:26 -0800101 static int compare(const PatchDescription& lhs, const PatchDescription& rhs);
102
103 bool operator==(const PatchDescription& other) const {
104 return compare(*this, other) == 0;
105 }
106
107 bool operator!=(const PatchDescription& other) const {
108 return compare(*this, other) != 0;
109 }
110
111 friend inline int strictly_order_type(const PatchDescription& lhs,
112 const PatchDescription& rhs) {
113 return PatchDescription::compare(lhs, rhs) < 0;
114 }
115
116 friend inline int compare_type(const PatchDescription& lhs,
117 const PatchDescription& rhs) {
118 return PatchDescription::compare(lhs, rhs);
Romain Guy6f72beb2010-11-30 12:04:14 -0800119 }
120
Romain Guy3b748a42013-04-17 18:54:38 -0700121 friend inline hash_t hash_type(const PatchDescription& entry) {
122 return entry.hash();
123 }
124
Romain Guy6f72beb2010-11-30 12:04:14 -0800125 private:
Romain Guy3b748a42013-04-17 18:54:38 -0700126 const Res_png_9patch* mPatch;
127 uint32_t mBitmapWidth;
128 uint32_t mBitmapHeight;
129 float mPixelWidth;
130 float mPixelHeight;
Romain Guy6f72beb2010-11-30 12:04:14 -0800131
132 }; // struct PatchDescription
133
Romain Guye3b0a012013-06-26 15:45:41 -0700134 /**
135 * A buffer block represents an empty range in the mesh buffer
136 * that can be used to store vertices.
137 *
138 * The patch cache maintains a linked-list of buffer blocks
139 * to track available regions of memory in the VBO.
140 */
141 struct BufferBlock {
Chris Craike84a2082014-12-22 14:28:49 -0800142 BufferBlock(uint32_t offset, uint32_t size): offset(offset), size(size), next(nullptr) {
Romain Guye3b0a012013-06-26 15:45:41 -0700143 }
144
145 uint32_t offset;
146 uint32_t size;
147
148 BufferBlock* next;
149 }; // struct BufferBlock
150
151 typedef Pair<const PatchDescription*, Patch*> patch_pair_t;
152
153 void clearCache();
154 void createVertexBuffer();
155
Chris Craik8820fd12015-03-03 14:20:47 -0800156 void setupMesh(Patch* newMesh);
Romain Guye3b0a012013-06-26 15:45:41 -0700157
158 void remove(Vector<patch_pair_t>& patchesToRemove, Res_png_9patch* patch);
159
160#if DEBUG_PATCHES
161 void dumpFreeBlocks(const char* prefix);
162#endif
163
Chris Craik96a5c4c2015-01-27 15:46:35 -0800164 RenderState& mRenderState;
Chris Craik48a8f432016-02-05 15:59:29 -0800165 const uint32_t mMaxSize;
Romain Guy3b748a42013-04-17 18:54:38 -0700166 uint32_t mSize;
Romain Guy6f72beb2010-11-30 12:04:14 -0800167
Romain Guy4c2547f2013-06-11 16:19:24 -0700168 LruCache<PatchDescription, Patch*> mCache;
169
Romain Guy3b748a42013-04-17 18:54:38 -0700170 GLuint mMeshBuffer;
Romain Guye3b0a012013-06-26 15:45:41 -0700171 // First available free block inside the mesh buffer
172 BufferBlock* mFreeBlocks;
Romain Guy7d9b1b32013-05-28 14:25:09 -0700173
Romain Guye3b0a012013-06-26 15:45:41 -0700174 // Garbage tracking, required to handle GC events on the VM side
175 Vector<Res_png_9patch*> mGarbage;
176 mutable Mutex mLock;
Romain Guyf7f93552010-07-08 19:17:03 -0700177}; // class PatchCache
178
179}; // namespace uirenderer
180}; // namespace android