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