blob: 1714e6d6523a9124f1a9d79088c67cc4867369ae [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
Chris Craik96a5c4c2015-01-27 15:46:35 -080020#include <memory>
21
Romain Guyb4880042013-04-05 11:17:55 -070022#include <GLES3/gl3.h>
Romain Guy8dcfd5e2012-07-20 11:36:03 -070023
Romain Guyc0ac1932010-07-19 18:43:02 -070024#include <SkShader.h>
25
Romain Guy059e12c2012-11-28 17:35:51 -080026#include <utils/LruCache.h>
Derek Sollenberger029f6432012-03-05 16:48:32 -050027#include <utils/Mutex.h>
Romain Guyfe48f652010-11-11 15:36:56 -080028#include <utils/Vector.h>
29
Romain Guyc0ac1932010-07-19 18:43:02 -070030namespace android {
31namespace uirenderer {
32
Tom Hudson2dc236b2014-10-15 15:46:42 -040033class Texture;
34
Romain Guy6203f6c2011-08-01 18:56:21 -070035struct GradientCacheEntry {
36 GradientCacheEntry() {
37 count = 0;
Chris Craike84a2082014-12-22 14:28:49 -080038 colors = nullptr;
39 positions = nullptr;
Romain Guy6203f6c2011-08-01 18:56:21 -070040 }
41
Romain Guy059e12c2012-11-28 17:35:51 -080042 GradientCacheEntry(uint32_t* colors, float* positions, uint32_t count) {
Romain Guy42e1e0d2012-07-30 14:47:51 -070043 copy(colors, positions, count);
Romain Guy6203f6c2011-08-01 18:56:21 -070044 }
45
46 GradientCacheEntry(const GradientCacheEntry& entry) {
Chris Craik51d6a3d2014-12-22 17:16:56 -080047 copy(entry.colors.get(), entry.positions.get(), entry.count);
Romain Guye5df2312011-08-12 14:23:53 -070048 }
49
50 GradientCacheEntry& operator=(const GradientCacheEntry& entry) {
51 if (this != &entry) {
Chris Craik51d6a3d2014-12-22 17:16:56 -080052 copy(entry.colors.get(), entry.positions.get(), entry.count);
Romain Guye5df2312011-08-12 14:23:53 -070053 }
54
55 return *this;
Romain Guy6203f6c2011-08-01 18:56:21 -070056 }
57
Romain Guy059e12c2012-11-28 17:35:51 -080058 hash_t hash() const;
59
60 static int compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs);
61
62 bool operator==(const GradientCacheEntry& other) const {
63 return compare(*this, other) == 0;
64 }
65
66 bool operator!=(const GradientCacheEntry& other) const {
67 return compare(*this, other) != 0;
Romain Guy6203f6c2011-08-01 18:56:21 -070068 }
69
Chris Craik51d6a3d2014-12-22 17:16:56 -080070 std::unique_ptr<uint32_t[]> colors;
71 std::unique_ptr<float[]> positions;
Romain Guy059e12c2012-11-28 17:35:51 -080072 uint32_t count;
Romain Guy6203f6c2011-08-01 18:56:21 -070073
Romain Guye5df2312011-08-12 14:23:53 -070074private:
Romain Guy059e12c2012-11-28 17:35:51 -080075 void copy(uint32_t* colors, float* positions, uint32_t count) {
Romain Guye5df2312011-08-12 14:23:53 -070076 this->count = count;
Chris Craik51d6a3d2014-12-22 17:16:56 -080077 this->colors.reset(new uint32_t[count]);
78 this->positions.reset(new float[count]);
Romain Guye5df2312011-08-12 14:23:53 -070079
Chris Craik51d6a3d2014-12-22 17:16:56 -080080 memcpy(this->colors.get(), colors, count * sizeof(uint32_t));
81 memcpy(this->positions.get(), positions, count * sizeof(float));
Romain Guye5df2312011-08-12 14:23:53 -070082 }
83
Romain Guy6203f6c2011-08-01 18:56:21 -070084}; // GradientCacheEntry
85
Romain Guy059e12c2012-11-28 17:35:51 -080086// Caching support
87
88inline int strictly_order_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
89 return GradientCacheEntry::compare(lhs, rhs) < 0;
90}
91
92inline int compare_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
93 return GradientCacheEntry::compare(lhs, rhs);
94}
95
96inline hash_t hash_type(const GradientCacheEntry& entry) {
97 return entry.hash();
98}
99
Romain Guyc0ac1932010-07-19 18:43:02 -0700100/**
101 * A simple LRU gradient cache. The cache has a maximum size expressed in bytes.
102 * Any texture added to the cache causing the cache to grow beyond the maximum
103 * allowed size will also cause the oldest texture to be kicked out.
104 */
Romain Guy6203f6c2011-08-01 18:56:21 -0700105class GradientCache: public OnEntryRemoved<GradientCacheEntry, Texture*> {
Romain Guyc0ac1932010-07-19 18:43:02 -0700106public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700107 GradientCache();
Romain Guyc0ac1932010-07-19 18:43:02 -0700108 GradientCache(uint32_t maxByteSize);
109 ~GradientCache();
110
111 /**
112 * Used as a callback when an entry is removed from the cache.
113 * Do not invoke directly.
114 */
Chris Craike84a2082014-12-22 14:28:49 -0800115 void operator()(GradientCacheEntry& shader, Texture*& texture) override;
Romain Guyc0ac1932010-07-19 18:43:02 -0700116
117 /**
Romain Guyc0ac1932010-07-19 18:43:02 -0700118 * Returns the texture associated with the specified shader.
119 */
Romain Guy42e1e0d2012-07-30 14:47:51 -0700120 Texture* get(uint32_t* colors, float* positions, int count);
121
Romain Guyfe48f652010-11-11 15:36:56 -0800122 /**
Romain Guyc0ac1932010-07-19 18:43:02 -0700123 * Clears the cache. This causes all textures to be deleted.
124 */
125 void clear();
126
127 /**
128 * Sets the maximum size of the cache in bytes.
129 */
130 void setMaxSize(uint32_t maxSize);
131 /**
132 * Returns the maximum size of the cache in bytes.
133 */
134 uint32_t getMaxSize();
135 /**
136 * Returns the current size of the cache in bytes.
137 */
138 uint32_t getSize();
139
140private:
Romain Guy6203f6c2011-08-01 18:56:21 -0700141 /**
142 * Adds a new linear gradient to the cache. The generated texture is
143 * returned.
144 */
145 Texture* addLinearGradient(GradientCacheEntry& gradient,
Romain Guy42e1e0d2012-07-30 14:47:51 -0700146 uint32_t* colors, float* positions, int count);
Romain Guy6203f6c2011-08-01 18:56:21 -0700147
Chris Craike63f7c622013-10-17 10:30:55 -0700148 void generateTexture(uint32_t* colors, float* positions, Texture* texture);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700149
150 struct GradientInfo {
151 uint32_t width;
152 bool hasAlpha;
153 };
154
155 void getGradientInfo(const uint32_t* colors, const int count, GradientInfo& info);
Romain Guyc0ac1932010-07-19 18:43:02 -0700156
Romain Guyb4880042013-04-05 11:17:55 -0700157 size_t bytesPerPixel() const;
158
159 struct GradientColor {
160 float r;
161 float g;
162 float b;
163 float a;
164 };
165
166 typedef void (GradientCache::*ChannelSplitter)(uint32_t inColor,
167 GradientColor& outColor) const;
168
169 void splitToBytes(uint32_t inColor, GradientColor& outColor) const;
170 void splitToFloats(uint32_t inColor, GradientColor& outColor) const;
171
172 typedef void (GradientCache::*ChannelMixer)(GradientColor& start, GradientColor& end,
173 float amount, uint8_t*& dst) const;
174
175 void mixBytes(GradientColor& start, GradientColor& end, float amount, uint8_t*& dst) const;
176 void mixFloats(GradientColor& start, GradientColor& end, float amount, uint8_t*& dst) const;
177
Romain Guy059e12c2012-11-28 17:35:51 -0800178 LruCache<GradientCacheEntry, Texture*> mCache;
Romain Guyc0ac1932010-07-19 18:43:02 -0700179
180 uint32_t mSize;
181 uint32_t mMaxSize;
Romain Guya2341a92010-09-08 18:04:33 -0700182
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700183 GLint mMaxTextureSize;
Romain Guyb4880042013-04-05 11:17:55 -0700184 bool mUseFloatTexture;
185 bool mHasNpot;
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700186
Romain Guyfe48f652010-11-11 15:36:56 -0800187 Vector<SkShader*> mGarbage;
Romain Guya2341a92010-09-08 18:04:33 -0700188 mutable Mutex mLock;
Romain Guyc0ac1932010-07-19 18:43:02 -0700189}; // class GradientCache
190
191}; // namespace uirenderer
192}; // namespace android
193
Romain Guy5b3b3522010-10-27 18:57:51 -0700194#endif // ANDROID_HWUI_GRADIENT_CACHE_H