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