blob: 5e35435ed64c0616d21ac6da4c364adf7ab0411b [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 Guy253f2c22016-09-28 17:34:42 -070029#include "FloatColor.h"
30
Romain Guyc0ac1932010-07-19 18:43:02 -070031namespace android {
32namespace uirenderer {
33
Tom Hudson2dc236b2014-10-15 15:46:42 -040034class Texture;
35
Romain Guy6203f6c2011-08-01 18:56:21 -070036struct GradientCacheEntry {
37 GradientCacheEntry() {
38 count = 0;
Chris Craike84a2082014-12-22 14:28:49 -080039 colors = nullptr;
40 positions = nullptr;
Romain Guy6203f6c2011-08-01 18:56:21 -070041 }
42
Romain Guy059e12c2012-11-28 17:35:51 -080043 GradientCacheEntry(uint32_t* colors, float* positions, uint32_t count) {
Romain Guy42e1e0d2012-07-30 14:47:51 -070044 copy(colors, positions, count);
Romain Guy6203f6c2011-08-01 18:56:21 -070045 }
46
47 GradientCacheEntry(const GradientCacheEntry& entry) {
Chris Craik51d6a3d2014-12-22 17:16:56 -080048 copy(entry.colors.get(), entry.positions.get(), entry.count);
Romain Guye5df2312011-08-12 14:23:53 -070049 }
50
51 GradientCacheEntry& operator=(const GradientCacheEntry& entry) {
52 if (this != &entry) {
Chris Craik51d6a3d2014-12-22 17:16:56 -080053 copy(entry.colors.get(), entry.positions.get(), entry.count);
Romain Guye5df2312011-08-12 14:23:53 -070054 }
55
56 return *this;
Romain Guy6203f6c2011-08-01 18:56:21 -070057 }
58
Romain Guy059e12c2012-11-28 17:35:51 -080059 hash_t hash() const;
60
61 static int compare(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs);
62
63 bool operator==(const GradientCacheEntry& other) const {
64 return compare(*this, other) == 0;
65 }
66
67 bool operator!=(const GradientCacheEntry& other) const {
68 return compare(*this, other) != 0;
Romain Guy6203f6c2011-08-01 18:56:21 -070069 }
70
Chris Craik51d6a3d2014-12-22 17:16:56 -080071 std::unique_ptr<uint32_t[]> colors;
72 std::unique_ptr<float[]> positions;
Romain Guy059e12c2012-11-28 17:35:51 -080073 uint32_t count;
Romain Guy6203f6c2011-08-01 18:56:21 -070074
Romain Guye5df2312011-08-12 14:23:53 -070075private:
Romain Guy059e12c2012-11-28 17:35:51 -080076 void copy(uint32_t* colors, float* positions, uint32_t count) {
Romain Guye5df2312011-08-12 14:23:53 -070077 this->count = count;
Chris Craik51d6a3d2014-12-22 17:16:56 -080078 this->colors.reset(new uint32_t[count]);
79 this->positions.reset(new float[count]);
Romain Guye5df2312011-08-12 14:23:53 -070080
Chris Craik51d6a3d2014-12-22 17:16:56 -080081 memcpy(this->colors.get(), colors, count * sizeof(uint32_t));
82 memcpy(this->positions.get(), positions, count * sizeof(float));
Romain Guye5df2312011-08-12 14:23:53 -070083 }
84
Romain Guy6203f6c2011-08-01 18:56:21 -070085}; // GradientCacheEntry
86
Romain Guy059e12c2012-11-28 17:35:51 -080087// Caching support
88
89inline int strictly_order_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
90 return GradientCacheEntry::compare(lhs, rhs) < 0;
91}
92
93inline int compare_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
94 return GradientCacheEntry::compare(lhs, rhs);
95}
96
97inline hash_t hash_type(const GradientCacheEntry& entry) {
98 return entry.hash();
99}
100
Romain Guyc0ac1932010-07-19 18:43:02 -0700101/**
102 * A simple LRU gradient cache. The cache has a maximum size expressed in bytes.
103 * Any texture added to the cache causing the cache to grow beyond the maximum
104 * allowed size will also cause the oldest texture to be kicked out.
105 */
Romain Guy6203f6c2011-08-01 18:56:21 -0700106class GradientCache: public OnEntryRemoved<GradientCacheEntry, Texture*> {
Romain Guyc0ac1932010-07-19 18:43:02 -0700107public:
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -0700108 explicit GradientCache(Extensions& extensions);
Romain Guyc0ac1932010-07-19 18:43:02 -0700109 ~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 /**
Romain Guyc0ac1932010-07-19 18:43:02 -0700128 * Returns the maximum size of the cache in bytes.
129 */
130 uint32_t getMaxSize();
131 /**
132 * Returns the current size of the cache in bytes.
133 */
134 uint32_t getSize();
135
136private:
Romain Guy6203f6c2011-08-01 18:56:21 -0700137 /**
138 * Adds a new linear gradient to the cache. The generated texture is
139 * returned.
140 */
141 Texture* addLinearGradient(GradientCacheEntry& gradient,
Romain Guy42e1e0d2012-07-30 14:47:51 -0700142 uint32_t* colors, float* positions, int count);
Romain Guy6203f6c2011-08-01 18:56:21 -0700143
John Reck38e0c322015-11-10 12:19:17 -0800144 void generateTexture(uint32_t* colors, float* positions,
145 const uint32_t width, const uint32_t height, Texture* texture);
Romain Guy42e1e0d2012-07-30 14:47:51 -0700146
147 struct GradientInfo {
148 uint32_t width;
149 bool hasAlpha;
150 };
151
152 void getGradientInfo(const uint32_t* colors, const int count, GradientInfo& info);
Romain Guyc0ac1932010-07-19 18:43:02 -0700153
Romain Guyb4880042013-04-05 11:17:55 -0700154 size_t bytesPerPixel() const;
Romain Guy253f2c22016-09-28 17:34:42 -0700155 size_t sourceBytesPerPixel() const;
Romain Guyb4880042013-04-05 11:17:55 -0700156
Romain Guy8762e332016-10-12 12:14:07 -0700157 typedef void (GradientCache::*ChannelMixer)(const FloatColor& start, const FloatColor& end,
Romain Guyb4880042013-04-05 11:17:55 -0700158 float amount, uint8_t*& dst) const;
159
Romain Guy8762e332016-10-12 12:14:07 -0700160 void mixBytes(const FloatColor& start, const FloatColor& end,
161 float amount, uint8_t*& dst) const;
162 void mixFloats(const FloatColor& start, const FloatColor& end,
163 float amount, uint8_t*& dst) const;
Romain Guyb4880042013-04-05 11:17:55 -0700164
Romain Guy059e12c2012-11-28 17:35:51 -0800165 LruCache<GradientCacheEntry, Texture*> mCache;
Romain Guyc0ac1932010-07-19 18:43:02 -0700166
167 uint32_t mSize;
Chris Craik48a8f432016-02-05 15:59:29 -0800168 const uint32_t mMaxSize;
Romain Guya2341a92010-09-08 18:04:33 -0700169
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700170 GLint mMaxTextureSize;
Romain Guyb4880042013-04-05 11:17:55 -0700171 bool mUseFloatTexture;
172 bool mHasNpot;
Romain Guy253f2c22016-09-28 17:34:42 -0700173 bool mHasSRGB;
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700174
Romain Guya2341a92010-09-08 18:04:33 -0700175 mutable Mutex mLock;
Romain Guyc0ac1932010-07-19 18:43:02 -0700176}; // class GradientCache
177
178}; // namespace uirenderer
179}; // namespace android
180
Romain Guy5b3b3522010-10-27 18:57:51 -0700181#endif // ANDROID_HWUI_GRADIENT_CACHE_H