| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 1 | /* |
| junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 2 | * Copyright 2012 Google Inc. |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkDeferredCanvas_DEFINED |
| 9 | #define SkDeferredCanvas_DEFINED |
| 10 | |
| 11 | #include "SkCanvas.h" |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 12 | #include "SkPixelRef.h" |
| 13 | |
| junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 14 | class DeferredDevice; |
| junov@chromium.org | 77eec24 | 2012-07-18 17:54:45 +0000 | [diff] [blame] | 15 | |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 16 | /** \class SkDeferredCanvas |
| junov@chromium.org | 77eec24 | 2012-07-18 17:54:45 +0000 | [diff] [blame] | 17 | Subclass of SkCanvas that encapsulates an SkPicture or SkGPipe for deferred |
| 18 | drawing. The main difference between this class and SkPictureRecord (the |
| 19 | canvas provided by SkPicture) is that this is a full drop-in replacement |
| 20 | for SkCanvas, while SkPictureRecord only supports draw operations. |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 21 | SkDeferredCanvas will transparently trigger the flushing of deferred |
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 22 | draw operations when an attempt is made to access the pixel data. |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 23 | */ |
| 24 | class SK_API SkDeferredCanvas : public SkCanvas { |
| 25 | public: |
| junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 26 | class NotificationClient; |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 27 | |
| 28 | SkDeferredCanvas(); |
| 29 | |
| 30 | /** Construct a canvas with the specified device to draw into. |
| 31 | Equivalent to calling default constructor, then setDevice. |
| 32 | @param device Specifies a device for the canvas to draw into. |
| 33 | */ |
| 34 | explicit SkDeferredCanvas(SkDevice* device); |
| 35 | |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 36 | virtual ~SkDeferredCanvas(); |
| 37 | |
| 38 | /** |
| 39 | * Specify a device to be used by this canvas. Calling setDevice will |
| junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 40 | * release the previously set device, if any. Takes a reference on the |
| 41 | * device. |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 42 | * |
| 43 | * @param device The device that the canvas will raw into |
| 44 | * @return The device argument, for convenience. |
| 45 | */ |
| 46 | virtual SkDevice* setDevice(SkDevice* device); |
| 47 | |
| 48 | /** |
| junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 49 | * Specify a NotificationClient to be used by this canvas. Calling |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 50 | * setNotificationClient will release the previously set |
| junov@chromium.org | 5280548 | 2012-08-20 14:25:04 +0000 | [diff] [blame] | 51 | * NotificationClient, if any. SkDeferredCanvas does not take ownership |
| 52 | * of the notification client. Therefore user code is resposible |
| 53 | * for its destruction. The notification client must be unregistered |
| 54 | * by calling setNotificationClient(NULL) if it is destroyed before |
| 55 | * this canvas. |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 56 | * Note: Must be called after the device is set with setDevice. |
| 57 | * |
| junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 58 | * @param notificationClient interface for dispatching notifications |
| 59 | * @return The notificationClient argument, for convenience. |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 60 | */ |
| junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 61 | NotificationClient* setNotificationClient(NotificationClient* notificationClient); |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * Enable or disable deferred drawing. When deferral is disabled, |
| 65 | * pending draw operations are immediately flushed and from then on, |
| 66 | * the SkDeferredCanvas behaves just like a regular SkCanvas. |
| 67 | * This method must not be called while the save/restore stack is in use. |
| 68 | * @param deferred true/false |
| 69 | */ |
| 70 | void setDeferredDrawing(bool deferred); |
| 71 | |
| junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 72 | /** |
| junov@chromium.org | b10a6bd | 2012-07-25 17:27:13 +0000 | [diff] [blame] | 73 | * Returns true if deferred drawing is currenlty enabled. |
| 74 | */ |
| junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 75 | bool isDeferredDrawing() const; |
| 76 | |
| 77 | /** |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 78 | * Returns true if the canvas contains a fresh frame. A frame is |
| junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 79 | * considered fresh when its content do not depend on the contents |
| 80 | * of the previous frame. For example, if a canvas is cleared before |
| 81 | * drawing each frame, the frames will all be considered fresh. |
| 82 | * A frame is defined as the graphics image produced by as a result |
| 83 | * of all the canvas draws operation executed between two successive |
| 84 | * calls to isFreshFrame. The result of isFreshFrame is computed |
| 85 | * conservatively, so it may report false negatives. |
| 86 | */ |
| 87 | bool isFreshFrame() const; |
| junov@chromium.org | b10a6bd | 2012-07-25 17:27:13 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
| junov@chromium.org | a38dfb6 | 2012-09-20 22:10:33 +0000 | [diff] [blame^] | 90 | * Returns true if the canvas has recorded draw commands that have |
| 91 | * not yet been played back. |
| 92 | */ |
| 93 | bool hasPendingCommands() const; |
| 94 | |
| 95 | /** |
| junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 96 | * Specify the maximum number of bytes to be allocated for the purpose |
| 97 | * of recording draw commands to this canvas. The default limit, is |
| 98 | * 64MB. |
| 99 | * @param maxStorage The maximum number of bytes to be allocated. |
| 100 | */ |
| 101 | void setMaxRecordingStorage(size_t maxStorage); |
| 102 | |
| junov@chromium.org | 2e14ba8 | 2012-08-07 14:26:57 +0000 | [diff] [blame] | 103 | /** |
| 104 | * Returns the number of bytes currently allocated for the purpose of |
| 105 | * recording draw commands. |
| 106 | */ |
| 107 | size_t storageAllocatedForRecording() const; |
| 108 | |
| 109 | /** |
| 110 | * Attempt to reduce the storage allocated for recording by evicting |
| 111 | * cache resources. |
| 112 | * @param bytesToFree minimum number of bytes that should be attempted to |
| 113 | * be freed. |
| 114 | * @return number of bytes actually freed. |
| 115 | */ |
| 116 | size_t freeMemoryIfPossible(size_t bytesToFree); |
| 117 | |
| junov@chromium.org | fb10389 | 2012-09-20 19:35:43 +0000 | [diff] [blame] | 118 | /** |
| 119 | * Executes all pending commands without drawing |
| 120 | */ |
| 121 | void silentFlush(); |
| 122 | |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 123 | // Overrides of the SkCanvas interface |
| 124 | virtual int save(SaveFlags flags) SK_OVERRIDE; |
| 125 | virtual int saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 126 | SaveFlags flags) SK_OVERRIDE; |
| 127 | virtual void restore() SK_OVERRIDE; |
| junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 128 | virtual bool isDrawingToLayer() const SK_OVERRIDE; |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 129 | virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE; |
| 130 | virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE; |
| 131 | virtual bool rotate(SkScalar degrees) SK_OVERRIDE; |
| 132 | virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE; |
| 133 | virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE; |
| 134 | virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE; |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 135 | virtual bool clipRect(const SkRect& rect, SkRegion::Op op, |
| 136 | bool doAntiAlias) SK_OVERRIDE; |
| 137 | virtual bool clipPath(const SkPath& path, SkRegion::Op op, |
| 138 | bool doAntiAlias) SK_OVERRIDE; |
| 139 | virtual bool clipRegion(const SkRegion& deviceRgn, |
| 140 | SkRegion::Op op) SK_OVERRIDE; |
| 141 | virtual void clear(SkColor) SK_OVERRIDE; |
| 142 | virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE; |
| 143 | virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| 144 | const SkPaint& paint) SK_OVERRIDE; |
| 145 | virtual void drawRect(const SkRect& rect, const SkPaint& paint) |
| 146 | SK_OVERRIDE; |
| 147 | virtual void drawPath(const SkPath& path, const SkPaint& paint) |
| 148 | SK_OVERRIDE; |
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 149 | virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 150 | SkScalar top, const SkPaint* paint) |
| 151 | SK_OVERRIDE; |
| reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 152 | virtual void drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 153 | const SkRect& dst, const SkPaint* paint) |
| 154 | SK_OVERRIDE; |
| 155 | |
| 156 | virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m, |
| 157 | const SkPaint* paint) SK_OVERRIDE; |
| 158 | virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 159 | const SkRect& dst, const SkPaint* paint) |
| 160 | SK_OVERRIDE; |
| 161 | virtual void drawSprite(const SkBitmap& bitmap, int left, int top, |
| 162 | const SkPaint* paint) SK_OVERRIDE; |
| 163 | virtual void drawText(const void* text, size_t byteLength, SkScalar x, |
| 164 | SkScalar y, const SkPaint& paint) SK_OVERRIDE; |
| 165 | virtual void drawPosText(const void* text, size_t byteLength, |
| vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 166 | const SkPoint pos[], const SkPaint& paint) |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 167 | SK_OVERRIDE; |
| 168 | virtual void drawPosTextH(const void* text, size_t byteLength, |
| 169 | const SkScalar xpos[], SkScalar constY, |
| 170 | const SkPaint& paint) SK_OVERRIDE; |
| 171 | virtual void drawTextOnPath(const void* text, size_t byteLength, |
| 172 | const SkPath& path, const SkMatrix* matrix, |
| 173 | const SkPaint& paint) SK_OVERRIDE; |
| 174 | virtual void drawPicture(SkPicture& picture) SK_OVERRIDE; |
| 175 | virtual void drawVertices(VertexMode vmode, int vertexCount, |
| 176 | const SkPoint vertices[], const SkPoint texs[], |
| 177 | const SkColor colors[], SkXfermode* xmode, |
| 178 | const uint16_t indices[], int indexCount, |
| 179 | const SkPaint& paint) SK_OVERRIDE; |
| 180 | virtual SkBounder* setBounder(SkBounder* bounder) SK_OVERRIDE; |
| 181 | virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter) SK_OVERRIDE; |
| 182 | |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 183 | public: |
| junov@chromium.org | 5280548 | 2012-08-20 14:25:04 +0000 | [diff] [blame] | 184 | class NotificationClient { |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 185 | public: |
| junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 186 | /** |
| 187 | * Called before executing one or several draw commands, which means |
| 188 | * once per flush when deferred rendering is enabled. |
| 189 | */ |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 190 | virtual void prepareForDraw() {} |
| junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * Called after a recording a draw command if additional memory |
| 194 | * had to be allocated for recording. |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 195 | * @param newAllocatedStorage same value as would be returned by |
| junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 196 | * storageAllocatedForRecording(), for convenience. |
| 197 | */ |
| 198 | virtual void storageAllocatedForRecordingChanged( |
| 199 | size_t newAllocatedStorage) {} |
| 200 | |
| 201 | /** |
| 202 | * Called after pending draw commands have been flushed |
| 203 | */ |
| 204 | virtual void flushedDrawCommands() {} |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 205 | |
| reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 206 | private: |
| 207 | typedef SkRefCnt INHERITED; |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| junov@chromium.org | 77eec24 | 2012-07-18 17:54:45 +0000 | [diff] [blame] | 210 | protected: |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 211 | virtual SkCanvas* canvasForDrawIter(); |
| junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 212 | DeferredDevice* getDeferredDevice() const; |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 213 | |
| 214 | private: |
| junov@chromium.org | 9ed02b9 | 2012-08-14 13:36:26 +0000 | [diff] [blame] | 215 | void recordedDrawCommand(); |
| junov@chromium.org | 5e5a095 | 2012-02-28 15:27:59 +0000 | [diff] [blame] | 216 | SkCanvas* drawingCanvas() const; |
| junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 217 | SkCanvas* immediateCanvas() const; |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 218 | bool isFullFrame(const SkRect*, const SkPaint*) const; |
| 219 | void validate() const; |
| 220 | void init(); |
| junov@chromium.org | 5e5a095 | 2012-02-28 15:27:59 +0000 | [diff] [blame] | 221 | bool fDeferredDrawing; |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 222 | |
| junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame] | 223 | friend class SkDeferredCanvasTester; // for unit testing |
| junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 224 | typedef SkCanvas INHERITED; |
| 225 | }; |
| 226 | |
| 227 | |
| 228 | #endif |