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