Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 1 | /* |
| 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 Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 19 | #include <utils/threads.h> |
| 20 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 21 | #include "PathCache.h" |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 22 | #include "Properties.h" |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace uirenderer { |
| 26 | |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 27 | // Defined in ShapeCache.h |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 28 | |
| 29 | void computePathBounds(const SkPath* path, const SkPaint* paint, |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 30 | float& left, float& top, float& offset, uint32_t& width, uint32_t& height) { |
| 31 | const SkRect& bounds = path->getBounds(); |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 32 | computeBounds(bounds, paint, left, top, offset, width, height); |
| 33 | } |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 34 | |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 35 | void computeBounds(const SkRect& bounds, const SkPaint* paint, |
| 36 | float& left, float& top, float& offset, uint32_t& width, uint32_t& height) { |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 37 | 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 Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 49 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 50 | // Path cache |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 51 | /////////////////////////////////////////////////////////////////////////////// |
| 52 | |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 53 | PathCache::PathCache(): ShapeCache<PathCacheEntry>("path", |
| 54 | PROPERTY_PATH_CACHE_SIZE, DEFAULT_PATH_CACHE_SIZE) { |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Romain Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 57 | void PathCache::remove(SkPath* path) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 58 | 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 Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 67 | |
| 68 | for (size_t i = 0; i < pathsToRemove.size(); i++) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 69 | mCache.remove(pathsToRemove.itemAt(i)); |
Romain Guy | 9e10841 | 2010-11-09 14:35:20 -0800 | [diff] [blame] | 70 | } |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void PathCache::removeDeferred(SkPath* path) { |
| 74 | Mutex::Autolock _l(mLock); |
| 75 | mGarbage.push(path); |
| 76 | } |
| 77 | |
| 78 | void 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 Guy | a2341a9 | 2010-09-08 18:04:33 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 87 | PathTexture* PathCache::get(SkPath* path, SkPaint* paint) { |
Romain Guy | 4bcb746 | 2012-02-23 17:08:38 -0800 | [diff] [blame] | 88 | const SkPath* sourcePath = path->getSourcePath(); |
| 89 | if (sourcePath && sourcePath->getGenerationID() == path->getGenerationID()) { |
| 90 | path = const_cast<SkPath*>(sourcePath); |
| 91 | } |
| 92 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 93 | PathCacheEntry entry(path, paint); |
| 94 | PathTexture* texture = mCache.get(entry); |
| 95 | |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 96 | float left, top, offset; |
| 97 | uint32_t width, height; |
| 98 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 99 | 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 Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 106 | return texture; |
| 107 | } |
| 108 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 109 | }; // namespace uirenderer |
| 110 | }; // namespace android |