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