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 | |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 20 | #include <GLES2/gl2.h> |
| 21 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 22 | #include <SkShader.h> |
| 23 | |
Derek Sollenberger | 029f643 | 2012-03-05 16:48:32 -0500 | [diff] [blame] | 24 | #include <utils/Mutex.h> |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 25 | #include <utils/Vector.h> |
| 26 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 27 | #include "Texture.h" |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 28 | #include "utils/Compare.h" |
Romain Guy | 21b028a | 2010-10-08 18:43:58 -0700 | [diff] [blame] | 29 | #include "utils/GenerationCache.h" |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 34 | struct GradientCacheEntry { |
| 35 | GradientCacheEntry() { |
| 36 | count = 0; |
| 37 | colors = NULL; |
| 38 | positions = NULL; |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame^] | 41 | GradientCacheEntry(uint32_t* colors, float* positions, int count) { |
| 42 | copy(colors, positions, count); |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | GradientCacheEntry(const GradientCacheEntry& entry) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame^] | 46 | copy(entry.colors, entry.positions, entry.count); |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | ~GradientCacheEntry() { |
Romain Guy | e5df231 | 2011-08-12 14:23:53 -0700 | [diff] [blame] | 50 | delete[] colors; |
| 51 | delete[] positions; |
| 52 | } |
| 53 | |
| 54 | GradientCacheEntry& operator=(const GradientCacheEntry& entry) { |
| 55 | if (this != &entry) { |
| 56 | delete[] colors; |
| 57 | delete[] positions; |
| 58 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame^] | 59 | copy(entry.colors, entry.positions, entry.count); |
Romain Guy | e5df231 | 2011-08-12 14:23:53 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | return *this; |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | bool operator<(const GradientCacheEntry& r) const { |
| 66 | const GradientCacheEntry& rhs = (const GradientCacheEntry&) r; |
| 67 | LTE_INT(count) { |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame^] | 68 | int result = memcmp(colors, rhs.colors, count * sizeof(uint32_t)); |
| 69 | if (result< 0) return true; |
| 70 | else if (result == 0) { |
| 71 | result = memcmp(positions, rhs.positions, count * sizeof(float)); |
| 72 | if (result < 0) return true; |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | uint32_t* colors; |
| 79 | float* positions; |
| 80 | int count; |
| 81 | SkShader::TileMode tileMode; |
| 82 | |
Romain Guy | e5df231 | 2011-08-12 14:23:53 -0700 | [diff] [blame] | 83 | private: |
| 84 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame^] | 85 | void copy(uint32_t* colors, float* positions, int count) { |
Romain Guy | e5df231 | 2011-08-12 14:23:53 -0700 | [diff] [blame] | 86 | this->count = count; |
| 87 | this->colors = new uint32_t[count]; |
| 88 | this->positions = new float[count]; |
Romain Guy | e5df231 | 2011-08-12 14:23:53 -0700 | [diff] [blame] | 89 | |
| 90 | memcpy(this->colors, colors, count * sizeof(uint32_t)); |
| 91 | memcpy(this->positions, positions, count * sizeof(float)); |
| 92 | } |
| 93 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 94 | }; // GradientCacheEntry |
| 95 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 96 | /** |
| 97 | * A simple LRU gradient cache. The cache has a maximum size expressed in bytes. |
| 98 | * Any texture added to the cache causing the cache to grow beyond the maximum |
| 99 | * allowed size will also cause the oldest texture to be kicked out. |
| 100 | */ |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 101 | class GradientCache: public OnEntryRemoved<GradientCacheEntry, Texture*> { |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 102 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 103 | GradientCache(); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 104 | GradientCache(uint32_t maxByteSize); |
| 105 | ~GradientCache(); |
| 106 | |
| 107 | /** |
| 108 | * Used as a callback when an entry is removed from the cache. |
| 109 | * Do not invoke directly. |
| 110 | */ |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 111 | void operator()(GradientCacheEntry& shader, Texture*& texture); |
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 | /** |
| 124 | * Sets the maximum size of the cache in bytes. |
| 125 | */ |
| 126 | void setMaxSize(uint32_t maxSize); |
| 127 | /** |
| 128 | * Returns the maximum size of the cache in bytes. |
| 129 | */ |
| 130 | uint32_t getMaxSize(); |
| 131 | /** |
| 132 | * Returns the current size of the cache in bytes. |
| 133 | */ |
| 134 | uint32_t getSize(); |
| 135 | |
| 136 | private: |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 137 | /** |
| 138 | * Adds a new linear gradient to the cache. The generated texture is |
| 139 | * returned. |
| 140 | */ |
| 141 | Texture* addLinearGradient(GradientCacheEntry& gradient, |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame^] | 142 | uint32_t* colors, float* positions, int count); |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 143 | |
Romain Guy | 42e1e0d | 2012-07-30 14:47:51 -0700 | [diff] [blame^] | 144 | void generateTexture(uint32_t* colors, float* positions, int count, Texture* texture); |
| 145 | |
| 146 | struct GradientInfo { |
| 147 | uint32_t width; |
| 148 | bool hasAlpha; |
| 149 | }; |
| 150 | |
| 151 | void getGradientInfo(const uint32_t* colors, const int count, GradientInfo& info); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 152 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 153 | GenerationCache<GradientCacheEntry, Texture*> mCache; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 154 | |
| 155 | uint32_t mSize; |
| 156 | uint32_t mMaxSize; |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 157 | |
Romain Guy | 8dcfd5e | 2012-07-20 11:36:03 -0700 | [diff] [blame] | 158 | GLint mMaxTextureSize; |
| 159 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 160 | Vector<SkShader*> mGarbage; |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 161 | mutable Mutex mLock; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 162 | }; // class GradientCache |
| 163 | |
| 164 | }; // namespace uirenderer |
| 165 | }; // namespace android |
| 166 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 167 | #endif // ANDROID_HWUI_GRADIENT_CACHE_H |