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