GradientCache.h revision ee916f14cbd1fe1422c063ce2ef7b185e2bc5c6f
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_UI_GRADIENT_CACHE_H 18#define ANDROID_UI_GRADIENT_CACHE_H 19 20#include <SkShader.h> 21 22#include "Texture.h" 23#include "GenerationCache.h" 24 25namespace android { 26namespace uirenderer { 27 28/** 29 * A simple LRU gradient cache. The cache has a maximum size expressed in bytes. 30 * Any texture added to the cache causing the cache to grow beyond the maximum 31 * allowed size will also cause the oldest texture to be kicked out. 32 */ 33class GradientCache: public OnEntryRemoved<SkShader*, Texture*> { 34public: 35 GradientCache(); 36 GradientCache(uint32_t maxByteSize); 37 ~GradientCache(); 38 39 /** 40 * Used as a callback when an entry is removed from the cache. 41 * Do not invoke directly. 42 */ 43 void operator()(SkShader*& shader, Texture*& texture); 44 45 /** 46 * Adds a new linear gradient to the cache. The generated texture is 47 * returned. 48 */ 49 Texture* addLinearGradient(SkShader* shader, uint32_t* colors, float* positions, 50 int count, SkShader::TileMode tileMode = SkShader::kClamp_TileMode); 51 /** 52 * Returns the texture associated with the specified shader. 53 */ 54 Texture* get(SkShader* shader); 55 /** 56 * Removes the texture associated with the specified shader. Returns NULL 57 * if the texture cannot be found. Upon remove the texture is freed. 58 */ 59 void remove(SkShader* shader); 60 /** 61 * Clears the cache. This causes all textures to be deleted. 62 */ 63 void clear(); 64 65 /** 66 * Sets the maximum size of the cache in bytes. 67 */ 68 void setMaxSize(uint32_t maxSize); 69 /** 70 * Returns the maximum size of the cache in bytes. 71 */ 72 uint32_t getMaxSize(); 73 /** 74 * Returns the current size of the cache in bytes. 75 */ 76 uint32_t getSize(); 77 78private: 79 void generateTexture(SkBitmap* bitmap, Texture* texture); 80 81 GenerationCache<SkShader*, Texture*> mCache; 82 83 uint32_t mSize; 84 uint32_t mMaxSize; 85 86 /** 87 * Used to access mCache and mSize. All methods are accessed from a single 88 * thread except for remove(). 89 */ 90 mutable Mutex mLock; 91}; // class GradientCache 92 93}; // namespace uirenderer 94}; // namespace android 95 96#endif // ANDROID_UI_GRADIENT_CACHE_H 97