blob: 9957370a5ae641b3c530beca41af9bffe01f646f [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 Guyc0ac1932010-07-19 18:43:02 -070026#include "GradientCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070027#include "Properties.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070028
29namespace android {
30namespace uirenderer {
31
32///////////////////////////////////////////////////////////////////////////////
33// Constructors/destructor
34///////////////////////////////////////////////////////////////////////////////
35
Romain Guyfb8b7632010-08-23 21:05:08 -070036GradientCache::GradientCache():
37 mCache(GenerationCache<SkShader*, Texture*>::kUnlimitedCapacity),
38 mSize(0), mMaxSize(MB(DEFAULT_GRADIENT_CACHE_SIZE)) {
39 char property[PROPERTY_VALUE_MAX];
40 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
41 LOGD(" Setting gradient cache size to %sMB", property);
42 setMaxSize(MB(atof(property)));
43 } else {
44 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
45 }
46
47 mCache.setOnEntryRemovedListener(this);
48}
49
Romain Guyc0ac1932010-07-19 18:43:02 -070050GradientCache::GradientCache(uint32_t maxByteSize):
51 mCache(GenerationCache<SkShader*, Texture*>::kUnlimitedCapacity),
52 mSize(0), mMaxSize(maxByteSize) {
53 mCache.setOnEntryRemovedListener(this);
54}
55
56GradientCache::~GradientCache() {
Romain Guya2341a92010-09-08 18:04:33 -070057 Mutex::Autolock _l(mLock);
Romain Guyc0ac1932010-07-19 18:43:02 -070058 mCache.clear();
59}
60
61///////////////////////////////////////////////////////////////////////////////
62// Size management
63///////////////////////////////////////////////////////////////////////////////
64
65uint32_t GradientCache::getSize() {
Romain Guya2341a92010-09-08 18:04:33 -070066 Mutex::Autolock _l(mLock);
Romain Guyc0ac1932010-07-19 18:43:02 -070067 return mSize;
68}
69
70uint32_t GradientCache::getMaxSize() {
Romain Guya2341a92010-09-08 18:04:33 -070071 Mutex::Autolock _l(mLock);
Romain Guyc0ac1932010-07-19 18:43:02 -070072 return mMaxSize;
73}
74
75void GradientCache::setMaxSize(uint32_t maxSize) {
Romain Guya2341a92010-09-08 18:04:33 -070076 Mutex::Autolock _l(mLock);
Romain Guyc0ac1932010-07-19 18:43:02 -070077 mMaxSize = maxSize;
78 while (mSize > mMaxSize) {
79 mCache.removeOldest();
80 }
81}
82
83///////////////////////////////////////////////////////////////////////////////
84// Callbacks
85///////////////////////////////////////////////////////////////////////////////
86
87void GradientCache::operator()(SkShader*& shader, Texture*& texture) {
Romain Guya2341a92010-09-08 18:04:33 -070088 // Already locked here
Romain Guyc0ac1932010-07-19 18:43:02 -070089 if (shader) {
90 const uint32_t size = texture->width * texture->height * 4;
91 mSize -= size;
92 }
93
94 if (texture) {
95 glDeleteTextures(1, &texture->id);
96 delete texture;
97 }
98}
99
100///////////////////////////////////////////////////////////////////////////////
101// Caching
102///////////////////////////////////////////////////////////////////////////////
103
104Texture* GradientCache::get(SkShader* shader) {
Romain Guya2341a92010-09-08 18:04:33 -0700105 Mutex::Autolock _l(mLock);
Romain Guy9cccc2b2010-08-07 23:46:15 -0700106 return mCache.get(shader);
Romain Guyc0ac1932010-07-19 18:43:02 -0700107}
108
109void GradientCache::remove(SkShader* shader) {
Romain Guya2341a92010-09-08 18:04:33 -0700110 Mutex::Autolock _l(mLock);
Romain Guyc0ac1932010-07-19 18:43:02 -0700111 mCache.remove(shader);
112}
113
114void GradientCache::clear() {
Romain Guya2341a92010-09-08 18:04:33 -0700115 Mutex::Autolock _l(mLock);
Romain Guyc0ac1932010-07-19 18:43:02 -0700116 mCache.clear();
117}
118
119Texture* GradientCache::addLinearGradient(SkShader* shader, float* bounds, uint32_t* colors,
120 float* positions, int count, SkShader::TileMode tileMode) {
121 SkBitmap bitmap;
122 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1024, 1);
123 bitmap.allocPixels();
124 bitmap.eraseColor(0);
125
126 SkCanvas canvas(bitmap);
127
128 SkPoint points[2];
129 points[0].set(0.0f, 0.0f);
130 points[1].set(bitmap.width(), 0.0f);
131
132 SkShader* localShader = SkGradientShader::CreateLinear(points,
133 reinterpret_cast<const SkColor*>(colors), positions, count, tileMode);
134
135 SkPaint p;
136 p.setStyle(SkPaint::kStrokeAndFill_Style);
137 p.setShader(localShader)->unref();
138
139 canvas.drawRectCoords(0.0f, 0.0f, bitmap.width(), 1.0f, p);
140
Romain Guya2341a92010-09-08 18:04:33 -0700141 mLock.lock();
Romain Guyc0ac1932010-07-19 18:43:02 -0700142 // Asume the cache is always big enough
143 const uint32_t size = bitmap.rowBytes() * bitmap.height();
144 while (mSize + size > mMaxSize) {
145 mCache.removeOldest();
146 }
Romain Guya2341a92010-09-08 18:04:33 -0700147 mLock.unlock();
Romain Guyc0ac1932010-07-19 18:43:02 -0700148
149 Texture* texture = new Texture;
150 generateTexture(&bitmap, texture);
151
Romain Guya2341a92010-09-08 18:04:33 -0700152 mLock.lock();
Romain Guyc0ac1932010-07-19 18:43:02 -0700153 mSize += size;
154 mCache.put(shader, texture);
Romain Guya2341a92010-09-08 18:04:33 -0700155 mLock.unlock();
Romain Guyc0ac1932010-07-19 18:43:02 -0700156
157 return texture;
158}
159
160void GradientCache::generateTexture(SkBitmap* bitmap, Texture* texture) {
161 SkAutoLockPixels autoLock(*bitmap);
162 if (!bitmap->readyToDraw()) {
163 LOGE("Cannot generate texture from shader");
164 return;
165 }
166
167 texture->generation = bitmap->getGenerationID();
168 texture->width = bitmap->width();
169 texture->height = bitmap->height();
170
171 glGenTextures(1, &texture->id);
172
173 glBindTexture(GL_TEXTURE_2D, texture->id);
174 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
175
176 texture->blend = !bitmap->isOpaque();
177 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0,
178 GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels());
179
180 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
181 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
182
183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
185}
186
187}; // namespace uirenderer
188}; // namespace android