2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #ifndef SkGPipe_DEFINED
11 #define SkGPipe_DEFINED
13 #include "SkFlattenable.h"
14 #include "SkPicture.h"
15 #include "SkWriter32.h"
19 // XLib.h might have defined Status already (ugh)
27 SkGPipeReader(SkCanvas* target);
31 kDone_Status, //!< no more data expected from reader
32 kEOF_Status, //!< need more data from reader
33 kError_Status, //!< encountered error
34 kReadAtom_Status//!< finished reading an atom
38 kReadAtom_PlaybackFlag = 0x1, //!< playback a single command from the stream
39 kSilent_PlaybackFlag = 0x2, //!< playback without drawing
42 void setCanvas(SkCanvas*);
45 * Set a function for decoding bitmaps that have encoded data.
47 void setBitmapDecoder(SkPicture::InstallPixelRefProc proc) { fProc = proc; }
49 // data must be 4-byte aligned
50 // length must be a multiple of 4
51 Status playback(const void* data, size_t length, uint32_t playbackFlags = 0,
52 size_t* bytesRead = NULL);
55 class SkGPipeState* fState;
56 SkPicture::InstallPixelRefProc fProc;
59 ///////////////////////////////////////////////////////////////////////////////
63 class SkGPipeController {
65 SkGPipeController() : fCanvas(NULL) {}
66 virtual ~SkGPipeController();
69 * Called periodically by the writer, to get a working buffer of RAM to
70 * write into. The actual size of the block is also returned, and must be
71 * actual >= minRequest. If NULL is returned, then actual is ignored and
74 * The returned block must be 4-byte aligned, and actual must be a
76 * minRequest will always be a multiple of 4.
78 virtual void* requestBlock(size_t minRequest, size_t* actual) = 0;
81 * This is called each time some atomic portion of the data has been
82 * written to the block (most recently returned by requestBlock()).
83 * If bytes == 0, then the writer has finished.
85 * bytes will always be a multiple of 4.
87 virtual void notifyWritten(size_t bytes) = 0;
88 virtual int numberOfReaders() const { return 1; }
91 friend class SkGPipeWriter;
92 void setCanvas(SkGPipeCanvas*);
94 SkGPipeCanvas* fCanvas;
102 bool isRecording() const { return SkToBool(fCanvas); }
106 * Tells the writer that the reader will be in a different process, so
107 * (for example) we cannot put function pointers in the stream.
109 kCrossProcess_Flag = 1 << 0,
112 * Only meaningful if kCrossProcess_Flag is set. Tells the writer that
113 * in spite of being cross process, it will have shared address space
114 * with the reader, so the two can share large objects (like SkBitmaps).
116 kSharedAddressSpace_Flag = 1 << 1,
119 * Tells the writer that there will be multiple threads reading the stream
122 kSimultaneousReaders_Flag = 1 << 2,
125 SkCanvas* startRecording(SkGPipeController*, uint32_t flags = 0,
126 uint32_t width = kDefaultRecordingCanvasSize,
127 uint32_t height = kDefaultRecordingCanvasSize);
129 // called in destructor, but can be called sooner once you know there
130 // should be no more drawing calls made into the recording canvas.
134 * Tells the writer to commit all recorded draw commands to the
135 * controller immediately.
136 * @param detachCurrentBlock Set to true to request that the next draw
137 * command be recorded in a new block.
139 void flushRecording(bool detachCurrentBlock);
142 * Return the amount of bytes being used for recording. Note that this
143 * does not include the amount of storage written to the stream, which is
144 * controlled by the SkGPipeController.
145 * Currently only returns the amount used for SkBitmaps, since they are
146 * potentially unbounded (if the client is not calling playback).
148 size_t storageAllocatedForRecording() const;
151 * Attempt to reduce the storage allocated for recording by evicting
153 * @param bytesToFree minimum number of bytes that should be attempted to
155 * @return number of bytes actually freed.
157 size_t freeMemoryIfPossible(size_t bytesToFree);
161 kDefaultRecordingCanvasSize = 32767,
164 SkGPipeCanvas* fCanvas;