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