blob: c7ab9c5ad5cd5829954842c674e9ecfe9ddbd062 [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
Romain Guyc0ac1932010-07-19 18:43:02 -070019#include <SkCanvas.h>
20#include <SkGradientShader.h>
21
Romain Guya2341a92010-09-08 18:04:33 -070022#include <utils/threads.h>
23
Romain Guyc9855a52011-01-21 21:14:15 -080024#include "Debug.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070025#include "GradientCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070026#include "Properties.h"
Romain Guyc0ac1932010-07-19 18:43:02 -070027
28namespace android {
29namespace uirenderer {
30
31///////////////////////////////////////////////////////////////////////////////
32// Constructors/destructor
33///////////////////////////////////////////////////////////////////////////////
34
Romain Guyfb8b7632010-08-23 21:05:08 -070035GradientCache::GradientCache():
Romain Guy6203f6c2011-08-01 18:56:21 -070036 mCache(GenerationCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity),
Romain Guyfb8b7632010-08-23 21:05:08 -070037 mSize(0), mMaxSize(MB(DEFAULT_GRADIENT_CACHE_SIZE)) {
38 char property[PROPERTY_VALUE_MAX];
39 if (property_get(PROPERTY_GRADIENT_CACHE_SIZE, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080040 INIT_LOGD(" Setting gradient cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070041 setMaxSize(MB(atof(property)));
42 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080043 INIT_LOGD(" Using default gradient cache size of %.2fMB", DEFAULT_GRADIENT_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070044 }
45
Romain Guy8dcfd5e2012-07-20 11:36:03 -070046 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
47
Romain Guyfb8b7632010-08-23 21:05:08 -070048 mCache.setOnEntryRemovedListener(this);
49}
50
Romain Guyc0ac1932010-07-19 18:43:02 -070051GradientCache::GradientCache(uint32_t maxByteSize):
Romain Guy6203f6c2011-08-01 18:56:21 -070052 mCache(GenerationCache<GradientCacheEntry, Texture*>::kUnlimitedCapacity),
Romain Guyc0ac1932010-07-19 18:43:02 -070053 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
Romain Guy6203f6c2011-08-01 18:56:21 -070084void GradientCache::operator()(GradientCacheEntry& shader, Texture*& texture) {
85 if (texture) {
Romain Guyc0ac1932010-07-19 18:43:02 -070086 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
Romain Guy6203f6c2011-08-01 18:56:21 -0700100Texture* GradientCache::get(uint32_t* colors, float* positions,
101 int count, SkShader::TileMode tileMode) {
Romain Guyc0ac1932010-07-19 18:43:02 -0700102
Romain Guy6203f6c2011-08-01 18:56:21 -0700103 GradientCacheEntry gradient(colors, positions, count, tileMode);
104 Texture* texture = mCache.get(gradient);
Romain Guyc0ac1932010-07-19 18:43:02 -0700105
Romain Guy6203f6c2011-08-01 18:56:21 -0700106 if (!texture) {
107 texture = addLinearGradient(gradient, colors, positions, count, tileMode);
Romain Guyfe48f652010-11-11 15:36:56 -0800108 }
Romain Guy6203f6c2011-08-01 18:56:21 -0700109
110 return texture;
Romain Guyfe48f652010-11-11 15:36:56 -0800111}
112
113void GradientCache::clear() {
Romain Guyc0ac1932010-07-19 18:43:02 -0700114 mCache.clear();
115}
116
Romain Guy6203f6c2011-08-01 18:56:21 -0700117Texture* GradientCache::addLinearGradient(GradientCacheEntry& gradient,
118 uint32_t* colors, float* positions, int count, SkShader::TileMode tileMode) {
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700119 int width = 256 * (count - 1);
120 width = width < mMaxTextureSize ? width : mMaxTextureSize;
121
Romain Guyc0ac1932010-07-19 18:43:02 -0700122 SkBitmap bitmap;
Romain Guy8dcfd5e2012-07-20 11:36:03 -0700123 bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, 1);
Romain Guyc0ac1932010-07-19 18:43:02 -0700124 bitmap.allocPixels();
125 bitmap.eraseColor(0);
126
127 SkCanvas canvas(bitmap);
128
129 SkPoint points[2];
130 points[0].set(0.0f, 0.0f);
131 points[1].set(bitmap.width(), 0.0f);
132
133 SkShader* localShader = SkGradientShader::CreateLinear(points,
134 reinterpret_cast<const SkColor*>(colors), positions, count, tileMode);
135
136 SkPaint p;
137 p.setStyle(SkPaint::kStrokeAndFill_Style);
138 p.setShader(localShader)->unref();
139
140 canvas.drawRectCoords(0.0f, 0.0f, bitmap.width(), 1.0f, p);
141
142 // 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 }
147
148 Texture* texture = new Texture;
149 generateTexture(&bitmap, texture);
150
151 mSize += size;
Romain Guy6203f6c2011-08-01 18:56:21 -0700152 mCache.put(gradient, texture);
Romain Guyc0ac1932010-07-19 18:43:02 -0700153
154 return texture;
155}
156
157void GradientCache::generateTexture(SkBitmap* bitmap, Texture* texture) {
158 SkAutoLockPixels autoLock(*bitmap);
159 if (!bitmap->readyToDraw()) {
Steve Block3762c312012-01-06 19:20:56 +0000160 ALOGE("Cannot generate texture from shader");
Romain Guyc0ac1932010-07-19 18:43:02 -0700161 return;
162 }
163
164 texture->generation = bitmap->getGenerationID();
165 texture->width = bitmap->width();
166 texture->height = bitmap->height();
167
168 glGenTextures(1, &texture->id);
169
170 glBindTexture(GL_TEXTURE_2D, texture->id);
171 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap->bytesPerPixel());
172
173 texture->blend = !bitmap->isOpaque();
174 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap->rowBytesAsPixels(), texture->height, 0,
175 GL_RGBA, GL_UNSIGNED_BYTE, bitmap->getPixels());
176
Romain Guy39d252a2011-12-12 18:14:06 -0800177 texture->setFilter(GL_LINEAR);
178 texture->setWrap(GL_CLAMP_TO_EDGE);
Romain Guyc0ac1932010-07-19 18:43:02 -0700179}
180
181}; // namespace uirenderer
182}; // namespace android