GradientCache.h revision fe48f65922d4a3cc4aefe058cee5acec51504a20
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 <SkShader.h>
21
22#include <utils/Vector.h>
23
24#include "Texture.h"
25#include "utils/GenerationCache.h"
26
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:
37    GradientCache();
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     */
51    Texture* addLinearGradient(SkShader* shader, uint32_t* colors, float* positions,
52            int count, SkShader::TileMode tileMode = SkShader::kClamp_TileMode);
53    /**
54     * Returns the texture associated with the specified shader.
55     */
56    Texture* get(SkShader* shader);
57    /**
58     * Removes the texture associated with the specified shader.
59     * Upon remove the texture is freed.
60     */
61    void remove(SkShader* shader);
62    /**
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    /**
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
89private:
90    void generateTexture(SkBitmap* bitmap, Texture* texture);
91
92    GenerationCache<SkShader*, Texture*> mCache;
93
94    uint32_t mSize;
95    uint32_t mMaxSize;
96
97    Vector<SkShader*> mGarbage;
98    mutable Mutex mLock;
99}; // class GradientCache
100
101}; // namespace uirenderer
102}; // namespace android
103
104#endif // ANDROID_HWUI_GRADIENT_CACHE_H
105