PathCache.h revision 7fbcc0492fca03857e3c45064f4aa040af817d55
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#ifndef ANDROID_UI_PATH_CACHE_H 18#define ANDROID_UI_PATH_CACHE_H 19 20#include <SkBitmap.h> 21#include <SkPaint.h> 22#include <SkPath.h> 23 24#include "Texture.h" 25#include "GenerationCache.h" 26 27namespace android { 28namespace uirenderer { 29 30/** 31 * Describe a path in the path cache. 32 */ 33struct PathCacheEntry { 34 PathCacheEntry() { 35 path = NULL; 36 join = SkPaint::kDefault_Join; 37 cap = SkPaint::kDefault_Cap; 38 style = SkPaint::kFill_Style; 39 miter = 4.0f; 40 strokeWidth = 1.0f; 41 } 42 43 PathCacheEntry(const PathCacheEntry& entry): 44 path(entry.path), join(entry.join), cap(entry.cap), 45 style(entry.style), miter(entry.miter), 46 strokeWidth(entry.strokeWidth) { 47 } 48 49 PathCacheEntry(SkPath* path, SkPaint* paint) { 50 this->path = path; 51 join = paint->getStrokeJoin(); 52 cap = paint->getStrokeCap(); 53 miter = paint->getStrokeMiter(); 54 strokeWidth = paint->getStrokeWidth(); 55 style = paint->getStyle(); 56 } 57 58 SkPath* path; 59 SkPaint::Join join; 60 SkPaint::Cap cap; 61 SkPaint::Style style; 62 float miter; 63 float strokeWidth; 64 65 bool operator<(const PathCacheEntry& rhs) const { 66 return memcmp(this, &rhs, sizeof(PathCacheEntry)) < 0; 67 } 68}; // struct PathCacheEntry 69 70/** 71 * Alpha texture used to represent a path. 72 */ 73struct PathTexture: public Texture { 74 /** 75 * Left coordinate of the path bounds. 76 */ 77 float left; 78 /** 79 * Top coordinate of the path bounds. 80 */ 81 float top; 82 /** 83 * Offset to draw the path at the correct origin. 84 */ 85 float offset; 86}; // struct PathTexture 87 88/** 89 * A simple LRU path cache. The cache has a maximum size expressed in bytes. 90 * Any texture added to the cache causing the cache to grow beyond the maximum 91 * allowed size will also cause the oldest texture to be kicked out. 92 */ 93class PathCache: public OnEntryRemoved<PathCacheEntry, PathTexture*> { 94public: 95 PathCache(uint32_t maxByteSize); 96 ~PathCache(); 97 98 /** 99 * Used as a callback when an entry is removed from the cache. 100 * Do not invoke directly. 101 */ 102 void operator()(PathCacheEntry& path, PathTexture*& texture); 103 104 /** 105 * Returns the texture associated with the specified path. If the texture 106 * cannot be found in the cache, a new texture is generated. 107 */ 108 PathTexture* get(SkPath* path, SkPaint* paint); 109 /** 110 * Clears the cache. This causes all textures to be deleted. 111 */ 112 void clear(); 113 114 /** 115 * Sets the maximum size of the cache in bytes. 116 */ 117 void setMaxSize(uint32_t maxSize); 118 /** 119 * Returns the maximum size of the cache in bytes. 120 */ 121 uint32_t getMaxSize(); 122 /** 123 * Returns the current size of the cache in bytes. 124 */ 125 uint32_t getSize(); 126 127private: 128 /** 129 * Generates the texture from a bitmap into the specified texture structure. 130 */ 131 void generateTexture(SkBitmap& bitmap, Texture* texture); 132 133 PathTexture* addTexture(const PathCacheEntry& entry, const SkPath *path, const SkPaint* paint); 134 135 GenerationCache<PathCacheEntry, PathTexture*> mCache; 136 137 uint32_t mSize; 138 uint32_t mMaxSize; 139}; // class PathCache 140 141}; // namespace uirenderer 142}; // namespace android 143 144#endif // ANDROID_UI_PATH_CACHE_H 145