blob: 58920bd6f96d4c2d3179c23c6348bb11ad7baa73 [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
24#include "GradientCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070025#include "Properties.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070026
27namespace android {
28namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
31// Constructors/destructor
32///////////////////////////////////////////////////////////////////////////////
33
Romain Guyfb8b7632010-08-23 21:05:08 -070034GradientCache::GradientCache():
35 mCache(GenerationCache<SkShader*, Texture*>::kUnlimitedCapacity),
36 mSize(0), mMaxSize(MB(DEFAULT_GRADIENT_CACHE_SIZE)) {
37 char property[PROPERTY_VALUE_MAX];
38 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
39 LOGD(" Setting gradient cache size to %sMB", property);
40 setMaxSize(MB(atof(property)));
41 } else {
42 LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
43 }
44
45 mCache.setOnEntryRemovedListener(this);
46}
47
Romain Guyc0ac1932010-07-19 18:43:02 -070048GradientCache::GradientCache(uint32_t maxByteSize):
49 mCache(GenerationCache<SkShader*, Texture*>::kUnlimitedCapacity),
50 mSize(0), mMaxSize(maxByteSize) {
51 mCache.setOnEntryRemovedListener(this);
52}
53
54GradientCache::~GradientCache() {
55 mCache.clear();
56}
57
58///////////////////////////////////////////////////////////////////////////////
59// Size management
60///////////////////////////////////////////////////////////////////////////////
61
62uint32_t GradientCache::getSize() {
63 return mSize;
64}
65
66uint32_t GradientCache::getMaxSize() {
67 return mMaxSize;
68}
69
70void GradientCache::setMaxSize(uint32_t maxSize) {
71 mMaxSize = maxSize;
72 while (mSize > mMaxSize) {
73 mCache.removeOldest();
74 }
75}
76
77///////////////////////////////////////////////////////////////////////////////
78// Callbacks
79///////////////////////////////////////////////////////////////////////////////
80
81void GradientCache::operator()(SkShader*& shader, Texture*& texture) {
82 if (shader) {
83 const uint32_t size = texture->width * texture->height * 4;
84 mSize -= size;
85 }
86
87 if (texture) {
88 glDeleteTextures(1, &texture->id);
89 delete texture;
90 }
91}
92
93///////////////////////////////////////////////////////////////////////////////
94// Caching
95///////////////////////////////////////////////////////////////////////////////
96
97Texture* GradientCache::get(SkShader* shader) {
Romain Guy9cccc2b2010-08-07 23:46:15 -070098 return mCache.get(shader);
Romain Guyc0ac1932010-07-19 18:43:02 -070099}
100
101void GradientCache::remove(SkShader* shader) {
102 mCache.remove(shader);
103}
104
105void GradientCache::clear() {
106 mCache.clear();
107}
108
109Texture* GradientCache::addLinearGradient(SkShader* shader, float* bounds, uint32_t* colors,
110 float* positions, int count, SkShader::TileMode tileMode) {
111 SkBitmap bitmap;
112 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 1024, 1);
113 bitmap.allocPixels();
114 bitmap.eraseColor(0);
115
116 SkCanvas canvas(bitmap);
117
118 SkPoint points[2];
119 points[0].set(0.0f, 0.0f);
120 points[1].set(bitmap.width(), 0.0f);
121
122 SkShader* localShader = SkGradientShader::CreateLinear(points,
123 reinterpret_cast<const SkColor*>(colors), positions, count, tileMode);
124
125 SkPaint p;
126 p.setStyle(SkPaint::kStrokeAndFill_Style);
127 p.setShader(localShader)->unref();
128
129 canvas.drawRectCoords(0.0f, 0.0f, bitmap.width(), 1.0f, p);
130
131 // Asume the cache is always big enough
132 const uint32_t size = bitmap.rowBytes() * bitmap.height();
133 while (mSize + size > mMaxSize) {
134 mCache.removeOldest();
135 }
136
137 Texture* texture = new Texture;
138 generateTexture(&bitmap, texture);
139
140 mSize += size;
141 mCache.put(shader, texture);
142
143 return texture;
144}
145
146void GradientCache::generateTexture(SkBitmap* bitmap, Texture* texture) {
147 SkAutoLockPixels autoLock(*bitmap);
148 if (!bitmap->readyToDraw()) {
149 LOGE("Cannot generate texture from shader");
150 return;
151 }
152
153 texture->generation = bitmap->getGenerationID();
154 texture->width = bitmap->width();
155 texture->height = bitmap->height();
156
157 glGenTextures(1, &texture->id);
158
159 glBindTexture(GL_TEXTURE_2D, texture->id);
160 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
161
162 texture->blend = !bitmap->isOpaque();
163 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0,
164 GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels());
165
166 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
167 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
168
169 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
170 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
171}
172
173}; // namespace uirenderer
174}; // namespace android