Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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_SHAPE_CACHE_H |
| 18 | #define ANDROID_HWUI_SHAPE_CACHE_H |
| 19 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 20 | #define ATRACE_TAG ATRACE_TAG_VIEW |
| 21 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 22 | #include <GLES2/gl2.h> |
| 23 | |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 24 | #include <SkBitmap.h> |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 25 | #include <SkCanvas.h> |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 26 | #include <SkPaint.h> |
| 27 | #include <SkPath.h> |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 28 | #include <SkRect.h> |
| 29 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 30 | #include <utils/JenkinsHash.h> |
| 31 | #include <utils/LruCache.h> |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 32 | #include <utils/Trace.h> |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 33 | |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 34 | #include "Debug.h" |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 35 | #include "Properties.h" |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 36 | #include "Texture.h" |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 37 | #include "thread/Task.h" |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | namespace uirenderer { |
| 41 | |
| 42 | /////////////////////////////////////////////////////////////////////////////// |
| 43 | // Defines |
| 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | |
| 46 | // Debug |
| 47 | #if DEBUG_SHAPES |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 48 | #define SHAPE_LOGD(...) ALOGD(__VA_ARGS__) |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 49 | #else |
| 50 | #define SHAPE_LOGD(...) |
| 51 | #endif |
| 52 | |
| 53 | /////////////////////////////////////////////////////////////////////////////// |
| 54 | // Classes |
| 55 | /////////////////////////////////////////////////////////////////////////////// |
| 56 | |
| 57 | /** |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 58 | * Alpha texture used to represent a path. |
| 59 | */ |
| 60 | struct PathTexture: public Texture { |
| 61 | PathTexture(): Texture() { |
| 62 | } |
| 63 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 64 | ~PathTexture() { |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 65 | clearTask(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 68 | /** |
| 69 | * Left coordinate of the path bounds. |
| 70 | */ |
| 71 | float left; |
| 72 | /** |
| 73 | * Top coordinate of the path bounds. |
| 74 | */ |
| 75 | float top; |
| 76 | /** |
| 77 | * Offset to draw the path at the correct origin. |
| 78 | */ |
| 79 | float offset; |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 80 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 81 | sp<Task<SkBitmap*> > task() const { |
| 82 | return mTask; |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 85 | void setTask(const sp<Task<SkBitmap*> >& task) { |
| 86 | mTask = task; |
| 87 | } |
| 88 | |
| 89 | void clearTask() { |
| 90 | if (mTask != NULL) { |
| 91 | mTask.clear(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | private: |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 96 | sp<Task<SkBitmap*> > mTask; |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 97 | }; // struct PathTexture |
| 98 | |
| 99 | /** |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 100 | * Describe a shape in the shape cache. |
| 101 | */ |
| 102 | struct ShapeCacheEntry { |
| 103 | enum ShapeType { |
| 104 | kShapeNone, |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 105 | kShapeRect, |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 106 | kShapeRoundRect, |
| 107 | kShapeCircle, |
| 108 | kShapeOval, |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 109 | kShapeArc, |
| 110 | kShapePath |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | ShapeCacheEntry() { |
| 114 | shapeType = kShapeNone; |
| 115 | join = SkPaint::kDefault_Join; |
| 116 | cap = SkPaint::kDefault_Cap; |
| 117 | style = SkPaint::kFill_Style; |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 118 | miter = 4.0f; |
| 119 | strokeWidth = 1.0f; |
Romain Guy | 1af23a3 | 2011-03-24 16:03:55 -0700 | [diff] [blame] | 120 | pathEffect = NULL; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 123 | ShapeCacheEntry(ShapeType type, SkPaint* paint) { |
| 124 | shapeType = type; |
| 125 | join = paint->getStrokeJoin(); |
| 126 | cap = paint->getStrokeCap(); |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 127 | miter = paint->getStrokeMiter(); |
| 128 | strokeWidth = paint->getStrokeWidth(); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 129 | style = paint->getStyle(); |
Romain Guy | b29cfbf | 2011-03-18 16:24:19 -0700 | [diff] [blame] | 130 | pathEffect = paint->getPathEffect(); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | virtual ~ShapeCacheEntry() { |
| 134 | } |
| 135 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 136 | virtual hash_t hash() const { |
| 137 | uint32_t hash = JenkinsHashMix(0, shapeType); |
| 138 | hash = JenkinsHashMix(hash, join); |
| 139 | hash = JenkinsHashMix(hash, cap); |
| 140 | hash = JenkinsHashMix(hash, style); |
| 141 | hash = JenkinsHashMix(hash, android::hash_type(miter)); |
| 142 | hash = JenkinsHashMix(hash, android::hash_type(strokeWidth)); |
| 143 | hash = JenkinsHashMix(hash, android::hash_type(pathEffect)); |
| 144 | return JenkinsHashWhiten(hash); |
| 145 | } |
| 146 | |
| 147 | virtual int compare(const ShapeCacheEntry& rhs) const { |
| 148 | int deltaInt = shapeType - rhs.shapeType; |
| 149 | if (deltaInt != 0) return deltaInt; |
| 150 | |
| 151 | deltaInt = join - rhs.join; |
| 152 | if (deltaInt != 0) return deltaInt; |
| 153 | |
| 154 | deltaInt = cap - rhs.cap; |
| 155 | if (deltaInt != 0) return deltaInt; |
| 156 | |
| 157 | deltaInt = style - rhs.style; |
| 158 | if (deltaInt != 0) return deltaInt; |
| 159 | |
| 160 | if (miter < rhs.miter) return -1; |
| 161 | if (miter > rhs.miter) return +1; |
| 162 | |
| 163 | if (strokeWidth < rhs.strokeWidth) return -1; |
| 164 | if (strokeWidth > rhs.strokeWidth) return +1; |
| 165 | |
| 166 | if (pathEffect < rhs.pathEffect) return -1; |
| 167 | if (pathEffect > rhs.pathEffect) return +1; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | bool operator==(const ShapeCacheEntry& other) const { |
| 173 | return compare(other) == 0; |
| 174 | } |
| 175 | |
| 176 | bool operator!=(const ShapeCacheEntry& other) const { |
| 177 | return compare(other) != 0; |
| 178 | } |
| 179 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 180 | ShapeType shapeType; |
| 181 | SkPaint::Join join; |
| 182 | SkPaint::Cap cap; |
| 183 | SkPaint::Style style; |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 184 | float miter; |
| 185 | float strokeWidth; |
Romain Guy | b29cfbf | 2011-03-18 16:24:19 -0700 | [diff] [blame] | 186 | SkPathEffect* pathEffect; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 187 | }; // struct ShapeCacheEntry |
| 188 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 189 | // Cache support |
| 190 | |
| 191 | inline int strictly_order_type(const ShapeCacheEntry& lhs, const ShapeCacheEntry& rhs) { |
| 192 | return lhs.compare(rhs) < 0; |
| 193 | } |
| 194 | |
| 195 | inline int compare_type(const ShapeCacheEntry& lhs, const ShapeCacheEntry& rhs) { |
| 196 | return lhs.compare(rhs); |
| 197 | } |
| 198 | |
| 199 | inline hash_t hash_type(const ShapeCacheEntry& entry) { |
| 200 | return entry.hash(); |
| 201 | } |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 202 | |
| 203 | struct RoundRectShapeCacheEntry: public ShapeCacheEntry { |
| 204 | RoundRectShapeCacheEntry(float width, float height, float rx, float ry, SkPaint* paint): |
| 205 | ShapeCacheEntry(ShapeCacheEntry::kShapeRoundRect, paint) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 206 | mWidth = width; |
| 207 | mHeight = height; |
| 208 | mRx = rx; |
| 209 | mRy = ry; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | RoundRectShapeCacheEntry(): ShapeCacheEntry() { |
| 213 | mWidth = 0; |
| 214 | mHeight = 0; |
| 215 | mRx = 0; |
| 216 | mRy = 0; |
| 217 | } |
| 218 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 219 | hash_t hash() const { |
| 220 | uint32_t hash = ShapeCacheEntry::hash(); |
| 221 | hash = JenkinsHashMix(hash, android::hash_type(mWidth)); |
| 222 | hash = JenkinsHashMix(hash, android::hash_type(mHeight)); |
| 223 | hash = JenkinsHashMix(hash, android::hash_type(mRx)); |
| 224 | hash = JenkinsHashMix(hash, android::hash_type(mRy)); |
| 225 | return JenkinsHashWhiten(hash); |
| 226 | } |
| 227 | |
| 228 | int compare(const ShapeCacheEntry& r) const { |
| 229 | int deltaInt = ShapeCacheEntry::compare(r); |
| 230 | if (deltaInt != 0) return deltaInt; |
| 231 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 232 | const RoundRectShapeCacheEntry& rhs = (const RoundRectShapeCacheEntry&) r; |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 233 | |
| 234 | if (mWidth < rhs.mWidth) return -1; |
| 235 | if (mWidth > rhs.mWidth) return +1; |
| 236 | |
| 237 | if (mHeight < rhs.mHeight) return -1; |
| 238 | if (mHeight > rhs.mHeight) return +1; |
| 239 | |
| 240 | if (mRx < rhs.mRx) return -1; |
| 241 | if (mRx > rhs.mRx) return +1; |
| 242 | |
| 243 | if (mRy < rhs.mRy) return -1; |
| 244 | if (mRy > rhs.mRy) return +1; |
| 245 | |
| 246 | return 0; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | private: |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 250 | float mWidth; |
| 251 | float mHeight; |
| 252 | float mRx; |
| 253 | float mRy; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 254 | }; // RoundRectShapeCacheEntry |
| 255 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 256 | inline hash_t hash_type(const RoundRectShapeCacheEntry& entry) { |
| 257 | return entry.hash(); |
| 258 | } |
| 259 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 260 | struct CircleShapeCacheEntry: public ShapeCacheEntry { |
| 261 | CircleShapeCacheEntry(float radius, SkPaint* paint): |
| 262 | ShapeCacheEntry(ShapeCacheEntry::kShapeCircle, paint) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 263 | mRadius = radius; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | CircleShapeCacheEntry(): ShapeCacheEntry() { |
| 267 | mRadius = 0; |
| 268 | } |
| 269 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 270 | hash_t hash() const { |
| 271 | uint32_t hash = ShapeCacheEntry::hash(); |
| 272 | hash = JenkinsHashMix(hash, android::hash_type(mRadius)); |
| 273 | return JenkinsHashWhiten(hash); |
| 274 | } |
| 275 | |
| 276 | int compare(const ShapeCacheEntry& r) const { |
| 277 | int deltaInt = ShapeCacheEntry::compare(r); |
| 278 | if (deltaInt != 0) return deltaInt; |
| 279 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 280 | const CircleShapeCacheEntry& rhs = (const CircleShapeCacheEntry&) r; |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 281 | |
| 282 | if (mRadius < rhs.mRadius) return -1; |
| 283 | if (mRadius > rhs.mRadius) return +1; |
| 284 | |
| 285 | return 0; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | private: |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 289 | float mRadius; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 290 | }; // CircleShapeCacheEntry |
| 291 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 292 | inline hash_t hash_type(const CircleShapeCacheEntry& entry) { |
| 293 | return entry.hash(); |
| 294 | } |
| 295 | |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 296 | struct OvalShapeCacheEntry: public ShapeCacheEntry { |
| 297 | OvalShapeCacheEntry(float width, float height, SkPaint* paint): |
| 298 | ShapeCacheEntry(ShapeCacheEntry::kShapeOval, paint) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 299 | mWidth = width; |
| 300 | mHeight = height; |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | OvalShapeCacheEntry(): ShapeCacheEntry() { |
| 304 | mWidth = mHeight = 0; |
| 305 | } |
| 306 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 307 | hash_t hash() const { |
| 308 | uint32_t hash = ShapeCacheEntry::hash(); |
| 309 | hash = JenkinsHashMix(hash, android::hash_type(mWidth)); |
| 310 | hash = JenkinsHashMix(hash, android::hash_type(mHeight)); |
| 311 | return JenkinsHashWhiten(hash); |
| 312 | } |
| 313 | |
| 314 | int compare(const ShapeCacheEntry& r) const { |
| 315 | int deltaInt = ShapeCacheEntry::compare(r); |
| 316 | if (deltaInt != 0) return deltaInt; |
| 317 | |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 318 | const OvalShapeCacheEntry& rhs = (const OvalShapeCacheEntry&) r; |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 319 | |
| 320 | if (mWidth < rhs.mWidth) return -1; |
| 321 | if (mWidth > rhs.mWidth) return +1; |
| 322 | |
| 323 | if (mHeight < rhs.mHeight) return -1; |
| 324 | if (mHeight > rhs.mHeight) return +1; |
| 325 | |
| 326 | return 0; |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | private: |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 330 | float mWidth; |
| 331 | float mHeight; |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 332 | }; // OvalShapeCacheEntry |
| 333 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 334 | inline hash_t hash_type(const OvalShapeCacheEntry& entry) { |
| 335 | return entry.hash(); |
| 336 | } |
| 337 | |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 338 | struct RectShapeCacheEntry: public ShapeCacheEntry { |
| 339 | RectShapeCacheEntry(float width, float height, SkPaint* paint): |
| 340 | ShapeCacheEntry(ShapeCacheEntry::kShapeRect, paint) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 341 | mWidth = width; |
| 342 | mHeight = height; |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | RectShapeCacheEntry(): ShapeCacheEntry() { |
| 346 | mWidth = mHeight = 0; |
| 347 | } |
| 348 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 349 | hash_t hash() const { |
| 350 | uint32_t hash = ShapeCacheEntry::hash(); |
| 351 | hash = JenkinsHashMix(hash, android::hash_type(mWidth)); |
| 352 | hash = JenkinsHashMix(hash, android::hash_type(mHeight)); |
| 353 | return JenkinsHashWhiten(hash); |
| 354 | } |
| 355 | |
| 356 | int compare(const ShapeCacheEntry& r) const { |
| 357 | int deltaInt = ShapeCacheEntry::compare(r); |
| 358 | if (deltaInt != 0) return deltaInt; |
| 359 | |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 360 | const RectShapeCacheEntry& rhs = (const RectShapeCacheEntry&) r; |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 361 | |
| 362 | if (mWidth < rhs.mWidth) return -1; |
| 363 | if (mWidth > rhs.mWidth) return +1; |
| 364 | |
| 365 | if (mHeight < rhs.mHeight) return -1; |
| 366 | if (mHeight > rhs.mHeight) return +1; |
| 367 | |
| 368 | return 0; |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | private: |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 372 | float mWidth; |
| 373 | float mHeight; |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 374 | }; // RectShapeCacheEntry |
| 375 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 376 | inline hash_t hash_type(const RectShapeCacheEntry& entry) { |
| 377 | return entry.hash(); |
| 378 | } |
| 379 | |
Romain Guy | 8b2f526 | 2011-01-23 16:15:02 -0800 | [diff] [blame] | 380 | struct ArcShapeCacheEntry: public ShapeCacheEntry { |
| 381 | ArcShapeCacheEntry(float width, float height, float startAngle, float sweepAngle, |
| 382 | bool useCenter, SkPaint* paint): |
| 383 | ShapeCacheEntry(ShapeCacheEntry::kShapeArc, paint) { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 384 | mWidth = width; |
| 385 | mHeight = height; |
| 386 | mStartAngle = startAngle; |
| 387 | mSweepAngle = sweepAngle; |
Romain Guy | 8b2f526 | 2011-01-23 16:15:02 -0800 | [diff] [blame] | 388 | mUseCenter = useCenter ? 1 : 0; |
| 389 | } |
| 390 | |
| 391 | ArcShapeCacheEntry(): ShapeCacheEntry() { |
| 392 | mWidth = 0; |
| 393 | mHeight = 0; |
| 394 | mStartAngle = 0; |
| 395 | mSweepAngle = 0; |
| 396 | mUseCenter = 0; |
| 397 | } |
| 398 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 399 | hash_t hash() const { |
| 400 | uint32_t hash = ShapeCacheEntry::hash(); |
| 401 | hash = JenkinsHashMix(hash, android::hash_type(mWidth)); |
| 402 | hash = JenkinsHashMix(hash, android::hash_type(mHeight)); |
| 403 | hash = JenkinsHashMix(hash, android::hash_type(mStartAngle)); |
| 404 | hash = JenkinsHashMix(hash, android::hash_type(mSweepAngle)); |
| 405 | hash = JenkinsHashMix(hash, mUseCenter); |
| 406 | return JenkinsHashWhiten(hash); |
| 407 | } |
| 408 | |
| 409 | int compare(const ShapeCacheEntry& r) const { |
| 410 | int deltaInt = ShapeCacheEntry::compare(r); |
| 411 | if (deltaInt != 0) return deltaInt; |
| 412 | |
Romain Guy | 8b2f526 | 2011-01-23 16:15:02 -0800 | [diff] [blame] | 413 | const ArcShapeCacheEntry& rhs = (const ArcShapeCacheEntry&) r; |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 414 | |
| 415 | if (mWidth < rhs.mWidth) return -1; |
| 416 | if (mWidth > rhs.mWidth) return +1; |
| 417 | |
| 418 | if (mHeight < rhs.mHeight) return -1; |
| 419 | if (mHeight > rhs.mHeight) return +1; |
| 420 | |
| 421 | if (mStartAngle < rhs.mStartAngle) return -1; |
| 422 | if (mStartAngle > rhs.mStartAngle) return +1; |
| 423 | |
| 424 | if (mSweepAngle < rhs.mSweepAngle) return -1; |
| 425 | if (mSweepAngle > rhs.mSweepAngle) return +1; |
| 426 | |
| 427 | return mUseCenter - rhs.mUseCenter; |
Romain Guy | 8b2f526 | 2011-01-23 16:15:02 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | private: |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 431 | float mWidth; |
| 432 | float mHeight; |
| 433 | float mStartAngle; |
| 434 | float mSweepAngle; |
Romain Guy | 8b2f526 | 2011-01-23 16:15:02 -0800 | [diff] [blame] | 435 | uint32_t mUseCenter; |
| 436 | }; // ArcShapeCacheEntry |
| 437 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 438 | inline hash_t hash_type(const ArcShapeCacheEntry& entry) { |
| 439 | return entry.hash(); |
| 440 | } |
| 441 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 442 | /** |
| 443 | * A simple LRU shape cache. The cache has a maximum size expressed in bytes. |
| 444 | * Any texture added to the cache causing the cache to grow beyond the maximum |
| 445 | * allowed size will also cause the oldest texture to be kicked out. |
| 446 | */ |
| 447 | template<typename Entry> |
| 448 | class ShapeCache: public OnEntryRemoved<Entry, PathTexture*> { |
| 449 | public: |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 450 | ShapeCache(const char* name, const char* propertyName, float defaultSize); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 451 | ~ShapeCache(); |
| 452 | |
| 453 | /** |
| 454 | * Used as a callback when an entry is removed from the cache. |
| 455 | * Do not invoke directly. |
| 456 | */ |
| 457 | void operator()(Entry& path, PathTexture*& texture); |
| 458 | |
| 459 | /** |
| 460 | * Clears the cache. This causes all textures to be deleted. |
| 461 | */ |
| 462 | void clear(); |
| 463 | |
| 464 | /** |
| 465 | * Sets the maximum size of the cache in bytes. |
| 466 | */ |
| 467 | void setMaxSize(uint32_t maxSize); |
| 468 | /** |
| 469 | * Returns the maximum size of the cache in bytes. |
| 470 | */ |
| 471 | uint32_t getMaxSize(); |
| 472 | /** |
| 473 | * Returns the current size of the cache in bytes. |
| 474 | */ |
| 475 | uint32_t getSize(); |
| 476 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 477 | /** |
| 478 | * Trims the contents of the cache, removing items until it's under its |
| 479 | * specified limit. |
| 480 | * |
| 481 | * Trimming is used for caches that support pre-caching from a worker |
| 482 | * thread. During pre-caching the maximum limit of the cache can be |
| 483 | * exceeded for the duration of the frame. It is therefore required to |
| 484 | * trim the cache at the end of the frame to keep the total amount of |
| 485 | * memory used under control. |
| 486 | * |
| 487 | * Only the PathCache currently supports pre-caching. |
| 488 | */ |
| 489 | void trim(); |
| 490 | |
| 491 | static void computePathBounds(const SkPath* path, const SkPaint* paint, |
| 492 | float& left, float& top, float& offset, uint32_t& width, uint32_t& height) { |
| 493 | const SkRect& bounds = path->getBounds(); |
| 494 | computeBounds(bounds, paint, left, top, offset, width, height); |
| 495 | } |
| 496 | |
| 497 | static void computeBounds(const SkRect& bounds, const SkPaint* paint, |
| 498 | float& left, float& top, float& offset, uint32_t& width, uint32_t& height) { |
| 499 | const float pathWidth = fmax(bounds.width(), 1.0f); |
| 500 | const float pathHeight = fmax(bounds.height(), 1.0f); |
| 501 | |
| 502 | left = bounds.fLeft; |
| 503 | top = bounds.fTop; |
| 504 | |
| 505 | offset = (int) floorf(fmax(paint->getStrokeWidth(), 1.0f) * 1.5f + 0.5f); |
| 506 | |
| 507 | width = uint32_t(pathWidth + offset * 2.0 + 0.5); |
| 508 | height = uint32_t(pathHeight + offset * 2.0 + 0.5); |
| 509 | } |
| 510 | |
| 511 | static void drawPath(const SkPath *path, const SkPaint* paint, SkBitmap& bitmap, |
| 512 | float left, float top, float offset, uint32_t width, uint32_t height) { |
| 513 | initBitmap(bitmap, width, height); |
| 514 | |
| 515 | SkPaint pathPaint(*paint); |
| 516 | initPaint(pathPaint); |
| 517 | |
| 518 | SkCanvas canvas(bitmap); |
| 519 | canvas.translate(-left + offset, -top + offset); |
| 520 | canvas.drawPath(*path, pathPaint); |
| 521 | } |
| 522 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 523 | protected: |
| 524 | PathTexture* addTexture(const Entry& entry, const SkPath *path, const SkPaint* paint); |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 525 | PathTexture* addTexture(const Entry& entry, SkBitmap* bitmap); |
| 526 | void addTexture(const Entry& entry, SkBitmap* bitmap, PathTexture* texture); |
| 527 | |
| 528 | /** |
| 529 | * Ensures there is enough space in the cache for a texture of the specified |
| 530 | * dimensions. |
| 531 | */ |
| 532 | void purgeCache(uint32_t width, uint32_t height); |
| 533 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 534 | PathTexture* get(Entry entry) { |
| 535 | return mCache.get(entry); |
| 536 | } |
| 537 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 538 | void removeTexture(PathTexture* texture); |
| 539 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 540 | bool checkTextureSize(uint32_t width, uint32_t height) { |
| 541 | if (width > mMaxTextureSize || height > mMaxTextureSize) { |
| 542 | ALOGW("Shape %s too large to be rendered into a texture (%dx%d, max=%dx%d)", |
| 543 | mName, width, height, mMaxTextureSize, mMaxTextureSize); |
| 544 | return false; |
| 545 | } |
| 546 | return true; |
| 547 | } |
| 548 | |
| 549 | static PathTexture* createTexture(float left, float top, float offset, |
Romain Guy | 5dc7fa7 | 2013-03-11 20:48:31 -0700 | [diff] [blame] | 550 | uint32_t width, uint32_t height, uint32_t id) { |
| 551 | PathTexture* texture = new PathTexture(); |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 552 | texture->left = left; |
| 553 | texture->top = top; |
| 554 | texture->offset = offset; |
| 555 | texture->width = width; |
| 556 | texture->height = height; |
| 557 | texture->generation = id; |
| 558 | return texture; |
| 559 | } |
| 560 | |
| 561 | static void initBitmap(SkBitmap& bitmap, uint32_t width, uint32_t height) { |
| 562 | bitmap.setConfig(SkBitmap::kA8_Config, width, height); |
| 563 | bitmap.allocPixels(); |
| 564 | bitmap.eraseColor(0); |
| 565 | } |
| 566 | |
| 567 | static void initPaint(SkPaint& paint) { |
| 568 | // Make sure the paint is opaque, color, alpha, filter, etc. |
| 569 | // will be applied later when compositing the alpha8 texture |
| 570 | paint.setColor(0xff000000); |
| 571 | paint.setAlpha(255); |
| 572 | paint.setColorFilter(NULL); |
| 573 | paint.setMaskFilter(NULL); |
| 574 | paint.setShader(NULL); |
| 575 | SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrc_Mode); |
| 576 | SkSafeUnref(paint.setXfermode(mode)); |
| 577 | } |
| 578 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 579 | LruCache<Entry, PathTexture*> mCache; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 580 | uint32_t mSize; |
| 581 | uint32_t mMaxSize; |
| 582 | GLuint mMaxTextureSize; |
| 583 | |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 584 | char* mName; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 585 | bool mDebugEnabled; |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 586 | |
| 587 | private: |
| 588 | /** |
| 589 | * Generates the texture from a bitmap into the specified texture structure. |
| 590 | */ |
| 591 | void generateTexture(SkBitmap& bitmap, Texture* texture); |
| 592 | |
| 593 | void init(); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 594 | }; // class ShapeCache |
| 595 | |
| 596 | class RoundRectShapeCache: public ShapeCache<RoundRectShapeCacheEntry> { |
| 597 | public: |
| 598 | RoundRectShapeCache(); |
| 599 | |
| 600 | PathTexture* getRoundRect(float width, float height, float rx, float ry, SkPaint* paint); |
| 601 | }; // class RoundRectShapeCache |
| 602 | |
| 603 | class CircleShapeCache: public ShapeCache<CircleShapeCacheEntry> { |
| 604 | public: |
| 605 | CircleShapeCache(); |
| 606 | |
| 607 | PathTexture* getCircle(float radius, SkPaint* paint); |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 608 | }; // class CircleShapeCache |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 609 | |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 610 | class OvalShapeCache: public ShapeCache<OvalShapeCacheEntry> { |
| 611 | public: |
| 612 | OvalShapeCache(); |
| 613 | |
| 614 | PathTexture* getOval(float width, float height, SkPaint* paint); |
| 615 | }; // class OvalShapeCache |
| 616 | |
| 617 | class RectShapeCache: public ShapeCache<RectShapeCacheEntry> { |
| 618 | public: |
| 619 | RectShapeCache(); |
| 620 | |
| 621 | PathTexture* getRect(float width, float height, SkPaint* paint); |
| 622 | }; // class RectShapeCache |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 623 | |
Romain Guy | 8b2f526 | 2011-01-23 16:15:02 -0800 | [diff] [blame] | 624 | class ArcShapeCache: public ShapeCache<ArcShapeCacheEntry> { |
| 625 | public: |
| 626 | ArcShapeCache(); |
| 627 | |
| 628 | PathTexture* getArc(float width, float height, float startAngle, float sweepAngle, |
| 629 | bool useCenter, SkPaint* paint); |
| 630 | }; // class ArcShapeCache |
| 631 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 632 | /////////////////////////////////////////////////////////////////////////////// |
| 633 | // Constructors/destructor |
| 634 | /////////////////////////////////////////////////////////////////////////////// |
| 635 | |
| 636 | template<class Entry> |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 637 | ShapeCache<Entry>::ShapeCache(const char* name, const char* propertyName, float defaultSize): |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 638 | mCache(LruCache<ShapeCacheEntry, PathTexture*>::kUnlimitedCapacity), |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 639 | mSize(0), mMaxSize(MB(defaultSize)) { |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 640 | char property[PROPERTY_VALUE_MAX]; |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 641 | if (property_get(propertyName, property, NULL) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 642 | INIT_LOGD(" Setting %s cache size to %sMB", name, property); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 643 | setMaxSize(MB(atof(property))); |
| 644 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 645 | INIT_LOGD(" Using default %s cache size of %.2fMB", name, defaultSize); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 646 | } |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 647 | |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 648 | size_t len = strlen(name); |
| 649 | mName = new char[len + 1]; |
| 650 | strcpy(mName, name); |
| 651 | mName[len] = '\0'; |
| 652 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 653 | init(); |
| 654 | } |
| 655 | |
| 656 | template<class Entry> |
| 657 | ShapeCache<Entry>::~ShapeCache() { |
| 658 | mCache.clear(); |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 659 | delete[] mName; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | template<class Entry> |
| 663 | void ShapeCache<Entry>::init() { |
| 664 | mCache.setOnEntryRemovedListener(this); |
| 665 | |
| 666 | GLint maxTextureSize; |
| 667 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); |
| 668 | mMaxTextureSize = maxTextureSize; |
| 669 | |
| 670 | mDebugEnabled = readDebugLevel() & kDebugCaches; |
| 671 | } |
| 672 | |
| 673 | /////////////////////////////////////////////////////////////////////////////// |
| 674 | // Size management |
| 675 | /////////////////////////////////////////////////////////////////////////////// |
| 676 | |
| 677 | template<class Entry> |
| 678 | uint32_t ShapeCache<Entry>::getSize() { |
| 679 | return mSize; |
| 680 | } |
| 681 | |
| 682 | template<class Entry> |
| 683 | uint32_t ShapeCache<Entry>::getMaxSize() { |
| 684 | return mMaxSize; |
| 685 | } |
| 686 | |
| 687 | template<class Entry> |
| 688 | void ShapeCache<Entry>::setMaxSize(uint32_t maxSize) { |
| 689 | mMaxSize = maxSize; |
| 690 | while (mSize > mMaxSize) { |
| 691 | mCache.removeOldest(); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /////////////////////////////////////////////////////////////////////////////// |
| 696 | // Callbacks |
| 697 | /////////////////////////////////////////////////////////////////////////////// |
| 698 | |
| 699 | template<class Entry> |
| 700 | void ShapeCache<Entry>::operator()(Entry& path, PathTexture*& texture) { |
| 701 | removeTexture(texture); |
| 702 | } |
| 703 | |
| 704 | /////////////////////////////////////////////////////////////////////////////// |
| 705 | // Caching |
| 706 | /////////////////////////////////////////////////////////////////////////////// |
| 707 | |
| 708 | template<class Entry> |
| 709 | void ShapeCache<Entry>::removeTexture(PathTexture* texture) { |
| 710 | if (texture) { |
| 711 | const uint32_t size = texture->width * texture->height; |
| 712 | mSize -= size; |
| 713 | |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 714 | SHAPE_LOGD("ShapeCache::callback: delete %s: name, size, mSize = %d, %d, %d", |
| 715 | mName, texture->id, size, mSize); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 716 | if (mDebugEnabled) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 717 | ALOGD("Shape %s deleted, size = %d", mName, size); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | glDeleteTextures(1, &texture->id); |
| 721 | delete texture; |
| 722 | } |
| 723 | } |
| 724 | |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 725 | template<class Entry> |
| 726 | void ShapeCache<Entry>::purgeCache(uint32_t width, uint32_t height) { |
| 727 | const uint32_t size = width * height; |
| 728 | // Don't even try to cache a bitmap that's bigger than the cache |
| 729 | if (size < mMaxSize) { |
| 730 | while (mSize + size > mMaxSize) { |
| 731 | mCache.removeOldest(); |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | template<class Entry> |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 737 | void ShapeCache<Entry>::trim() { |
| 738 | while (mSize > mMaxSize) { |
| 739 | mCache.removeOldest(); |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 740 | } |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 741 | } |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 742 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 743 | template<class Entry> |
| 744 | PathTexture* ShapeCache<Entry>::addTexture(const Entry& entry, const SkPath *path, |
| 745 | const SkPaint* paint) { |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 746 | ATRACE_CALL(); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 747 | |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 748 | float left, top, offset; |
| 749 | uint32_t width, height; |
| 750 | computePathBounds(path, paint, left, top, offset, width, height); |
Romain Guy | 98029c8 | 2011-06-17 15:47:07 -0700 | [diff] [blame] | 751 | |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 752 | if (!checkTextureSize(width, height)) return NULL; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 753 | |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 754 | purgeCache(width, height); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 755 | |
| 756 | SkBitmap bitmap; |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 757 | drawPath(path, paint, bitmap, left, top, offset, width, height); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 758 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 759 | PathTexture* texture = createTexture(left, top, offset, width, height, |
| 760 | path->getGenerationID()); |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 761 | addTexture(entry, &bitmap, texture); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 762 | |
Romain Guy | fdd6fc1 | 2012-04-27 11:47:13 -0700 | [diff] [blame] | 763 | return texture; |
| 764 | } |
| 765 | |
| 766 | template<class Entry> |
| 767 | void ShapeCache<Entry>::addTexture(const Entry& entry, SkBitmap* bitmap, PathTexture* texture) { |
| 768 | generateTexture(*bitmap, texture); |
| 769 | |
| 770 | uint32_t size = texture->width * texture->height; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 771 | if (size < mMaxSize) { |
| 772 | mSize += size; |
Romain Guy | ff26a0c | 2011-01-20 11:35:46 -0800 | [diff] [blame] | 773 | SHAPE_LOGD("ShapeCache::get: create %s: name, size, mSize = %d, %d, %d", |
| 774 | mName, texture->id, size, mSize); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 775 | if (mDebugEnabled) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 776 | ALOGD("Shape %s created, size = %d", mName, size); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 777 | } |
| 778 | mCache.put(entry, texture); |
| 779 | } else { |
| 780 | texture->cleanup = true; |
| 781 | } |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | template<class Entry> |
| 785 | void ShapeCache<Entry>::clear() { |
| 786 | mCache.clear(); |
| 787 | } |
| 788 | |
| 789 | template<class Entry> |
| 790 | void ShapeCache<Entry>::generateTexture(SkBitmap& bitmap, Texture* texture) { |
| 791 | SkAutoLockPixels alp(bitmap); |
| 792 | if (!bitmap.readyToDraw()) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 793 | ALOGE("Cannot generate texture from bitmap"); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 794 | return; |
| 795 | } |
| 796 | |
| 797 | glGenTextures(1, &texture->id); |
| 798 | |
| 799 | glBindTexture(GL_TEXTURE_2D, texture->id); |
| 800 | // Textures are Alpha8 |
| 801 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 802 | |
| 803 | texture->blend = true; |
| 804 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0, |
| 805 | GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.getPixels()); |
| 806 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 807 | texture->setFilter(GL_LINEAR); |
| 808 | texture->setWrap(GL_CLAMP_TO_EDGE); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | }; // namespace uirenderer |
| 812 | }; // namespace android |
| 813 | |
| 814 | #endif // ANDROID_HWUI_SHAPE_CACHE_H |