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