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 | |
| 20 | #include <SkShader.h> |
| 21 | |
Derek Sollenberger | 029f643 | 2012-03-05 16:48:32 -0500 | [diff] [blame] | 22 | #include <utils/Mutex.h> |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 23 | #include <utils/Vector.h> |
| 24 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 25 | #include "Texture.h" |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 26 | #include "utils/Compare.h" |
Romain Guy | 21b028a | 2010-10-08 18:43:58 -0700 | [diff] [blame] | 27 | #include "utils/GenerationCache.h" |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 32 | struct GradientCacheEntry { |
| 33 | GradientCacheEntry() { |
| 34 | count = 0; |
| 35 | colors = NULL; |
| 36 | positions = NULL; |
| 37 | tileMode = SkShader::kClamp_TileMode; |
| 38 | } |
| 39 | |
| 40 | GradientCacheEntry(uint32_t* colors, float* positions, int count, |
| 41 | SkShader::TileMode tileMode) { |
Romain Guy | e5df231 | 2011-08-12 14:23:53 -0700 | [diff] [blame] | 42 | copy(colors, positions, count, tileMode); |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | GradientCacheEntry(const GradientCacheEntry& entry) { |
Romain Guy | e5df231 | 2011-08-12 14:23:53 -0700 | [diff] [blame] | 46 | copy(entry.colors, entry.positions, entry.count, entry.tileMode); |
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 | |
| 59 | copy(entry.colors, entry.positions, entry.count, entry.tileMode); |
| 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) { |
| 68 | LTE_INT(tileMode) { |
| 69 | int result = memcmp(colors, rhs.colors, count * sizeof(uint32_t)); |
| 70 | if (result< 0) return true; |
| 71 | else if (result == 0) { |
| 72 | result = memcmp(positions, rhs.positions, count * sizeof(float)); |
| 73 | if (result < 0) return true; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | uint32_t* colors; |
| 81 | float* positions; |
| 82 | int count; |
| 83 | SkShader::TileMode tileMode; |
| 84 | |
Romain Guy | e5df231 | 2011-08-12 14:23:53 -0700 | [diff] [blame] | 85 | private: |
| 86 | |
| 87 | void copy(uint32_t* colors, float* positions, int count, SkShader::TileMode tileMode) { |
| 88 | this->count = count; |
| 89 | this->colors = new uint32_t[count]; |
| 90 | this->positions = new float[count]; |
| 91 | this->tileMode = tileMode; |
| 92 | |
| 93 | memcpy(this->colors, colors, count * sizeof(uint32_t)); |
| 94 | memcpy(this->positions, positions, count * sizeof(float)); |
| 95 | } |
| 96 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 97 | }; // GradientCacheEntry |
| 98 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 99 | /** |
| 100 | * A simple LRU gradient cache. The cache has a maximum size expressed in bytes. |
| 101 | * Any texture added to the cache causing the cache to grow beyond the maximum |
| 102 | * allowed size will also cause the oldest texture to be kicked out. |
| 103 | */ |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 104 | class GradientCache: public OnEntryRemoved<GradientCacheEntry, Texture*> { |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 105 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 106 | GradientCache(); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 107 | GradientCache(uint32_t maxByteSize); |
| 108 | ~GradientCache(); |
| 109 | |
| 110 | /** |
| 111 | * Used as a callback when an entry is removed from the cache. |
| 112 | * Do not invoke directly. |
| 113 | */ |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 114 | void operator()(GradientCacheEntry& shader, Texture*& texture); |
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 | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 119 | Texture* get(uint32_t* colors, float* positions, |
| 120 | int count, SkShader::TileMode tileMode = SkShader::kClamp_TileMode); |
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, |
| 145 | uint32_t* colors, float* positions, int count, |
| 146 | SkShader::TileMode tileMode = SkShader::kClamp_TileMode); |
| 147 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 148 | void generateTexture(SkBitmap* bitmap, Texture* texture); |
| 149 | |
Romain Guy | 6203f6c | 2011-08-01 18:56:21 -0700 | [diff] [blame] | 150 | GenerationCache<GradientCacheEntry, Texture*> mCache; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 151 | |
| 152 | uint32_t mSize; |
| 153 | uint32_t mMaxSize; |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 154 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 155 | Vector<SkShader*> mGarbage; |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 156 | mutable Mutex mLock; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 157 | }; // class GradientCache |
| 158 | |
| 159 | }; // namespace uirenderer |
| 160 | }; // namespace android |
| 161 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 162 | #endif // ANDROID_HWUI_GRADIENT_CACHE_H |