blob: 30da46209800753d407aacefcb194541992dc2d2 [file] [log] [blame]
Romain Guyc0ac1932010-07-19 18:43:02 -07001/*
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 Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_GRADIENT_CACHE_H
18#define ANDROID_HWUI_GRADIENT_CACHE_H
Romain Guyc0ac1932010-07-19 18:43:02 -070019
20#include <SkShader.h>
21
Romain Guyfe48f652010-11-11 15:36:56 -080022#include <utils/Vector.h>
23
Romain Guyc0ac1932010-07-19 18:43:02 -070024#include "Texture.h"
Romain Guy21b028a2010-10-08 18:43:58 -070025#include "utils/GenerationCache.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070026
27namespace android {
28namespace 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 */
35class GradientCache: public OnEntryRemoved<SkShader*, Texture*> {
36public:
Romain Guyfb8b7632010-08-23 21:05:08 -070037 GradientCache();
Romain Guyc0ac1932010-07-19 18:43:02 -070038 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 Guyee916f12010-09-20 17:53:08 -070051 Texture* addLinearGradient(SkShader* shader, uint32_t* colors, float* positions,
52 int count, SkShader::TileMode tileMode = SkShader::kClamp_TileMode);
Romain Guyc0ac1932010-07-19 18:43:02 -070053 /**
54 * Returns the texture associated with the specified shader.
55 */
56 Texture* get(SkShader* shader);
57 /**
Romain Guyfe48f652010-11-11 15:36:56 -080058 * Removes the texture associated with the specified shader.
59 * Upon remove the texture is freed.
Romain Guyc0ac1932010-07-19 18:43:02 -070060 */
61 void remove(SkShader* shader);
62 /**
Romain Guyfe48f652010-11-11 15:36:56 -080063 * 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 Guyc0ac1932010-07-19 18:43:02 -070072 * 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
89private:
90 void generateTexture(SkBitmap* bitmap, Texture* texture);
91
92 GenerationCache<SkShader*, Texture*> mCache;
93
94 uint32_t mSize;
95 uint32_t mMaxSize;
Romain Guya2341a92010-09-08 18:04:33 -070096
Romain Guyfe48f652010-11-11 15:36:56 -080097 Vector<SkShader*> mGarbage;
Romain Guya2341a92010-09-08 18:04:33 -070098 mutable Mutex mLock;
Romain Guyc0ac1932010-07-19 18:43:02 -070099}; // class GradientCache
100
101}; // namespace uirenderer
102}; // namespace android
103
Romain Guy5b3b3522010-10-27 18:57:51 -0700104#endif // ANDROID_HWUI_GRADIENT_CACHE_H