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 |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 39 | #define PATCH_LOGD(...) ALOGD(__VA_ARGS__) |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 40 | #else |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 41 | #define PATCH_LOGD(...) |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 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, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -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 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 60 | uint32_t getSize() const { return mSize; } |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 61 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 62 | uint32_t getMaxSize() const { return mMaxSize; } |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 63 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 64 | GLuint getMeshBuffer() const { return mMeshBuffer; } |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 65 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Removes the entries associated with the specified 9-patch. This is meant |
| 68 | * to be called from threads that are not the EGL context thread (GC thread |
| 69 | * on the VM side for instance.) |
| 70 | */ |
| 71 | void removeDeferred(Res_png_9patch* patch); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 72 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 73 | /** |
| 74 | * Process deferred removals. |
| 75 | */ |
| 76 | void clearGarbage(); |
| 77 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 78 | private: |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 79 | struct PatchDescription { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 80 | PatchDescription() |
| 81 | : mPatch(nullptr) |
| 82 | , mBitmapWidth(0) |
| 83 | , mBitmapHeight(0) |
| 84 | , mPixelWidth(0) |
| 85 | , mPixelHeight(0) {} |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 86 | |
Romain Guy | 13ba005 | 2013-02-15 12:47:26 -0800 | [diff] [blame] | 87 | PatchDescription(const uint32_t bitmapWidth, const uint32_t bitmapHeight, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 88 | const float pixelWidth, const float pixelHeight, |
| 89 | const Res_png_9patch* patch) |
| 90 | : mPatch(patch) |
| 91 | , mBitmapWidth(bitmapWidth) |
| 92 | , mBitmapHeight(bitmapHeight) |
| 93 | , mPixelWidth(pixelWidth) |
| 94 | , mPixelHeight(pixelHeight) {} |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 95 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 96 | hash_t hash() const; |
| 97 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 98 | const Res_png_9patch* getPatch() const { return mPatch; } |
| 99 | |
Romain Guy | 13ba005 | 2013-02-15 12:47:26 -0800 | [diff] [blame] | 100 | static int compare(const PatchDescription& lhs, const PatchDescription& rhs); |
| 101 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 102 | bool operator==(const PatchDescription& other) const { return compare(*this, other) == 0; } |
Romain Guy | 13ba005 | 2013-02-15 12:47:26 -0800 | [diff] [blame] | 103 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 104 | bool operator!=(const PatchDescription& other) const { return compare(*this, other) != 0; } |
Romain Guy | 13ba005 | 2013-02-15 12:47:26 -0800 | [diff] [blame] | 105 | |
| 106 | friend inline int strictly_order_type(const PatchDescription& lhs, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 107 | const PatchDescription& rhs) { |
Romain Guy | 13ba005 | 2013-02-15 12:47:26 -0800 | [diff] [blame] | 108 | return PatchDescription::compare(lhs, rhs) < 0; |
| 109 | } |
| 110 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 111 | friend inline int compare_type(const PatchDescription& lhs, const PatchDescription& rhs) { |
Romain Guy | 13ba005 | 2013-02-15 12:47:26 -0800 | [diff] [blame] | 112 | return PatchDescription::compare(lhs, rhs); |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 113 | } |
| 114 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 115 | friend inline hash_t hash_type(const PatchDescription& entry) { return entry.hash(); } |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 116 | |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 117 | private: |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 118 | const Res_png_9patch* mPatch; |
| 119 | uint32_t mBitmapWidth; |
| 120 | uint32_t mBitmapHeight; |
| 121 | float mPixelWidth; |
| 122 | float mPixelHeight; |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 123 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 124 | }; // struct PatchDescription |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 125 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 126 | /** |
| 127 | * A buffer block represents an empty range in the mesh buffer |
| 128 | * that can be used to store vertices. |
| 129 | * |
| 130 | * The patch cache maintains a linked-list of buffer blocks |
| 131 | * to track available regions of memory in the VBO. |
| 132 | */ |
| 133 | struct BufferBlock { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 134 | 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] | 135 | |
| 136 | uint32_t offset; |
| 137 | uint32_t size; |
| 138 | |
| 139 | BufferBlock* next; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 140 | }; // struct BufferBlock |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 141 | |
| 142 | typedef Pair<const PatchDescription*, Patch*> patch_pair_t; |
| 143 | |
| 144 | void clearCache(); |
| 145 | void createVertexBuffer(); |
| 146 | |
Chris Craik | 8820fd1 | 2015-03-03 14:20:47 -0800 | [diff] [blame] | 147 | void setupMesh(Patch* newMesh); |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 148 | |
| 149 | void remove(Vector<patch_pair_t>& patchesToRemove, Res_png_9patch* patch); |
| 150 | |
| 151 | #if DEBUG_PATCHES |
| 152 | void dumpFreeBlocks(const char* prefix); |
| 153 | #endif |
| 154 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 155 | RenderState& mRenderState; |
Chris Craik | 48a8f43 | 2016-02-05 15:59:29 -0800 | [diff] [blame] | 156 | const uint32_t mMaxSize; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 157 | uint32_t mSize; |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame] | 158 | |
Romain Guy | 4c2547f | 2013-06-11 16:19:24 -0700 | [diff] [blame] | 159 | LruCache<PatchDescription, Patch*> mCache; |
| 160 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 161 | GLuint mMeshBuffer; |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 162 | // First available free block inside the mesh buffer |
| 163 | BufferBlock* mFreeBlocks; |
Romain Guy | 7d9b1b3 | 2013-05-28 14:25:09 -0700 | [diff] [blame] | 164 | |
Romain Guy | e3b0a01 | 2013-06-26 15:45:41 -0700 | [diff] [blame] | 165 | // Garbage tracking, required to handle GC events on the VM side |
| 166 | Vector<Res_png_9patch*> mGarbage; |
| 167 | mutable Mutex mLock; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 168 | }; // class PatchCache |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 169 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 170 | }; // namespace uirenderer |
| 171 | }; // namespace android |