2012-01-18 16:21:08 +00:00
|
|
|
/*
|
2012-08-14 13:36:26 +00:00
|
|
|
* Copyright 2012 Google Inc.
|
2012-01-18 16:21:08 +00:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkDeferredCanvas_DEFINED
|
|
|
|
#define SkDeferredCanvas_DEFINED
|
|
|
|
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkPixelRef.h"
|
|
|
|
|
2012-08-07 16:48:22 +00:00
|
|
|
class DeferredDevice;
|
2013-04-12 13:33:01 +00:00
|
|
|
class SkImage;
|
|
|
|
class SkSurface;
|
2012-07-18 17:54:45 +00:00
|
|
|
|
2012-01-18 16:21:08 +00:00
|
|
|
/** \class SkDeferredCanvas
|
2012-07-18 17:54:45 +00:00
|
|
|
Subclass of SkCanvas that encapsulates an SkPicture or SkGPipe for deferred
|
|
|
|
drawing. The main difference between this class and SkPictureRecord (the
|
|
|
|
canvas provided by SkPicture) is that this is a full drop-in replacement
|
|
|
|
for SkCanvas, while SkPictureRecord only supports draw operations.
|
2012-01-18 16:21:08 +00:00
|
|
|
SkDeferredCanvas will transparently trigger the flushing of deferred
|
2012-01-28 01:45:11 +00:00
|
|
|
draw operations when an attempt is made to access the pixel data.
|
2012-01-18 16:21:08 +00:00
|
|
|
*/
|
|
|
|
class SK_API SkDeferredCanvas : public SkCanvas {
|
|
|
|
public:
|
2012-08-14 13:36:26 +00:00
|
|
|
class NotificationClient;
|
2012-01-18 16:21:08 +00:00
|
|
|
|
|
|
|
SkDeferredCanvas();
|
|
|
|
|
|
|
|
/** Construct a canvas with the specified device to draw into.
|
|
|
|
Equivalent to calling default constructor, then setDevice.
|
|
|
|
@param device Specifies a device for the canvas to draw into.
|
|
|
|
*/
|
|
|
|
explicit SkDeferredCanvas(SkDevice* device);
|
|
|
|
|
2013-04-12 13:33:01 +00:00
|
|
|
/** Construct a canvas with the specified surface to draw into.
|
|
|
|
This constructor must be used for newImageSnapshot to work.
|
|
|
|
@param surface Specifies a surface for the canvas to draw into.
|
|
|
|
*/
|
|
|
|
explicit SkDeferredCanvas(SkSurface* surface);
|
|
|
|
|
2012-01-18 16:21:08 +00:00
|
|
|
virtual ~SkDeferredCanvas();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify a device to be used by this canvas. Calling setDevice will
|
2012-08-14 13:36:26 +00:00
|
|
|
* release the previously set device, if any. Takes a reference on the
|
|
|
|
* device.
|
2012-01-18 16:21:08 +00:00
|
|
|
*
|
|
|
|
* @param device The device that the canvas will raw into
|
|
|
|
* @return The device argument, for convenience.
|
|
|
|
*/
|
|
|
|
virtual SkDevice* setDevice(SkDevice* device);
|
|
|
|
|
2013-05-24 17:13:00 +00:00
|
|
|
/**
|
|
|
|
* Specify the surface to be used by this canvas. Calling setSurface will
|
|
|
|
* release the previously set surface or device. Takes a reference on the
|
|
|
|
* surface.
|
|
|
|
*
|
|
|
|
* @param surface The surface that the canvas will raw into
|
|
|
|
* @return The surface argument, for convenience.
|
|
|
|
*/
|
|
|
|
SkSurface* setSurface(SkSurface* surface);
|
|
|
|
|
2012-01-18 16:21:08 +00:00
|
|
|
/**
|
2012-08-14 13:36:26 +00:00
|
|
|
* Specify a NotificationClient to be used by this canvas. Calling
|
2012-08-23 18:09:54 +00:00
|
|
|
* setNotificationClient will release the previously set
|
2012-08-20 14:25:04 +00:00
|
|
|
* NotificationClient, if any. SkDeferredCanvas does not take ownership
|
|
|
|
* of the notification client. Therefore user code is resposible
|
|
|
|
* for its destruction. The notification client must be unregistered
|
|
|
|
* by calling setNotificationClient(NULL) if it is destroyed before
|
|
|
|
* this canvas.
|
2012-01-18 16:21:08 +00:00
|
|
|
* Note: Must be called after the device is set with setDevice.
|
|
|
|
*
|
2012-08-14 13:36:26 +00:00
|
|
|
* @param notificationClient interface for dispatching notifications
|
|
|
|
* @return The notificationClient argument, for convenience.
|
2012-01-18 16:21:08 +00:00
|
|
|
*/
|
2012-08-14 13:36:26 +00:00
|
|
|
NotificationClient* setNotificationClient(NotificationClient* notificationClient);
|
2012-01-18 16:21:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable or disable deferred drawing. When deferral is disabled,
|
|
|
|
* pending draw operations are immediately flushed and from then on,
|
|
|
|
* the SkDeferredCanvas behaves just like a regular SkCanvas.
|
|
|
|
* This method must not be called while the save/restore stack is in use.
|
|
|
|
* @param deferred true/false
|
|
|
|
*/
|
|
|
|
void setDeferredDrawing(bool deferred);
|
|
|
|
|
2012-07-25 17:27:13 +00:00
|
|
|
/**
|
|
|
|
* Returns true if deferred drawing is currenlty enabled.
|
|
|
|
*/
|
2012-08-07 16:48:22 +00:00
|
|
|
bool isDeferredDrawing() const;
|
|
|
|
|
|
|
|
/**
|
2012-08-23 18:09:54 +00:00
|
|
|
* Returns true if the canvas contains a fresh frame. A frame is
|
2012-08-07 16:48:22 +00:00
|
|
|
* considered fresh when its content do not depend on the contents
|
|
|
|
* of the previous frame. For example, if a canvas is cleared before
|
|
|
|
* drawing each frame, the frames will all be considered fresh.
|
|
|
|
* A frame is defined as the graphics image produced by as a result
|
|
|
|
* of all the canvas draws operation executed between two successive
|
|
|
|
* calls to isFreshFrame. The result of isFreshFrame is computed
|
|
|
|
* conservatively, so it may report false negatives.
|
|
|
|
*/
|
|
|
|
bool isFreshFrame() const;
|
2012-07-25 17:27:13 +00:00
|
|
|
|
2012-09-20 22:10:33 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the canvas has recorded draw commands that have
|
|
|
|
* not yet been played back.
|
|
|
|
*/
|
|
|
|
bool hasPendingCommands() const;
|
|
|
|
|
2013-04-12 13:33:01 +00:00
|
|
|
/**
|
|
|
|
* Flushes pending draw commands, if any, and returns an image of the
|
|
|
|
* current state of the surface pixels up to this point. Subsequent
|
|
|
|
* changes to the surface (by drawing into its canvas) will not be
|
|
|
|
* reflected in this image. Will return NULL if the deferred canvas
|
|
|
|
* was not constructed from an SkSurface.
|
|
|
|
*/
|
2013-04-12 20:20:50 +00:00
|
|
|
SkImage* newImageSnapshot();
|
2013-04-12 13:33:01 +00:00
|
|
|
|
2012-07-23 13:35:14 +00:00
|
|
|
/**
|
|
|
|
* Specify the maximum number of bytes to be allocated for the purpose
|
|
|
|
* of recording draw commands to this canvas. The default limit, is
|
|
|
|
* 64MB.
|
|
|
|
* @param maxStorage The maximum number of bytes to be allocated.
|
|
|
|
*/
|
|
|
|
void setMaxRecordingStorage(size_t maxStorage);
|
|
|
|
|
2012-08-07 14:26:57 +00:00
|
|
|
/**
|
|
|
|
* Returns the number of bytes currently allocated for the purpose of
|
|
|
|
* recording draw commands.
|
|
|
|
*/
|
|
|
|
size_t storageAllocatedForRecording() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to reduce the storage allocated for recording by evicting
|
|
|
|
* cache resources.
|
|
|
|
* @param bytesToFree minimum number of bytes that should be attempted to
|
|
|
|
* be freed.
|
|
|
|
* @return number of bytes actually freed.
|
|
|
|
*/
|
|
|
|
size_t freeMemoryIfPossible(size_t bytesToFree);
|
|
|
|
|
2012-11-21 15:47:04 +00:00
|
|
|
/**
|
|
|
|
* Specifies the maximum size (in bytes) allowed for a given image to be
|
|
|
|
* rendered using the deferred canvas.
|
|
|
|
*/
|
|
|
|
void setBitmapSizeThreshold(size_t sizeThreshold);
|
|
|
|
|
2012-09-20 19:35:43 +00:00
|
|
|
/**
|
|
|
|
* Executes all pending commands without drawing
|
|
|
|
*/
|
|
|
|
void silentFlush();
|
|
|
|
|
2012-01-18 16:21:08 +00:00
|
|
|
// Overrides of the SkCanvas interface
|
|
|
|
virtual int save(SaveFlags flags) SK_OVERRIDE;
|
|
|
|
virtual int saveLayer(const SkRect* bounds, const SkPaint* paint,
|
|
|
|
SaveFlags flags) SK_OVERRIDE;
|
|
|
|
virtual void restore() SK_OVERRIDE;
|
2012-02-24 21:54:07 +00:00
|
|
|
virtual bool isDrawingToLayer() const SK_OVERRIDE;
|
2012-01-18 16:21:08 +00:00
|
|
|
virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE;
|
|
|
|
virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE;
|
|
|
|
virtual bool rotate(SkScalar degrees) SK_OVERRIDE;
|
|
|
|
virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE;
|
|
|
|
virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE;
|
|
|
|
virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE;
|
|
|
|
virtual bool clipRect(const SkRect& rect, SkRegion::Op op,
|
|
|
|
bool doAntiAlias) SK_OVERRIDE;
|
2012-12-12 20:48:18 +00:00
|
|
|
virtual bool clipRRect(const SkRRect& rect, SkRegion::Op op,
|
|
|
|
bool doAntiAlias) SK_OVERRIDE;
|
2012-01-18 16:21:08 +00:00
|
|
|
virtual bool clipPath(const SkPath& path, SkRegion::Op op,
|
|
|
|
bool doAntiAlias) SK_OVERRIDE;
|
|
|
|
virtual bool clipRegion(const SkRegion& deviceRgn,
|
|
|
|
SkRegion::Op op) SK_OVERRIDE;
|
|
|
|
virtual void clear(SkColor) SK_OVERRIDE;
|
|
|
|
virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE;
|
|
|
|
virtual void drawPoints(PointMode mode, size_t count, const SkPoint pts[],
|
|
|
|
const SkPaint& paint) SK_OVERRIDE;
|
2012-12-12 20:48:18 +00:00
|
|
|
virtual void drawOval(const SkRect&, const SkPaint& paint) SK_OVERRIDE;
|
|
|
|
virtual void drawRect(const SkRect& rect, const SkPaint& paint) SK_OVERRIDE;
|
|
|
|
virtual void drawRRect(const SkRRect&, const SkPaint& paint) SK_OVERRIDE;
|
2012-01-18 16:21:08 +00:00
|
|
|
virtual void drawPath(const SkPath& path, const SkPaint& paint)
|
|
|
|
SK_OVERRIDE;
|
2012-01-28 01:45:11 +00:00
|
|
|
virtual void drawBitmap(const SkBitmap& bitmap, SkScalar left,
|
2012-01-18 16:21:08 +00:00
|
|
|
SkScalar top, const SkPaint* paint)
|
|
|
|
SK_OVERRIDE;
|
2012-09-18 15:14:33 +00:00
|
|
|
virtual void drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
|
2012-01-18 16:21:08 +00:00
|
|
|
const SkRect& dst, const SkPaint* paint)
|
|
|
|
SK_OVERRIDE;
|
|
|
|
|
|
|
|
virtual void drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
|
|
|
|
const SkPaint* paint) SK_OVERRIDE;
|
|
|
|
virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
|
|
|
|
const SkRect& dst, const SkPaint* paint)
|
|
|
|
SK_OVERRIDE;
|
|
|
|
virtual void drawSprite(const SkBitmap& bitmap, int left, int top,
|
|
|
|
const SkPaint* paint) SK_OVERRIDE;
|
|
|
|
virtual void drawText(const void* text, size_t byteLength, SkScalar x,
|
|
|
|
SkScalar y, const SkPaint& paint) SK_OVERRIDE;
|
|
|
|
virtual void drawPosText(const void* text, size_t byteLength,
|
2012-01-28 01:45:11 +00:00
|
|
|
const SkPoint pos[], const SkPaint& paint)
|
2012-01-18 16:21:08 +00:00
|
|
|
SK_OVERRIDE;
|
|
|
|
virtual void drawPosTextH(const void* text, size_t byteLength,
|
|
|
|
const SkScalar xpos[], SkScalar constY,
|
|
|
|
const SkPaint& paint) SK_OVERRIDE;
|
|
|
|
virtual void drawTextOnPath(const void* text, size_t byteLength,
|
|
|
|
const SkPath& path, const SkMatrix* matrix,
|
|
|
|
const SkPaint& paint) SK_OVERRIDE;
|
|
|
|
virtual void drawPicture(SkPicture& picture) SK_OVERRIDE;
|
|
|
|
virtual void drawVertices(VertexMode vmode, int vertexCount,
|
|
|
|
const SkPoint vertices[], const SkPoint texs[],
|
|
|
|
const SkColor colors[], SkXfermode* xmode,
|
|
|
|
const uint16_t indices[], int indexCount,
|
|
|
|
const SkPaint& paint) SK_OVERRIDE;
|
|
|
|
virtual SkBounder* setBounder(SkBounder* bounder) SK_OVERRIDE;
|
|
|
|
virtual SkDrawFilter* setDrawFilter(SkDrawFilter* filter) SK_OVERRIDE;
|
|
|
|
|
|
|
|
public:
|
2012-08-20 14:25:04 +00:00
|
|
|
class NotificationClient {
|
2012-01-18 16:21:08 +00:00
|
|
|
public:
|
2013-02-07 21:02:23 +00:00
|
|
|
virtual ~NotificationClient() {}
|
|
|
|
|
2012-08-14 13:36:26 +00:00
|
|
|
/**
|
|
|
|
* Called before executing one or several draw commands, which means
|
|
|
|
* once per flush when deferred rendering is enabled.
|
|
|
|
*/
|
2012-01-18 16:21:08 +00:00
|
|
|
virtual void prepareForDraw() {}
|
2012-08-14 13:36:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called after a recording a draw command if additional memory
|
|
|
|
* had to be allocated for recording.
|
2012-08-23 18:09:54 +00:00
|
|
|
* @param newAllocatedStorage same value as would be returned by
|
2012-08-14 13:36:26 +00:00
|
|
|
* storageAllocatedForRecording(), for convenience.
|
|
|
|
*/
|
|
|
|
virtual void storageAllocatedForRecordingChanged(
|
|
|
|
size_t newAllocatedStorage) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called after pending draw commands have been flushed
|
|
|
|
*/
|
|
|
|
virtual void flushedDrawCommands() {}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2012-10-01 15:27:14 +00:00
|
|
|
/**
|
|
|
|
* Called after pending draw commands have been skipped, meaning
|
|
|
|
* that they were optimized-out because the canvas is cleared
|
|
|
|
* or completely overwritten by the command currently being recorded.
|
|
|
|
*/
|
|
|
|
virtual void skippedPendingDrawCommands() {}
|
2012-01-18 16:21:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual SkCanvas* canvasForDrawIter();
|
2012-08-07 16:48:22 +00:00
|
|
|
DeferredDevice* getDeferredDevice() const;
|
2012-01-18 16:21:08 +00:00
|
|
|
|
|
|
|
private:
|
2012-08-14 13:36:26 +00:00
|
|
|
void recordedDrawCommand();
|
2012-02-28 15:27:59 +00:00
|
|
|
SkCanvas* drawingCanvas() const;
|
2012-08-07 16:48:22 +00:00
|
|
|
SkCanvas* immediateCanvas() const;
|
2012-01-18 16:21:08 +00:00
|
|
|
bool isFullFrame(const SkRect*, const SkPaint*) const;
|
|
|
|
void validate() const;
|
|
|
|
void init();
|
2012-02-28 15:27:59 +00:00
|
|
|
bool fDeferredDrawing;
|
2012-01-18 16:21:08 +00:00
|
|
|
|
2012-08-07 16:48:22 +00:00
|
|
|
friend class SkDeferredCanvasTester; // for unit testing
|
2012-01-18 16:21:08 +00:00
|
|
|
typedef SkCanvas INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|