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 | |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 17 | #pragma once |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 18 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 19 | #include <GLES2/gl2.h> |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 20 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 21 | #include <utils/LruCache.h> |
| 22 | |
| 23 | #include <androidfw/ResourceTypes.h> |
| 24 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 25 | #include "Debug.h" |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 26 | #include "utils/Pair.h" |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 31 | class Patch; |
| 32 | |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 33 | /////////////////////////////////////////////////////////////////////////////// |
| 34 | // Defines |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
| 37 | // Debug |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 38 | #if DEBUG_PATCHES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 39 | #define PATCH_LOGD(...) ALOGD(__VA_ARGS__) |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 40 | #else |
| 41 | #define PATCH_LOGD(...) |
| 42 | #endif |
| 43 | |
| 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | // Cache |
| 46 | /////////////////////////////////////////////////////////////////////////////// |
| 47 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 48 | class Caches; |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 49 | class RenderState; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 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: |
Chih-Hung Hsieh | faecb78 | 2016-07-21 11:23:06 -0700 | [diff] [blame] | 53 | explicit PatchCache(RenderState& renderState); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 54 | ~PatchCache(); |
| 55 | |
Romain Guy | 253f2c2 | 2016-09-28 17:34:42 -0700 | [diff] [blame] | 56 | const Patch* get(const uint32_t bitmapWidth, const uint32_t bitmapHeight, |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 57 | const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 58 | void clear(); |
| 59 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 60 | uint32_t getSize() const { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 61 | return mSize; |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | uint32_t getMaxSize() const { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 65 | return mMaxSize; |
| 66 | } |
| 67 | |
| 68 | GLuint getMeshBuffer() const { |
| 69 | return mMeshBuffer; |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 72 | /** |
| 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 Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 78 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Process deferred removals. |
| 81 | */ |
| 82 | void clearGarbage(); |
| 83 | |
| 84 | |
| 85 | private: |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 86 | struct PatchDescription { |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 87 | PatchDescription(): mPatch(nullptr), mBitmapWidth(0), mBitmapHeight(0), |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 88 | mPixelWidth(0), mPixelHeight(0) { |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Romain Guy | 13ba005 | 2013-02-15 12:47:26 -0800 | [diff] [blame] | 91 | PatchDescription(const uint32_t bitmapWidth, const uint32_t bitmapHeight, |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 92 | const float pixelWidth, const float pixelHeight, const Res_png_9patch* patch): |
| 93 | mPatch(patch), mBitmapWidth(bitmapWidth), mBitmapHeight(bitmapHeight), |
| 94 | mPixelWidth(pixelWidth), mPixelHeight(pixelHeight) { |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 97 | hash_t hash() const; |
| 98 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 99 | const Res_png_9patch* getPatch() const { return mPatch; } |
| 100 | |
Romain Guy | 13ba005 | 2013-02-15 12:47:26 -0800 | [diff] [blame] | 101 | 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 Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 121 | friend inline hash_t hash_type(const PatchDescription& entry) { |
| 122 | return entry.hash(); |
| 123 | } |
| 124 | |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 125 | private: |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 126 | const Res_png_9patch* mPatch; |
| 127 | uint32_t mBitmapWidth; |
| 128 | uint32_t mBitmapHeight; |
| 129 | float mPixelWidth; |
| 130 | float mPixelHeight; |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 131 | |
| 132 | }; // struct PatchDescription |
| 133 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 134 | /** |
| 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 Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 142 | 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] | 143 | } |
| 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 Craik | 8820fd1 | 2015-03-03 14:20:47 -0800 | [diff] [blame] | 156 | void setupMesh(Patch* newMesh); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 157 | |
| 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 Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 164 | RenderState& mRenderState; |
Chris Craik | 48a8f43 | 2016-02-05 15:59:29 -0800 | [diff] [blame] | 165 | const uint32_t mMaxSize; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 166 | uint32_t mSize; |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 167 | |
Romain Guy | 4c2547f | 2013-06-11 16:19:24 -0700 | [diff] [blame] | 168 | LruCache<PatchDescription, Patch*> mCache; |
| 169 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 170 | GLuint mMeshBuffer; |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 171 | // First available free block inside the mesh buffer |
| 172 | BufferBlock* mFreeBlocks; |
Romain Guy | 7d9b1b3 | 2013-05-28 14:25:09 -0700 | [diff] [blame] | 173 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 174 | // Garbage tracking, required to handle GC events on the VM side |
| 175 | Vector<Res_png_9patch*> mGarbage; |
| 176 | mutable Mutex mLock; |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 177 | }; // class PatchCache |
| 178 | |
| 179 | }; // namespace uirenderer |
| 180 | }; // namespace android |