blob: 9176c763a65de76c22dcdafae9884f67964d5faf [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;
Chris Craike84a2082014-12-22 14:28:49 -080036 colors = nullptr;
37 positions = nullptr;
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) {
Chris Craik51d6a3d2014-12-22 17:16:56 -080045 copy(entry.colors.get(), entry.positions.get(), entry.count);
Romain Guye5df2312011-08-12 14:23:53 -070046 }
47
48 GradientCacheEntry& operator=(const GradientCacheEntry& entry) {
49 if (this != &entry) {
Chris Craik51d6a3d2014-12-22 17:16:56 -080050 copy(entry.colors.get(), entry.positions.get(), entry.count);
Romain Guye5df2312011-08-12 14:23:53 -070051 }
52
53 return *this;
Romain Guy6203f6c2011-08-01 18:56:21 -070054 }
55
Romain Guy059e12c2012-11-28 17:35:51 -080056 hash_t hash() const;
57
58 static int compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs);
59
60 bool operator==(const GradientCacheEntry& other) const {
61 return compare(*this, other) == 0;
62 }
63
64 bool operator!=(const GradientCacheEntry& other) const {
65 return compare(*this, other) != 0;
Romain Guy6203f6c2011-08-01 18:56:21 -070066 }
67
Chris Craik51d6a3d2014-12-22 17:16:56 -080068 std::unique_ptr<uint32_t[]> colors;
69 std::unique_ptr<float[]> positions;
Romain Guy059e12c2012-11-28 17:35:51 -080070 uint32_t count;
Romain Guy6203f6c2011-08-01 18:56:21 -070071
Romain Guye5df2312011-08-12 14:23:53 -070072private:
Romain Guy059e12c2012-11-28 17:35:51 -080073 void copy(uint32_t* colors, float* positions, uint32_t count) {
Romain Guye5df2312011-08-12 14:23:53 -070074 this->count = count;
Chris Craik51d6a3d2014-12-22 17:16:56 -080075 this->colors.reset(new uint32_t[count]);
76 this->positions.reset(new float[count]);
Romain Guye5df2312011-08-12 14:23:53 -070077
Chris Craik51d6a3d2014-12-22 17:16:56 -080078 memcpy(this->colors.get(), colors, count * sizeof(uint32_t));
79 memcpy(this->positions.get(), positions, count * sizeof(float));
Romain Guye5df2312011-08-12 14:23:53 -070080 }
81
Romain Guy6203f6c2011-08-01 18:56:21 -070082}; // GradientCacheEntry
83
Romain Guy059e12c2012-11-28 17:35:51 -080084// Caching support
85
86inline int strictly_order_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
87 return GradientCacheEntry::compare(lhs, rhs) < 0;
88}
89
90inline int compare_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
91 return GradientCacheEntry::compare(lhs, rhs);
92}
93
94inline hash_t hash_type(const GradientCacheEntry& entry) {
95 return entry.hash();
96}
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 */
Chris Craike84a2082014-12-22 14:28:49 -0800113 void operator()(GradientCacheEntry& shader, Texture*& texture) override;
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 Guy42e1e0d2012-07-30 14:47:51 -0700118 Texture* get(uint32_t* colors, float* positions, int count);
119
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,
Romain Guy42e1e0d2012-07-30 14:47:51 -0700144 uint32_t* colors, float* positions, int count);
Romain Guy6203f6c2011-08-01 18:56:21 -0700145
Chris Craike63f7c622013-10-17 10:30:55 -0700146 void generateTexture(uint32_t* colors, float* positions, Texture* texture);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700147
148 struct GradientInfo {
149 uint32_t width;
150 bool hasAlpha;
151 };
152
153 void getGradientInfo(const uint32_t* colors, const int count, GradientInfo& info);
Romain Guyc0ac1932010-07-19 18:43:02 -0700154
Romain Guyb4880042013-04-05 11:17:55 -0700155 size_t bytesPerPixel() const;
156
157 struct GradientColor {
158 float r;
159 float g;
160 float b;
161 float a;
162 };
163
164 typedef void (GradientCache::*ChannelSplitter)(uint32_t inColor,
165 GradientColor& outColor) const;
166
167 void splitToBytes(uint32_t inColor, GradientColor& outColor) const;
168 void splitToFloats(uint32_t inColor, GradientColor& outColor) const;
169
170 typedef void (GradientCache::*ChannelMixer)(GradientColor& start, GradientColor& end,
171 float amount, uint8_t*& dst) const;
172
173 void mixBytes(GradientColor& start, GradientColor& end, float amount, uint8_t*& dst) const;
174 void mixFloats(GradientColor& start, GradientColor& end, float amount, uint8_t*& dst) const;
175
Romain Guy059e12c2012-11-28 17:35:51 -0800176 LruCache<GradientCacheEntry, Texture*> mCache;
Romain Guyc0ac1932010-07-19 18:43:02 -0700177
178 uint32_t mSize;
179 uint32_t mMaxSize;
Romain Guya2341a92010-09-08 18:04:33 -0700180
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700181 GLint mMaxTextureSize;
Romain Guyb4880042013-04-05 11:17:55 -0700182 bool mUseFloatTexture;
183 bool mHasNpot;
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700184
Romain Guyfe48f652010-11-11 15:36:56 -0800185 Vector<SkShader*> mGarbage;
Romain Guya2341a92010-09-08 18:04:33 -0700186 mutable Mutex mLock;
Romain Guyc0ac1932010-07-19 18:43:02 -0700187}; // class GradientCache
188
189}; // namespace uirenderer
190}; // namespace android
191
Romain Guy5b3b3522010-10-27 18:57:51 -0700192#endif // ANDROID_HWUI_GRADIENT_CACHE_H