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 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 22 | #include <utils/Vector.h> |
| 23 | |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 24 | #include "Texture.h" |
Romain Guy | 21b028a | 2010-10-08 18:43:58 -0700 | [diff] [blame] | 25 | #include "utils/GenerationCache.h" |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
| 30 | /** |
| 31 | * A simple LRU gradient cache. The cache has a maximum size expressed in bytes. |
| 32 | * Any texture added to the cache causing the cache to grow beyond the maximum |
| 33 | * allowed size will also cause the oldest texture to be kicked out. |
| 34 | */ |
| 35 | class GradientCache: public OnEntryRemoved<SkShader*, Texture*> { |
| 36 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 37 | GradientCache(); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 38 | GradientCache(uint32_t maxByteSize); |
| 39 | ~GradientCache(); |
| 40 | |
| 41 | /** |
| 42 | * Used as a callback when an entry is removed from the cache. |
| 43 | * Do not invoke directly. |
| 44 | */ |
| 45 | void operator()(SkShader*& shader, Texture*& texture); |
| 46 | |
| 47 | /** |
| 48 | * Adds a new linear gradient to the cache. The generated texture is |
| 49 | * returned. |
| 50 | */ |
Romain Guy | ee916f1 | 2010-09-20 17:53:08 -0700 | [diff] [blame] | 51 | Texture* addLinearGradient(SkShader* shader, uint32_t* colors, float* positions, |
| 52 | int count, SkShader::TileMode tileMode = SkShader::kClamp_TileMode); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Returns the texture associated with the specified shader. |
| 55 | */ |
| 56 | Texture* get(SkShader* shader); |
| 57 | /** |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 58 | * Removes the texture associated with the specified shader. |
| 59 | * Upon remove the texture is freed. |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 60 | */ |
| 61 | void remove(SkShader* shader); |
| 62 | /** |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 63 | * Removes the texture associated with the specified shader. This is meant |
| 64 | * to be called from threads that are not the EGL context thread. |
| 65 | */ |
| 66 | void removeDeferred(SkShader* shader); |
| 67 | /** |
| 68 | * Process deferred removals. |
| 69 | */ |
| 70 | void clearGarbage(); |
| 71 | /** |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 72 | * Clears the cache. This causes all textures to be deleted. |
| 73 | */ |
| 74 | void clear(); |
| 75 | |
| 76 | /** |
| 77 | * Sets the maximum size of the cache in bytes. |
| 78 | */ |
| 79 | void setMaxSize(uint32_t maxSize); |
| 80 | /** |
| 81 | * Returns the maximum size of the cache in bytes. |
| 82 | */ |
| 83 | uint32_t getMaxSize(); |
| 84 | /** |
| 85 | * Returns the current size of the cache in bytes. |
| 86 | */ |
| 87 | uint32_t getSize(); |
| 88 | |
| 89 | private: |
| 90 | void generateTexture(SkBitmap* bitmap, Texture* texture); |
| 91 | |
| 92 | GenerationCache<SkShader*, Texture*> mCache; |
| 93 | |
| 94 | uint32_t mSize; |
| 95 | uint32_t mMaxSize; |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 96 | |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 97 | Vector<SkShader*> mGarbage; |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 98 | mutable Mutex mLock; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 99 | }; // class GradientCache |
| 100 | |
| 101 | }; // namespace uirenderer |
| 102 | }; // namespace android |
| 103 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 104 | #endif // ANDROID_HWUI_GRADIENT_CACHE_H |