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