blob: 3b7c1fa92499d0f605fbb6778c557213fa154627 [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
Derek Sollenberger029f6432012-03-05 16:48:32 -050024#include <utils/Mutex.h>
Romain Guyfe48f652010-11-11 15:36:56 -080025#include <utils/Vector.h>
26
Romain Guyc0ac1932010-07-19 18:43:02 -070027#include "Texture.h"
Romain Guy6203f6c2011-08-01 18:56:21 -070028#include "utils/Compare.h"
Romain Guy21b028a2010-10-08 18:43:58 -070029#include "utils/GenerationCache.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070030
31namespace android {
32namespace uirenderer {
33
Romain Guy6203f6c2011-08-01 18:56:21 -070034struct GradientCacheEntry {
35 GradientCacheEntry() {
36 count = 0;
37 colors = NULL;
38 positions = NULL;
Romain Guy6203f6c2011-08-01 18:56:21 -070039 }
40
Romain Guy42e1e0d2012-07-30 14:47:51 -070041 GradientCacheEntry(uint32_t* colors, float* positions, int count) {
42 copy(colors, positions, count);
Romain Guy6203f6c2011-08-01 18:56:21 -070043 }
44
45 GradientCacheEntry(const GradientCacheEntry& entry) {
Romain Guy42e1e0d2012-07-30 14:47:51 -070046 copy(entry.colors, entry.positions, entry.count);
Romain Guy6203f6c2011-08-01 18:56:21 -070047 }
48
49 ~GradientCacheEntry() {
Romain Guye5df2312011-08-12 14:23:53 -070050 delete[] colors;
51 delete[] positions;
52 }
53
54 GradientCacheEntry& operator=(const GradientCacheEntry& entry) {
55 if (this != &entry) {
56 delete[] colors;
57 delete[] positions;
58
Romain Guy42e1e0d2012-07-30 14:47:51 -070059 copy(entry.colors, entry.positions, entry.count);
Romain Guye5df2312011-08-12 14:23:53 -070060 }
61
62 return *this;
Romain Guy6203f6c2011-08-01 18:56:21 -070063 }
64
65 bool operator<(const GradientCacheEntry& r) const {
66 const GradientCacheEntry& rhs = (const GradientCacheEntry&) r;
67 LTE_INT(count) {
Romain Guy42e1e0d2012-07-30 14:47:51 -070068 int result = memcmp(colors, rhs.colors, count * sizeof(uint32_t));
69 if (result< 0) return true;
70 else if (result == 0) {
71 result = memcmp(positions, rhs.positions, count * sizeof(float));
72 if (result < 0) return true;
Romain Guy6203f6c2011-08-01 18:56:21 -070073 }
74 }
75 return false;
76 }
77
78 uint32_t* colors;
79 float* positions;
80 int count;
81 SkShader::TileMode tileMode;
82
Romain Guye5df2312011-08-12 14:23:53 -070083private:
84
Romain Guy42e1e0d2012-07-30 14:47:51 -070085 void copy(uint32_t* colors, float* positions, int count) {
Romain Guye5df2312011-08-12 14:23:53 -070086 this->count = count;
87 this->colors = new uint32_t[count];
88 this->positions = new float[count];
Romain Guye5df2312011-08-12 14:23:53 -070089
90 memcpy(this->colors, colors, count * sizeof(uint32_t));
91 memcpy(this->positions, positions, count * sizeof(float));
92 }
93
Romain Guy6203f6c2011-08-01 18:56:21 -070094}; // GradientCacheEntry
95
Romain Guyc0ac1932010-07-19 18:43:02 -070096/**
97 * A simple LRU gradient cache. The cache has a maximum size expressed in bytes.
98 * Any texture added to the cache causing the cache to grow beyond the maximum
99 * allowed size will also cause the oldest texture to be kicked out.
100 */
Romain Guy6203f6c2011-08-01 18:56:21 -0700101class GradientCache: public OnEntryRemoved<GradientCacheEntry, Texture*> {
Romain Guyc0ac1932010-07-19 18:43:02 -0700102public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700103 GradientCache();
Romain Guyc0ac1932010-07-19 18:43:02 -0700104 GradientCache(uint32_t maxByteSize);
105 ~GradientCache();
106
107 /**
108 * Used as a callback when an entry is removed from the cache.
109 * Do not invoke directly.
110 */
Romain Guy6203f6c2011-08-01 18:56:21 -0700111 void operator()(GradientCacheEntry& shader, Texture*& texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700112
113 /**
Romain Guyc0ac1932010-07-19 18:43:02 -0700114 * Returns the texture associated with the specified shader.
115 */
Romain Guy42e1e0d2012-07-30 14:47:51 -0700116 Texture* get(uint32_t* colors, float* positions, int count);
117
Romain Guyfe48f652010-11-11 15:36:56 -0800118 /**
Romain Guyc0ac1932010-07-19 18:43:02 -0700119 * Clears the cache. This causes all textures to be deleted.
120 */
121 void clear();
122
123 /**
124 * Sets the maximum size of the cache in bytes.
125 */
126 void setMaxSize(uint32_t maxSize);
127 /**
128 * 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
Romain Guy42e1e0d2012-07-30 14:47:51 -0700144 void generateTexture(uint32_t* colors, float* positions, int count, Texture* texture);
145
146 struct GradientInfo {
147 uint32_t width;
148 bool hasAlpha;
149 };
150
151 void getGradientInfo(const uint32_t* colors, const int count, GradientInfo& info);
Romain Guyc0ac1932010-07-19 18:43:02 -0700152
Romain Guy6203f6c2011-08-01 18:56:21 -0700153 GenerationCache<GradientCacheEntry, Texture*> mCache;
Romain Guyc0ac1932010-07-19 18:43:02 -0700154
155 uint32_t mSize;
156 uint32_t mMaxSize;
Romain Guya2341a92010-09-08 18:04:33 -0700157
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700158 GLint mMaxTextureSize;
159
Romain Guyfe48f652010-11-11 15:36:56 -0800160 Vector<SkShader*> mGarbage;
Romain Guya2341a92010-09-08 18:04:33 -0700161 mutable Mutex mLock;
Romain Guyc0ac1932010-07-19 18:43:02 -0700162}; // class GradientCache
163
164}; // namespace uirenderer
165}; // namespace android
166
Romain Guy5b3b3522010-10-27 18:57:51 -0700167#endif // ANDROID_HWUI_GRADIENT_CACHE_H