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