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