blob: 733985317c7c658e6962c532476e9ec4f369367a [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 Guy6203f6c2011-08-01 18:56:21 -070025#include "utils/Compare.h"
Romain Guy21b028a2010-10-08 18:43:58 -070026#include "utils/GenerationCache.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070027
28namespace android {
29namespace uirenderer {
30
Romain Guy6203f6c2011-08-01 18:56:21 -070031struct GradientCacheEntry {
32 GradientCacheEntry() {
33 count = 0;
34 colors = NULL;
35 positions = NULL;
36 tileMode = SkShader::kClamp_TileMode;
37 }
38
39 GradientCacheEntry(uint32_t* colors, float* positions, int count,
40 SkShader::TileMode tileMode) {
Romain Guye5df2312011-08-12 14:23:53 -070041 copy(colors, positions, count, tileMode);
Romain Guy6203f6c2011-08-01 18:56:21 -070042 }
43
44 GradientCacheEntry(const GradientCacheEntry& entry) {
Romain Guye5df2312011-08-12 14:23:53 -070045 copy(entry.colors, entry.positions, entry.count, entry.tileMode);
Romain Guy6203f6c2011-08-01 18:56:21 -070046 }
47
48 ~GradientCacheEntry() {
Romain Guye5df2312011-08-12 14:23:53 -070049 delete[] colors;
50 delete[] positions;
51 }
52
53 GradientCacheEntry& operator=(const GradientCacheEntry& entry) {
54 if (this != &entry) {
55 delete[] colors;
56 delete[] positions;
57
58 copy(entry.colors, entry.positions, entry.count, entry.tileMode);
59 }
60
61 return *this;
Romain Guy6203f6c2011-08-01 18:56:21 -070062 }
63
64 bool operator<(const GradientCacheEntry& r) const {
65 const GradientCacheEntry& rhs = (const GradientCacheEntry&) r;
66 LTE_INT(count) {
67 LTE_INT(tileMode) {
68 int result = memcmp(colors, rhs.colors, count * sizeof(uint32_t));
69 if (result< 0) return true;
70 else if (result == 0) {
71 result = memcmp(positions, rhs.positions, count * sizeof(float));
72 if (result < 0) return true;
73 }
74 }
75 }
76 return false;
77 }
78
79 uint32_t* colors;
80 float* positions;
81 int count;
82 SkShader::TileMode tileMode;
83
Romain Guye5df2312011-08-12 14:23:53 -070084private:
85
86 void copy(uint32_t* colors, float* positions, int count, SkShader::TileMode tileMode) {
87 this->count = count;
88 this->colors = new uint32_t[count];
89 this->positions = new float[count];
90 this->tileMode = tileMode;
91
92 memcpy(this->colors, colors, count * sizeof(uint32_t));
93 memcpy(this->positions, positions, count * sizeof(float));
94 }
95
Romain Guy6203f6c2011-08-01 18:56:21 -070096}; // GradientCacheEntry
97
Romain Guyc0ac1932010-07-19 18:43:02 -070098/**
99 * A simple LRU gradient cache. The cache has a maximum size expressed in bytes.
100 * Any texture added to the cache causing the cache to grow beyond the maximum
101 * allowed size will also cause the oldest texture to be kicked out.
102 */
Romain Guy6203f6c2011-08-01 18:56:21 -0700103class GradientCache: public OnEntryRemoved<GradientCacheEntry, Texture*> {
Romain Guyc0ac1932010-07-19 18:43:02 -0700104public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700105 GradientCache();
Romain Guyc0ac1932010-07-19 18:43:02 -0700106 GradientCache(uint32_t maxByteSize);
107 ~GradientCache();
108
109 /**
110 * Used as a callback when an entry is removed from the cache.
111 * Do not invoke directly.
112 */
Romain Guy6203f6c2011-08-01 18:56:21 -0700113 void operator()(GradientCacheEntry& shader, Texture*& texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700114
115 /**
Romain Guyc0ac1932010-07-19 18:43:02 -0700116 * Returns the texture associated with the specified shader.
117 */
Romain Guy6203f6c2011-08-01 18:56:21 -0700118 Texture* get(uint32_t* colors, float* positions,
119 int count, SkShader::TileMode tileMode = SkShader::kClamp_TileMode);
Romain Guyfe48f652010-11-11 15:36:56 -0800120 /**
Romain Guyc0ac1932010-07-19 18:43:02 -0700121 * Clears the cache. This causes all textures to be deleted.
122 */
123 void clear();
124
125 /**
126 * Sets the maximum size of the cache in bytes.
127 */
128 void setMaxSize(uint32_t maxSize);
129 /**
130 * Returns the maximum size of the cache in bytes.
131 */
132 uint32_t getMaxSize();
133 /**
134 * Returns the current size of the cache in bytes.
135 */
136 uint32_t getSize();
137
138private:
Romain Guy6203f6c2011-08-01 18:56:21 -0700139 /**
140 * Adds a new linear gradient to the cache. The generated texture is
141 * returned.
142 */
143 Texture* addLinearGradient(GradientCacheEntry& gradient,
144 uint32_t* colors, float* positions, int count,
145 SkShader::TileMode tileMode = SkShader::kClamp_TileMode);
146
Romain Guyc0ac1932010-07-19 18:43:02 -0700147 void generateTexture(SkBitmap* bitmap, Texture* texture);
148
Romain Guy6203f6c2011-08-01 18:56:21 -0700149 GenerationCache<GradientCacheEntry, Texture*> mCache;
Romain Guyc0ac1932010-07-19 18:43:02 -0700150
151 uint32_t mSize;
152 uint32_t mMaxSize;
Romain Guya2341a92010-09-08 18:04:33 -0700153
Romain Guyfe48f652010-11-11 15:36:56 -0800154 Vector<SkShader*> mGarbage;
Romain Guya2341a92010-09-08 18:04:33 -0700155 mutable Mutex mLock;
Romain Guyc0ac1932010-07-19 18:43:02 -0700156}; // class GradientCache
157
158}; // namespace uirenderer
159}; // namespace android
160
Romain Guy5b3b3522010-10-27 18:57:51 -0700161#endif // ANDROID_HWUI_GRADIENT_CACHE_H