PathCache.h revision 9cccc2b9bdd4850a3f9679569aaec3ab98477a5d
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 PathTexture(): Texture() { 75 } 76 77 /** 78 * Left coordinate of the path bounds. 79 */ 80 float left; 81 /** 82 * Top coordinate of the path bounds. 83 */ 84 float top; 85 /** 86 * Offset to draw the path at the correct origin. 87 */ 88 float offset; 89}; // struct PathTexture 90 91/** 92 * A simple LRU path cache. The cache has a maximum size expressed in bytes. 93 * Any texture added to the cache causing the cache to grow beyond the maximum 94 * allowed size will also cause the oldest texture to be kicked out. 95 */ 96class PathCache: public OnEntryRemoved<PathCacheEntry, PathTexture*> { 97public: 98 PathCache(uint32_t maxByteSize); 99 ~PathCache(); 100 101 /** 102 * Used as a callback when an entry is removed from the cache. 103 * Do not invoke directly. 104 */ 105 void operator()(PathCacheEntry& path, PathTexture*& texture); 106 107 /** 108 * Returns the texture associated with the specified path. If the texture 109 * cannot be found in the cache, a new texture is generated. 110 */ 111 PathTexture* get(SkPath* path, SkPaint* paint); 112 /** 113 * Clears the cache. This causes all textures to be deleted. 114 */ 115 void clear(); 116 117 /** 118 * Sets the maximum size of the cache in bytes. 119 */ 120 void setMaxSize(uint32_t maxSize); 121 /** 122 * Returns the maximum size of the cache in bytes. 123 */ 124 uint32_t getMaxSize(); 125 /** 126 * Returns the current size of the cache in bytes. 127 */ 128 uint32_t getSize(); 129 130private: 131 /** 132 * Generates the texture from a bitmap into the specified texture structure. 133 */ 134 void generateTexture(SkBitmap& bitmap, Texture* texture); 135 136 PathTexture* addTexture(const PathCacheEntry& entry, const SkPath *path, const SkPaint* paint); 137 138 GenerationCache<PathCacheEntry, PathTexture*> mCache; 139 140 uint32_t mSize; 141 uint32_t mMaxSize; 142 GLuint mMaxTextureSize; 143}; // class PathCache 144 145}; // namespace uirenderer 146}; // namespace android 147 148#endif // ANDROID_UI_PATH_CACHE_H 149