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"
|
|
|
|
|
2014-03-12 18:28:35 +00:00
|
|
|
class SkDeferredDevice;
|
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:
|
2013-11-25 15:44:37 +00:00
|
|
|
class SK_API NotificationClient;
|
2012-01-18 16:21:08 +00:00
|
|
|
|
2013-05-28 17:39:08 +00:00
|
|
|
/** Construct a canvas with the specified surface to draw into.
|
|
|
|
This factory must be used for newImageSnapshot to work.
|
|
|
|
@param surface Specifies a surface for the canvas to draw into.
|
|
|
|
*/
|
|
|
|
static SkDeferredCanvas* Create(SkSurface* surface);
|
|
|
|
|
2014-02-04 15:34:10 +00:00
|
|
|
// static SkDeferredCanvas* Create(SkBaseDevice* device);
|
2013-04-12 13:33:01 +00:00
|
|
|
|
2012-01-18 16:21:08 +00:00
|
|
|
virtual ~SkDeferredCanvas();
|
|
|
|
|
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);
|
2014-06-30 16:05:34 +00:00
|
|
|
size_t getBitmapSizeThreshold() const { return fBitmapSizeThreshold; }
|
2012-11-21 15:47:04 +00:00
|
|
|
|
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
|
2012-02-24 21:54:07 +00:00
|
|
|
virtual bool isDrawingToLayer() const SK_OVERRIDE;
|
2012-01-18 16:21:08 +00:00
|
|
|
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;
|
2013-10-22 16:54:15 +00:00
|
|
|
virtual void drawRect(const SkRect& rect, const SkPaint& paint) SK_OVERRIDE;
|
2012-12-12 20:48:18 +00:00
|
|
|
virtual void drawRRect(const SkRRect&, const SkPaint& paint) SK_OVERRIDE;
|
2013-10-22 16:54:15 +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,
|
2013-08-16 10:24:37 +00:00
|
|
|
const SkRect& dst, const SkPaint* paint,
|
|
|
|
DrawBitmapRectFlags flags) SK_OVERRIDE;
|
2012-01-18 16:21:08 +00:00
|
|
|
|
|
|
|
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 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 SkDrawFilter* setDrawFilter(SkDrawFilter* filter) SK_OVERRIDE;
|
|
|
|
|
2014-02-21 12:20:45 +00:00
|
|
|
protected:
|
2014-06-30 14:13:28 +00:00
|
|
|
virtual void willSave() SK_OVERRIDE;
|
2014-03-12 20:21:48 +00:00
|
|
|
virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE;
|
|
|
|
virtual void willRestore() SK_OVERRIDE;
|
|
|
|
|
2014-03-13 20:03:58 +00:00
|
|
|
virtual void didConcat(const SkMatrix&) SK_OVERRIDE;
|
|
|
|
virtual void didSetMatrix(const SkMatrix&) SK_OVERRIDE;
|
|
|
|
|
2014-04-23 04:00:17 +00:00
|
|
|
virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE;
|
|
|
|
virtual void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
|
|
|
|
const SkPaint&) SK_OVERRIDE;
|
|
|
|
virtual void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
|
|
|
|
const SkPaint&) SK_OVERRIDE;
|
|
|
|
virtual void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
|
|
|
|
SkScalar constY, const SkPaint&) SK_OVERRIDE;
|
|
|
|
virtual void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
|
|
|
|
const SkMatrix* matrix, const SkPaint&) SK_OVERRIDE;
|
2014-02-21 12:20:45 +00:00
|
|
|
|
2014-02-28 18:19:39 +00:00
|
|
|
virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
|
|
|
|
virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
|
|
|
|
virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE;
|
|
|
|
virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE;
|
|
|
|
|
2014-06-04 12:40:44 +00:00
|
|
|
virtual void onDrawPicture(const SkPicture* picture) SK_OVERRIDE;
|
|
|
|
|
2012-01-18 16:21:08 +00:00
|
|
|
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();
|
2014-03-12 18:28:35 +00:00
|
|
|
SkDeferredDevice* getDeferredDevice() const;
|
2012-01-18 16:21:08 +00:00
|
|
|
|
|
|
|
private:
|
2014-03-12 18:28:35 +00:00
|
|
|
SkDeferredCanvas(SkDeferredDevice*);
|
2013-05-28 17:39:08 +00:00
|
|
|
|
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();
|
2014-06-30 16:05:34 +00:00
|
|
|
|
|
|
|
size_t fBitmapSizeThreshold;
|
|
|
|
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
|