blob: 10440ea3cebf43a7604720d8714ace5c237d3020 [file] [log] [blame]
Romain Guy7fbcc042010-08-04 15:40:07 -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 <SkRect.h>
23
24#include "PathCache.h"
25
26namespace android {
27namespace uirenderer {
28
29///////////////////////////////////////////////////////////////////////////////
30// Constructors/destructor
31///////////////////////////////////////////////////////////////////////////////
32
33PathCache::PathCache(uint32_t maxByteSize):
34 mCache(GenerationCache<PathCacheEntry, PathTexture*>::kUnlimitedCapacity),
35 mSize(0), mMaxSize(maxByteSize) {
36 mCache.setOnEntryRemovedListener(this);
Romain Guy9cccc2b2010-08-07 23:46:15 -070037
38 GLint maxTextureSize;
39 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
40 mMaxTextureSize = maxTextureSize;
Romain Guy7fbcc042010-08-04 15:40:07 -070041}
42
43PathCache::~PathCache() {
44 mCache.clear();
45}
46
47///////////////////////////////////////////////////////////////////////////////
48// Size management
49///////////////////////////////////////////////////////////////////////////////
50
51uint32_t PathCache::getSize() {
52 return mSize;
53}
54
55uint32_t PathCache::getMaxSize() {
56 return mMaxSize;
57}
58
59void PathCache::setMaxSize(uint32_t maxSize) {
60 mMaxSize = maxSize;
61 while (mSize > mMaxSize) {
62 mCache.removeOldest();
63 }
64}
65
66///////////////////////////////////////////////////////////////////////////////
67// Callbacks
68///////////////////////////////////////////////////////////////////////////////
69
70void PathCache::operator()(PathCacheEntry& path, PathTexture*& texture) {
71 const uint32_t size = texture->width * texture->height;
72 mSize -= size;
73
74 if (texture) {
75 glDeleteTextures(1, &texture->id);
76 delete texture;
77 }
78}
79
80///////////////////////////////////////////////////////////////////////////////
81// Caching
82///////////////////////////////////////////////////////////////////////////////
83
84PathTexture* PathCache::get(SkPath* path, SkPaint* paint) {
85 PathCacheEntry entry(path, paint);
86 PathTexture* texture = mCache.get(entry);
87
88 if (!texture) {
89 texture = addTexture(entry, path, paint);
90 } else if (path->getGenerationID() != texture->generation) {
91 mCache.remove(entry);
92 texture = addTexture(entry, path, paint);
93 }
94
Romain Guy7fbcc042010-08-04 15:40:07 -070095 return texture;
96}
97
98PathTexture* PathCache::addTexture(const PathCacheEntry& entry,
99 const SkPath *path, const SkPaint* paint) {
100 const SkRect& bounds = path->getBounds();
Romain Guy9cccc2b2010-08-07 23:46:15 -0700101
102 const float pathWidth = bounds.width();
103 const float pathHeight = bounds.height();
104
105 if (pathWidth > mMaxTextureSize || pathHeight > mMaxTextureSize) {
106 LOGW("Path too large to be rendered into a texture");
107 return NULL;
108 }
109
Romain Guy7fbcc042010-08-04 15:40:07 -0700110 const float offset = entry.strokeWidth * 1.5f;
Romain Guy9cccc2b2010-08-07 23:46:15 -0700111 const uint32_t width = uint32_t(pathWidth + offset * 2.0 + 0.5);
112 const uint32_t height = uint32_t(pathHeight + offset * 2.0 + 0.5);
Romain Guy7fbcc042010-08-04 15:40:07 -0700113
114 const uint32_t size = width * height;
115 // Don't even try to cache a bitmap that's bigger than the cache
116 if (size < mMaxSize) {
117 while (mSize + size > mMaxSize) {
118 mCache.removeOldest();
119 }
120 }
121
122 PathTexture* texture = new PathTexture;
123 texture->left = bounds.fLeft;
124 texture->top = bounds.fTop;
125 texture->offset = offset;
126 texture->width = width;
127 texture->height = height;
128 texture->generation = path->getGenerationID();
129
130 SkBitmap bitmap;
131 bitmap.setConfig(SkBitmap::kA8_Config, width, height);
132 bitmap.allocPixels();
133 bitmap.eraseColor(0);
134
135 SkCanvas canvas(bitmap);
136 canvas.translate(-bounds.fLeft + offset, -bounds.fTop + offset);
137 canvas.drawPath(*path, *paint);
138
139 generateTexture(bitmap, texture);
140
141 if (size < mMaxSize) {
142 mSize += size;
143 mCache.put(entry, texture);
Romain Guy22158e12010-08-06 11:18:34 -0700144 } else {
145 texture->cleanup = true;
Romain Guy7fbcc042010-08-04 15:40:07 -0700146 }
147
148 return texture;
149}
150
151void PathCache::clear() {
152 mCache.clear();
153}
154
155void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) {
156 SkAutoLockPixels alp(bitmap);
157 if (!bitmap.readyToDraw()) {
158 LOGE("Cannot generate texture from bitmap");
159 return;
160 }
161
162 glGenTextures(1, &texture->id);
163
164 glBindTexture(GL_TEXTURE_2D, texture->id);
Romain Guy61c8c9c2010-08-09 20:48:09 -0700165 // Textures are Alpha8
166 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
Romain Guy7fbcc042010-08-04 15:40:07 -0700167
168 texture->blend = true;
Romain Guy1e45aae2010-08-13 19:39:53 -0700169 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
Romain Guy7fbcc042010-08-04 15:40:07 -0700170 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels());
171
172 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
173 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
174
175 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
176 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
177}
178
179}; // namespace uirenderer
180}; // namespace android