blob: 03ddf597b33a3af82ec05a4080ac9d51d2e1ffad [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 Guy059e12c2012-11-28 17:35:51 -080058 Vector<PathCacheEntry> pathsToRemove;
59 LruCache<PathCacheEntry, PathTexture*>::Iterator i(mCache);
60
61 while (i.next()) {
62 const PathCacheEntry& key = i.key();
63 if (key.path == path) {
64 pathsToRemove.push(key);
Romain Guya2341a92010-09-08 18:04:33 -070065 }
66 }
Romain Guy9e108412010-11-09 14:35:20 -080067
68 for (size_t i = 0; i < pathsToRemove.size(); i++) {
Romain Guy059e12c2012-11-28 17:35:51 -080069 mCache.remove(pathsToRemove.itemAt(i));
Romain Guy9e108412010-11-09 14:35:20 -080070 }
Romain Guyfe48f652010-11-11 15:36:56 -080071}
72
73void PathCache::removeDeferred(SkPath* path) {
74 Mutex::Autolock _l(mLock);
75 mGarbage.push(path);
76}
77
78void PathCache::clearGarbage() {
79 Mutex::Autolock _l(mLock);
80 size_t count = mGarbage.size();
81 for (size_t i = 0; i < count; i++) {
82 remove(mGarbage.itemAt(i));
83 }
84 mGarbage.clear();
Romain Guya2341a92010-09-08 18:04:33 -070085}
86
Romain Guy7fbcc042010-08-04 15:40:07 -070087PathTexture* PathCache::get(SkPath* path, SkPaint* paint) {
Romain Guy4bcb7462012-02-23 17:08:38 -080088 const SkPath* sourcePath = path->getSourcePath();
89 if (sourcePath && sourcePath->getGenerationID() == path->getGenerationID()) {
90 path = const_cast<SkPath*>(sourcePath);
91 }
92
Romain Guy7fbcc042010-08-04 15:40:07 -070093 PathCacheEntry entry(path, paint);
94 PathTexture* texture = mCache.get(entry);
95
Romain Guy33f6beb2012-02-16 19:24:51 -080096 float left, top, offset;
97 uint32_t width, height;
98
Romain Guy7fbcc042010-08-04 15:40:07 -070099 if (!texture) {
100 texture = addTexture(entry, path, paint);
101 } else if (path->getGenerationID() != texture->generation) {
102 mCache.remove(entry);
103 texture = addTexture(entry, path, paint);
104 }
105
Romain Guy7fbcc042010-08-04 15:40:07 -0700106 return texture;
107}
108
Romain Guy7fbcc042010-08-04 15:40:07 -0700109}; // namespace uirenderer
110}; // namespace android