GradientCache.h revision 059e12ccd20f5c249724a8362d6bac325334ea76
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 17#ifndef ANDROID_HWUI_GRADIENT_CACHE_H 18#define ANDROID_HWUI_GRADIENT_CACHE_H 19 20#include <GLES2/gl2.h> 21 22#include <SkShader.h> 23 24#include <utils/LruCache.h> 25#include <utils/Mutex.h> 26#include <utils/Vector.h> 27 28#include "Texture.h" 29 30namespace android { 31namespace uirenderer { 32 33struct GradientCacheEntry { 34 GradientCacheEntry() { 35 count = 0; 36 colors = NULL; 37 positions = NULL; 38 } 39 40 GradientCacheEntry(uint32_t* colors, float* positions, uint32_t count) { 41 copy(colors, positions, count); 42 } 43 44 GradientCacheEntry(const GradientCacheEntry& entry) { 45 copy(entry.colors, entry.positions, entry.count); 46 } 47 48 ~GradientCacheEntry() { 49 delete[] colors; 50 delete[] positions; 51 } 52 53 GradientCacheEntry& operator=(const GradientCacheEntry& entry) { 54 if (this != &entry) { 55 delete[] colors; 56 delete[] positions; 57 58 copy(entry.colors, entry.positions, entry.count); 59 } 60 61 return *this; 62 } 63 64 hash_t hash() const; 65 66 static int compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs); 67 68 bool operator==(const GradientCacheEntry& other) const { 69 return compare(*this, other) == 0; 70 } 71 72 bool operator!=(const GradientCacheEntry& other) const { 73 return compare(*this, other) != 0; 74 } 75 76 uint32_t* colors; 77 float* positions; 78 uint32_t count; 79 SkShader::TileMode tileMode; 80 81private: 82 83 void copy(uint32_t* colors, float* positions, uint32_t count) { 84 this->count = count; 85 this->colors = new uint32_t[count]; 86 this->positions = new float[count]; 87 88 memcpy(this->colors, colors, count * sizeof(uint32_t)); 89 memcpy(this->positions, positions, count * sizeof(float)); 90 } 91 92}; // GradientCacheEntry 93 94// Caching support 95 96inline int strictly_order_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) { 97 return GradientCacheEntry::compare(lhs, rhs) < 0; 98} 99 100inline int compare_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) { 101 return GradientCacheEntry::compare(lhs, rhs); 102} 103 104inline hash_t hash_type(const GradientCacheEntry& entry) { 105 return entry.hash(); 106} 107 108/** 109 * A simple LRU gradient cache. The cache has a maximum size expressed in bytes. 110 * Any texture added to the cache causing the cache to grow beyond the maximum 111 * allowed size will also cause the oldest texture to be kicked out. 112 */ 113class GradientCache: public OnEntryRemoved<GradientCacheEntry, Texture*> { 114public: 115 GradientCache(); 116 GradientCache(uint32_t maxByteSize); 117 ~GradientCache(); 118 119 /** 120 * Used as a callback when an entry is removed from the cache. 121 * Do not invoke directly. 122 */ 123 void operator()(GradientCacheEntry& shader, Texture*& texture); 124 125 /** 126 * Returns the texture associated with the specified shader. 127 */ 128 Texture* get(uint32_t* colors, float* positions, int count); 129 130 /** 131 * Clears the cache. This causes all textures to be deleted. 132 */ 133 void clear(); 134 135 /** 136 * Sets the maximum size of the cache in bytes. 137 */ 138 void setMaxSize(uint32_t maxSize); 139 /** 140 * Returns the maximum size of the cache in bytes. 141 */ 142 uint32_t getMaxSize(); 143 /** 144 * Returns the current size of the cache in bytes. 145 */ 146 uint32_t getSize(); 147 148private: 149 /** 150 * Adds a new linear gradient to the cache. The generated texture is 151 * returned. 152 */ 153 Texture* addLinearGradient(GradientCacheEntry& gradient, 154 uint32_t* colors, float* positions, int count); 155 156 void generateTexture(uint32_t* colors, float* positions, int count, Texture* texture); 157 158 struct GradientInfo { 159 uint32_t width; 160 bool hasAlpha; 161 }; 162 163 void getGradientInfo(const uint32_t* colors, const int count, GradientInfo& info); 164 165 LruCache<GradientCacheEntry, Texture*> mCache; 166 167 uint32_t mSize; 168 uint32_t mMaxSize; 169 170 GLint mMaxTextureSize; 171 172 Vector<SkShader*> mGarbage; 173 mutable Mutex mLock; 174}; // class GradientCache 175 176}; // namespace uirenderer 177}; // namespace android 178 179#endif // ANDROID_HWUI_GRADIENT_CACHE_H 180