blob: fa6ea2591cc88f20f13446e820c47c4c49e4d080 [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);
37}
38
39PathCache::~PathCache() {
40 mCache.clear();
41}
42
43///////////////////////////////////////////////////////////////////////////////
44// Size management
45///////////////////////////////////////////////////////////////////////////////
46
47uint32_t PathCache::getSize() {
48 return mSize;
49}
50
51uint32_t PathCache::getMaxSize() {
52 return mMaxSize;
53}
54
55void PathCache::setMaxSize(uint32_t maxSize) {
56 mMaxSize = maxSize;
57 while (mSize > mMaxSize) {
58 mCache.removeOldest();
59 }
60}
61
62///////////////////////////////////////////////////////////////////////////////
63// Callbacks
64///////////////////////////////////////////////////////////////////////////////
65
66void PathCache::operator()(PathCacheEntry& path, PathTexture*& texture) {
67 const uint32_t size = texture->width * texture->height;
68 mSize -= size;
69
70 if (texture) {
71 glDeleteTextures(1, &texture->id);
72 delete texture;
73 }
74}
75
76///////////////////////////////////////////////////////////////////////////////
77// Caching
78///////////////////////////////////////////////////////////////////////////////
79
80PathTexture* PathCache::get(SkPath* path, SkPaint* paint) {
81 PathCacheEntry entry(path, paint);
82 PathTexture* texture = mCache.get(entry);
83
84 if (!texture) {
85 texture = addTexture(entry, path, paint);
86 } else if (path->getGenerationID() != texture->generation) {
87 mCache.remove(entry);
88 texture = addTexture(entry, path, paint);
89 }
90
Romain Guy7fbcc042010-08-04 15:40:07 -070091 return texture;
92}
93
94PathTexture* PathCache::addTexture(const PathCacheEntry& entry,
95 const SkPath *path, const SkPaint* paint) {
96 const SkRect& bounds = path->getBounds();
97 const float offset = entry.strokeWidth * 1.5f;
98 const uint32_t width = uint32_t(bounds.width() + offset * 2.0 + 0.5);
99 const uint32_t height = uint32_t(bounds.height() + offset * 2.0 + 0.5);
100
101 const uint32_t size = width * height;
102 // Don't even try to cache a bitmap that's bigger than the cache
103 if (size < mMaxSize) {
104 while (mSize + size > mMaxSize) {
105 mCache.removeOldest();
106 }
107 }
108
109 PathTexture* texture = new PathTexture;
110 texture->left = bounds.fLeft;
111 texture->top = bounds.fTop;
112 texture->offset = offset;
113 texture->width = width;
114 texture->height = height;
115 texture->generation = path->getGenerationID();
116
117 SkBitmap bitmap;
118 bitmap.setConfig(SkBitmap::kA8_Config, width, height);
119 bitmap.allocPixels();
120 bitmap.eraseColor(0);
121
122 SkCanvas canvas(bitmap);
123 canvas.translate(-bounds.fLeft + offset, -bounds.fTop + offset);
124 canvas.drawPath(*path, *paint);
125
126 generateTexture(bitmap, texture);
127
128 if (size < mMaxSize) {
129 mSize += size;
130 mCache.put(entry, texture);
Romain Guy22158e12010-08-06 11:18:34 -0700131 } else {
132 texture->cleanup = true;
Romain Guy7fbcc042010-08-04 15:40:07 -0700133 }
134
135 return texture;
136}
137
138void PathCache::clear() {
139 mCache.clear();
140}
141
142void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) {
143 SkAutoLockPixels alp(bitmap);
144 if (!bitmap.readyToDraw()) {
145 LOGE("Cannot generate texture from bitmap");
146 return;
147 }
148
149 glGenTextures(1, &texture->id);
150
151 glBindTexture(GL_TEXTURE_2D, texture->id);
152 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap.bytesPerPixel());
153
154 texture->blend = true;
155 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, bitmap.rowBytesAsPixels(), texture->height, 0,
156 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels());
157
158 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
159 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
160
161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
162 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
163}
164
165}; // namespace uirenderer
166}; // namespace android