PathCache.cpp revision 4bcb7467a174ed03a67b0c62950c555813ddf00d
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 19#include <utils/threads.h> 20 21#include "PathCache.h" 22#include "Properties.h" 23 24namespace android { 25namespace uirenderer { 26 27// Defined in ShapeCache.h 28void computePathBounds(const SkPath *path, const SkPaint* paint, 29 float& left, float& top, float& offset, uint32_t& width, uint32_t& height) { 30 const SkRect& bounds = path->getBounds(); 31 32 const float pathWidth = fmax(bounds.width(), 1.0f); 33 const float pathHeight = fmax(bounds.height(), 1.0f); 34 35 left = bounds.fLeft; 36 top = bounds.fTop; 37 38 offset = (int) floorf(fmax(paint->getStrokeWidth(), 1.0f) * 1.5f + 0.5f); 39 40 width = uint32_t(pathWidth + offset * 2.0 + 0.5); 41 height = uint32_t(pathHeight + offset * 2.0 + 0.5); 42} 43 44/////////////////////////////////////////////////////////////////////////////// 45// Path cache 46/////////////////////////////////////////////////////////////////////////////// 47 48PathCache::PathCache(): ShapeCache<PathCacheEntry>("path", 49 PROPERTY_PATH_CACHE_SIZE, DEFAULT_PATH_CACHE_SIZE) { 50} 51 52void PathCache::remove(SkPath* path) { 53 // TODO: Linear search... 54 Vector<size_t> pathsToRemove; 55 for (size_t i = 0; i < mCache.size(); i++) { 56 if (mCache.getKeyAt(i).path == path) { 57 pathsToRemove.push(i); 58 removeTexture(mCache.getValueAt(i)); 59 } 60 } 61 62 mCache.setOnEntryRemovedListener(NULL); 63 for (size_t i = 0; i < pathsToRemove.size(); i++) { 64 // This will work because pathsToRemove is sorted 65 // and because the cache is a sorted keyed vector 66 mCache.removeAt(pathsToRemove.itemAt(i) - i); 67 } 68 mCache.setOnEntryRemovedListener(this); 69} 70 71void PathCache::removeDeferred(SkPath* path) { 72 Mutex::Autolock _l(mLock); 73 mGarbage.push(path); 74} 75 76void PathCache::clearGarbage() { 77 Mutex::Autolock _l(mLock); 78 size_t count = mGarbage.size(); 79 for (size_t i = 0; i < count; i++) { 80 remove(mGarbage.itemAt(i)); 81 } 82 mGarbage.clear(); 83} 84 85PathTexture* PathCache::get(SkPath* path, SkPaint* paint) { 86 const SkPath* sourcePath = path->getSourcePath(); 87 if (sourcePath && sourcePath->getGenerationID() == path->getGenerationID()) { 88 path = const_cast<SkPath*>(sourcePath); 89 } 90 91 PathCacheEntry entry(path, paint); 92 PathTexture* texture = mCache.get(entry); 93 94 float left, top, offset; 95 uint32_t width, height; 96 97 if (!texture) { 98 texture = addTexture(entry, path, paint); 99 } else if (path->getGenerationID() != texture->generation) { 100 mCache.remove(entry); 101 texture = addTexture(entry, path, paint); 102 } 103 104 return texture; 105} 106 107}; // namespace uirenderer 108}; // namespace android 109