blob: a4aff9df117c87084d05993ee7ce8916a2b0c39e [file] [log] [blame]
Romain Guy01d58e42011-01-19 21:54:02 -08001/*
2 * Copyright (C) 2011 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#ifndef ANDROID_HWUI_SHAPE_CACHE_H
18#define ANDROID_HWUI_SHAPE_CACHE_H
19
20#include <GLES2/gl2.h>
21
Romain Guyff26a0c2011-01-20 11:35:46 -080022#include <SkBitmap.h>
Romain Guy01d58e42011-01-19 21:54:02 -080023#include <SkCanvas.h>
Romain Guyff26a0c2011-01-20 11:35:46 -080024#include <SkPaint.h>
25#include <SkPath.h>
Romain Guy01d58e42011-01-19 21:54:02 -080026#include <SkRect.h>
27
Romain Guyff26a0c2011-01-20 11:35:46 -080028#include "Debug.h"
Romain Guy01d58e42011-01-19 21:54:02 -080029#include "Properties.h"
Romain Guyff26a0c2011-01-20 11:35:46 -080030#include "Texture.h"
31#include "utils/Compare.h"
32#include "utils/GenerationCache.h"
Romain Guy01d58e42011-01-19 21:54:02 -080033
34namespace android {
35namespace uirenderer {
36
37///////////////////////////////////////////////////////////////////////////////
38// Defines
39///////////////////////////////////////////////////////////////////////////////
40
41// Debug
42#if DEBUG_SHAPES
43 #define SHAPE_LOGD(...) LOGD(__VA_ARGS__)
44#else
45 #define SHAPE_LOGD(...)
46#endif
47
48///////////////////////////////////////////////////////////////////////////////
49// Classes
50///////////////////////////////////////////////////////////////////////////////
51
52/**
Romain Guyff26a0c2011-01-20 11:35:46 -080053 * Alpha texture used to represent a path.
54 */
55struct PathTexture: public Texture {
56 PathTexture(): Texture() {
57 }
58
59 /**
60 * Left coordinate of the path bounds.
61 */
62 float left;
63 /**
64 * Top coordinate of the path bounds.
65 */
66 float top;
67 /**
68 * Offset to draw the path at the correct origin.
69 */
70 float offset;
71}; // struct PathTexture
72
73/**
Romain Guy01d58e42011-01-19 21:54:02 -080074 * Describe a shape in the shape cache.
75 */
76struct ShapeCacheEntry {
77 enum ShapeType {
78 kShapeNone,
Romain Guyc1cd9ba32011-01-23 14:18:41 -080079 kShapeRect,
Romain Guy01d58e42011-01-19 21:54:02 -080080 kShapeRoundRect,
81 kShapeCircle,
82 kShapeOval,
Romain Guyff26a0c2011-01-20 11:35:46 -080083 kShapeArc,
84 kShapePath
Romain Guy01d58e42011-01-19 21:54:02 -080085 };
86
87 ShapeCacheEntry() {
88 shapeType = kShapeNone;
89 join = SkPaint::kDefault_Join;
90 cap = SkPaint::kDefault_Cap;
91 style = SkPaint::kFill_Style;
92 miter = 4.0f;
93 strokeWidth = 1.0f;
94 }
95
96 ShapeCacheEntry(const ShapeCacheEntry& entry):
97 shapeType(entry.shapeType), join(entry.join), cap(entry.cap),
98 style(entry.style), miter(entry.miter),
99 strokeWidth(entry.strokeWidth) {
100 }
101
102 ShapeCacheEntry(ShapeType type, SkPaint* paint) {
103 shapeType = type;
104 join = paint->getStrokeJoin();
105 cap = paint->getStrokeCap();
106 float v = paint->getStrokeMiter();
107 miter = *(uint32_t*) &v;
108 v = paint->getStrokeWidth();
109 strokeWidth = *(uint32_t*) &v;
110 style = paint->getStyle();
111 }
112
113 virtual ~ShapeCacheEntry() {
114 }
115
116 // shapeType must be checked in subclasses operator<
117 ShapeType shapeType;
118 SkPaint::Join join;
119 SkPaint::Cap cap;
120 SkPaint::Style style;
121 uint32_t miter;
122 uint32_t strokeWidth;
123
124 bool operator<(const ShapeCacheEntry& rhs) const {
125 LTE_INT(shapeType) {
126 LTE_INT(join) {
127 LTE_INT(cap) {
128 LTE_INT(style) {
129 LTE_INT(miter) {
130 LTE_INT(strokeWidth) {
131 return lessThan(rhs);
132 }
133 }
134 }
135 }
136 }
137 }
138 return false;
139 }
140
141protected:
142 virtual bool lessThan(const ShapeCacheEntry& rhs) const {
143 return false;
144 }
145}; // struct ShapeCacheEntry
146
147
148struct RoundRectShapeCacheEntry: public ShapeCacheEntry {
149 RoundRectShapeCacheEntry(float width, float height, float rx, float ry, SkPaint* paint):
150 ShapeCacheEntry(ShapeCacheEntry::kShapeRoundRect, paint) {
151 mWidth = *(uint32_t*) &width;
152 mHeight = *(uint32_t*) &height;
153 mRx = *(uint32_t*) &rx;
154 mRy = *(uint32_t*) &ry;
155 }
156
157 RoundRectShapeCacheEntry(): ShapeCacheEntry() {
158 mWidth = 0;
159 mHeight = 0;
160 mRx = 0;
161 mRy = 0;
162 }
163
164 RoundRectShapeCacheEntry(const RoundRectShapeCacheEntry& entry):
165 ShapeCacheEntry(entry) {
166 mWidth = entry.mWidth;
167 mHeight = entry.mHeight;
168 mRx = entry.mRx;
169 mRy = entry.mRy;
170 }
171
172 bool lessThan(const ShapeCacheEntry& r) const {
173 const RoundRectShapeCacheEntry& rhs = (const RoundRectShapeCacheEntry&) r;
174 LTE_INT(mWidth) {
175 LTE_INT(mHeight) {
176 LTE_INT(mRx) {
177 LTE_INT(mRy) {
178 return false;
179 }
180 }
181 }
182 }
183 return false;
184 }
185
186private:
187 uint32_t mWidth;
188 uint32_t mHeight;
189 uint32_t mRx;
190 uint32_t mRy;
191}; // RoundRectShapeCacheEntry
192
193struct CircleShapeCacheEntry: public ShapeCacheEntry {
194 CircleShapeCacheEntry(float radius, SkPaint* paint):
195 ShapeCacheEntry(ShapeCacheEntry::kShapeCircle, paint) {
196 mRadius = *(uint32_t*) &radius;
197 }
198
199 CircleShapeCacheEntry(): ShapeCacheEntry() {
200 mRadius = 0;
201 }
202
203 CircleShapeCacheEntry(const CircleShapeCacheEntry& entry):
204 ShapeCacheEntry(entry) {
205 mRadius = entry.mRadius;
206 }
207
208 bool lessThan(const ShapeCacheEntry& r) const {
209 const CircleShapeCacheEntry& rhs = (const CircleShapeCacheEntry&) r;
210 LTE_INT(mRadius) {
211 return false;
212 }
213 return false;
214 }
215
216private:
217 uint32_t mRadius;
218}; // CircleShapeCacheEntry
219
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800220struct OvalShapeCacheEntry: public ShapeCacheEntry {
221 OvalShapeCacheEntry(float width, float height, SkPaint* paint):
222 ShapeCacheEntry(ShapeCacheEntry::kShapeOval, paint) {
223 mWidth = *(uint32_t*) &width;
224 mHeight = *(uint32_t*) &height;
225 }
226
227 OvalShapeCacheEntry(): ShapeCacheEntry() {
228 mWidth = mHeight = 0;
229 }
230
231 OvalShapeCacheEntry(const OvalShapeCacheEntry& entry):
232 ShapeCacheEntry(entry) {
233 mWidth = entry.mWidth;
234 mHeight = entry.mHeight;
235 }
236
237 bool lessThan(const ShapeCacheEntry& r) const {
238 const OvalShapeCacheEntry& rhs = (const OvalShapeCacheEntry&) r;
239 LTE_INT(mWidth) {
240 LTE_INT(mHeight) {
241 return false;
242 }
243 }
244 return false;
245 }
246
247private:
248 uint32_t mWidth;
249 uint32_t mHeight;
250}; // OvalShapeCacheEntry
251
252struct RectShapeCacheEntry: public ShapeCacheEntry {
253 RectShapeCacheEntry(float width, float height, SkPaint* paint):
254 ShapeCacheEntry(ShapeCacheEntry::kShapeRect, paint) {
255 mWidth = *(uint32_t*) &width;
256 mHeight = *(uint32_t*) &height;
257 }
258
259 RectShapeCacheEntry(): ShapeCacheEntry() {
260 mWidth = mHeight = 0;
261 }
262
263 RectShapeCacheEntry(const RectShapeCacheEntry& entry):
264 ShapeCacheEntry(entry) {
265 mWidth = entry.mWidth;
266 mHeight = entry.mHeight;
267 }
268
269 bool lessThan(const ShapeCacheEntry& r) const {
270 const RectShapeCacheEntry& rhs = (const RectShapeCacheEntry&) r;
271 LTE_INT(mWidth) {
272 LTE_INT(mHeight) {
273 return false;
274 }
275 }
276 return false;
277 }
278
279private:
280 uint32_t mWidth;
281 uint32_t mHeight;
282}; // RectShapeCacheEntry
283
Romain Guy01d58e42011-01-19 21:54:02 -0800284/**
285 * A simple LRU shape cache. The cache has a maximum size expressed in bytes.
286 * Any texture added to the cache causing the cache to grow beyond the maximum
287 * allowed size will also cause the oldest texture to be kicked out.
288 */
289template<typename Entry>
290class ShapeCache: public OnEntryRemoved<Entry, PathTexture*> {
291public:
Romain Guyff26a0c2011-01-20 11:35:46 -0800292 ShapeCache(const char* name, const char* propertyName, float defaultSize);
Romain Guy01d58e42011-01-19 21:54:02 -0800293 ~ShapeCache();
294
295 /**
296 * Used as a callback when an entry is removed from the cache.
297 * Do not invoke directly.
298 */
299 void operator()(Entry& path, PathTexture*& texture);
300
301 /**
302 * Clears the cache. This causes all textures to be deleted.
303 */
304 void clear();
305
306 /**
307 * Sets the maximum size of the cache in bytes.
308 */
309 void setMaxSize(uint32_t maxSize);
310 /**
311 * Returns the maximum size of the cache in bytes.
312 */
313 uint32_t getMaxSize();
314 /**
315 * Returns the current size of the cache in bytes.
316 */
317 uint32_t getSize();
318
319protected:
320 PathTexture* addTexture(const Entry& entry, const SkPath *path, const SkPaint* paint);
321
322 PathTexture* get(Entry entry) {
323 return mCache.get(entry);
324 }
325
Romain Guy01d58e42011-01-19 21:54:02 -0800326 void removeTexture(PathTexture* texture);
327
Romain Guy01d58e42011-01-19 21:54:02 -0800328 GenerationCache<Entry, PathTexture*> mCache;
329 uint32_t mSize;
330 uint32_t mMaxSize;
331 GLuint mMaxTextureSize;
332
Romain Guyff26a0c2011-01-20 11:35:46 -0800333 char* mName;
Romain Guy01d58e42011-01-19 21:54:02 -0800334 bool mDebugEnabled;
Romain Guyff26a0c2011-01-20 11:35:46 -0800335
336private:
337 /**
338 * Generates the texture from a bitmap into the specified texture structure.
339 */
340 void generateTexture(SkBitmap& bitmap, Texture* texture);
341
342 void init();
Romain Guy01d58e42011-01-19 21:54:02 -0800343}; // class ShapeCache
344
345class RoundRectShapeCache: public ShapeCache<RoundRectShapeCacheEntry> {
346public:
347 RoundRectShapeCache();
348
349 PathTexture* getRoundRect(float width, float height, float rx, float ry, SkPaint* paint);
350}; // class RoundRectShapeCache
351
352class CircleShapeCache: public ShapeCache<CircleShapeCacheEntry> {
353public:
354 CircleShapeCache();
355
356 PathTexture* getCircle(float radius, SkPaint* paint);
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800357}; // class CircleShapeCache
Romain Guy01d58e42011-01-19 21:54:02 -0800358
Romain Guyc1cd9ba32011-01-23 14:18:41 -0800359class OvalShapeCache: public ShapeCache<OvalShapeCacheEntry> {
360public:
361 OvalShapeCache();
362
363 PathTexture* getOval(float width, float height, SkPaint* paint);
364}; // class OvalShapeCache
365
366class RectShapeCache: public ShapeCache<RectShapeCacheEntry> {
367public:
368 RectShapeCache();
369
370 PathTexture* getRect(float width, float height, SkPaint* paint);
371}; // class RectShapeCache
Romain Guy01d58e42011-01-19 21:54:02 -0800372
373///////////////////////////////////////////////////////////////////////////////
374// Constructors/destructor
375///////////////////////////////////////////////////////////////////////////////
376
377template<class Entry>
Romain Guyff26a0c2011-01-20 11:35:46 -0800378ShapeCache<Entry>::ShapeCache(const char* name, const char* propertyName, float defaultSize):
Romain Guy01d58e42011-01-19 21:54:02 -0800379 mCache(GenerationCache<ShapeCacheEntry, PathTexture*>::kUnlimitedCapacity),
Romain Guyff26a0c2011-01-20 11:35:46 -0800380 mSize(0), mMaxSize(MB(defaultSize)) {
Romain Guy01d58e42011-01-19 21:54:02 -0800381 char property[PROPERTY_VALUE_MAX];
Romain Guyff26a0c2011-01-20 11:35:46 -0800382 if (property_get(propertyName, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -0800383 INIT_LOGD(" Setting %s cache size to %sMB", name, property);
Romain Guy01d58e42011-01-19 21:54:02 -0800384 setMaxSize(MB(atof(property)));
385 } else {
Romain Guyc9855a52011-01-21 21:14:15 -0800386 INIT_LOGD(" Using default %s cache size of %.2fMB", name, defaultSize);
Romain Guy01d58e42011-01-19 21:54:02 -0800387 }
Romain Guy01d58e42011-01-19 21:54:02 -0800388
Romain Guyff26a0c2011-01-20 11:35:46 -0800389 size_t len = strlen(name);
390 mName = new char[len + 1];
391 strcpy(mName, name);
392 mName[len] = '\0';
393
Romain Guy01d58e42011-01-19 21:54:02 -0800394 init();
395}
396
397template<class Entry>
398ShapeCache<Entry>::~ShapeCache() {
399 mCache.clear();
Romain Guyff26a0c2011-01-20 11:35:46 -0800400 delete[] mName;
Romain Guy01d58e42011-01-19 21:54:02 -0800401}
402
403template<class Entry>
404void ShapeCache<Entry>::init() {
405 mCache.setOnEntryRemovedListener(this);
406
407 GLint maxTextureSize;
408 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
409 mMaxTextureSize = maxTextureSize;
410
411 mDebugEnabled = readDebugLevel() & kDebugCaches;
412}
413
414///////////////////////////////////////////////////////////////////////////////
415// Size management
416///////////////////////////////////////////////////////////////////////////////
417
418template<class Entry>
419uint32_t ShapeCache<Entry>::getSize() {
420 return mSize;
421}
422
423template<class Entry>
424uint32_t ShapeCache<Entry>::getMaxSize() {
425 return mMaxSize;
426}
427
428template<class Entry>
429void ShapeCache<Entry>::setMaxSize(uint32_t maxSize) {
430 mMaxSize = maxSize;
431 while (mSize > mMaxSize) {
432 mCache.removeOldest();
433 }
434}
435
436///////////////////////////////////////////////////////////////////////////////
437// Callbacks
438///////////////////////////////////////////////////////////////////////////////
439
440template<class Entry>
441void ShapeCache<Entry>::operator()(Entry& path, PathTexture*& texture) {
442 removeTexture(texture);
443}
444
445///////////////////////////////////////////////////////////////////////////////
446// Caching
447///////////////////////////////////////////////////////////////////////////////
448
449template<class Entry>
450void ShapeCache<Entry>::removeTexture(PathTexture* texture) {
451 if (texture) {
452 const uint32_t size = texture->width * texture->height;
453 mSize -= size;
454
Romain Guyff26a0c2011-01-20 11:35:46 -0800455 SHAPE_LOGD("ShapeCache::callback: delete %s: name, size, mSize = %d, %d, %d",
456 mName, texture->id, size, mSize);
Romain Guy01d58e42011-01-19 21:54:02 -0800457 if (mDebugEnabled) {
Romain Guyff26a0c2011-01-20 11:35:46 -0800458 LOGD("Shape %s deleted, size = %d", mName, size);
Romain Guy01d58e42011-01-19 21:54:02 -0800459 }
460
461 glDeleteTextures(1, &texture->id);
462 delete texture;
463 }
464}
465
466template<class Entry>
467PathTexture* ShapeCache<Entry>::addTexture(const Entry& entry, const SkPath *path,
468 const SkPaint* paint) {
469 const SkRect& bounds = path->getBounds();
470
471 const float pathWidth = fmax(bounds.width(), 1.0f);
472 const float pathHeight = fmax(bounds.height(), 1.0f);
473
474 if (pathWidth > mMaxTextureSize || pathHeight > mMaxTextureSize) {
Romain Guyff26a0c2011-01-20 11:35:46 -0800475 LOGW("Shape %s too large to be rendered into a texture", mName);
Romain Guy01d58e42011-01-19 21:54:02 -0800476 return NULL;
477 }
478
479 const float offset = paint->getStrokeWidth() * 1.5f;
480 const uint32_t width = uint32_t(pathWidth + offset * 2.0 + 0.5);
481 const uint32_t height = uint32_t(pathHeight + offset * 2.0 + 0.5);
482
483 const uint32_t size = width * height;
484 // Don't even try to cache a bitmap that's bigger than the cache
485 if (size < mMaxSize) {
486 while (mSize + size > mMaxSize) {
487 mCache.removeOldest();
488 }
489 }
490
491 PathTexture* texture = new PathTexture;
492 texture->left = bounds.fLeft;
493 texture->top = bounds.fTop;
494 texture->offset = offset;
495 texture->width = width;
496 texture->height = height;
497 texture->generation = path->getGenerationID();
498
499 SkBitmap bitmap;
500 bitmap.setConfig(SkBitmap::kA8_Config, width, height);
501 bitmap.allocPixels();
502 bitmap.eraseColor(0);
503
504 SkPaint pathPaint(*paint);
505
506 // Make sure the paint is opaque, color, alpha, filter, etc.
507 // will be applied later when compositing the alpha8 texture
508 pathPaint.setColor(0xff000000);
509 pathPaint.setAlpha(255);
510 pathPaint.setColorFilter(NULL);
511 pathPaint.setMaskFilter(NULL);
512 pathPaint.setShader(NULL);
513 SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrc_Mode);
514 pathPaint.setXfermode(mode)->safeUnref();
515
516 SkCanvas canvas(bitmap);
517 canvas.translate(-bounds.fLeft + offset, -bounds.fTop + offset);
518 canvas.drawPath(*path, pathPaint);
519
520 generateTexture(bitmap, texture);
521
522 if (size < mMaxSize) {
523 mSize += size;
Romain Guyff26a0c2011-01-20 11:35:46 -0800524 SHAPE_LOGD("ShapeCache::get: create %s: name, size, mSize = %d, %d, %d",
525 mName, texture->id, size, mSize);
Romain Guy01d58e42011-01-19 21:54:02 -0800526 if (mDebugEnabled) {
Romain Guyff26a0c2011-01-20 11:35:46 -0800527 LOGD("Shape %s created, size = %d", mName, size);
Romain Guy01d58e42011-01-19 21:54:02 -0800528 }
529 mCache.put(entry, texture);
530 } else {
531 texture->cleanup = true;
532 }
533
534 return texture;
535}
536
537template<class Entry>
538void ShapeCache<Entry>::clear() {
539 mCache.clear();
540}
541
542template<class Entry>
543void ShapeCache<Entry>::generateTexture(SkBitmap& bitmap, Texture* texture) {
544 SkAutoLockPixels alp(bitmap);
545 if (!bitmap.readyToDraw()) {
546 LOGE("Cannot generate texture from bitmap");
547 return;
548 }
549
550 glGenTextures(1, &texture->id);
551
552 glBindTexture(GL_TEXTURE_2D, texture->id);
553 // Textures are Alpha8
554 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
555
556 texture->blend = true;
557 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
558 GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels());
559
560 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
561 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
562
563 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
564 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
565}
566
567}; // namespace uirenderer
568}; // namespace android
569
570#endif // ANDROID_HWUI_SHAPE_CACHE_H