blob: c94b6f0e1d014ef5ec91f1eca27a361890599536 [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
Romain Guyb4880042013-04-05 11:17:55 -070020#include <GLES3/gl3.h>
Romain Guy8dcfd5e2012-07-20 11:36:03 -070021
Romain Guyc0ac1932010-07-19 18:43:02 -070022#include <SkShader.h>
23
Romain Guy059e12c2012-11-28 17:35:51 -080024#include <utils/LruCache.h>
Derek Sollenberger029f6432012-03-05 16:48:32 -050025#include <utils/Mutex.h>
Romain Guyfe48f652010-11-11 15:36:56 -080026#include <utils/Vector.h>
27
Romain Guyc0ac1932010-07-19 18:43:02 -070028namespace android {
29namespace uirenderer {
30
Tom Hudson2dc236b2014-10-15 15:46:42 -040031class Texture;
32
Romain Guy6203f6c2011-08-01 18:56:21 -070033struct GradientCacheEntry {
34 GradientCacheEntry() {
35 count = 0;
36 colors = NULL;
37 positions = NULL;
Romain Guy6203f6c2011-08-01 18:56:21 -070038 }
39
Romain Guy059e12c2012-11-28 17:35:51 -080040 GradientCacheEntry(uint32_t* colors, float* positions, uint32_t count) {
Romain Guy42e1e0d2012-07-30 14:47:51 -070041 copy(colors, positions, count);
Romain Guy6203f6c2011-08-01 18:56:21 -070042 }
43
44 GradientCacheEntry(const GradientCacheEntry& entry) {
Romain Guy42e1e0d2012-07-30 14:47:51 -070045 copy(entry.colors, entry.positions, entry.count);
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
Romain Guy42e1e0d2012-07-30 14:47:51 -070058 copy(entry.colors, entry.positions, entry.count);
Romain Guye5df2312011-08-12 14:23:53 -070059 }
60
61 return *this;
Romain Guy6203f6c2011-08-01 18:56:21 -070062 }
63
Romain Guy059e12c2012-11-28 17:35:51 -080064 hash_t hash() const;
65
66 static int compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs);
67
68 bool operator==(const GradientCacheEntry& other) const {
69 return compare(*this, other) == 0;
70 }
71
72 bool operator!=(const GradientCacheEntry& other) const {
73 return compare(*this, other) != 0;
Romain Guy6203f6c2011-08-01 18:56:21 -070074 }
75
76 uint32_t* colors;
77 float* positions;
Romain Guy059e12c2012-11-28 17:35:51 -080078 uint32_t count;
Romain Guy6203f6c2011-08-01 18:56:21 -070079
Romain Guye5df2312011-08-12 14:23:53 -070080private:
Romain Guy059e12c2012-11-28 17:35:51 -080081 void copy(uint32_t* colors, float* positions, uint32_t count) {
Romain Guye5df2312011-08-12 14:23:53 -070082 this->count = count;
83 this->colors = new uint32_t[count];
84 this->positions = new float[count];
Romain Guye5df2312011-08-12 14:23:53 -070085
86 memcpy(this->colors, colors, count * sizeof(uint32_t));
87 memcpy(this->positions, positions, count * sizeof(float));
88 }
89
Romain Guy6203f6c2011-08-01 18:56:21 -070090}; // GradientCacheEntry
91
Romain Guy059e12c2012-11-28 17:35:51 -080092// Caching support
93
94inline int strictly_order_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
95 return GradientCacheEntry::compare(lhs, rhs) < 0;
96}
97
98inline int compare_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
99 return GradientCacheEntry::compare(lhs, rhs);
100}
101
102inline hash_t hash_type(const GradientCacheEntry& entry) {
103 return entry.hash();
104}
105
Romain Guyc0ac1932010-07-19 18:43:02 -0700106/**
107 * A simple LRU gradient cache. The cache has a maximum size expressed in bytes.
108 * Any texture added to the cache causing the cache to grow beyond the maximum
109 * allowed size will also cause the oldest texture to be kicked out.
110 */
Romain Guy6203f6c2011-08-01 18:56:21 -0700111class GradientCache: public OnEntryRemoved<GradientCacheEntry, Texture*> {
Romain Guyc0ac1932010-07-19 18:43:02 -0700112public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700113 GradientCache();
Romain Guyc0ac1932010-07-19 18:43:02 -0700114 GradientCache(uint32_t maxByteSize);
115 ~GradientCache();
116
117 /**
118 * Used as a callback when an entry is removed from the cache.
119 * Do not invoke directly.
120 */
Romain Guy6203f6c2011-08-01 18:56:21 -0700121 void operator()(GradientCacheEntry& shader, Texture*& texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700122
123 /**
Romain Guyc0ac1932010-07-19 18:43:02 -0700124 * Returns the texture associated with the specified shader.
125 */
Romain Guy42e1e0d2012-07-30 14:47:51 -0700126 Texture* get(uint32_t* colors, float* positions, int count);
127
Romain Guyfe48f652010-11-11 15:36:56 -0800128 /**
Romain Guyc0ac1932010-07-19 18:43:02 -0700129 * Clears the cache. This causes all textures to be deleted.
130 */
131 void clear();
132
133 /**
134 * Sets the maximum size of the cache in bytes.
135 */
136 void setMaxSize(uint32_t maxSize);
137 /**
138 * Returns the maximum size of the cache in bytes.
139 */
140 uint32_t getMaxSize();
141 /**
142 * Returns the current size of the cache in bytes.
143 */
144 uint32_t getSize();
145
146private:
Romain Guy6203f6c2011-08-01 18:56:21 -0700147 /**
148 * Adds a new linear gradient to the cache. The generated texture is
149 * returned.
150 */
151 Texture* addLinearGradient(GradientCacheEntry& gradient,
Romain Guy42e1e0d2012-07-30 14:47:51 -0700152 uint32_t* colors, float* positions, int count);
Romain Guy6203f6c2011-08-01 18:56:21 -0700153
Chris Craike63f7c622013-10-17 10:30:55 -0700154 void generateTexture(uint32_t* colors, float* positions, Texture* texture);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700155
156 struct GradientInfo {
157 uint32_t width;
158 bool hasAlpha;
159 };
160
161 void getGradientInfo(const uint32_t* colors, const int count, GradientInfo& info);
Romain Guyc0ac1932010-07-19 18:43:02 -0700162
Romain Guyb4880042013-04-05 11:17:55 -0700163 size_t bytesPerPixel() const;
164
165 struct GradientColor {
166 float r;
167 float g;
168 float b;
169 float a;
170 };
171
172 typedef void (GradientCache::*ChannelSplitter)(uint32_t inColor,
173 GradientColor& outColor) const;
174
175 void splitToBytes(uint32_t inColor, GradientColor& outColor) const;
176 void splitToFloats(uint32_t inColor, GradientColor& outColor) const;
177
178 typedef void (GradientCache::*ChannelMixer)(GradientColor& start, GradientColor& end,
179 float amount, uint8_t*& dst) const;
180
181 void mixBytes(GradientColor& start, GradientColor& end, float amount, uint8_t*& dst) const;
182 void mixFloats(GradientColor& start, GradientColor& end, float amount, uint8_t*& dst) const;
183
Romain Guy059e12c2012-11-28 17:35:51 -0800184 LruCache<GradientCacheEntry, Texture*> mCache;
Romain Guyc0ac1932010-07-19 18:43:02 -0700185
186 uint32_t mSize;
187 uint32_t mMaxSize;
Romain Guya2341a92010-09-08 18:04:33 -0700188
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700189 GLint mMaxTextureSize;
Romain Guyb4880042013-04-05 11:17:55 -0700190 bool mUseFloatTexture;
191 bool mHasNpot;
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700192
Romain Guyfe48f652010-11-11 15:36:56 -0800193 Vector<SkShader*> mGarbage;
Romain Guya2341a92010-09-08 18:04:33 -0700194 mutable Mutex mLock;
Romain Guyc0ac1932010-07-19 18:43:02 -0700195}; // class GradientCache
196
197}; // namespace uirenderer
198}; // namespace android
199
Romain Guy5b3b3522010-10-27 18:57:51 -0700200#endif // ANDROID_HWUI_GRADIENT_CACHE_H