blob: a37964eae4cdbcf5673f2db73263d1a0d69090cc [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() {
57 mCache.clear();
58}
59
60///////////////////////////////////////////////////////////////////////////////
61// Size management
62///////////////////////////////////////////////////////////////////////////////
63
64uint32_t GradientCache::getSize() {
65 return mSize;
66}
67
68uint32_t GradientCache::getMaxSize() {
69 return mMaxSize;
70}
71
72void GradientCache::setMaxSize(uint32_t maxSize) {
73 mMaxSize = maxSize;
74 while (mSize > mMaxSize) {
75 mCache.removeOldest();
76 }
77}
78
79///////////////////////////////////////////////////////////////////////////////
80// Callbacks
81///////////////////////////////////////////////////////////////////////////////
82
83void GradientCache::operator()(SkShader*& shader, Texture*& texture) {
Romain Guya2341a92010-09-08 18:04:33 -070084 // Already locked here
Romain Guyc0ac1932010-07-19 18:43:02 -070085 if (shader) {
86 const uint32_t size = texture->width * texture->height * 4;
87 mSize -= size;
88 }
89
90 if (texture) {
91 glDeleteTextures(1, &texture->id);
92 delete texture;
93 }
94}
95
96///////////////////////////////////////////////////////////////////////////////
97// Caching
98///////////////////////////////////////////////////////////////////////////////
99
100Texture* GradientCache::get(SkShader* shader) {
Romain Guy9cccc2b2010-08-07 23:46:15 -0700101 return mCache.get(shader);
Romain Guyc0ac1932010-07-19 18:43:02 -0700102}
103
104void GradientCache::remove(SkShader* shader) {
105 mCache.remove(shader);
106}
107
Romain Guyfe48f652010-11-11 15:36:56 -0800108void GradientCache::removeDeferred(SkShader* shader) {
Romain Guya2341a92010-09-08 18:04:33 -0700109 Mutex::Autolock _l(mLock);
Romain Guyfe48f652010-11-11 15:36:56 -0800110 mGarbage.push(shader);
111}
112
113void GradientCache::clearGarbage() {
114 Mutex::Autolock _l(mLock);
115 size_t count = mGarbage.size();
116 for (size_t i = 0; i < count; i++) {
117 mCache.remove(mGarbage.itemAt(i));
118 }
119 mGarbage.clear();
120}
121
122void GradientCache::clear() {
Romain Guyc0ac1932010-07-19 18:43:02 -0700123 mCache.clear();
124}
125
Romain Guyee916f12010-09-20 17:53:08 -0700126Texture* GradientCache::addLinearGradient(SkShader* shader, uint32_t* colors,
Romain Guyc0ac1932010-07-19 18:43:02 -0700127 float* positions, int count, SkShader::TileMode tileMode) {
128 SkBitmap bitmap;
129 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1024, 1);
130 bitmap.allocPixels();
131 bitmap.eraseColor(0);
132
133 SkCanvas canvas(bitmap);
134
135 SkPoint points[2];
136 points[0].set(0.0f, 0.0f);
137 points[1].set(bitmap.width(), 0.0f);
138
139 SkShader* localShader = SkGradientShader::CreateLinear(points,
140 reinterpret_cast<const SkColor*>(colors), positions, count, tileMode);
141
142 SkPaint p;
143 p.setStyle(SkPaint::kStrokeAndFill_Style);
144 p.setShader(localShader)->unref();
145
146 canvas.drawRectCoords(0.0f, 0.0f, bitmap.width(), 1.0f, p);
147
148 // Asume the cache is always big enough
149 const uint32_t size = bitmap.rowBytes() * bitmap.height();
150 while (mSize + size > mMaxSize) {
151 mCache.removeOldest();
152 }
153
154 Texture* texture = new Texture;
155 generateTexture(&bitmap, texture);
156
157 mSize += size;
158 mCache.put(shader, texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700159
160 return texture;
161}
162
163void GradientCache::generateTexture(SkBitmap* bitmap, Texture* texture) {
164 SkAutoLockPixels autoLock(*bitmap);
165 if (!bitmap->readyToDraw()) {
166 LOGE("Cannot generate texture from shader");
167 return;
168 }
169
170 texture->generation = bitmap->getGenerationID();
171 texture->width = bitmap->width();
172 texture->height = bitmap->height();
173
174 glGenTextures(1, &texture->id);
175
176 glBindTexture(GL_TEXTURE_2D, texture->id);
177 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
178
179 texture->blend = !bitmap->isOpaque();
180 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0,
181 GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels());
182
183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
185
186 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
187 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
188}
189
190}; // namespace uirenderer
191}; // namespace android