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