junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 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: |
| 26 | class DeviceContext; |
| 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 | |
| 36 | /** Construct a canvas with the specified device to draw into, and |
| 37 | * a device context. Equivalent to calling default constructor, then |
| 38 | * setDevice. |
| 39 | * @param device Specifies a device for the canvas to draw into. |
| 40 | * @param deviceContext interface for the device's the graphics context |
| 41 | */ |
| 42 | explicit SkDeferredCanvas(SkDevice* device, DeviceContext* deviceContext); |
| 43 | |
| 44 | virtual ~SkDeferredCanvas(); |
| 45 | |
| 46 | /** |
| 47 | * Specify a device to be used by this canvas. Calling setDevice will |
| 48 | * release the previously set device, if any. |
| 49 | * |
| 50 | * @param device The device that the canvas will raw into |
| 51 | * @return The device argument, for convenience. |
| 52 | */ |
| 53 | virtual SkDevice* setDevice(SkDevice* device); |
| 54 | |
| 55 | /** |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 56 | * Specify a deviceContext to be used by this canvas. Calling |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 57 | * setDeviceContext will release the previously set deviceContext, if any. |
| 58 | * A deviceContext must be specified if the device uses a graphics context |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 59 | * that requires some form of state initialization prior to drawing |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 60 | * and/or explicit flushing to synchronize the execution of rendering |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 61 | * operations. |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 62 | * Note: Must be called after the device is set with setDevice. |
| 63 | * |
| 64 | * @deviceContext interface for the device's the graphics context |
| 65 | * @return The deviceContext argument, for convenience. |
| 66 | */ |
| 67 | DeviceContext* setDeviceContext(DeviceContext* deviceContext); |
| 68 | |
| 69 | /** |
| 70 | * Enable or disable deferred drawing. When deferral is disabled, |
| 71 | * pending draw operations are immediately flushed and from then on, |
| 72 | * the SkDeferredCanvas behaves just like a regular SkCanvas. |
| 73 | * This method must not be called while the save/restore stack is in use. |
| 74 | * @param deferred true/false |
| 75 | */ |
| 76 | void setDeferredDrawing(bool deferred); |
| 77 | |
junov@chromium.org | bfeddae | 2012-07-23 13:35:14 +0000 | [diff] [blame] | 78 | /** |
junov@chromium.org | b10a6bd | 2012-07-25 17:27:13 +0000 | [diff] [blame] | 79 | * Returns true if deferred drawing is currenlty enabled. |
| 80 | */ |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame^] | 81 | bool isDeferredDrawing() const; |
| 82 | |
| 83 | /** |
| 84 | * Returns true if the canvas contains a fresh frame. A frame is |
| 85 | * considered fresh when its content do not depend on the contents |
| 86 | * of the previous frame. For example, if a canvas is cleared before |
| 87 | * drawing each frame, the frames will all be considered fresh. |
| 88 | * A frame is defined as the graphics image produced by as a result |
| 89 | * of all the canvas draws operation executed between two successive |
| 90 | * calls to isFreshFrame. The result of isFreshFrame is computed |
| 91 | * conservatively, so it may report false negatives. |
| 92 | */ |
| 93 | bool isFreshFrame() const; |
junov@chromium.org | b10a6bd | 2012-07-25 17:27:13 +0000 | [diff] [blame] | 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@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 118 | // Overrides of the SkCanvas interface |
| 119 | virtual int save(SaveFlags flags) SK_OVERRIDE; |
| 120 | virtual int saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 121 | SaveFlags flags) SK_OVERRIDE; |
| 122 | virtual void restore() SK_OVERRIDE; |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 123 | virtual bool isDrawingToLayer() const SK_OVERRIDE; |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 124 | virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE; |
| 125 | virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE; |
| 126 | virtual bool rotate(SkScalar degrees) SK_OVERRIDE; |
| 127 | virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE; |
| 128 | virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE; |
| 129 | virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE; |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 130 | virtual bool clipRect(const SkRect& rect, SkRegion::Op op, |
| 131 | bool doAntiAlias) SK_OVERRIDE; |
| 132 | virtual bool clipPath(const SkPath& path, SkRegion::Op op, |
| 133 | bool doAntiAlias) SK_OVERRIDE; |
| 134 | virtual bool clipRegion(const SkRegion& deviceRgn, |
| 135 | SkRegion::Op op) SK_OVERRIDE; |
| 136 | virtual void clear(SkColor) SK_OVERRIDE; |
| 137 | virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE; |
| 138 | virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| 139 | const SkPaint& paint) SK_OVERRIDE; |
| 140 | virtual void drawRect(const SkRect& rect, const SkPaint& paint) |
| 141 | SK_OVERRIDE; |
| 142 | virtual void drawPath(const SkPath& path, const SkPaint& paint) |
| 143 | SK_OVERRIDE; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 144 | virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left, |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 145 | SkScalar top, const SkPaint* paint) |
| 146 | SK_OVERRIDE; |
| 147 | virtual void drawBitmapRect(const SkBitmap& bitmap, const SkIRect* src, |
| 148 | const SkRect& dst, const SkPaint* paint) |
| 149 | SK_OVERRIDE; |
| 150 | |
| 151 | virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m, |
| 152 | const SkPaint* paint) SK_OVERRIDE; |
| 153 | virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 154 | const SkRect& dst, const SkPaint* paint) |
| 155 | SK_OVERRIDE; |
| 156 | virtual void drawSprite(const SkBitmap& bitmap, int left, int top, |
| 157 | const SkPaint* paint) SK_OVERRIDE; |
| 158 | virtual void drawText(const void* text, size_t byteLength, SkScalar x, |
| 159 | SkScalar y, const SkPaint& paint) SK_OVERRIDE; |
| 160 | virtual void drawPosText(const void* text, size_t byteLength, |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 161 | const SkPoint pos[], const SkPaint& paint) |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 162 | SK_OVERRIDE; |
| 163 | virtual void drawPosTextH(const void* text, size_t byteLength, |
| 164 | const SkScalar xpos[], SkScalar constY, |
| 165 | const SkPaint& paint) SK_OVERRIDE; |
| 166 | virtual void drawTextOnPath(const void* text, size_t byteLength, |
| 167 | const SkPath& path, const SkMatrix* matrix, |
| 168 | const SkPaint& paint) SK_OVERRIDE; |
| 169 | virtual void drawPicture(SkPicture& picture) SK_OVERRIDE; |
| 170 | virtual void drawVertices(VertexMode vmode, int vertexCount, |
| 171 | const SkPoint vertices[], const SkPoint texs[], |
| 172 | const SkColor colors[], SkXfermode* xmode, |
| 173 | const uint16_t indices[], int indexCount, |
| 174 | const SkPaint& paint) SK_OVERRIDE; |
| 175 | virtual SkBounder* setBounder(SkBounder* bounder) SK_OVERRIDE; |
| 176 | virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter) SK_OVERRIDE; |
| 177 | |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 178 | public: |
| 179 | class DeviceContext : public SkRefCnt { |
| 180 | public: |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 181 | SK_DECLARE_INST_COUNT(DeviceContext) |
| 182 | |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 183 | virtual void prepareForDraw() {} |
reed@google.com | 563a3b4 | 2012-06-26 19:24:50 +0000 | [diff] [blame] | 184 | |
| 185 | private: |
| 186 | typedef SkRefCnt INHERITED; |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
junov@chromium.org | 77eec24 | 2012-07-18 17:54:45 +0000 | [diff] [blame] | 189 | protected: |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 190 | virtual SkCanvas* canvasForDrawIter(); |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame^] | 191 | DeferredDevice* getDeferredDevice() const; |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 192 | |
| 193 | private: |
junov@chromium.org | 5e5a095 | 2012-02-28 15:27:59 +0000 | [diff] [blame] | 194 | SkCanvas* drawingCanvas() const; |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame^] | 195 | SkCanvas* immediateCanvas() const; |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 196 | bool isFullFrame(const SkRect*, const SkPaint*) const; |
| 197 | void validate() const; |
| 198 | void init(); |
junov@chromium.org | 5e5a095 | 2012-02-28 15:27:59 +0000 | [diff] [blame] | 199 | bool fDeferredDrawing; |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 200 | |
junov@chromium.org | 88e2914 | 2012-08-07 16:48:22 +0000 | [diff] [blame^] | 201 | friend class SkDeferredCanvasTester; // for unit testing |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 202 | typedef SkCanvas INHERITED; |
| 203 | }; |
| 204 | |
| 205 | |
| 206 | #endif |