blob: d183a851a2befba5fd75611c6c9e338c85dc9126 [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 Guy8dcfd5e2012-07-20 11:36:03 -070020#include <GLES2/gl2.h>
21
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 -070028#include "Texture.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070029
30namespace android {
31namespace uirenderer {
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 SkShader::TileMode tileMode;
80
Romain Guye5df2312011-08-12 14:23:53 -070081private:
82
Romain Guy059e12c2012-11-28 17:35:51 -080083 void copy(uint32_t* colors, float* positions, uint32_t count) {
Romain Guye5df2312011-08-12 14:23:53 -070084 this->count = count;
85 this->colors = new uint32_t[count];
86 this->positions = new float[count];
Romain Guye5df2312011-08-12 14:23:53 -070087
88 memcpy(this->colors, colors, count * sizeof(uint32_t));
89 memcpy(this->positions, positions, count * sizeof(float));
90 }
91
Romain Guy6203f6c2011-08-01 18:56:21 -070092}; // GradientCacheEntry
93
Romain Guy059e12c2012-11-28 17:35:51 -080094// Caching support
95
96inline int strictly_order_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
97 return GradientCacheEntry::compare(lhs, rhs) < 0;
98}
99
100inline int compare_type(const GradientCacheEntry& lhs, const GradientCacheEntry& rhs) {
101 return GradientCacheEntry::compare(lhs, rhs);
102}
103
104inline hash_t hash_type(const GradientCacheEntry& entry) {
105 return entry.hash();
106}
107
Romain Guyc0ac1932010-07-19 18:43:02 -0700108/**
109 * A simple LRU gradient cache. The cache has a maximum size expressed in bytes.
110 * Any texture added to the cache causing the cache to grow beyond the maximum
111 * allowed size will also cause the oldest texture to be kicked out.
112 */
Romain Guy6203f6c2011-08-01 18:56:21 -0700113class GradientCache: public OnEntryRemoved<GradientCacheEntry, Texture*> {
Romain Guyc0ac1932010-07-19 18:43:02 -0700114public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700115 GradientCache();
Romain Guyc0ac1932010-07-19 18:43:02 -0700116 GradientCache(uint32_t maxByteSize);
117 ~GradientCache();
118
119 /**
120 * Used as a callback when an entry is removed from the cache.
121 * Do not invoke directly.
122 */
Romain Guy6203f6c2011-08-01 18:56:21 -0700123 void operator()(GradientCacheEntry& shader, Texture*& texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700124
125 /**
Romain Guyc0ac1932010-07-19 18:43:02 -0700126 * Returns the texture associated with the specified shader.
127 */
Romain Guy42e1e0d2012-07-30 14:47:51 -0700128 Texture* get(uint32_t* colors, float* positions, int count);
129
Romain Guyfe48f652010-11-11 15:36:56 -0800130 /**
Romain Guyc0ac1932010-07-19 18:43:02 -0700131 * Clears the cache. This causes all textures to be deleted.
132 */
133 void clear();
134
135 /**
136 * Sets the maximum size of the cache in bytes.
137 */
138 void setMaxSize(uint32_t maxSize);
139 /**
140 * Returns the maximum size of the cache in bytes.
141 */
142 uint32_t getMaxSize();
143 /**
144 * Returns the current size of the cache in bytes.
145 */
146 uint32_t getSize();
147
148private:
Romain Guy6203f6c2011-08-01 18:56:21 -0700149 /**
150 * Adds a new linear gradient to the cache. The generated texture is
151 * returned.
152 */
153 Texture* addLinearGradient(GradientCacheEntry& gradient,
Romain Guy42e1e0d2012-07-30 14:47:51 -0700154 uint32_t* colors, float* positions, int count);
Romain Guy6203f6c2011-08-01 18:56:21 -0700155
Romain Guy42e1e0d2012-07-30 14:47:51 -0700156 void generateTexture(uint32_t* colors, float* positions, int count, Texture* texture);
157
158 struct GradientInfo {
159 uint32_t width;
160 bool hasAlpha;
161 };
162
163 void getGradientInfo(const uint32_t* colors, const int count, GradientInfo& info);
Romain Guyc0ac1932010-07-19 18:43:02 -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;
168 uint32_t mMaxSize;
Romain Guya2341a92010-09-08 18:04:33 -0700169
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700170 GLint mMaxTextureSize;
171
Romain Guyfe48f652010-11-11 15:36:56 -0800172 Vector<SkShader*> mGarbage;
Romain Guya2341a92010-09-08 18:04:33 -0700173 mutable Mutex mLock;
Romain Guyc0ac1932010-07-19 18:43:02 -0700174}; // class GradientCache
175
176}; // namespace uirenderer
177}; // namespace android
178
Romain Guy5b3b3522010-10-27 18:57:51 -0700179#endif // ANDROID_HWUI_GRADIENT_CACHE_H