blob: 27af16db8c134fdacd97fb03102eb33f5d868b1b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.combb6992a2011-04-26 17:41:56 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.combb6992a2011-04-26 17:41:56 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.combb6992a2011-04-26 17:41:56 +000011#ifndef SkGPipe_DEFINED
12#define SkGPipe_DEFINED
13
14#include "SkWriter32.h"
reed@google.comdde09562011-05-23 12:21:05 +000015#include "SkFlattenable.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*);
reed@google.comacd471f2011-05-03 21:26:46 +000043 // data must be 4-byte aligned
44 // length must be a multiple of 4
junov@chromium.orgfb103892012-09-20 19:35:43 +000045 Status playback(const void* data, size_t length, uint32_t playbackFlags = 0,
46 size_t* bytesRead = NULL);
reed@google.combb6992a2011-04-26 17:41:56 +000047private:
48 SkCanvas* fCanvas;
49 class SkGPipeState* fState;
50};
51
52///////////////////////////////////////////////////////////////////////////////
53
scroggo@google.com3cb969f2012-07-27 20:39:19 +000054class SkGPipeCanvas;
55
reed@google.comacd471f2011-05-03 21:26:46 +000056class SkGPipeController {
reed@google.comf3166342011-04-26 20:06:08 +000057public:
scroggo@google.com3cb969f2012-07-27 20:39:19 +000058 SkGPipeController() : fCanvas(NULL) {}
59 virtual ~SkGPipeController();
60
reed@google.comacd471f2011-05-03 21:26:46 +000061 /**
62 * Called periodically by the writer, to get a working buffer of RAM to
63 * write into. The actual size of the block is also returned, and must be
64 * actual >= minRequest. If NULL is returned, then actual is ignored and
65 * writing will stop.
66 *
67 * The returned block must be 4-byte aligned, and actual must be a
68 * multiple of 4.
69 * minRequest will always be a multiple of 4.
70 */
71 virtual void* requestBlock(size_t minRequest, size_t* actual) = 0;
reed@google.comf3166342011-04-26 20:06:08 +000072
73 /**
reed@google.comacd471f2011-05-03 21:26:46 +000074 * This is called each time some atomic portion of the data has been
75 * written to the block (most recently returned by requestBlock()).
76 * If bytes == 0, then the writer has finished.
reed@google.comf3166342011-04-26 20:06:08 +000077 *
reed@google.comacd471f2011-05-03 21:26:46 +000078 * bytes will always be a multiple of 4.
reed@google.comf3166342011-04-26 20:06:08 +000079 */
reed@google.comacd471f2011-05-03 21:26:46 +000080 virtual void notifyWritten(size_t bytes) = 0;
scroggo@google.com284bf502012-07-17 16:10:34 +000081 virtual int numberOfReaders() const { return 1; }
scroggo@google.com3cb969f2012-07-27 20:39:19 +000082
83private:
84 friend class SkGPipeWriter;
85 void setCanvas(SkGPipeCanvas*);
86
87 SkGPipeCanvas* fCanvas;
reed@google.comf3166342011-04-26 20:06:08 +000088};
89
reed@google.combb6992a2011-04-26 17:41:56 +000090class SkGPipeWriter {
91public:
92 SkGPipeWriter();
93 ~SkGPipeWriter();
94
95 bool isRecording() const { return NULL != fCanvas; }
reed@google.comdde09562011-05-23 12:21:05 +000096
97 enum Flags {
scroggo@google.com565254b2012-06-28 15:41:32 +000098 /**
99 * Tells the writer that the reader will be in a different process, so
100 * (for example) we cannot put function pointers in the stream.
101 */
102 kCrossProcess_Flag = 1 << 0,
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000103
scroggo@google.com565254b2012-06-28 15:41:32 +0000104 /**
105 * Only meaningful if kCrossProcess_Flag is set. Tells the writer that
106 * in spite of being cross process, it will have shared address space
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000107 * with the reader, so the two can share large objects (like SkBitmaps).
scroggo@google.com565254b2012-06-28 15:41:32 +0000108 */
scroggo@google.com58b4ead2012-08-31 16:15:22 +0000109 kSharedAddressSpace_Flag = 1 << 1,
110
111 /**
112 * Tells the writer that there will be multiple threads reading the stream
113 * simultaneously.
114 */
115 kSimultaneousReaders_Flag = 1 << 2,
reed@google.comdde09562011-05-23 12:21:05 +0000116 };
117
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000118 SkCanvas* startRecording(SkGPipeController*, uint32_t flags = 0,
119 uint32_t width = kDefaultRecordingCanvasSize,
120 uint32_t height = kDefaultRecordingCanvasSize);
reed@google.combb6992a2011-04-26 17:41:56 +0000121
reed@google.comacd471f2011-05-03 21:26:46 +0000122 // called in destructor, but can be called sooner once you know there
123 // should be no more drawing calls made into the recording canvas.
124 void endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +0000125
junov@chromium.org77eec242012-07-18 17:54:45 +0000126 /**
127 * Tells the writer to commit all recorded draw commands to the
128 * controller immediately.
129 * @param detachCurrentBlock Set to true to request that the next draw
130 * command be recorded in a new block.
131 */
132 void flushRecording(bool detachCurrentBlock);
133
scroggo@google.com15011ee2012-07-26 20:03:32 +0000134 /**
135 * Return the amount of bytes being used for recording. Note that this
136 * does not include the amount of storage written to the stream, which is
137 * controlled by the SkGPipeController.
138 * Currently only returns the amount used for SkBitmaps, since they are
139 * potentially unbounded (if the client is not calling playback).
140 */
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000141 size_t storageAllocatedForRecording() const;
142
143 /**
144 * Attempt to reduce the storage allocated for recording by evicting
145 * cache resources.
146 * @param bytesToFree minimum number of bytes that should be attempted to
147 * be freed.
148 * @return number of bytes actually freed.
149 */
150 size_t freeMemoryIfPossible(size_t bytesToFree);
scroggo@google.com15011ee2012-07-26 20:03:32 +0000151
reed@google.combb6992a2011-04-26 17:41:56 +0000152private:
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000153 enum {
154 kDefaultRecordingCanvasSize = 32767,
155 };
156
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000157 SkGPipeCanvas* fCanvas;
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000158 SkWriter32 fWriter;
reed@google.combb6992a2011-04-26 17:41:56 +0000159};
160
161#endif