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