junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "SkDeferredCanvas.h" |
| 10 | |
| 11 | #include "SkPaint.h" |
| 12 | #include "SkShader.h" |
| 13 | #include "SkColorFilter.h" |
| 14 | #include "SkDrawFilter.h" |
| 15 | |
| 16 | namespace { |
| 17 | |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 18 | bool isPaintOpaque(const SkPaint* paint, const SkBitmap* bmpReplacesShader = NULL) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 19 | // TODO: SkXfermode should have a virtual isOpaque method, which would |
| 20 | // make it possible to test modes that do not have a Coeff representation. |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 21 | |
| 22 | if (!paint) { |
| 23 | return bmpReplacesShader ? bmpReplacesShader->isOpaque() : true; |
| 24 | } |
| 25 | |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 26 | SkXfermode::Coeff srcCoeff, dstCoeff; |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 27 | if (SkXfermode::AsCoeff(paint->getXfermode(), &srcCoeff, &dstCoeff)){ |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 28 | switch (dstCoeff) { |
| 29 | case SkXfermode::kZero_Coeff: |
| 30 | return true; |
| 31 | case SkXfermode::kISA_Coeff: |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 32 | if (paint->getAlpha() != 255) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 33 | break; |
| 34 | } |
| 35 | if (bmpReplacesShader) { |
| 36 | if (!bmpReplacesShader->isOpaque()) { |
| 37 | break; |
| 38 | } |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 39 | } else if (paint->getShader() && !paint->getShader()->isOpaque()) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 40 | break; |
| 41 | } |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 42 | if (paint->getColorFilter() && ((paint->getColorFilter()->getFlags() & |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 43 | SkColorFilter::kAlphaUnchanged_Flag) == 0)) { |
| 44 | break; |
| 45 | } |
| 46 | return true; |
| 47 | case SkXfermode::kSA_Coeff: |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 48 | if (paint->getAlpha() != 0) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 49 | break; |
| 50 | } |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 51 | if (paint->getColorFilter() && ((paint->getColorFilter()->getFlags() & |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 52 | SkColorFilter::kAlphaUnchanged_Flag) == 0)) { |
| 53 | break; |
| 54 | } |
| 55 | return true; |
| 56 | case SkXfermode::kSC_Coeff: |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 57 | if (paint->getColor() != 0) { // all components must be 0 |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 58 | break; |
| 59 | } |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 60 | if (bmpReplacesShader || paint->getShader()) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 61 | break; |
| 62 | } |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 63 | if (paint->getColorFilter() && ((paint->getColorFilter()->getFlags() & |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 64 | SkColorFilter::kAlphaUnchanged_Flag) == 0)) { |
| 65 | break; |
| 66 | } |
| 67 | return true; |
| 68 | default: |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | } // unnamed namespace |
| 76 | |
| 77 | SkDeferredCanvas::SkDeferredCanvas() |
| 78 | { |
| 79 | init(); |
| 80 | } |
| 81 | |
| 82 | SkDeferredCanvas::SkDeferredCanvas(SkDevice* device) |
| 83 | { |
| 84 | init(); |
| 85 | setDevice(device); |
| 86 | } |
| 87 | |
| 88 | SkDeferredCanvas::SkDeferredCanvas(SkDevice* device, |
| 89 | DeviceContext* deviceContext) |
| 90 | { |
| 91 | init(); |
| 92 | setDevice(device); |
| 93 | setDeviceContext(deviceContext); |
| 94 | } |
| 95 | |
| 96 | void SkDeferredCanvas::init() |
| 97 | { |
| 98 | fDeferredDrawing = true; // On by default |
| 99 | } |
| 100 | |
| 101 | void SkDeferredCanvas::validate() const |
| 102 | { |
| 103 | SkASSERT(getDevice()); |
| 104 | SkASSERT(INHERITED::getTotalMatrix().isIdentity()); |
| 105 | } |
| 106 | |
| 107 | SkCanvas* SkDeferredCanvas::drawingCanvas() const |
| 108 | { |
| 109 | validate(); |
| 110 | return fDeferredDrawing ? getDeferredDevice()->recordingCanvas() : |
| 111 | getDeferredDevice()->immediateCanvas(); |
| 112 | } |
| 113 | |
| 114 | void SkDeferredCanvas::flushIfNeeded(const SkBitmap& bitmap) |
| 115 | { |
| 116 | validate(); |
| 117 | if (fDeferredDrawing) { |
| 118 | getDeferredDevice()->flushIfNeeded(bitmap); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | SkDeferredCanvas::DeferredDevice* SkDeferredCanvas::getDeferredDevice() const |
| 123 | { |
| 124 | return static_cast<SkDeferredCanvas::DeferredDevice*>(getDevice()); |
| 125 | } |
| 126 | |
| 127 | void SkDeferredCanvas::setDeferredDrawing(bool val) |
| 128 | { |
| 129 | validate(); // Must set device before calling this method |
| 130 | SkASSERT(drawingCanvas()->getSaveCount() == 1); |
| 131 | if (val != fDeferredDrawing) { |
| 132 | if (fDeferredDrawing) { |
| 133 | // Going live. |
| 134 | getDeferredDevice()->flushPending(); |
| 135 | } |
| 136 | fDeferredDrawing = val; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | SkDeferredCanvas::~SkDeferredCanvas() |
| 141 | { |
| 142 | } |
| 143 | |
| 144 | SkDevice* SkDeferredCanvas::setDevice(SkDevice* device) |
| 145 | { |
| 146 | INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (device)))->unref(); |
| 147 | return device; |
| 148 | } |
| 149 | |
| 150 | SkDeferredCanvas::DeviceContext* SkDeferredCanvas::setDeviceContext( |
| 151 | DeviceContext* deviceContext) |
| 152 | { |
| 153 | DeferredDevice* deferredDevice = getDeferredDevice(); |
| 154 | SkASSERT(deferredDevice); |
| 155 | if (deferredDevice) { |
| 156 | deferredDevice->setDeviceContext(deviceContext); |
| 157 | } |
| 158 | return deviceContext; |
| 159 | } |
| 160 | |
| 161 | bool SkDeferredCanvas::isFullFrame(const SkRect* rect, |
| 162 | const SkPaint* paint) const |
| 163 | { |
| 164 | SkCanvas* canvas = drawingCanvas(); |
| 165 | SkISize canvasSize = getDeviceSize(); |
| 166 | if (rect) { |
| 167 | if (!canvas->getTotalMatrix().rectStaysRect()) { |
| 168 | return false; // conservative |
| 169 | } |
| 170 | |
| 171 | SkRect transformedRect; |
| 172 | canvas->getTotalMatrix().mapRect(&transformedRect, *rect); |
| 173 | |
| 174 | if (paint) { |
| 175 | SkPaint::Style paintStyle = paint->getStyle(); |
| 176 | if (!(paintStyle == SkPaint::kFill_Style || |
| 177 | paintStyle == SkPaint::kStrokeAndFill_Style)) { |
| 178 | return false; |
| 179 | } |
| 180 | if (paint->getMaskFilter() || paint->getLooper() |
| 181 | || paint->getPathEffect() || paint->getImageFilter()) { |
| 182 | return false; // conservative |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // The following test holds with AA enabled, and is conservative |
| 187 | // by a 0.5 pixel margin with AA disabled |
junov@chromium.org | b1e218e | 2012-02-13 22:27:58 +0000 | [diff] [blame] | 188 | if (transformedRect.fLeft > SkIntToScalar(0) || |
| 189 | transformedRect.fTop > SkIntToScalar(0) || |
| 190 | transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) || |
| 191 | transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | switch (canvas->getClipType()) { |
| 197 | case SkCanvas::kRect_ClipType : |
| 198 | { |
| 199 | SkIRect bounds; |
| 200 | canvas->getClipDeviceBounds(&bounds); |
| 201 | if (bounds.fLeft > 0 || bounds.fTop > 0 || |
| 202 | bounds.fRight < canvasSize.fWidth || |
| 203 | bounds.fBottom < canvasSize.fHeight) |
| 204 | return false; |
| 205 | } |
| 206 | break; |
| 207 | case SkCanvas::kComplex_ClipType : |
| 208 | return false; // conservative |
| 209 | case SkCanvas::kEmpty_ClipType: |
| 210 | default: |
| 211 | break; |
| 212 | }; |
| 213 | |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | int SkDeferredCanvas::save(SaveFlags flags) |
| 218 | { |
| 219 | return drawingCanvas()->save(flags); |
| 220 | } |
| 221 | |
| 222 | int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 223 | SaveFlags flags) |
| 224 | { |
| 225 | return drawingCanvas()->saveLayer(bounds, paint, flags); |
| 226 | } |
| 227 | |
| 228 | void SkDeferredCanvas::restore() |
| 229 | { |
| 230 | drawingCanvas()->restore(); |
| 231 | } |
| 232 | |
| 233 | int SkDeferredCanvas::getSaveCount() const |
| 234 | { |
| 235 | return drawingCanvas()->getSaveCount(); |
| 236 | } |
| 237 | |
| 238 | bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) |
| 239 | { |
| 240 | return drawingCanvas()->translate(dx, dy); |
| 241 | } |
| 242 | |
| 243 | bool SkDeferredCanvas::scale(SkScalar sx, SkScalar sy) |
| 244 | { |
| 245 | return drawingCanvas()->scale(sx, sy); |
| 246 | } |
| 247 | |
| 248 | bool SkDeferredCanvas::rotate(SkScalar degrees) |
| 249 | { |
| 250 | return drawingCanvas()->rotate(degrees); |
| 251 | } |
| 252 | |
| 253 | bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) |
| 254 | { |
| 255 | return drawingCanvas()->skew(sx, sy); |
| 256 | } |
| 257 | |
| 258 | bool SkDeferredCanvas::concat(const SkMatrix& matrix) |
| 259 | { |
| 260 | return drawingCanvas()->concat(matrix); |
| 261 | } |
| 262 | |
| 263 | void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) |
| 264 | { |
| 265 | drawingCanvas()->setMatrix(matrix); |
| 266 | } |
| 267 | |
| 268 | const SkMatrix& SkDeferredCanvas::getTotalMatrix() const |
| 269 | { |
| 270 | return drawingCanvas()->getTotalMatrix(); |
| 271 | } |
| 272 | |
| 273 | bool SkDeferredCanvas::clipRect(const SkRect& rect, |
| 274 | SkRegion::Op op, |
| 275 | bool doAntiAlias) |
| 276 | { |
| 277 | return drawingCanvas()->clipRect(rect, op, doAntiAlias); |
| 278 | } |
| 279 | |
| 280 | bool SkDeferredCanvas::clipPath(const SkPath& path, |
| 281 | SkRegion::Op op, |
| 282 | bool doAntiAlias) |
| 283 | { |
| 284 | return drawingCanvas()->clipPath(path, op, doAntiAlias); |
| 285 | } |
| 286 | |
| 287 | bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn, |
| 288 | SkRegion::Op op) |
| 289 | { |
| 290 | return drawingCanvas()->clipRegion(deviceRgn, op); |
| 291 | } |
| 292 | |
| 293 | void SkDeferredCanvas::clear(SkColor color) |
| 294 | { |
| 295 | // purge pending commands |
| 296 | if (fDeferredDrawing) { |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 297 | getDeferredDevice()->contentsCleared(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | drawingCanvas()->clear(color); |
| 301 | } |
| 302 | |
| 303 | void SkDeferredCanvas::drawPaint(const SkPaint& paint) |
| 304 | { |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 305 | if (fDeferredDrawing && isFullFrame(NULL, &paint) && isPaintOpaque(&paint)) { |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 306 | getDeferredDevice()->contentsCleared(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | drawingCanvas()->drawPaint(paint); |
| 310 | } |
| 311 | |
| 312 | void SkDeferredCanvas::drawPoints(PointMode mode, size_t count, |
| 313 | const SkPoint pts[], const SkPaint& paint) |
| 314 | { |
| 315 | drawingCanvas()->drawPoints(mode, count, pts, paint); |
| 316 | } |
| 317 | |
| 318 | void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) |
| 319 | { |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 320 | if (fDeferredDrawing && isFullFrame(&rect, &paint) && isPaintOpaque(&paint)) { |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 321 | getDeferredDevice()->contentsCleared(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | drawingCanvas()->drawRect(rect, paint); |
| 325 | } |
| 326 | |
| 327 | void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) |
| 328 | { |
| 329 | drawingCanvas()->drawPath(path, paint); |
| 330 | } |
| 331 | |
| 332 | void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left, |
| 333 | SkScalar top, const SkPaint* paint) |
| 334 | { |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 335 | SkRect bitmapRect = SkRect::MakeXYWH(left, top, |
| 336 | SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 337 | if (fDeferredDrawing && |
| 338 | isFullFrame(&bitmapRect, paint) && |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 339 | isPaintOpaque(paint, &bitmap)) { |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 340 | getDeferredDevice()->contentsCleared(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | drawingCanvas()->drawBitmap(bitmap, left, top, paint); |
| 344 | flushIfNeeded(bitmap); |
| 345 | } |
| 346 | |
| 347 | void SkDeferredCanvas::drawBitmapRect(const SkBitmap& bitmap, |
| 348 | const SkIRect* src, |
| 349 | const SkRect& dst, const SkPaint* paint) |
| 350 | { |
| 351 | if (fDeferredDrawing && |
| 352 | isFullFrame(&dst, paint) && |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 353 | isPaintOpaque(paint, &bitmap)) { |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 354 | getDeferredDevice()->contentsCleared(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | drawingCanvas()->drawBitmapRect(bitmap, src, |
| 358 | dst, paint); |
| 359 | flushIfNeeded(bitmap); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap, |
| 364 | const SkMatrix& m, |
| 365 | const SkPaint* paint) |
| 366 | { |
| 367 | // TODO: reset recording canvas if paint+bitmap is opaque and clip rect |
| 368 | // covers canvas entirely and transformed bitmap covers canvas entirely |
| 369 | drawingCanvas()->drawBitmapMatrix(bitmap, m, paint); |
| 370 | flushIfNeeded(bitmap); |
| 371 | } |
| 372 | |
| 373 | void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap, |
| 374 | const SkIRect& center, const SkRect& dst, |
| 375 | const SkPaint* paint) |
| 376 | { |
| 377 | // TODO: reset recording canvas if paint+bitmap is opaque and clip rect |
| 378 | // covers canvas entirely and dst covers canvas entirely |
| 379 | drawingCanvas()->drawBitmapNine(bitmap, center, |
| 380 | dst, paint); |
| 381 | flushIfNeeded(bitmap); |
| 382 | } |
| 383 | |
| 384 | void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top, |
| 385 | const SkPaint* paint) |
| 386 | { |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 387 | SkRect bitmapRect = SkRect::MakeXYWH( |
| 388 | SkIntToScalar(left), |
| 389 | SkIntToScalar(top), |
| 390 | SkIntToScalar(bitmap.width()), |
| 391 | SkIntToScalar(bitmap.height())); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 392 | if (fDeferredDrawing && |
| 393 | isFullFrame(&bitmapRect, paint) && |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame^] | 394 | isPaintOpaque(paint, &bitmap)) { |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 395 | getDeferredDevice()->contentsCleared(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | drawingCanvas()->drawSprite(bitmap, left, top, |
| 399 | paint); |
| 400 | flushIfNeeded(bitmap); |
| 401 | } |
| 402 | |
| 403 | void SkDeferredCanvas::drawText(const void* text, size_t byteLength, |
| 404 | SkScalar x, SkScalar y, const SkPaint& paint) |
| 405 | { |
| 406 | drawingCanvas()->drawText(text, byteLength, x, |
| 407 | y, paint); |
| 408 | } |
| 409 | |
| 410 | void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength, |
| 411 | const SkPoint pos[], const SkPaint& paint) |
| 412 | { |
| 413 | drawingCanvas()->drawPosText(text, byteLength, |
| 414 | pos, paint); |
| 415 | } |
| 416 | |
| 417 | void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength, |
| 418 | const SkScalar xpos[], SkScalar constY, |
| 419 | const SkPaint& paint) |
| 420 | { |
| 421 | drawingCanvas()->drawPosTextH(text, byteLength, |
| 422 | xpos, constY, |
| 423 | paint); |
| 424 | } |
| 425 | |
| 426 | void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength, |
| 427 | const SkPath& path, |
| 428 | const SkMatrix* matrix, |
| 429 | const SkPaint& paint) |
| 430 | { |
| 431 | drawingCanvas()->drawTextOnPath(text, byteLength, |
| 432 | path, matrix, |
| 433 | paint); |
| 434 | } |
| 435 | |
| 436 | void SkDeferredCanvas::drawPicture(SkPicture& picture) |
| 437 | { |
| 438 | drawingCanvas()->drawPicture(picture); |
| 439 | } |
| 440 | |
| 441 | void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount, |
| 442 | const SkPoint vertices[], |
| 443 | const SkPoint texs[], |
| 444 | const SkColor colors[], SkXfermode* xmode, |
| 445 | const uint16_t indices[], int indexCount, |
| 446 | const SkPaint& paint) |
| 447 | { |
| 448 | drawingCanvas()->drawVertices(vmode, vertexCount, |
| 449 | vertices, texs, |
| 450 | colors, xmode, |
| 451 | indices, indexCount, |
| 452 | paint); |
| 453 | } |
| 454 | |
| 455 | SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) |
| 456 | { |
| 457 | INHERITED::setBounder(bounder); // So non-virtual getBounder works |
| 458 | return drawingCanvas()->setBounder(bounder); |
| 459 | } |
| 460 | |
| 461 | SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) |
| 462 | { |
| 463 | INHERITED::setDrawFilter(filter); // So non-virtual getDrawFilter works |
| 464 | return drawingCanvas()->setDrawFilter(filter); |
| 465 | } |
| 466 | |
| 467 | SkCanvas* SkDeferredCanvas::canvasForDrawIter() { |
| 468 | return drawingCanvas(); |
| 469 | } |
| 470 | |
| 471 | // SkDeferredCanvas::DeferredDevice |
| 472 | //------------------------------------ |
| 473 | |
| 474 | SkDeferredCanvas::DeferredDevice::DeferredDevice( |
| 475 | SkDevice* immediateDevice, DeviceContext* deviceContext) : |
| 476 | SkDevice(SkBitmap::kNo_Config, immediateDevice->width(), |
| 477 | immediateDevice->height(), immediateDevice->isOpaque()) |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 478 | , fFreshFrame(true) |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 479 | { |
| 480 | fDeviceContext = deviceContext; |
| 481 | SkSafeRef(fDeviceContext); |
| 482 | fImmediateDevice = immediateDevice; // ref counted via fImmediateCanvas |
| 483 | fImmediateCanvas = SkNEW_ARGS(SkCanvas, (fImmediateDevice)); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 484 | fRecordingCanvas = fPicture.beginRecording(fImmediateDevice->width(), |
| 485 | fImmediateDevice->height(), |
| 486 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 487 | } |
| 488 | |
| 489 | SkDeferredCanvas::DeferredDevice::~DeferredDevice() |
| 490 | { |
| 491 | SkSafeUnref(fImmediateCanvas); |
| 492 | SkSafeUnref(fDeviceContext); |
| 493 | } |
| 494 | |
| 495 | void SkDeferredCanvas::DeferredDevice::setDeviceContext( |
| 496 | DeviceContext* deviceContext) |
| 497 | { |
| 498 | SkRefCnt_SafeAssign(fDeviceContext, deviceContext); |
| 499 | } |
| 500 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 501 | void SkDeferredCanvas::DeferredDevice::contentsCleared() |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 502 | { |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 503 | if (!fRecordingCanvas->isDrawingToLayer()) { |
| 504 | fFreshFrame = true; |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 505 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 506 | // TODO: find a way to transfer the state stack and layers |
| 507 | // to the new recording canvas. For now, purging only works |
| 508 | // with an empty stack. |
| 509 | if (fRecordingCanvas->getSaveCount() == 0) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 510 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 511 | // Save state that is trashed by the purge |
| 512 | SkDrawFilter* drawFilter = fRecordingCanvas->getDrawFilter(); |
| 513 | SkSafeRef(drawFilter); // So that it survives the purge |
| 514 | SkMatrix matrix = fRecordingCanvas->getTotalMatrix(); |
| 515 | SkRegion clipRegion = fRecordingCanvas->getTotalClip(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 516 | |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 517 | // beginRecording creates a new recording canvas and discards the |
| 518 | // old one, hence purging deferred draw ops. |
| 519 | fRecordingCanvas = fPicture.beginRecording( |
| 520 | fImmediateDevice->width(), |
| 521 | fImmediateDevice->height(), |
| 522 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 523 | |
| 524 | // Restore pre-purge state |
| 525 | if (!clipRegion.isEmpty()) { |
| 526 | fRecordingCanvas->clipRegion(clipRegion, SkRegion::kReplace_Op); |
| 527 | } |
| 528 | if (!matrix.isIdentity()) { |
| 529 | fRecordingCanvas->setMatrix(matrix); |
| 530 | } |
| 531 | if (drawFilter) { |
| 532 | fRecordingCanvas->setDrawFilter(drawFilter)->unref(); |
| 533 | } |
| 534 | } |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 535 | } |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | bool SkDeferredCanvas::DeferredDevice::isFreshFrame() { |
| 539 | bool ret = fFreshFrame; |
| 540 | fFreshFrame = false; |
| 541 | return ret; |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | void SkDeferredCanvas::DeferredDevice::flushPending() |
| 545 | { |
| 546 | if (fDeviceContext) { |
| 547 | fDeviceContext->prepareForDraw(); |
| 548 | } |
| 549 | fPicture.draw(fImmediateCanvas); |
| 550 | fRecordingCanvas = fPicture.beginRecording(fImmediateDevice->width(), |
| 551 | fImmediateDevice->height(), |
| 552 | SkPicture::kUsePathBoundsForClip_RecordingFlag); |
| 553 | } |
| 554 | |
| 555 | void SkDeferredCanvas::DeferredDevice::flush() |
| 556 | { |
| 557 | flushPending(); |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 558 | fImmediateCanvas->flush(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 559 | } |
| 560 | |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 561 | void SkDeferredCanvas::DeferredDevice::flushIfNeeded(const SkBitmap& bitmap) |
| 562 | { |
| 563 | if (bitmap.isImmutable()) { |
| 564 | return; // safe to deffer without registering a dependency |
| 565 | } |
| 566 | |
| 567 | // For now, drawing a writable bitmap triggers a flush |
| 568 | // TODO: implement read-only semantics and auto buffer duplication on write |
| 569 | // in SkBitmap/SkPixelRef, which will make deferral possible in this case. |
| 570 | flushPending(); |
| 571 | } |
| 572 | |
| 573 | uint32_t SkDeferredCanvas::DeferredDevice::getDeviceCapabilities() |
| 574 | { |
| 575 | return fImmediateDevice->getDeviceCapabilities(); |
| 576 | } |
| 577 | |
| 578 | int SkDeferredCanvas::DeferredDevice::width() const |
| 579 | { |
| 580 | return fImmediateDevice->width(); |
| 581 | } |
| 582 | |
| 583 | int SkDeferredCanvas::DeferredDevice::height() const |
| 584 | { |
| 585 | return fImmediateDevice->height(); |
| 586 | } |
| 587 | |
| 588 | SkGpuRenderTarget* SkDeferredCanvas::DeferredDevice::accessRenderTarget() |
| 589 | { |
| 590 | flushPending(); |
| 591 | return fImmediateDevice->accessRenderTarget(); |
| 592 | } |
| 593 | |
| 594 | void SkDeferredCanvas::DeferredDevice::writePixels(const SkBitmap& bitmap, |
| 595 | int x, int y, SkCanvas::Config8888 config8888) |
| 596 | { |
| 597 | if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() && |
| 598 | (y + bitmap.height()) >= height()) { |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 599 | contentsCleared(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | if (SkBitmap::kARGB_8888_Config == bitmap.config() && |
| 603 | SkCanvas::kNative_Premul_Config8888 != config8888 && |
| 604 | kPMColorAlias != config8888) { |
| 605 | //Special case config: no deferral |
| 606 | flushPending(); |
| 607 | fImmediateDevice->writePixels(bitmap, x, y, config8888); |
| 608 | } |
| 609 | |
| 610 | SkPaint paint; |
| 611 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 612 | fRecordingCanvas->drawSprite(bitmap, x, y, &paint); |
| 613 | flushIfNeeded(bitmap); |
| 614 | } |
| 615 | |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 616 | const SkBitmap& SkDeferredCanvas::DeferredDevice::onAccessBitmap(SkBitmap*) |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 617 | { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 618 | flushPending(); |
junov@chromium.org | 1f9767c | 2012-02-07 16:27:57 +0000 | [diff] [blame] | 619 | return fImmediateDevice->accessBitmap(false); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | SkDevice* SkDeferredCanvas::DeferredDevice::onCreateCompatibleDevice( |
| 623 | SkBitmap::Config config, int width, int height, bool isOpaque, Usage usage) |
| 624 | { |
| 625 | // Save layer usage not supported, and not required by SkDeferredCanvas. |
| 626 | SkASSERT(usage != kSaveLayer_Usage); |
| 627 | // Create a compatible non-deferred device. |
| 628 | SkDevice* compatibleDevice = fImmediateDevice->createCompatibleDevice(config, width, |
| 629 | height, isOpaque); |
| 630 | return SkNEW_ARGS(DeferredDevice, (compatibleDevice, fDeviceContext)); |
| 631 | } |
| 632 | |
| 633 | bool SkDeferredCanvas::DeferredDevice::onReadPixels( |
| 634 | const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) |
| 635 | { |
| 636 | flushPending(); |
| 637 | return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap), |
| 638 | x, y, config8888); |
| 639 | } |