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