blob: 67a5f923d59845315143243921f8129af37065ac [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
91 // TODO: Do something to destroy the texture object if it's too big for the cache
92 return texture;
93}
94
95PathTexture* PathCache::addTexture(const PathCacheEntry& entry,
96 const SkPath *path, const SkPaint* paint) {
97 const SkRect& bounds = path->getBounds();
98 const float offset = entry.strokeWidth * 1.5f;
99 const uint32_t width = uint32_t(bounds.width() + offset * 2.0 + 0.5);
100 const uint32_t height = uint32_t(bounds.height() + offset * 2.0 + 0.5);
101
102 const uint32_t size = width * height;
103 // Don't even try to cache a bitmap that's bigger than the cache
104 if (size < mMaxSize) {
105 while (mSize + size > mMaxSize) {
106 mCache.removeOldest();
107 }
108 }
109
110 PathTexture* texture = new PathTexture;
111 texture->left = bounds.fLeft;
112 texture->top = bounds.fTop;
113 texture->offset = offset;
114 texture->width = width;
115 texture->height = height;
116 texture->generation = path->getGenerationID();
117
118 SkBitmap bitmap;
119 bitmap.setConfig(SkBitmap::kA8_Config, width, height);
120 bitmap.allocPixels();
121 bitmap.eraseColor(0);
122
123 SkCanvas canvas(bitmap);
124 canvas.translate(-bounds.fLeft + offset, -bounds.fTop + offset);
125 canvas.drawPath(*path, *paint);
126
127 generateTexture(bitmap, texture);
128
129 if (size < mMaxSize) {
130 mSize += size;
131 mCache.put(entry, texture);
132 }
133
134 return texture;
135}
136
137void PathCache::clear() {
138 mCache.clear();
139}
140
141void PathCache::generateTexture(SkBitmap& bitmap, Texture* texture) {
142 SkAutoLockPixels alp(bitmap);
143 if (!bitmap.readyToDraw()) {
144 LOGE("Cannot generate texture from bitmap");
145 return;
146 }
147
148 glGenTextures(1, &texture->id);
149
150 glBindTexture(GL_TEXTURE_2D, texture->id);
151 glPixelStorei(GL_UNPACK_ALIGNMENT, bitmap.bytesPerPixel());
152
153 texture->blend = true;
154 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, bitmap.rowBytesAsPixels(), texture->height, 0,
155 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels());
156
157 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
158 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
159
160 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
162}
163
164}; // namespace uirenderer
165}; // namespace android