GradientCache.h revision 38e0c32852e3b9d8ca4a9d3791577f52536419cb
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 <memory> 21 22#include <GLES3/gl3.h> 23 24#include <SkShader.h> 25 26#include <utils/LruCache.h> 27#include <utils/Mutex.h> 28 29namespace android { 30namespace uirenderer { 31 32class Texture; 33 34struct GradientCacheEntry { 35 GradientCacheEntry() { 36 count = 0; 37 colors = nullptr; 38 positions = nullptr; 39 } 40 41 GradientCacheEntry(uint32_t* colors, float* positions, uint32_t count) { 42 copy(colors, positions, count); 43 } 44 45 GradientCacheEntry(const GradientCacheEntry& entry) { 46 copy(entry.colors.get(), entry.positions.get(), entry.count); 47 } 48 49 GradientCacheEntry& operator=(const GradientCacheEntry& entry) { 50 if (this != &entry) { 51 copy(entry.colors.get(), entry.positions.get(), entry.count); 52 } 53 54 return *this; 55 } 56 57 hash_t hash() const; 58 59 static int compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs); 60 61 bool operator==(const GradientCacheEntry& other) const { 62 return compare(*this, other) == 0; 63 } 64 65 bool operator!=(const GradientCacheEntry& other) const { 66 return compare(*this, other) != 0; 67 } 68 69 std::unique_ptr<uint32_t[]> colors; 70 std::unique_ptr<float[]> positions; 71 uint32_t count; 72 73private: 74 void copy(uint32_t* colors, float* positions, uint32_t count) { 75 this->count = count; 76 this->colors.reset(new uint32_t[count]); 77 this->positions.reset(new float[count]); 78 79 memcpy(this->colors.get(), colors, count * sizeof(uint32_t)); 80 memcpy(this->positions.get(), positions, count * sizeof(float)); 81 } 82 83}; // GradientCacheEntry 84 85// Caching support 86 87inline int strictly_order_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) { 88 return GradientCacheEntry::compare(lhs, rhs) < 0; 89} 90 91inline int compare_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) { 92 return GradientCacheEntry::compare(lhs, rhs); 93} 94 95inline hash_t hash_type(const GradientCacheEntry& entry) { 96 return entry.hash(); 97} 98 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 */ 104class GradientCache: public OnEntryRemoved<GradientCacheEntry, Texture*> { 105public: 106 GradientCache(Extensions& extensions); 107 ~GradientCache(); 108 109 /** 110 * Used as a callback when an entry is removed from the cache. 111 * Do not invoke directly. 112 */ 113 void operator()(GradientCacheEntry& shader, Texture*& texture) override; 114 115 /** 116 * Returns the texture associated with the specified shader. 117 */ 118 Texture* get(uint32_t* colors, float* positions, int count); 119 120 /** 121 * Clears the cache. This causes all textures to be deleted. 122 */ 123 void clear(); 124 125 /** 126 * Sets the maximum size of the cache in bytes. 127 */ 128 void setMaxSize(uint32_t maxSize); 129 /** 130 * Returns the maximum size of the cache in bytes. 131 */ 132 uint32_t getMaxSize(); 133 /** 134 * Returns the current size of the cache in bytes. 135 */ 136 uint32_t getSize(); 137 138private: 139 /** 140 * Adds a new linear gradient to the cache. The generated texture is 141 * returned. 142 */ 143 Texture* addLinearGradient(GradientCacheEntry& gradient, 144 uint32_t* colors, float* positions, int count); 145 146 void generateTexture(uint32_t* colors, float* positions, 147 const uint32_t width, const uint32_t height, Texture* texture); 148 149 struct GradientInfo { 150 uint32_t width; 151 bool hasAlpha; 152 }; 153 154 void getGradientInfo(const uint32_t* colors, const int count, GradientInfo& info); 155 156 size_t bytesPerPixel() const; 157 158 struct GradientColor { 159 float r; 160 float g; 161 float b; 162 float a; 163 }; 164 165 typedef void (GradientCache::*ChannelSplitter)(uint32_t inColor, 166 GradientColor& outColor) const; 167 168 void splitToBytes(uint32_t inColor, GradientColor& outColor) const; 169 void splitToFloats(uint32_t inColor, GradientColor& outColor) const; 170 171 typedef void (GradientCache::*ChannelMixer)(GradientColor& start, GradientColor& end, 172 float amount, uint8_t*& dst) const; 173 174 void mixBytes(GradientColor& start, GradientColor& end, float amount, uint8_t*& dst) const; 175 void mixFloats(GradientColor& start, GradientColor& end, float amount, uint8_t*& dst) const; 176 177 LruCache<GradientCacheEntry, Texture*> mCache; 178 179 uint32_t mSize; 180 uint32_t mMaxSize; 181 182 GLint mMaxTextureSize; 183 bool mUseFloatTexture; 184 bool mHasNpot; 185 186 mutable Mutex mLock; 187}; // class GradientCache 188 189}; // namespace uirenderer 190}; // namespace android 191 192#endif // ANDROID_HWUI_GRADIENT_CACHE_H 193