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 | |
| 22 | #include "Texture.h" |
Romain Guy | 21b028a | 2010-10-08 18:43:58 -0700 | [diff] [blame] | 23 | #include "utils/GenerationCache.h" |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace 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 | */ |
| 33 | class GradientCache: public OnEntryRemoved<SkShader*, Texture*> { |
| 34 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 35 | GradientCache(); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 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 | */ |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 49 | Texture* addLinearGradient(SkShader* shader, uint32_t* colors, float* positions, |
| 50 | int count, SkShader::TileMode tileMode = SkShader::kClamp_TileMode); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 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 | |
| 78 | private: |
| 79 | void generateTexture(SkBitmap* bitmap, Texture* texture); |
| 80 | |
| 81 | GenerationCache<SkShader*, Texture*> mCache; |
| 82 | |
| 83 | uint32_t mSize; |
| 84 | uint32_t mMaxSize; |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 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; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 91 | }; // class GradientCache |
| 92 | |
| 93 | }; // namespace uirenderer |
| 94 | }; // namespace android |
| 95 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 96 | #endif // ANDROID_HWUI_GRADIENT_CACHE_H |