blob: 98e081da5e6b8a306563e56b6deaf70bc34ec1c9 [file] [log] [blame]
reed@google.combb6992a2011-04-26 17:41:56 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
reed@google.combb6992a2011-04-26 17:41:56 +00006 */
7
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.combb6992a2011-04-26 17:41:56 +000010#ifndef SkGPipe_DEFINED
11#define SkGPipe_DEFINED
12
reed@google.comdde09562011-05-23 12:21:05 +000013#include "SkFlattenable.h"
scroggo@google.com74b7ffd2013-04-30 02:32:41 +000014#include "SkPicture.h"
15#include "SkWriter32.h"
reed@google.combb6992a2011-04-26 17:41:56 +000016
17class SkCanvas;
18
reed@google.combc754832011-08-12 14:49:55 +000019// XLib.h might have defined Status already (ugh)
20#ifdef Status
21 #undef Status
22#endif
23
reed@google.combb6992a2011-04-26 17:41:56 +000024class SkGPipeReader {
25public:
scroggo@google.com72c96722012-06-06 21:07:10 +000026 SkGPipeReader();
reed@google.combb6992a2011-04-26 17:41:56 +000027 SkGPipeReader(SkCanvas* target);
28 ~SkGPipeReader();
29
30 enum Status {
31 kDone_Status, //!< no more data expected from reader
32 kEOF_Status, //!< need more data from reader
yangsu@google.com1bce0a52011-06-16 21:08:19 +000033 kError_Status, //!< encountered error
34 kReadAtom_Status//!< finished reading an atom
reed@google.combb6992a2011-04-26 17:41:56 +000035 };
36
junov@chromium.orgfb103892012-09-20 19:35:43 +000037 enum PlaybackFlags {
38 kReadAtom_PlaybackFlag = 0x1, //!< playback a single command from the stream
39 kSilent_PlaybackFlag = 0x2, //!< playback without drawing
40 };
41
scroggo@google.com72c96722012-06-06 21:07:10 +000042 void setCanvas(SkCanvas*);
scroggo@google.com74b7ffd2013-04-30 02:32:41 +000043
44 /**
45 * Set a function for decoding bitmaps that have encoded data.
46 */
47 void setBitmapDecoder(SkPicture::InstallPixelRefProc proc) { fProc = proc; }
48
reed@google.comacd471f2011-05-03 21:26:46 +000049 // data must be 4-byte aligned
50 // length must be a multiple of 4
junov@chromium.orgfb103892012-09-20 19:35:43 +000051 Status playback(const void* data, size_t length, uint32_t playbackFlags = 0,
52 size_t* bytesRead = NULL);
reed@google.combb6992a2011-04-26 17:41:56 +000053private:
scroggo@google.com74b7ffd2013-04-30 02:32:41 +000054 SkCanvas* fCanvas;
55 class SkGPipeState* fState;
56 SkPicture::InstallPixelRefProc fProc;
reed@google.combb6992a2011-04-26 17:41:56 +000057};
58
59///////////////////////////////////////////////////////////////////////////////
60
scroggo@google.com3cb969f2012-07-27 20:39:19 +000061class SkGPipeCanvas;
62
reed@google.comacd471f2011-05-03 21:26:46 +000063class SkGPipeController {
reed@google.comf3166342011-04-26 20:06:08 +000064public:
scroggo@google.com3cb969f2012-07-27 20:39:19 +000065 SkGPipeController() : fCanvas(NULL) {}
66 virtual ~SkGPipeController();
67
reed@google.comacd471f2011-05-03 21:26:46 +000068 /**
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
72 * writing will stop.
73 *
74 * The returned block must be 4-byte aligned, and actual must be a
75 * multiple of 4.
76 * minRequest will always be a multiple of 4.
77 */
78 virtual void* requestBlock(size_t minRequest, size_t* actual) = 0;
reed@google.comf3166342011-04-26 20:06:08 +000079
80 /**
reed@google.comacd471f2011-05-03 21:26:46 +000081 * 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.
reed@google.comf3166342011-04-26 20:06:08 +000084 *
reed@google.comacd471f2011-05-03 21:26:46 +000085 * bytes will always be a multiple of 4.
reed@google.comf3166342011-04-26 20:06:08 +000086 */
reed@google.comacd471f2011-05-03 21:26:46 +000087 virtual void notifyWritten(size_t bytes) = 0;
scroggo@google.com284bf502012-07-17 16:10:34 +000088 virtual int numberOfReaders() const { return 1; }
scroggo@google.com3cb969f2012-07-27 20:39:19 +000089
90private:
91 friend class SkGPipeWriter;
92 void setCanvas(SkGPipeCanvas*);
93
94 SkGPipeCanvas* fCanvas;
reed@google.comf3166342011-04-26 20:06:08 +000095};
96
reed@google.combb6992a2011-04-26 17:41:56 +000097class SkGPipeWriter {
98public:
99 SkGPipeWriter();
100 ~SkGPipeWriter();
101
bsalomon49f085d2014-09-05 13:34:00 -0700102 bool isRecording() const { return SkToBool(fCanvas); }
reed@google.comdde09562011-05-23 12:21:05 +0000103
104 enum Flags {
scroggo@google.com565254b2012-06-28 15:41:32 +0000105 /**
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.
108 */
109 kCrossProcess_Flag = 1 << 0,
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000110
scroggo@google.com565254b2012-06-28 15:41:32 +0000111 /**
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
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000114 * with the reader, so the two can share large objects (like SkBitmaps).
scroggo@google.com565254b2012-06-28 15:41:32 +0000115 */
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000116 kSharedAddressSpace_Flag = 1 << 1,
117
118 /**
119 * Tells the writer that there will be multiple threads reading the stream
120 * simultaneously.
121 */
122 kSimultaneousReaders_Flag = 1 << 2,
reed@google.comdde09562011-05-23 12:21:05 +0000123 };
124
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000125 SkCanvas* startRecording(SkGPipeController*, uint32_t flags = 0,
126 uint32_t width = kDefaultRecordingCanvasSize,
127 uint32_t height = kDefaultRecordingCanvasSize);
reed@google.combb6992a2011-04-26 17:41:56 +0000128
reed@google.comacd471f2011-05-03 21:26:46 +0000129 // called in destructor, but can be called sooner once you know there
130 // should be no more drawing calls made into the recording canvas.
131 void endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +0000132
junov@chromium.org77eec242012-07-18 17:54:45 +0000133 /**
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.
138 */
139 void flushRecording(bool detachCurrentBlock);
140
scroggo@google.com15011ee2012-07-26 20:03:32 +0000141 /**
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).
147 */
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000148 size_t storageAllocatedForRecording() const;
149
150 /**
151 * Attempt to reduce the storage allocated for recording by evicting
152 * cache resources.
153 * @param bytesToFree minimum number of bytes that should be attempted to
154 * be freed.
155 * @return number of bytes actually freed.
156 */
157 size_t freeMemoryIfPossible(size_t bytesToFree);
scroggo@google.com15011ee2012-07-26 20:03:32 +0000158
reed@google.combb6992a2011-04-26 17:41:56 +0000159private:
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000160 enum {
161 kDefaultRecordingCanvasSize = 32767,
162 };
163
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000164 SkGPipeCanvas* fCanvas;
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000165 SkWriter32 fWriter;
reed@google.combb6992a2011-04-26 17:41:56 +0000166};
167
168#endif