blob: 71a4ed7966ed365bedd3093b16d9c645b9bd2d25 [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
Romain Guya2341a92010-09-08 18:04:33 -070019#include <utils/threads.h>
20
Romain Guy7fbcc042010-08-04 15:40:07 -070021#include "PathCache.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070022#include "Properties.h"
Romain Guy7fbcc042010-08-04 15:40:07 -070023
24namespace android {
25namespace uirenderer {
26
Romain Guy33f6beb2012-02-16 19:24:51 -080027// Defined in ShapeCache.h
Romain Guyfdd6fc12012-04-27 11:47:13 -070028
29void computePathBounds(const SkPath* path, const SkPaint* paint,
Romain Guy33f6beb2012-02-16 19:24:51 -080030 float& left, float& top, float& offset, uint32_t& width, uint32_t& height) {
31 const SkRect& bounds = path->getBounds();
Romain Guyfdd6fc12012-04-27 11:47:13 -070032 computeBounds(bounds, paint, left, top, offset, width, height);
33}
Romain Guy33f6beb2012-02-16 19:24:51 -080034
Romain Guyfdd6fc12012-04-27 11:47:13 -070035void computeBounds(const SkRect& bounds, const SkPaint* paint,
36 float& left, float& top, float& offset, uint32_t& width, uint32_t& height) {
Romain Guy33f6beb2012-02-16 19:24:51 -080037 const float pathWidth = fmax(bounds.width(), 1.0f);
38 const float pathHeight = fmax(bounds.height(), 1.0f);
39
40 left = bounds.fLeft;
41 top = bounds.fTop;
42
43 offset = (int) floorf(fmax(paint->getStrokeWidth(), 1.0f) * 1.5f + 0.5f);
44
45 width = uint32_t(pathWidth + offset * 2.0 + 0.5);
46 height = uint32_t(pathHeight + offset * 2.0 + 0.5);
47}
48
Romain Guy7fbcc042010-08-04 15:40:07 -070049///////////////////////////////////////////////////////////////////////////////
Romain Guyff26a0c2011-01-20 11:35:46 -080050// Path cache
Romain Guy7fbcc042010-08-04 15:40:07 -070051///////////////////////////////////////////////////////////////////////////////
52
Romain Guyff26a0c2011-01-20 11:35:46 -080053PathCache::PathCache(): ShapeCache<PathCacheEntry>("path",
54 PROPERTY_PATH_CACHE_SIZE, DEFAULT_PATH_CACHE_SIZE) {
Romain Guy7fbcc042010-08-04 15:40:07 -070055}
56
Romain Guya2341a92010-09-08 18:04:33 -070057void PathCache::remove(SkPath* path) {
Romain Guya2341a92010-09-08 18:04:33 -070058 // TODO: Linear search...
Jeff Brownd9e688c2011-11-11 15:40:13 -080059 Vector<size_t> pathsToRemove;
60 for (size_t i = 0; i < mCache.size(); i++) {
Romain Guya2341a92010-09-08 18:04:33 -070061 if (mCache.getKeyAt(i).path == path) {
Romain Guy9e108412010-11-09 14:35:20 -080062 pathsToRemove.push(i);
Romain Guyfe48f652010-11-11 15:36:56 -080063 removeTexture(mCache.getValueAt(i));
Romain Guya2341a92010-09-08 18:04:33 -070064 }
65 }
Romain Guy9e108412010-11-09 14:35:20 -080066
Romain Guyfe48f652010-11-11 15:36:56 -080067 mCache.setOnEntryRemovedListener(NULL);
Romain Guy9e108412010-11-09 14:35:20 -080068 for (size_t i = 0; i < pathsToRemove.size(); i++) {
Romain Guy7b8b7582011-02-24 19:52:37 -080069 // This will work because pathsToRemove is sorted
70 // and because the cache is a sorted keyed vector
71 mCache.removeAt(pathsToRemove.itemAt(i) - i);
Romain Guy9e108412010-11-09 14:35:20 -080072 }
Romain Guyfe48f652010-11-11 15:36:56 -080073 mCache.setOnEntryRemovedListener(this);
74}
75
76void PathCache::removeDeferred(SkPath* path) {
77 Mutex::Autolock _l(mLock);
78 mGarbage.push(path);
79}
80
81void PathCache::clearGarbage() {
82 Mutex::Autolock _l(mLock);
83 size_t count = mGarbage.size();
84 for (size_t i = 0; i < count; i++) {
85 remove(mGarbage.itemAt(i));
86 }
87 mGarbage.clear();
Romain Guya2341a92010-09-08 18:04:33 -070088}
89
Romain Guy7fbcc042010-08-04 15:40:07 -070090PathTexture* PathCache::get(SkPath* path, SkPaint* paint) {
Romain Guy4bcb7462012-02-23 17:08:38 -080091 const SkPath* sourcePath = path->getSourcePath();
92 if (sourcePath && sourcePath->getGenerationID() == path->getGenerationID()) {
93 path = const_cast<SkPath*>(sourcePath);
94 }
95
Romain Guy7fbcc042010-08-04 15:40:07 -070096 PathCacheEntry entry(path, paint);
97 PathTexture* texture = mCache.get(entry);
98
Romain Guy33f6beb2012-02-16 19:24:51 -080099 float left, top, offset;
100 uint32_t width, height;
101
Romain Guy7fbcc042010-08-04 15:40:07 -0700102 if (!texture) {
103 texture = addTexture(entry, path, paint);
104 } else if (path->getGenerationID() != texture->generation) {
105 mCache.remove(entry);
106 texture = addTexture(entry, path, paint);
107 }
108
Romain Guy7fbcc042010-08-04 15:40:07 -0700109 return texture;
110}
111
Romain Guy7fbcc042010-08-04 15:40:07 -0700112}; // namespace uirenderer
113}; // namespace android