blob: 49be19ab1c81d62ac4e2a76e79f37a05253a4f40 [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
Romain Guyc0ac1932010-07-19 18:43:02 -070029namespace android {
30namespace uirenderer {
31
Tom Hudson2dc236b2014-10-15 15:46:42 -040032class Texture;
33
Romain Guy6203f6c2011-08-01 18:56:21 -070034struct GradientCacheEntry {
35 GradientCacheEntry() {
36 count = 0;
Chris Craike84a2082014-12-22 14:28:49 -080037 colors = nullptr;
38 positions = nullptr;
Romain Guy6203f6c2011-08-01 18:56:21 -070039 }
40
Romain Guy059e12c2012-11-28 17:35:51 -080041 GradientCacheEntry(uint32_t* colors, float* positions, uint32_t count) {
Romain Guy42e1e0d2012-07-30 14:47:51 -070042 copy(colors, positions, count);
Romain Guy6203f6c2011-08-01 18:56:21 -070043 }
44
45 GradientCacheEntry(const GradientCacheEntry& entry) {
Chris Craik51d6a3d2014-12-22 17:16:56 -080046 copy(entry.colors.get(), entry.positions.get(), entry.count);
Romain Guye5df2312011-08-12 14:23:53 -070047 }
48
49 GradientCacheEntry& operator=(const GradientCacheEntry& entry) {
50 if (this != &entry) {
Chris Craik51d6a3d2014-12-22 17:16:56 -080051 copy(entry.colors.get(), entry.positions.get(), entry.count);
Romain Guye5df2312011-08-12 14:23:53 -070052 }
53
54 return *this;
Romain Guy6203f6c2011-08-01 18:56:21 -070055 }
56
Romain Guy059e12c2012-11-28 17:35:51 -080057 hash_t hash() const;
58
59 static int compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs);
60
61 bool operator==(const GradientCacheEntry& other) const {
62 return compare(*this, other) == 0;
63 }
64
65 bool operator!=(const GradientCacheEntry& other) const {
66 return compare(*this, other) != 0;
Romain Guy6203f6c2011-08-01 18:56:21 -070067 }
68
Chris Craik51d6a3d2014-12-22 17:16:56 -080069 std::unique_ptr<uint32_t[]> colors;
70 std::unique_ptr<float[]> positions;
Romain Guy059e12c2012-11-28 17:35:51 -080071 uint32_t count;
Romain Guy6203f6c2011-08-01 18:56:21 -070072
Romain Guye5df2312011-08-12 14:23:53 -070073private:
Romain Guy059e12c2012-11-28 17:35:51 -080074 void copy(uint32_t* colors, float* positions, uint32_t count) {
Romain Guye5df2312011-08-12 14:23:53 -070075 this->count = count;
Chris Craik51d6a3d2014-12-22 17:16:56 -080076 this->colors.reset(new uint32_t[count]);
77 this->positions.reset(new float[count]);
Romain Guye5df2312011-08-12 14:23:53 -070078
Chris Craik51d6a3d2014-12-22 17:16:56 -080079 memcpy(this->colors.get(), colors, count * sizeof(uint32_t));
80 memcpy(this->positions.get(), positions, count * sizeof(float));
Romain Guye5df2312011-08-12 14:23:53 -070081 }
82
Romain Guy6203f6c2011-08-01 18:56:21 -070083}; // GradientCacheEntry
84
Romain Guy059e12c2012-11-28 17:35:51 -080085// Caching support
86
87inline int strictly_order_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
88 return GradientCacheEntry::compare(lhs, rhs) < 0;
89}
90
91inline int compare_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
92 return GradientCacheEntry::compare(lhs, rhs);
93}
94
95inline hash_t hash_type(const GradientCacheEntry& entry) {
96 return entry.hash();
97}
98
Romain Guyc0ac1932010-07-19 18:43:02 -070099/**
100 * A simple LRU gradient cache. The cache has a maximum size expressed in bytes.
101 * Any texture added to the cache causing the cache to grow beyond the maximum
102 * allowed size will also cause the oldest texture to be kicked out.
103 */
Romain Guy6203f6c2011-08-01 18:56:21 -0700104class GradientCache: public OnEntryRemoved<GradientCacheEntry, Texture*> {
Romain Guyc0ac1932010-07-19 18:43:02 -0700105public:
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -0700106 explicit GradientCache(Extensions& extensions);
Romain Guyc0ac1932010-07-19 18:43:02 -0700107 ~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 /**
Romain Guyc0ac1932010-07-19 18:43:02 -0700126 * Returns the maximum size of the cache in bytes.
127 */
128 uint32_t getMaxSize();
129 /**
130 * Returns the current size of the cache in bytes.
131 */
132 uint32_t getSize();
133
134private:
Romain Guy6203f6c2011-08-01 18:56:21 -0700135 /**
136 * Adds a new linear gradient to the cache. The generated texture is
137 * returned.
138 */
139 Texture* addLinearGradient(GradientCacheEntry& gradient,
Romain Guy42e1e0d2012-07-30 14:47:51 -0700140 uint32_t* colors, float* positions, int count);
Romain Guy6203f6c2011-08-01 18:56:21 -0700141
John Reck38e0c322015-11-10 12:19:17 -0800142 void generateTexture(uint32_t* colors, float* positions,
143 const uint32_t width, const uint32_t height, Texture* texture);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700144
145 struct GradientInfo {
146 uint32_t width;
147 bool hasAlpha;
148 };
149
150 void getGradientInfo(const uint32_t* colors, const int count, GradientInfo& info);
Romain Guyc0ac1932010-07-19 18:43:02 -0700151
Romain Guyb4880042013-04-05 11:17:55 -0700152 size_t bytesPerPixel() const;
153
154 struct GradientColor {
155 float r;
156 float g;
157 float b;
158 float a;
159 };
160
161 typedef void (GradientCache::*ChannelSplitter)(uint32_t inColor,
162 GradientColor& outColor) const;
163
164 void splitToBytes(uint32_t inColor, GradientColor& outColor) const;
165 void splitToFloats(uint32_t inColor, GradientColor& outColor) const;
166
167 typedef void (GradientCache::*ChannelMixer)(GradientColor& start, GradientColor& end,
168 float amount, uint8_t*& dst) const;
169
170 void mixBytes(GradientColor& start, GradientColor& end, float amount, uint8_t*& dst) const;
171 void mixFloats(GradientColor& start, GradientColor& end, float amount, uint8_t*& dst) const;
172
Romain Guy059e12c2012-11-28 17:35:51 -0800173 LruCache<GradientCacheEntry, Texture*> mCache;
Romain Guyc0ac1932010-07-19 18:43:02 -0700174
175 uint32_t mSize;
Chris Craik48a8f432016-02-05 15:59:29 -0800176 const uint32_t mMaxSize;
Romain Guya2341a92010-09-08 18:04:33 -0700177
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700178 GLint mMaxTextureSize;
Romain Guyb4880042013-04-05 11:17:55 -0700179 bool mUseFloatTexture;
180 bool mHasNpot;
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700181
Romain Guya2341a92010-09-08 18:04:33 -0700182 mutable Mutex mLock;
Romain Guyc0ac1932010-07-19 18:43:02 -0700183}; // class GradientCache
184
185}; // namespace uirenderer
186}; // namespace android
187
Romain Guy5b3b3522010-10-27 18:57:51 -0700188#endif // ANDROID_HWUI_GRADIENT_CACHE_H