PathCache.h revision c15008e72ec00ca20a271c3006dac649fd07533b
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_HWUI_PATH_CACHE_H 18#define ANDROID_HWUI_PATH_CACHE_H 19 20#include <SkBitmap.h> 21#include <SkPaint.h> 22#include <SkPath.h> 23 24#include "Debug.h" 25#include "Texture.h" 26#include "utils/Compare.h" 27#include "utils/GenerationCache.h" 28 29namespace android { 30namespace uirenderer { 31 32/////////////////////////////////////////////////////////////////////////////// 33// Defines 34/////////////////////////////////////////////////////////////////////////////// 35 36// Debug 37#if DEBUG_PATHS 38 #define PATH_LOGD(...) LOGD(__VA_ARGS__) 39#else 40 #define PATH_LOGD(...) 41#endif 42 43/////////////////////////////////////////////////////////////////////////////// 44// Classes 45/////////////////////////////////////////////////////////////////////////////// 46 47/** 48 * Describe a path in the path cache. 49 */ 50struct PathCacheEntry { 51 PathCacheEntry() { 52 path = NULL; 53 join = SkPaint::kDefault_Join; 54 cap = SkPaint::kDefault_Cap; 55 style = SkPaint::kFill_Style; 56 miter = 4.0f; 57 strokeWidth = 1.0f; 58 } 59 60 PathCacheEntry(const PathCacheEntry& entry): 61 path(entry.path), join(entry.join), cap(entry.cap), 62 style(entry.style), miter(entry.miter), 63 strokeWidth(entry.strokeWidth) { 64 } 65 66 PathCacheEntry(SkPath* path, SkPaint* paint) { 67 this->path = path; 68 join = paint->getStrokeJoin(); 69 cap = paint->getStrokeCap(); 70 miter = paint->getStrokeMiter(); 71 strokeWidth = paint->getStrokeWidth(); 72 style = paint->getStyle(); 73 } 74 75 SkPath* path; 76 SkPaint::Join join; 77 SkPaint::Cap cap; 78 SkPaint::Style style; 79 float miter; 80 float strokeWidth; 81 82 bool operator<(const PathCacheEntry& rhs) const { 83 LTE_INT(path) { 84 LTE_INT(join) { 85 LTE_INT(cap) { 86 LTE_INT(style) { 87 LTE_FLOAT(miter) { 88 LTE_FLOAT(strokeWidth) return false; 89 } 90 } 91 } 92 } 93 } 94 return false; 95 } 96}; // struct PathCacheEntry 97 98/** 99 * Alpha texture used to represent a path. 100 */ 101struct PathTexture: public Texture { 102 PathTexture(): Texture() { 103 } 104 105 /** 106 * Left coordinate of the path bounds. 107 */ 108 float left; 109 /** 110 * Top coordinate of the path bounds. 111 */ 112 float top; 113 /** 114 * Offset to draw the path at the correct origin. 115 */ 116 float offset; 117}; // struct PathTexture 118 119/** 120 * A simple LRU path cache. The cache has a maximum size expressed in bytes. 121 * Any texture added to the cache causing the cache to grow beyond the maximum 122 * allowed size will also cause the oldest texture to be kicked out. 123 */ 124class PathCache: public OnEntryRemoved<PathCacheEntry, PathTexture*> { 125public: 126 PathCache(); 127 PathCache(uint32_t maxByteSize); 128 ~PathCache(); 129 130 /** 131 * Used as a callback when an entry is removed from the cache. 132 * Do not invoke directly. 133 */ 134 void operator()(PathCacheEntry& path, PathTexture*& texture); 135 136 /** 137 * Returns the texture associated with the specified path. If the texture 138 * cannot be found in the cache, a new texture is generated. 139 */ 140 PathTexture* get(SkPath* path, SkPaint* paint); 141 /** 142 * Clears the cache. This causes all textures to be deleted. 143 */ 144 void clear(); 145 /** 146 * Removes an entry. 147 */ 148 void remove(SkPath* path); 149 150 /** 151 * Sets the maximum size of the cache in bytes. 152 */ 153 void setMaxSize(uint32_t maxSize); 154 /** 155 * Returns the maximum size of the cache in bytes. 156 */ 157 uint32_t getMaxSize(); 158 /** 159 * Returns the current size of the cache in bytes. 160 */ 161 uint32_t getSize(); 162 163private: 164 /** 165 * Generates the texture from a bitmap into the specified texture structure. 166 */ 167 void generateTexture(SkBitmap& bitmap, Texture* texture); 168 169 PathTexture* addTexture(const PathCacheEntry& entry, const SkPath *path, const SkPaint* paint); 170 171 void init(); 172 173 GenerationCache<PathCacheEntry, PathTexture*> mCache; 174 175 uint32_t mSize; 176 uint32_t mMaxSize; 177 GLuint mMaxTextureSize; 178 179 /** 180 * Used to access mCache and mSize. All methods are accessed from a single 181 * thread except for remove(). 182 */ 183 mutable Mutex mLock; 184}; // class PathCache 185 186}; // namespace uirenderer 187}; // namespace android 188 189#endif // ANDROID_HWUI_PATH_CACHE_H 190