GradientCache.cpp revision a2341a9f6addcd79723965ec5b1a1c5ae0f8bd65
1/*
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 <utils/threads.h>
25
26#include "GradientCache.h"
27#include "Properties.h"
28
29namespace android {
30namespace uirenderer {
31
32///////////////////////////////////////////////////////////////////////////////
33// Constructors/destructor
34///////////////////////////////////////////////////////////////////////////////
35
36GradientCache::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
50GradientCache::GradientCache(uint32_t maxByteSize):
51        mCache(GenerationCache<SkShader*, Texture*>::kUnlimitedCapacity),
52        mSize(0), mMaxSize(maxByteSize) {
53    mCache.setOnEntryRemovedListener(this);
54}
55
56GradientCache::~GradientCache() {
57    Mutex::Autolock _l(mLock);
58    mCache.clear();
59}
60
61///////////////////////////////////////////////////////////////////////////////
62// Size management
63///////////////////////////////////////////////////////////////////////////////
64
65uint32_t GradientCache::getSize() {
66    Mutex::Autolock _l(mLock);
67    return mSize;
68}
69
70uint32_t GradientCache::getMaxSize() {
71    Mutex::Autolock _l(mLock);
72    return mMaxSize;
73}
74
75void GradientCache::setMaxSize(uint32_t maxSize) {
76    Mutex::Autolock _l(mLock);
77    mMaxSize = maxSize;
78    while (mSize > mMaxSize) {
79        mCache.removeOldest();
80    }
81}
82
83///////////////////////////////////////////////////////////////////////////////
84// Callbacks
85///////////////////////////////////////////////////////////////////////////////
86
87void GradientCache::operator()(SkShader*& shader, Texture*& texture) {
88    // Already locked here
89    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) {
105    Mutex::Autolock _l(mLock);
106    return mCache.get(shader);
107}
108
109void GradientCache::remove(SkShader* shader) {
110    Mutex::Autolock _l(mLock);
111    mCache.remove(shader);
112}
113
114void GradientCache::clear() {
115    Mutex::Autolock _l(mLock);
116    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
141    mLock.lock();
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    mLock.unlock();
148
149    Texture* texture = new Texture;
150    generateTexture(&bitmap, texture);
151
152    mLock.lock();
153    mSize += size;
154    mCache.put(shader, texture);
155    mLock.unlock();
156
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
189