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