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