2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2007 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SkPicture_DEFINED
|
|
|
|
#define SkPicture_DEFINED
|
|
|
|
|
2013-02-22 21:38:35 +00:00
|
|
|
#include "SkBitmap.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkRefCnt.h"
|
|
|
|
|
2012-11-01 17:10:32 +00:00
|
|
|
class SkBBoxHierarchy;
|
2008-12-17 15:59:43 +00:00
|
|
|
class SkCanvas;
|
2013-05-20 17:02:41 +00:00
|
|
|
class SkDrawPictureCallback;
|
2008-12-17 15:59:43 +00:00
|
|
|
class SkPicturePlayback;
|
|
|
|
class SkPictureRecord;
|
|
|
|
class SkStream;
|
|
|
|
class SkWStream;
|
|
|
|
|
|
|
|
/** \class SkPicture
|
|
|
|
|
|
|
|
The SkPicture class records the drawing commands made to a canvas, to
|
|
|
|
be played back at a later time.
|
|
|
|
*/
|
2011-04-25 20:02:38 +00:00
|
|
|
class SK_API SkPicture : public SkRefCnt {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2012-06-21 20:25:03 +00:00
|
|
|
SK_DECLARE_INST_COUNT(SkPicture)
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** The constructor prepares the picture to record.
|
|
|
|
@param width the width of the virtual device the picture records.
|
|
|
|
@param height the height of the virtual device the picture records.
|
|
|
|
*/
|
|
|
|
SkPicture();
|
|
|
|
/** Make a copy of the contents of src. If src records more drawing after
|
|
|
|
this call, those elements will not appear in this picture.
|
|
|
|
*/
|
|
|
|
SkPicture(const SkPicture& src);
|
2013-02-22 21:38:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Recreate a picture that was serialized into a stream.
|
|
|
|
* On failure, silently creates an empty picture.
|
|
|
|
* @param SkStream Serialized picture data.
|
|
|
|
*/
|
|
|
|
explicit SkPicture(SkStream*);
|
|
|
|
|
2012-06-25 14:36:28 +00:00
|
|
|
/**
|
2013-02-22 21:38:35 +00:00
|
|
|
* Function signature defining a function that sets up an SkBitmap from encoded data. On
|
|
|
|
* success, the SkBitmap should have its Config, width, height, rowBytes and pixelref set.
|
|
|
|
* If the installed pixelref has decoded the data into pixels, then the src buffer need not be
|
|
|
|
* copied. If the pixelref defers the actual decode until its lockPixels() is called, then it
|
|
|
|
* must make a copy of the src buffer.
|
|
|
|
* @param src Encoded data.
|
|
|
|
* @param length Size of the encoded data, in bytes.
|
|
|
|
* @param dst SkBitmap to install the pixel ref on.
|
|
|
|
* @param bool Whether or not a pixel ref was successfully installed.
|
2012-06-25 14:36:28 +00:00
|
|
|
*/
|
2013-02-22 21:38:35 +00:00
|
|
|
typedef bool (*InstallPixelRefProc)(const void* src, size_t length, SkBitmap* dst);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recreate a picture that was serialized into a stream.
|
|
|
|
* @param SkStream Serialized picture data.
|
|
|
|
* @param success Output parameter. If non-NULL, will be set to true if the picture was
|
|
|
|
* deserialized successfully and false otherwise.
|
|
|
|
* @param proc Function pointer for installing pixelrefs on SkBitmaps representing the
|
|
|
|
* encoded bitmap data from the stream.
|
|
|
|
*/
|
|
|
|
SkPicture(SkStream*, bool* success, InstallPixelRefProc proc);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual ~SkPicture();
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/**
|
|
|
|
* Swap the contents of the two pictures. Guaranteed to succeed.
|
|
|
|
*/
|
|
|
|
void swap(SkPicture& other);
|
2012-08-30 14:38:00 +00:00
|
|
|
|
2012-08-29 18:52:07 +00:00
|
|
|
/**
|
|
|
|
* Creates a thread-safe clone of the picture that is ready for playback.
|
|
|
|
*/
|
|
|
|
SkPicture* clone() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates multiple thread-safe clones of this picture that are ready for
|
|
|
|
* playback. The resulting clones are stored in the provided array of
|
|
|
|
* SkPictures.
|
|
|
|
*/
|
|
|
|
void clone(SkPicture* pictures, int count) const;
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2009-02-13 14:56:09 +00:00
|
|
|
enum RecordingFlags {
|
|
|
|
/* This flag specifies that when clipPath() is called, the path will
|
|
|
|
be faithfully recorded, but the recording canvas' current clip will
|
|
|
|
only see the path's bounds. This speeds up the recording process
|
|
|
|
without compromising the fidelity of the playback. The only side-
|
|
|
|
effect for recording is that calling getTotalClip() or related
|
|
|
|
clip-query calls will reflect the path's bounds, not the actual
|
|
|
|
path.
|
|
|
|
*/
|
2012-09-13 21:41:51 +00:00
|
|
|
kUsePathBoundsForClip_RecordingFlag = 0x01,
|
|
|
|
/* This flag causes the picture to compute bounding boxes and build
|
|
|
|
up a spatial hierarchy (currently an R-Tree), plus a tree of Canvas'
|
|
|
|
usually stack-based clip/etc state. This requires an increase in
|
|
|
|
recording time (often ~2x; likely more for very complex pictures),
|
|
|
|
but allows us to perform much faster culling at playback time, and
|
|
|
|
completely avoid some unnecessary clips and other operations. This
|
|
|
|
is ideal for tiled rendering, or any other situation where you're
|
2012-09-14 02:01:10 +00:00
|
|
|
drawing a fraction of a large scene into a smaller viewport.
|
2012-09-13 21:41:51 +00:00
|
|
|
|
|
|
|
In most cases the record cost is offset by the playback improvement
|
|
|
|
after a frame or two of tiled rendering (and complex pictures that
|
|
|
|
induce the worst record times will generally get the largest
|
|
|
|
speedups at playback time).
|
|
|
|
|
|
|
|
Note: Currently this is not serializable, the bounding data will be
|
|
|
|
discarded if you serialize into a stream and then deserialize.
|
|
|
|
*/
|
2013-04-12 15:13:35 +00:00
|
|
|
kOptimizeForClippedPlayback_RecordingFlag = 0x02,
|
|
|
|
/*
|
|
|
|
This flag disables all the picture recording optimizations (i.e.,
|
2013-04-13 07:01:15 +00:00
|
|
|
those in SkPictureRecord). It is mainly intended for testing the
|
2013-04-12 15:13:35 +00:00
|
|
|
existing optimizations (i.e., to actually have the pattern
|
|
|
|
appear in an .skp we have to disable the optimization). This
|
|
|
|
option doesn't affect the optimizations controlled by
|
|
|
|
'kOptimizeForClippedPlayback_RecordingFlag'.
|
|
|
|
*/
|
|
|
|
kDisableRecordOptimizations_RecordingFlag = 0x04
|
2009-02-13 14:56:09 +00:00
|
|
|
};
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns the canvas that records the drawing commands.
|
2009-02-13 14:56:09 +00:00
|
|
|
@param width the base width for the picture, as if the recording
|
|
|
|
canvas' bitmap had this width.
|
|
|
|
@param height the base width for the picture, as if the recording
|
|
|
|
canvas' bitmap had this height.
|
|
|
|
@param recordFlags optional flags that control recording.
|
2008-12-17 15:59:43 +00:00
|
|
|
@return the picture canvas.
|
|
|
|
*/
|
2009-02-13 14:56:09 +00:00
|
|
|
SkCanvas* beginRecording(int width, int height, uint32_t recordFlags = 0);
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Returns the recording canvas if one is active, or NULL if recording is
|
|
|
|
not active. This does not alter the refcnt on the canvas (if present).
|
|
|
|
*/
|
|
|
|
SkCanvas* getRecordingCanvas() const;
|
|
|
|
/** Signal that the caller is done recording. This invalidates the canvas
|
|
|
|
returned by beginRecording/getRecordingCanvas, and prepares the picture
|
|
|
|
for drawing. Note: this happens implicitly the first time the picture
|
|
|
|
is drawn.
|
|
|
|
*/
|
|
|
|
void endRecording();
|
2012-06-01 21:23:07 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Replays the drawing commands on the specified canvas. This internally
|
|
|
|
calls endRecording() if that has not already been called.
|
2013-05-20 17:02:41 +00:00
|
|
|
@param canvas the canvas receiving the drawing commands.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
2013-05-20 17:02:41 +00:00
|
|
|
void draw(SkCanvas* canvas, SkDrawPictureCallback* = NULL);
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the width of the picture's recording canvas. This
|
|
|
|
value reflects what was passed to setSize(), and does not necessarily
|
|
|
|
reflect the bounds of what has been recorded into the picture.
|
|
|
|
@return the width of the picture's recording canvas
|
|
|
|
*/
|
|
|
|
int width() const { return fWidth; }
|
|
|
|
|
|
|
|
/** Return the height of the picture's recording canvas. This
|
|
|
|
value reflects what was passed to setSize(), and does not necessarily
|
|
|
|
reflect the bounds of what has been recorded into the picture.
|
|
|
|
@return the height of the picture's recording canvas
|
|
|
|
*/
|
|
|
|
int height() const { return fHeight; }
|
|
|
|
|
2013-02-22 22:04:19 +00:00
|
|
|
/**
|
|
|
|
* Function to encode an SkBitmap to an SkWStream. A function with this
|
|
|
|
* signature can be passed to serialize() and SkOrderedWriteBuffer. The
|
|
|
|
* function should return true if it succeeds. Otherwise it should return
|
|
|
|
* false so that SkOrderedWriteBuffer can switch to another method of
|
|
|
|
* storing SkBitmaps.
|
|
|
|
*/
|
|
|
|
typedef bool (*EncodeBitmap)(SkWStream*, const SkBitmap&);
|
|
|
|
|
Add the ability to provide function pointers to SkPicture serialization
and deserialization for encoding and decoding bitmaps.
Remove kForceFlattenBitmapPixels_Flag, which is no longer used.
When an SkOrderedReadBuffer needs to read a bitmap, if it does not
have an image decoder, use a dummy bitmap.
In GM, add a tolerance option for color differences, used when
testing picture serialization, so it can assume two images are the
same even though PNG encoding/decoding may have resulted in small
differences.
Create dummy implementations for SkImageDecoder and SkImageEncoder
functions in SkImageDecoder_empty so that a project that does not
want to include the images project it can still build.
Allow ports to build without images project.
In Mac's image encoder, copy 4444 to 8888 before encoding.
Add SkWriter32::reservePad, to provide a pointer to write non 4 byte
aligned data, padded with zeroes.
In bench_ and render_ pictures, pass decode function to SkPicture
creation from a stream.
BUG=https://code.google.com/p/skia/issues/detail?id=842
Review URL: https://codereview.appspot.com/6551071
git-svn-id: http://skia.googlecode.com/svn/trunk@5818 2bbb7eff-a529-9590-31e7-b0007b416f81
2012-10-04 21:46:08 +00:00
|
|
|
/**
|
|
|
|
* Serialize to a stream. If non NULL, encoder will be used to encode
|
|
|
|
* any bitmaps in the picture.
|
|
|
|
*/
|
2013-02-22 22:04:19 +00:00
|
|
|
void serialize(SkWStream*, EncodeBitmap encoder = NULL) const;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-02-01 16:18:09 +00:00
|
|
|
#ifdef SK_BUILD_FOR_ANDROID
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Signals that the caller is prematurely done replaying the drawing
|
|
|
|
commands. This can be called from a canvas virtual while the picture
|
2012-08-23 18:09:54 +00:00
|
|
|
is drawing. Has no effect if the picture is not drawing.
|
2013-02-01 16:18:09 +00:00
|
|
|
@deprecated preserving for legacy purposes
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
void abortPlayback();
|
2013-02-01 16:18:09 +00:00
|
|
|
#endif
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2013-05-07 14:45:40 +00:00
|
|
|
protected:
|
2012-11-13 20:41:18 +00:00
|
|
|
// V2 : adds SkPixelRef's generation ID.
|
|
|
|
// V3 : PictInfo tag at beginning, and EOF tag at the end
|
|
|
|
// V4 : move SkPictInfo to be the header
|
|
|
|
// V5 : don't read/write FunctionPtr on cross-process (we can detect that)
|
|
|
|
// V6 : added serialization of SkPath's bounds (and packed its flags tighter)
|
|
|
|
// V7 : changed drawBitmapRect(IRect) to drawBitmapRectToRect(Rect)
|
|
|
|
// V8 : Add an option for encoding bitmaps
|
|
|
|
// V9 : Allow the reader and writer of an SKP disagree on whether to support
|
|
|
|
// SK_SUPPORT_HINTING_SCALE_FACTOR
|
2012-12-12 20:48:18 +00:00
|
|
|
// V10: add drawRRect, drawOval, clipRRect
|
2013-04-30 02:32:41 +00:00
|
|
|
// V11: modify how readBitmap and writeBitmap store their info.
|
|
|
|
static const uint32_t PICTURE_VERSION = 11;
|
2012-11-13 20:41:18 +00:00
|
|
|
|
|
|
|
// fPlayback, fRecord, fWidth & fHeight are protected to allow derived classes to
|
2012-11-13 21:46:06 +00:00
|
|
|
// install their own SkPicturePlayback-derived players,SkPictureRecord-derived
|
2012-11-13 20:41:18 +00:00
|
|
|
// recorders and set the picture size
|
|
|
|
SkPicturePlayback* fPlayback;
|
2012-10-24 19:30:41 +00:00
|
|
|
SkPictureRecord* fRecord;
|
2012-10-25 14:45:08 +00:00
|
|
|
int fWidth, fHeight;
|
2012-10-24 19:30:41 +00:00
|
|
|
|
2012-11-01 17:10:32 +00:00
|
|
|
// For testing. Derived classes may instantiate an alternate
|
|
|
|
// SkBBoxHierarchy implementation
|
|
|
|
virtual SkBBoxHierarchy* createBBoxHierarchy() const;
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2013-02-22 21:38:35 +00:00
|
|
|
void initFromStream(SkStream*, bool* success, InstallPixelRefProc);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
friend class SkFlatPicture;
|
|
|
|
friend class SkPicturePlayback;
|
2012-06-21 20:25:03 +00:00
|
|
|
|
|
|
|
typedef SkRefCnt INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SkAutoPictureRecord : SkNoncopyable {
|
|
|
|
public:
|
2009-02-13 14:56:09 +00:00
|
|
|
SkAutoPictureRecord(SkPicture* pict, int width, int height,
|
|
|
|
uint32_t recordingFlags = 0) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fPicture = pict;
|
2009-02-13 14:56:09 +00:00
|
|
|
fCanvas = pict->beginRecording(width, height, recordingFlags);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
~SkAutoPictureRecord() {
|
|
|
|
fPicture->endRecording();
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
/** Return the canvas to draw into for recording into the picture.
|
|
|
|
*/
|
|
|
|
SkCanvas* getRecordingCanvas() const { return fCanvas; }
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
SkPicture* fPicture;
|
|
|
|
SkCanvas* fCanvas;
|
|
|
|
};
|
|
|
|
|
2013-05-20 17:02:41 +00:00
|
|
|
/**
|
|
|
|
* Subclasses of this can be passed to canvas.drawPicture. During the drawing
|
|
|
|
* of the picture, this callback will periodically be invoked. If its
|
|
|
|
* abortDrawing() returns true, then picture playback will be interrupted.
|
|
|
|
*
|
|
|
|
* The resulting drawing is undefined, as there is no guarantee how often the
|
|
|
|
* callback will be invoked. If the abort happens inside some level of nested
|
|
|
|
* calls to save(), restore will automatically be called to return the state
|
|
|
|
* to the same level it was before the drawPicture call was made.
|
|
|
|
*/
|
|
|
|
class SkDrawPictureCallback {
|
|
|
|
public:
|
|
|
|
SkDrawPictureCallback() {}
|
|
|
|
virtual ~SkDrawPictureCallback() {}
|
|
|
|
|
|
|
|
virtual bool abortDrawing() = 0;
|
|
|
|
};
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
#endif
|