blob: 4a40a63485acdfafe06cc019a98f0aa53aee6afa [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
17#define LOG_TAG "OpenGLRenderer"
18
19#include <GLES2/gl2.h>
20
21#include <SkCanvas.h>
22#include <SkGradientShader.h>
23
Romain Guya2341a92010-09-08 18:04:33 -070024#include <utils/threads.h>
25
Romain Guyc9855a52011-01-21 21:14:15 -080026#include "Debug.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070027#include "GradientCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070028#include "Properties.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070029
30namespace android {
31namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Constructors/destructor
35///////////////////////////////////////////////////////////////////////////////
36
Romain Guyfb8b7632010-08-23 21:05:08 -070037GradientCache::GradientCache():
38 mCache(GenerationCache<SkShader*, Texture*>::kUnlimitedCapacity),
39 mSize(0), mMaxSize(MB(DEFAULT_GRADIENT_CACHE_SIZE)) {
40 char property[PROPERTY_VALUE_MAX];
41 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080042 INIT_LOGD(" Setting gradient cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070043 setMaxSize(MB(atof(property)));
44 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080045 INIT_LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070046 }
47
48 mCache.setOnEntryRemovedListener(this);
49}
50
Romain Guyc0ac1932010-07-19 18:43:02 -070051GradientCache::GradientCache(uint32_t maxByteSize):
52 mCache(GenerationCache<SkShader*, Texture*>::kUnlimitedCapacity),
53 mSize(0), mMaxSize(maxByteSize) {
54 mCache.setOnEntryRemovedListener(this);
55}
56
57GradientCache::~GradientCache() {
58 mCache.clear();
59}
60
61///////////////////////////////////////////////////////////////////////////////
62// Size management
63///////////////////////////////////////////////////////////////////////////////
64
65uint32_t GradientCache::getSize() {
66 return mSize;
67}
68
69uint32_t GradientCache::getMaxSize() {
70 return mMaxSize;
71}
72
73void GradientCache::setMaxSize(uint32_t maxSize) {
74 mMaxSize = maxSize;
75 while (mSize > mMaxSize) {
76 mCache.removeOldest();
77 }
78}
79
80///////////////////////////////////////////////////////////////////////////////
81// Callbacks
82///////////////////////////////////////////////////////////////////////////////
83
84void GradientCache::operator()(SkShader*& shader, Texture*& texture) {
Romain Guya2341a92010-09-08 18:04:33 -070085 // Already locked here
Romain Guyc0ac1932010-07-19 18:43:02 -070086 if (shader) {
87 const uint32_t size = texture->width * texture->height * 4;
88 mSize -= size;
89 }
90
91 if (texture) {
92 glDeleteTextures(1, &texture->id);
93 delete texture;
94 }
95}
96
97///////////////////////////////////////////////////////////////////////////////
98// Caching
99///////////////////////////////////////////////////////////////////////////////
100
101Texture* GradientCache::get(SkShader* shader) {
Romain Guy9cccc2b2010-08-07 23:46:15 -0700102 return mCache.get(shader);
Romain Guyc0ac1932010-07-19 18:43:02 -0700103}
104
105void GradientCache::remove(SkShader* shader) {
106 mCache.remove(shader);
107}
108
Romain Guyfe48f652010-11-11 15:36:56 -0800109void GradientCache::removeDeferred(SkShader* shader) {
Romain Guya2341a92010-09-08 18:04:33 -0700110 Mutex::Autolock _l(mLock);
Romain Guyfe48f652010-11-11 15:36:56 -0800111 mGarbage.push(shader);
112}
113
114void GradientCache::clearGarbage() {
115 Mutex::Autolock _l(mLock);
116 size_t count = mGarbage.size();
117 for (size_t i = 0; i < count; i++) {
118 mCache.remove(mGarbage.itemAt(i));
119 }
120 mGarbage.clear();
121}
122
123void GradientCache::clear() {
Romain Guyc0ac1932010-07-19 18:43:02 -0700124 mCache.clear();
125}
126
Romain Guyee916f12010-09-20 17:53:08 -0700127Texture* GradientCache::addLinearGradient(SkShader* shader, uint32_t* colors,
Romain Guyc0ac1932010-07-19 18:43:02 -0700128 float* positions, int count, SkShader::TileMode tileMode) {
129 SkBitmap bitmap;
130 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1024, 1);
131 bitmap.allocPixels();
132 bitmap.eraseColor(0);
133
134 SkCanvas canvas(bitmap);
135
136 SkPoint points[2];
137 points[0].set(0.0f, 0.0f);
138 points[1].set(bitmap.width(), 0.0f);
139
140 SkShader* localShader = SkGradientShader::CreateLinear(points,
141 reinterpret_cast<const SkColor*>(colors), positions, count, tileMode);
142
143 SkPaint p;
144 p.setStyle(SkPaint::kStrokeAndFill_Style);
145 p.setShader(localShader)->unref();
146
147 canvas.drawRectCoords(0.0f, 0.0f, bitmap.width(), 1.0f, p);
148
149 // Asume the cache is always big enough
150 const uint32_t size = bitmap.rowBytes() * bitmap.height();
151 while (mSize + size > mMaxSize) {
152 mCache.removeOldest();
153 }
154
155 Texture* texture = new Texture;
156 generateTexture(&bitmap, texture);
157
158 mSize += size;
159 mCache.put(shader, texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700160
161 return texture;
162}
163
164void GradientCache::generateTexture(SkBitmap* bitmap, Texture* texture) {
165 SkAutoLockPixels autoLock(*bitmap);
166 if (!bitmap->readyToDraw()) {
167 LOGE("Cannot generate texture from shader");
168 return;
169 }
170
171 texture->generation = bitmap->getGenerationID();
172 texture->width = bitmap->width();
173 texture->height = bitmap->height();
174
175 glGenTextures(1, &texture->id);
176
177 glBindTexture(GL_TEXTURE_2D, texture->id);
178 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
179
180 texture->blend = !bitmap->isOpaque();
181 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0,
182 GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels());
183
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
185 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
186
187 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
188 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
189}
190
191}; // namespace uirenderer
192}; // namespace android