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