junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 3 | * Copyright 2012 Google Inc. |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 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 | |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame^] | 11 | #include "SkChunkAlloc.h" |
| 12 | #include "SkColorFilter.h" |
| 13 | #include "SkDevice.h" |
| 14 | #include "SkDrawFilter.h" |
| 15 | #include "SkGPipe.h" |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 16 | #include "SkPaint.h" |
| 17 | #include "SkShader.h" |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 18 | |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 19 | SK_DEFINE_INST_COUNT(SkDeferredCanvas::DeviceContext) |
| 20 | |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 21 | enum { |
| 22 | // Deferred canvas will auto-flush when recording reaches this limit |
| 23 | kDefaultMaxRecordingStorageBytes = 64*1024*1024, |
| 24 | }; |
| 25 | |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 26 | namespace { |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 27 | bool shouldDrawImmediately(const SkBitmap* bitmap, const SkPaint* paint) { |
| 28 | if (bitmap && bitmap->getTexture() && !bitmap->isImmutable()) { |
| 29 | return true; |
| 30 | } |
| 31 | if (paint) { |
| 32 | SkShader* shader = paint->getShader(); |
| 33 | // Here we detect the case where the shader is an SkBitmapProcShader |
| 34 | // with a gpu texture attached. Checking this without RTTI |
| 35 | // requires making the assumption that only gradient shaders |
| 36 | // and SkBitmapProcShader implement asABitmap(). The following |
| 37 | // code may need to be revised if that assumption is ever broken. |
| 38 | if (shader && !shader->asAGradient(NULL)) { |
| 39 | SkBitmap bm; |
| 40 | if (shader->asABitmap(&bm, NULL, NULL) && |
| 41 | NULL != bm.getTexture()) { |
| 42 | return true; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | return false; |
junov@chromium.org | b10a6bd | 2012-07-25 17:27:13 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
| 50 | class AutoImmediateDrawIfNeeded { |
| 51 | public: |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 52 | AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap, |
| 53 | const SkPaint* paint) { |
| 54 | this->init(canvas, bitmap, paint); |
| 55 | } |
| 56 | |
| 57 | AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) { |
| 58 | this->init(canvas, NULL, paint); |
junov@chromium.org | b10a6bd | 2012-07-25 17:27:13 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | ~AutoImmediateDrawIfNeeded() { |
| 62 | if (fCanvas) { |
| 63 | fCanvas->setDeferredDrawing(true); |
| 64 | } |
| 65 | } |
| 66 | private: |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 67 | void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint) |
| 68 | { |
| 69 | if (canvas.isDeferredDrawing() && shouldDrawImmediately(bitmap, paint)) { |
| 70 | canvas.setDeferredDrawing(false); |
| 71 | fCanvas = &canvas; |
| 72 | } else { |
| 73 | fCanvas = NULL; |
| 74 | } |
| 75 | } |
| 76 | |
junov@chromium.org | b10a6bd | 2012-07-25 17:27:13 +0000 | [diff] [blame] | 77 | SkDeferredCanvas* fCanvas; |
| 78 | }; |
| 79 | |
| 80 | namespace { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 81 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 82 | bool isPaintOpaque(const SkPaint* paint, |
| 83 | const SkBitmap* bmpReplacesShader = NULL) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 84 | // TODO: SkXfermode should have a virtual isOpaque method, which would |
| 85 | // 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] | 86 | |
| 87 | if (!paint) { |
| 88 | return bmpReplacesShader ? bmpReplacesShader->isOpaque() : true; |
| 89 | } |
| 90 | |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 91 | SkXfermode::Coeff srcCoeff, dstCoeff; |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame] | 92 | if (SkXfermode::AsCoeff(paint->getXfermode(), &srcCoeff, &dstCoeff)){ |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 93 | switch (dstCoeff) { |
| 94 | case SkXfermode::kZero_Coeff: |
| 95 | return true; |
| 96 | case SkXfermode::kISA_Coeff: |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame] | 97 | if (paint->getAlpha() != 255) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 98 | break; |
| 99 | } |
| 100 | if (bmpReplacesShader) { |
| 101 | if (!bmpReplacesShader->isOpaque()) { |
| 102 | break; |
| 103 | } |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame] | 104 | } else if (paint->getShader() && !paint->getShader()->isOpaque()) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 105 | break; |
| 106 | } |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 107 | if (paint->getColorFilter() && |
| 108 | ((paint->getColorFilter()->getFlags() & |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 109 | SkColorFilter::kAlphaUnchanged_Flag) == 0)) { |
| 110 | break; |
| 111 | } |
| 112 | return true; |
| 113 | case SkXfermode::kSA_Coeff: |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame] | 114 | if (paint->getAlpha() != 0) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 115 | break; |
| 116 | } |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 117 | if (paint->getColorFilter() && |
| 118 | ((paint->getColorFilter()->getFlags() & |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 119 | SkColorFilter::kAlphaUnchanged_Flag) == 0)) { |
| 120 | break; |
| 121 | } |
| 122 | return true; |
| 123 | case SkXfermode::kSC_Coeff: |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame] | 124 | if (paint->getColor() != 0) { // all components must be 0 |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 125 | break; |
| 126 | } |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame] | 127 | if (bmpReplacesShader || paint->getShader()) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 128 | break; |
| 129 | } |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 130 | if (paint->getColorFilter() && ( |
| 131 | (paint->getColorFilter()->getFlags() & |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 132 | SkColorFilter::kAlphaUnchanged_Flag) == 0)) { |
| 133 | break; |
| 134 | } |
| 135 | return true; |
| 136 | default: |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | } // unnamed namespace |
| 144 | |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame^] | 145 | //----------------------------------------------------------------------------- |
| 146 | // DeferredPipeController |
| 147 | //----------------------------------------------------------------------------- |
| 148 | |
| 149 | class DeferredPipeController : public SkGPipeController { |
| 150 | public: |
| 151 | DeferredPipeController(); |
| 152 | void setPlaybackCanvas(SkCanvas*); |
| 153 | virtual ~DeferredPipeController(); |
| 154 | virtual void* requestBlock(size_t minRequest, size_t* actual) SK_OVERRIDE; |
| 155 | virtual void notifyWritten(size_t bytes) SK_OVERRIDE; |
| 156 | void playback(); |
| 157 | void reset(); |
| 158 | bool hasRecorded() const { return fAllocator.blockCount() != 0; } |
| 159 | size_t storageAllocatedForRecording() const { return fAllocator.totalCapacity(); } |
| 160 | private: |
| 161 | enum { |
| 162 | kMinBlockSize = 4096 |
| 163 | }; |
| 164 | struct PipeBlock { |
| 165 | PipeBlock(void* block, size_t size) { fBlock = block, fSize = size; } |
| 166 | void* fBlock; |
| 167 | size_t fSize; |
| 168 | }; |
| 169 | void* fBlock; |
| 170 | size_t fBytesWritten; |
| 171 | SkChunkAlloc fAllocator; |
| 172 | SkTDArray<PipeBlock> fBlockList; |
| 173 | SkGPipeReader fReader; |
| 174 | }; |
| 175 | |
| 176 | DeferredPipeController::DeferredPipeController() : |
| 177 | fAllocator(kMinBlockSize) { |
| 178 | fBlock = NULL; |
| 179 | fBytesWritten = 0; |
| 180 | } |
| 181 | |
| 182 | DeferredPipeController::~DeferredPipeController() { |
| 183 | fAllocator.reset(); |
| 184 | } |
| 185 | |
| 186 | void DeferredPipeController::setPlaybackCanvas(SkCanvas* canvas) { |
| 187 | fReader.setCanvas(canvas); |
| 188 | } |
| 189 | |
| 190 | void* DeferredPipeController::requestBlock(size_t minRequest, size_t *actual) { |
| 191 | if (fBlock) { |
| 192 | // Save the previous block for later |
| 193 | PipeBlock previousBloc(fBlock, fBytesWritten); |
| 194 | fBlockList.push(previousBloc); |
| 195 | } |
| 196 | int32_t blockSize = SkMax32(minRequest, kMinBlockSize); |
| 197 | fBlock = fAllocator.allocThrow(blockSize); |
| 198 | fBytesWritten = 0; |
| 199 | *actual = blockSize; |
| 200 | return fBlock; |
| 201 | } |
| 202 | |
| 203 | void DeferredPipeController::notifyWritten(size_t bytes) { |
| 204 | fBytesWritten += bytes; |
| 205 | } |
| 206 | |
| 207 | void DeferredPipeController::playback() { |
| 208 | |
| 209 | for (int currentBlock = 0; currentBlock < fBlockList.count(); currentBlock++ ) { |
| 210 | fReader.playback(fBlockList[currentBlock].fBlock, fBlockList[currentBlock].fSize); |
| 211 | } |
| 212 | fBlockList.reset(); |
| 213 | |
| 214 | if (fBlock) { |
| 215 | fReader.playback(fBlock, fBytesWritten); |
| 216 | fBlock = NULL; |
| 217 | } |
| 218 | |
| 219 | // Release all allocated blocks |
| 220 | fAllocator.reset(); |
| 221 | } |
| 222 | |
| 223 | void DeferredPipeController::reset() { |
| 224 | fBlockList.reset(); |
| 225 | fBlock = NULL; |
| 226 | fAllocator.reset(); |
| 227 | } |
| 228 | |
| 229 | //----------------------------------------------------------------------------- |
| 230 | // DeferredDevice |
| 231 | //----------------------------------------------------------------------------- |
| 232 | |
| 233 | class DeferredDevice : public SkDevice { |
| 234 | public: |
| 235 | DeferredDevice(SkDevice* immediateDevice, |
| 236 | SkDeferredCanvas::DeviceContext* deviceContext = NULL); |
| 237 | ~DeferredDevice(); |
| 238 | |
| 239 | void setDeviceContext(SkDeferredCanvas::DeviceContext* deviceContext); |
| 240 | SkCanvas* recordingCanvas(); |
| 241 | SkCanvas* immediateCanvas() const {return fImmediateCanvas;} |
| 242 | SkDevice* immediateDevice() const {return fImmediateDevice;} |
| 243 | bool isFreshFrame(); |
| 244 | size_t storageAllocatedForRecording() const; |
| 245 | size_t freeMemoryIfPossible(size_t bytesToFree); |
| 246 | void flushPending(); |
| 247 | void contentsCleared(); |
| 248 | void setMaxRecordingStorage(size_t); |
| 249 | |
| 250 | virtual uint32_t getDeviceCapabilities() SK_OVERRIDE; |
| 251 | virtual int width() const SK_OVERRIDE; |
| 252 | virtual int height() const SK_OVERRIDE; |
| 253 | virtual SkGpuRenderTarget* accessRenderTarget() SK_OVERRIDE; |
| 254 | |
| 255 | virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config, |
| 256 | int width, int height, |
| 257 | bool isOpaque, |
| 258 | Usage usage) SK_OVERRIDE; |
| 259 | |
| 260 | virtual void writePixels(const SkBitmap& bitmap, int x, int y, |
| 261 | SkCanvas::Config8888 config8888) SK_OVERRIDE; |
| 262 | |
| 263 | protected: |
| 264 | virtual const SkBitmap& onAccessBitmap(SkBitmap*) SK_OVERRIDE; |
| 265 | virtual bool onReadPixels(const SkBitmap& bitmap, |
| 266 | int x, int y, |
| 267 | SkCanvas::Config8888 config8888) SK_OVERRIDE; |
| 268 | |
| 269 | // The following methods are no-ops on a deferred device |
| 270 | virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) |
| 271 | SK_OVERRIDE |
| 272 | {return false;} |
| 273 | virtual void setMatrixClip(const SkMatrix&, const SkRegion&, |
| 274 | const SkClipStack&) SK_OVERRIDE |
| 275 | {} |
| 276 | |
| 277 | // None of the following drawing methods should ever get called on the |
| 278 | // deferred device |
| 279 | virtual void clear(SkColor color) |
| 280 | {SkASSERT(0);} |
| 281 | virtual void drawPaint(const SkDraw&, const SkPaint& paint) |
| 282 | {SkASSERT(0);} |
| 283 | virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, |
| 284 | size_t count, const SkPoint[], |
| 285 | const SkPaint& paint) |
| 286 | {SkASSERT(0);} |
| 287 | virtual void drawRect(const SkDraw&, const SkRect& r, |
| 288 | const SkPaint& paint) |
| 289 | {SkASSERT(0);} |
| 290 | virtual void drawPath(const SkDraw&, const SkPath& path, |
| 291 | const SkPaint& paint, |
| 292 | const SkMatrix* prePathMatrix = NULL, |
| 293 | bool pathIsMutable = false) |
| 294 | {SkASSERT(0);} |
| 295 | virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
| 296 | const SkIRect* srcRectOrNull, |
| 297 | const SkMatrix& matrix, const SkPaint& paint) |
| 298 | {SkASSERT(0);} |
| 299 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, |
| 300 | int x, int y, const SkPaint& paint) |
| 301 | {SkASSERT(0);} |
| 302 | virtual void drawText(const SkDraw&, const void* text, size_t len, |
| 303 | SkScalar x, SkScalar y, const SkPaint& paint) |
| 304 | {SkASSERT(0);} |
| 305 | virtual void drawPosText(const SkDraw&, const void* text, size_t len, |
| 306 | const SkScalar pos[], SkScalar constY, |
| 307 | int scalarsPerPos, const SkPaint& paint) |
| 308 | {SkASSERT(0);} |
| 309 | virtual void drawTextOnPath(const SkDraw&, const void* text, |
| 310 | size_t len, const SkPath& path, |
| 311 | const SkMatrix* matrix, |
| 312 | const SkPaint& paint) |
| 313 | {SkASSERT(0);} |
| 314 | virtual void drawPosTextOnPath(const SkDraw& draw, const void* text, |
| 315 | size_t len, const SkPoint pos[], |
| 316 | const SkPaint& paint, |
| 317 | const SkPath& path, |
| 318 | const SkMatrix* matrix) |
| 319 | {SkASSERT(0);} |
| 320 | virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, |
| 321 | int vertexCount, const SkPoint verts[], |
| 322 | const SkPoint texs[], const SkColor colors[], |
| 323 | SkXfermode* xmode, const uint16_t indices[], |
| 324 | int indexCount, const SkPaint& paint) |
| 325 | {SkASSERT(0);} |
| 326 | virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y, |
| 327 | const SkPaint&) |
| 328 | {SkASSERT(0);} |
| 329 | private: |
| 330 | virtual void flush(); |
| 331 | |
| 332 | void endRecording(); |
| 333 | void beginRecording(); |
| 334 | |
| 335 | DeferredPipeController fPipeController; |
| 336 | SkGPipeWriter fPipeWriter; |
| 337 | SkDevice* fImmediateDevice; |
| 338 | SkCanvas* fImmediateCanvas; |
| 339 | SkCanvas* fRecordingCanvas; |
| 340 | SkDeferredCanvas::DeviceContext* fDeviceContext; |
| 341 | bool fFreshFrame; |
| 342 | size_t fMaxRecordingStorageBytes; |
| 343 | }; |
| 344 | |
| 345 | DeferredDevice::DeferredDevice( |
| 346 | SkDevice* immediateDevice, SkDeferredCanvas::DeviceContext* deviceContext) : |
| 347 | SkDevice(SkBitmap::kNo_Config, immediateDevice->width(), |
| 348 | immediateDevice->height(), immediateDevice->isOpaque()) |
| 349 | , fFreshFrame(true) { |
| 350 | |
| 351 | fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes; |
| 352 | fDeviceContext = deviceContext; |
| 353 | SkSafeRef(fDeviceContext); |
| 354 | fImmediateDevice = immediateDevice; // ref counted via fImmediateCanvas |
| 355 | fImmediateCanvas = SkNEW_ARGS(SkCanvas, (fImmediateDevice)); |
| 356 | fPipeController.setPlaybackCanvas(fImmediateCanvas); |
| 357 | this->beginRecording(); |
| 358 | } |
| 359 | |
| 360 | DeferredDevice::~DeferredDevice() { |
| 361 | this->flushPending(); |
| 362 | SkSafeUnref(fImmediateCanvas); |
| 363 | SkSafeUnref(fDeviceContext); |
| 364 | } |
| 365 | |
| 366 | void DeferredDevice::setMaxRecordingStorage(size_t maxStorage) { |
| 367 | fMaxRecordingStorageBytes = maxStorage; |
| 368 | this->recordingCanvas(); // Accessing the recording canvas applies the new limit. |
| 369 | } |
| 370 | |
| 371 | void DeferredDevice::endRecording() { |
| 372 | fPipeWriter.endRecording(); |
| 373 | fPipeController.reset(); |
| 374 | fRecordingCanvas = NULL; |
| 375 | } |
| 376 | |
| 377 | void DeferredDevice::beginRecording() { |
| 378 | fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0); |
| 379 | } |
| 380 | |
| 381 | void DeferredDevice::setDeviceContext( |
| 382 | SkDeferredCanvas::DeviceContext* deviceContext) { |
| 383 | SkRefCnt_SafeAssign(fDeviceContext, deviceContext); |
| 384 | } |
| 385 | |
| 386 | void DeferredDevice::contentsCleared() { |
| 387 | if (!fRecordingCanvas->isDrawingToLayer()) { |
| 388 | fFreshFrame = true; |
| 389 | |
| 390 | // TODO: find a way to transfer the state stack and layers |
| 391 | // to the new recording canvas. For now, purging only works |
| 392 | // with an empty stack. |
| 393 | if (fRecordingCanvas->getSaveCount() == 0) { |
| 394 | |
| 395 | // Save state that is trashed by the purge |
| 396 | SkDrawFilter* drawFilter = fRecordingCanvas->getDrawFilter(); |
| 397 | SkSafeRef(drawFilter); // So that it survives the purge |
| 398 | SkMatrix matrix = fRecordingCanvas->getTotalMatrix(); |
| 399 | SkRegion clipRegion = fRecordingCanvas->getTotalClip(); |
| 400 | |
| 401 | // beginRecording creates a new recording canvas and discards the |
| 402 | // old one, hence purging deferred draw ops. |
| 403 | this->endRecording(); |
| 404 | this->beginRecording(); |
| 405 | |
| 406 | // Restore pre-purge state |
| 407 | if (!clipRegion.isEmpty()) { |
| 408 | fRecordingCanvas->clipRegion(clipRegion, |
| 409 | SkRegion::kReplace_Op); |
| 410 | } |
| 411 | if (!matrix.isIdentity()) { |
| 412 | fRecordingCanvas->setMatrix(matrix); |
| 413 | } |
| 414 | if (drawFilter) { |
| 415 | fRecordingCanvas->setDrawFilter(drawFilter)->unref(); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | bool DeferredDevice::isFreshFrame() { |
| 422 | bool ret = fFreshFrame; |
| 423 | fFreshFrame = false; |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | void DeferredDevice::flushPending() { |
| 428 | if (!fPipeController.hasRecorded()) { |
| 429 | return; |
| 430 | } |
| 431 | if (fDeviceContext) { |
| 432 | fDeviceContext->prepareForDraw(); |
| 433 | } |
| 434 | |
| 435 | fPipeWriter.flushRecording(true); |
| 436 | fPipeController.playback(); |
| 437 | } |
| 438 | |
| 439 | void DeferredDevice::flush() { |
| 440 | this->flushPending(); |
| 441 | fImmediateCanvas->flush(); |
| 442 | } |
| 443 | |
| 444 | size_t DeferredDevice::freeMemoryIfPossible(size_t bytesToFree) { |
| 445 | return fPipeWriter.freeMemoryIfPossible(bytesToFree); |
| 446 | } |
| 447 | |
| 448 | size_t DeferredDevice::storageAllocatedForRecording() const { |
| 449 | return (fPipeController.storageAllocatedForRecording() |
| 450 | + fPipeWriter.storageAllocatedForRecording()); |
| 451 | } |
| 452 | |
| 453 | SkCanvas* DeferredDevice::recordingCanvas() { |
| 454 | size_t storageAllocated = this->storageAllocatedForRecording(); |
| 455 | if (storageAllocated > fMaxRecordingStorageBytes) { |
| 456 | // First, attempt to reduce cache without flushing |
| 457 | size_t tryFree = storageAllocated - fMaxRecordingStorageBytes; |
| 458 | if (this->freeMemoryIfPossible(tryFree) < tryFree) { |
| 459 | // Flush is necessary to free more space. |
| 460 | this->flushPending(); |
| 461 | // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes |
| 462 | // which could cause a high flushing frequency. |
| 463 | this->freeMemoryIfPossible(~0); |
| 464 | } |
| 465 | } |
| 466 | return fRecordingCanvas; |
| 467 | } |
| 468 | |
| 469 | uint32_t DeferredDevice::getDeviceCapabilities() { |
| 470 | return fImmediateDevice->getDeviceCapabilities(); |
| 471 | } |
| 472 | |
| 473 | int DeferredDevice::width() const { |
| 474 | return fImmediateDevice->width(); |
| 475 | } |
| 476 | |
| 477 | int DeferredDevice::height() const { |
| 478 | return fImmediateDevice->height(); |
| 479 | } |
| 480 | |
| 481 | SkGpuRenderTarget* DeferredDevice::accessRenderTarget() { |
| 482 | this->flushPending(); |
| 483 | return fImmediateDevice->accessRenderTarget(); |
| 484 | } |
| 485 | |
| 486 | void DeferredDevice::writePixels(const SkBitmap& bitmap, |
| 487 | int x, int y, SkCanvas::Config8888 config8888) { |
| 488 | |
| 489 | if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() && |
| 490 | (y + bitmap.height()) >= height()) { |
| 491 | this->contentsCleared(); |
| 492 | } |
| 493 | |
| 494 | if (SkBitmap::kARGB_8888_Config == bitmap.config() && |
| 495 | SkCanvas::kNative_Premul_Config8888 != config8888 && |
| 496 | kPMColorAlias != config8888) { |
| 497 | //Special case config: no deferral |
| 498 | this->flushPending(); |
| 499 | fImmediateDevice->writePixels(bitmap, x, y, config8888); |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | SkPaint paint; |
| 504 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 505 | if (shouldDrawImmediately(&bitmap, NULL)) { |
| 506 | this->flushPending(); |
| 507 | fImmediateCanvas->drawSprite(bitmap, x, y, &paint); |
| 508 | } else { |
| 509 | this->recordingCanvas()->drawSprite(bitmap, x, y, &paint); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | const SkBitmap& DeferredDevice::onAccessBitmap(SkBitmap*) { |
| 514 | this->flushPending(); |
| 515 | return fImmediateDevice->accessBitmap(false); |
| 516 | } |
| 517 | |
| 518 | SkDevice* DeferredDevice::onCreateCompatibleDevice( |
| 519 | SkBitmap::Config config, int width, int height, bool isOpaque, |
| 520 | Usage usage) { |
| 521 | |
| 522 | // Save layer usage not supported, and not required by SkDeferredCanvas. |
| 523 | SkASSERT(usage != kSaveLayer_Usage); |
| 524 | // Create a compatible non-deferred device. |
| 525 | SkAutoTUnref<SkDevice> compatibleDevice |
| 526 | (fImmediateDevice->createCompatibleDevice(config, width, height, |
| 527 | isOpaque)); |
| 528 | return SkNEW_ARGS(DeferredDevice, (compatibleDevice, fDeviceContext)); |
| 529 | } |
| 530 | |
| 531 | bool DeferredDevice::onReadPixels( |
| 532 | const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) { |
| 533 | this->flushPending(); |
| 534 | return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap), |
| 535 | x, y, config8888); |
| 536 | } |
| 537 | |
| 538 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 539 | SkDeferredCanvas::SkDeferredCanvas() { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 540 | this->init(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 541 | } |
| 542 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 543 | SkDeferredCanvas::SkDeferredCanvas(SkDevice* device) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 544 | this->init(); |
| 545 | this->setDevice(device); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | SkDeferredCanvas::SkDeferredCanvas(SkDevice* device, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 549 | DeviceContext* deviceContext) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 550 | this->init(); |
| 551 | this->setDevice(device); |
| 552 | this->setDeviceContext(deviceContext); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 553 | } |
| 554 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 555 | void SkDeferredCanvas::init() { |
junov@chromium.org | 5e5a095 | 2012-02-28 15:27:59 +0000 | [diff] [blame] | 556 | fDeferredDrawing = true; // On by default |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 557 | } |
| 558 | |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 559 | void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 560 | this->validate(); |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 561 | this->getDeferredDevice()->setMaxRecordingStorage(maxStorage); |
| 562 | } |
| 563 | |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 564 | size_t SkDeferredCanvas::storageAllocatedForRecording() const { |
| 565 | return this->getDeferredDevice()->storageAllocatedForRecording(); |
| 566 | } |
| 567 | |
| 568 | size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) { |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 569 | return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree); |
junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 570 | } |
| 571 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 572 | void SkDeferredCanvas::validate() const { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 573 | SkASSERT(this->getDevice()); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 574 | } |
| 575 | |
junov@chromium.org | 5e5a095 | 2012-02-28 15:27:59 +0000 | [diff] [blame] | 576 | SkCanvas* SkDeferredCanvas::drawingCanvas() const { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 577 | this->validate(); |
| 578 | return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() : |
| 579 | this->getDeferredDevice()->immediateCanvas(); |
junov@chromium.org | 5e5a095 | 2012-02-28 15:27:59 +0000 | [diff] [blame] | 580 | } |
| 581 | |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame^] | 582 | SkCanvas* SkDeferredCanvas::immediateCanvas() const { |
| 583 | this->validate(); |
| 584 | return this->getDeferredDevice()->immediateCanvas(); |
| 585 | } |
| 586 | |
| 587 | DeferredDevice* SkDeferredCanvas::getDeferredDevice() const { |
| 588 | return static_cast<DeferredDevice*>(this->getDevice()); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 589 | } |
| 590 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 591 | void SkDeferredCanvas::setDeferredDrawing(bool val) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 592 | this->validate(); // Must set device before calling this method |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 593 | if (val != fDeferredDrawing) { |
| 594 | if (fDeferredDrawing) { |
junov@chromium.org | 5e5a095 | 2012-02-28 15:27:59 +0000 | [diff] [blame] | 595 | // Going live. |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 596 | this->getDeferredDevice()->flushPending(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 597 | } |
| 598 | fDeferredDrawing = val; |
| 599 | } |
| 600 | } |
| 601 | |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame^] | 602 | bool SkDeferredCanvas::isDeferredDrawing() const { |
junov@chromium.org | b10a6bd | 2012-07-25 17:27:13 +0000 | [diff] [blame] | 603 | return fDeferredDrawing; |
| 604 | } |
| 605 | |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame^] | 606 | bool SkDeferredCanvas::isFreshFrame() const { |
| 607 | return this->getDeferredDevice()->isFreshFrame(); |
| 608 | } |
| 609 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 610 | SkDeferredCanvas::~SkDeferredCanvas() { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 611 | } |
| 612 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 613 | SkDevice* SkDeferredCanvas::setDevice(SkDevice* device) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 614 | this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (device)))->unref(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 615 | return device; |
| 616 | } |
| 617 | |
| 618 | SkDeferredCanvas::DeviceContext* SkDeferredCanvas::setDeviceContext( |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 619 | DeviceContext* deviceContext) { |
| 620 | |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 621 | DeferredDevice* deferredDevice = this->getDeferredDevice(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 622 | SkASSERT(deferredDevice); |
| 623 | if (deferredDevice) { |
| 624 | deferredDevice->setDeviceContext(deviceContext); |
| 625 | } |
| 626 | return deviceContext; |
| 627 | } |
| 628 | |
| 629 | bool SkDeferredCanvas::isFullFrame(const SkRect* rect, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 630 | const SkPaint* paint) const { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 631 | SkCanvas* canvas = this->drawingCanvas(); |
| 632 | SkISize canvasSize = this->getDeviceSize(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 633 | if (rect) { |
| 634 | if (!canvas->getTotalMatrix().rectStaysRect()) { |
| 635 | return false; // conservative |
| 636 | } |
| 637 | |
| 638 | SkRect transformedRect; |
| 639 | canvas->getTotalMatrix().mapRect(&transformedRect, *rect); |
| 640 | |
| 641 | if (paint) { |
| 642 | SkPaint::Style paintStyle = paint->getStyle(); |
| 643 | if (!(paintStyle == SkPaint::kFill_Style || |
| 644 | paintStyle == SkPaint::kStrokeAndFill_Style)) { |
| 645 | return false; |
| 646 | } |
| 647 | if (paint->getMaskFilter() || paint->getLooper() |
| 648 | || paint->getPathEffect() || paint->getImageFilter()) { |
| 649 | return false; // conservative |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | // The following test holds with AA enabled, and is conservative |
| 654 | // by a 0.5 pixel margin with AA disabled |
junov@chromium.org | b1e218e | 2012-02-13 22:27:58 +0000 | [diff] [blame] | 655 | if (transformedRect.fLeft > SkIntToScalar(0) || |
| 656 | transformedRect.fTop > SkIntToScalar(0) || |
| 657 | transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) || |
| 658 | transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 659 | return false; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | switch (canvas->getClipType()) { |
| 664 | case SkCanvas::kRect_ClipType : |
| 665 | { |
| 666 | SkIRect bounds; |
| 667 | canvas->getClipDeviceBounds(&bounds); |
| 668 | if (bounds.fLeft > 0 || bounds.fTop > 0 || |
| 669 | bounds.fRight < canvasSize.fWidth || |
| 670 | bounds.fBottom < canvasSize.fHeight) |
| 671 | return false; |
| 672 | } |
| 673 | break; |
| 674 | case SkCanvas::kComplex_ClipType : |
| 675 | return false; // conservative |
| 676 | case SkCanvas::kEmpty_ClipType: |
| 677 | default: |
| 678 | break; |
| 679 | }; |
| 680 | |
| 681 | return true; |
| 682 | } |
| 683 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 684 | int SkDeferredCanvas::save(SaveFlags flags) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 685 | this->drawingCanvas()->save(flags); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 686 | return this->INHERITED::save(flags); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 690 | SaveFlags flags) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 691 | this->drawingCanvas()->saveLayer(bounds, paint, flags); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 692 | int count = this->INHERITED::save(flags); |
| 693 | this->clipRectBounds(bounds, flags, NULL); |
| 694 | return count; |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 695 | } |
| 696 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 697 | void SkDeferredCanvas::restore() { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 698 | this->drawingCanvas()->restore(); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 699 | this->INHERITED::restore(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 700 | } |
| 701 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 702 | bool SkDeferredCanvas::isDrawingToLayer() const { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 703 | return this->drawingCanvas()->isDrawingToLayer(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 704 | } |
| 705 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 706 | bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 707 | this->drawingCanvas()->translate(dx, dy); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 708 | return this->INHERITED::translate(dx, dy); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 709 | } |
| 710 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 711 | bool SkDeferredCanvas::scale(SkScalar sx, SkScalar sy) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 712 | this->drawingCanvas()->scale(sx, sy); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 713 | return this->INHERITED::scale(sx, sy); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 714 | } |
| 715 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 716 | bool SkDeferredCanvas::rotate(SkScalar degrees) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 717 | this->drawingCanvas()->rotate(degrees); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 718 | return this->INHERITED::rotate(degrees); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 719 | } |
| 720 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 721 | bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 722 | this->drawingCanvas()->skew(sx, sy); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 723 | return this->INHERITED::skew(sx, sy); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 724 | } |
| 725 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 726 | bool SkDeferredCanvas::concat(const SkMatrix& matrix) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 727 | this->drawingCanvas()->concat(matrix); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 728 | return this->INHERITED::concat(matrix); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 729 | } |
| 730 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 731 | void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 732 | this->drawingCanvas()->setMatrix(matrix); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 733 | this->INHERITED::setMatrix(matrix); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | bool SkDeferredCanvas::clipRect(const SkRect& rect, |
| 737 | SkRegion::Op op, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 738 | bool doAntiAlias) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 739 | this->drawingCanvas()->clipRect(rect, op, doAntiAlias); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 740 | return this->INHERITED::clipRect(rect, op, doAntiAlias); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | bool SkDeferredCanvas::clipPath(const SkPath& path, |
| 744 | SkRegion::Op op, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 745 | bool doAntiAlias) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 746 | this->drawingCanvas()->clipPath(path, op, doAntiAlias); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 747 | return this->INHERITED::clipPath(path, op, doAntiAlias); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 751 | SkRegion::Op op) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 752 | this->drawingCanvas()->clipRegion(deviceRgn, op); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 753 | return this->INHERITED::clipRegion(deviceRgn, op); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 754 | } |
| 755 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 756 | void SkDeferredCanvas::clear(SkColor color) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 757 | // purge pending commands |
| 758 | if (fDeferredDrawing) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 759 | this->getDeferredDevice()->contentsCleared(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 760 | } |
| 761 | |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 762 | this->drawingCanvas()->clear(color); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 763 | } |
| 764 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 765 | void SkDeferredCanvas::drawPaint(const SkPaint& paint) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 766 | if (fDeferredDrawing && this->isFullFrame(NULL, &paint) && |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 767 | isPaintOpaque(&paint)) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 768 | this->getDeferredDevice()->contentsCleared(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 769 | } |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 770 | AutoImmediateDrawIfNeeded autoDraw(*this, &paint); |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 771 | this->drawingCanvas()->drawPaint(paint); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | void SkDeferredCanvas::drawPoints(PointMode mode, size_t count, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 775 | const SkPoint pts[], const SkPaint& paint) { |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 776 | AutoImmediateDrawIfNeeded autoDraw(*this, &paint); |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 777 | this->drawingCanvas()->drawPoints(mode, count, pts, paint); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 778 | } |
| 779 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 780 | void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 781 | if (fDeferredDrawing && this->isFullFrame(&rect, &paint) && |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 782 | isPaintOpaque(&paint)) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 783 | this->getDeferredDevice()->contentsCleared(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 784 | } |
| 785 | |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 786 | AutoImmediateDrawIfNeeded autoDraw(*this, &paint); |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 787 | this->drawingCanvas()->drawRect(rect, paint); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 788 | } |
| 789 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 790 | void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 791 | AutoImmediateDrawIfNeeded autoDraw(*this, &paint); |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 792 | this->drawingCanvas()->drawPath(path, paint); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 796 | SkScalar top, const SkPaint* paint) { |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 797 | SkRect bitmapRect = SkRect::MakeXYWH(left, top, |
| 798 | SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 799 | if (fDeferredDrawing && |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 800 | this->isFullFrame(&bitmapRect, paint) && |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame] | 801 | isPaintOpaque(paint, &bitmap)) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 802 | this->getDeferredDevice()->contentsCleared(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 803 | } |
| 804 | |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 805 | AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint); |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 806 | this->drawingCanvas()->drawBitmap(bitmap, left, top, paint); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | void SkDeferredCanvas::drawBitmapRect(const SkBitmap& bitmap, |
| 810 | const SkIRect* src, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 811 | const SkRect& dst, |
| 812 | const SkPaint* paint) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 813 | if (fDeferredDrawing && |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 814 | this->isFullFrame(&dst, paint) && |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame] | 815 | isPaintOpaque(paint, &bitmap)) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 816 | this->getDeferredDevice()->contentsCleared(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 817 | } |
| 818 | |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 819 | AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint); |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 820 | this->drawingCanvas()->drawBitmapRect(bitmap, src, dst, paint); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | |
| 824 | void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap, |
| 825 | const SkMatrix& m, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 826 | const SkPaint* paint) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 827 | // TODO: reset recording canvas if paint+bitmap is opaque and clip rect |
| 828 | // covers canvas entirely and transformed bitmap covers canvas entirely |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 829 | AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint); |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 830 | this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap, |
| 834 | const SkIRect& center, const SkRect& dst, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 835 | const SkPaint* paint) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 836 | // TODO: reset recording canvas if paint+bitmap is opaque and clip rect |
| 837 | // covers canvas entirely and dst covers canvas entirely |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 838 | AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint); |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 839 | this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 843 | const SkPaint* paint) { |
junov@chromium.org | 8f9ecbd | 2012-02-13 21:53:45 +0000 | [diff] [blame] | 844 | SkRect bitmapRect = SkRect::MakeXYWH( |
| 845 | SkIntToScalar(left), |
| 846 | SkIntToScalar(top), |
| 847 | SkIntToScalar(bitmap.width()), |
| 848 | SkIntToScalar(bitmap.height())); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 849 | if (fDeferredDrawing && |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 850 | this->isFullFrame(&bitmapRect, paint) && |
junov@chromium.org | 87f982c | 2012-02-23 21:34:34 +0000 | [diff] [blame] | 851 | isPaintOpaque(paint, &bitmap)) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 852 | this->getDeferredDevice()->contentsCleared(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 853 | } |
| 854 | |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 855 | AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint); |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 856 | this->drawingCanvas()->drawSprite(bitmap, left, top, paint); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | void SkDeferredCanvas::drawText(const void* text, size_t byteLength, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 860 | SkScalar x, SkScalar y, const SkPaint& paint) { |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 861 | AutoImmediateDrawIfNeeded autoDraw(*this, &paint); |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 862 | this->drawingCanvas()->drawText(text, byteLength, x, y, paint); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 866 | const SkPoint pos[], const SkPaint& paint) { |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 867 | AutoImmediateDrawIfNeeded autoDraw(*this, &paint); |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 868 | this->drawingCanvas()->drawPosText(text, byteLength, pos, paint); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength, |
| 872 | const SkScalar xpos[], SkScalar constY, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 873 | const SkPaint& paint) { |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 874 | AutoImmediateDrawIfNeeded autoDraw(*this, &paint); |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 875 | this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength, |
| 879 | const SkPath& path, |
| 880 | const SkMatrix* matrix, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 881 | const SkPaint& paint) { |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 882 | AutoImmediateDrawIfNeeded autoDraw(*this, &paint); |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 883 | this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 884 | } |
| 885 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 886 | void SkDeferredCanvas::drawPicture(SkPicture& picture) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 887 | this->drawingCanvas()->drawPicture(picture); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount, |
| 891 | const SkPoint vertices[], |
| 892 | const SkPoint texs[], |
| 893 | const SkColor colors[], SkXfermode* xmode, |
| 894 | const uint16_t indices[], int indexCount, |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 895 | const SkPaint& paint) { |
junov@chromium.org | 10f7f97 | 2012-07-31 21:01:51 +0000 | [diff] [blame] | 896 | AutoImmediateDrawIfNeeded autoDraw(*this, &paint); |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 897 | this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode, |
| 898 | indices, indexCount, paint); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 899 | } |
| 900 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 901 | SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 902 | this->drawingCanvas()->setBounder(bounder); |
| 903 | return this->INHERITED::setBounder(bounder); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 904 | } |
| 905 | |
junov@chromium.org | c16ca92 | 2012-02-24 22:06:27 +0000 | [diff] [blame] | 906 | SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 907 | this->drawingCanvas()->setDrawFilter(filter); |
| 908 | return this->INHERITED::setDrawFilter(filter); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | SkCanvas* SkDeferredCanvas::canvasForDrawIter() { |
junov@chromium.org | 9060c9b | 2012-08-07 15:14:01 +0000 | [diff] [blame] | 912 | return this->drawingCanvas(); |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 913 | } |