Romain Guy | c0ac193 | 2010-07-19 18:43:02 -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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_GRADIENT_CACHE_H |
| 18 | #define ANDROID_HWUI_GRADIENT_CACHE_H |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 19 | |
Chris Craik | 96a5c4c | 2015-01-27 15:46:35 -0800 | [diff] [blame] | 20 | #include <memory> |
| 21 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 22 | #include <GLES3/gl3.h> |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 23 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 24 | #include <SkShader.h> |
| 25 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 26 | #include <utils/LruCache.h> |
Derek Sollenberger | 029f643 | 2012-03-05 16:48:32 -0500 | [diff] [blame] | 27 | #include <utils/Mutex.h> |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 28 | #include <utils/Vector.h> |
| 29 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 33 | class Texture; |
| 34 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 35 | struct GradientCacheEntry { |
| 36 | GradientCacheEntry() { |
| 37 | count = 0; |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 38 | colors = nullptr; |
| 39 | positions = nullptr; |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 42 | GradientCacheEntry(uint32_t* colors, float* positions, uint32_t count) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 43 | copy(colors, positions, count); |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | GradientCacheEntry(const GradientCacheEntry& entry) { |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 47 | copy(entry.colors.get(), entry.positions.get(), entry.count); |
Romain Guy | e5df231 | 2011-08-12 14:23:53 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | GradientCacheEntry& operator=(const GradientCacheEntry& entry) { |
| 51 | if (this != &entry) { |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 52 | copy(entry.colors.get(), entry.positions.get(), entry.count); |
Romain Guy | e5df231 | 2011-08-12 14:23:53 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | return *this; |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 58 | hash_t hash() const; |
| 59 | |
| 60 | static int compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs); |
| 61 | |
| 62 | bool operator==(const GradientCacheEntry& other) const { |
| 63 | return compare(*this, other) == 0; |
| 64 | } |
| 65 | |
| 66 | bool operator!=(const GradientCacheEntry& other) const { |
| 67 | return compare(*this, other) != 0; |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 70 | std::unique_ptr<uint32_t[]> colors; |
| 71 | std::unique_ptr<float[]> positions; |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 72 | uint32_t count; |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 73 | |
Romain Guy | e5df231 | 2011-08-12 14:23:53 -0700 | [diff] [blame] | 74 | private: |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 75 | void copy(uint32_t* colors, float* positions, uint32_t count) { |
Romain Guy | e5df231 | 2011-08-12 14:23:53 -0700 | [diff] [blame] | 76 | this->count = count; |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 77 | this->colors.reset(new uint32_t[count]); |
| 78 | this->positions.reset(new float[count]); |
Romain Guy | e5df231 | 2011-08-12 14:23:53 -0700 | [diff] [blame] | 79 | |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 80 | memcpy(this->colors.get(), colors, count * sizeof(uint32_t)); |
| 81 | memcpy(this->positions.get(), positions, count * sizeof(float)); |
Romain Guy | e5df231 | 2011-08-12 14:23:53 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 84 | }; // GradientCacheEntry |
| 85 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 86 | // Caching support |
| 87 | |
| 88 | inline int strictly_order_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) { |
| 89 | return GradientCacheEntry::compare(lhs, rhs) < 0; |
| 90 | } |
| 91 | |
| 92 | inline int compare_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) { |
| 93 | return GradientCacheEntry::compare(lhs, rhs); |
| 94 | } |
| 95 | |
| 96 | inline hash_t hash_type(const GradientCacheEntry& entry) { |
| 97 | return entry.hash(); |
| 98 | } |
| 99 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 100 | /** |
| 101 | * A simple LRU gradient cache. The cache has a maximum size expressed in bytes. |
| 102 | * Any texture added to the cache causing the cache to grow beyond the maximum |
| 103 | * allowed size will also cause the oldest texture to be kicked out. |
| 104 | */ |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 105 | class GradientCache: public OnEntryRemoved<GradientCacheEntry, Texture*> { |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 106 | public: |
Chris Craik | 117bdbc | 2015-02-05 10:12:38 -0800 | [diff] [blame] | 107 | GradientCache(Extensions& extensions); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 108 | ~GradientCache(); |
| 109 | |
| 110 | /** |
| 111 | * Used as a callback when an entry is removed from the cache. |
| 112 | * Do not invoke directly. |
| 113 | */ |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 114 | void operator()(GradientCacheEntry& shader, Texture*& texture) override; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 115 | |
| 116 | /** |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 117 | * Returns the texture associated with the specified shader. |
| 118 | */ |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 119 | Texture* get(uint32_t* colors, float* positions, int count); |
| 120 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 121 | /** |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 122 | * Clears the cache. This causes all textures to be deleted. |
| 123 | */ |
| 124 | void clear(); |
| 125 | |
| 126 | /** |
| 127 | * Sets the maximum size of the cache in bytes. |
| 128 | */ |
| 129 | void setMaxSize(uint32_t maxSize); |
| 130 | /** |
| 131 | * Returns the maximum size of the cache in bytes. |
| 132 | */ |
| 133 | uint32_t getMaxSize(); |
| 134 | /** |
| 135 | * Returns the current size of the cache in bytes. |
| 136 | */ |
| 137 | uint32_t getSize(); |
| 138 | |
| 139 | private: |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 140 | /** |
| 141 | * Adds a new linear gradient to the cache. The generated texture is |
| 142 | * returned. |
| 143 | */ |
| 144 | Texture* addLinearGradient(GradientCacheEntry& gradient, |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 145 | uint32_t* colors, float* positions, int count); |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 146 | |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 147 | void generateTexture(uint32_t* colors, float* positions, Texture* texture); |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame] | 148 | |
| 149 | struct GradientInfo { |
| 150 | uint32_t width; |
| 151 | bool hasAlpha; |
| 152 | }; |
| 153 | |
| 154 | void getGradientInfo(const uint32_t* colors, const int count, GradientInfo& info); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 155 | |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 156 | size_t bytesPerPixel() const; |
| 157 | |
| 158 | struct GradientColor { |
| 159 | float r; |
| 160 | float g; |
| 161 | float b; |
| 162 | float a; |
| 163 | }; |
| 164 | |
| 165 | typedef void (GradientCache::*ChannelSplitter)(uint32_t inColor, |
| 166 | GradientColor& outColor) const; |
| 167 | |
| 168 | void splitToBytes(uint32_t inColor, GradientColor& outColor) const; |
| 169 | void splitToFloats(uint32_t inColor, GradientColor& outColor) const; |
| 170 | |
| 171 | typedef void (GradientCache::*ChannelMixer)(GradientColor& start, GradientColor& end, |
| 172 | float amount, uint8_t*& dst) const; |
| 173 | |
| 174 | void mixBytes(GradientColor& start, GradientColor& end, float amount, uint8_t*& dst) const; |
| 175 | void mixFloats(GradientColor& start, GradientColor& end, float amount, uint8_t*& dst) const; |
| 176 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 177 | LruCache<GradientCacheEntry, Texture*> mCache; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 178 | |
| 179 | uint32_t mSize; |
| 180 | uint32_t mMaxSize; |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 181 | |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 182 | GLint mMaxTextureSize; |
Romain Guy | b488004 | 2013-04-05 11:17:55 -0700 | [diff] [blame] | 183 | bool mUseFloatTexture; |
| 184 | bool mHasNpot; |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 185 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 186 | Vector<SkShader*> mGarbage; |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 187 | mutable Mutex mLock; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 188 | }; // class GradientCache |
| 189 | |
| 190 | }; // namespace uirenderer |
| 191 | }; // namespace android |
| 192 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 193 | #endif // ANDROID_HWUI_GRADIENT_CACHE_H |