blob: bbe504aee64df54fbb0e0dbe8fe594ca3b976652 [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
scroggo@google.com72c96722012-06-06 21:07:10 +000037 void setCanvas(SkCanvas*);
reed@google.comacd471f2011-05-03 21:26:46 +000038 // data must be 4-byte aligned
39 // length must be a multiple of 4
yangsu@google.com1bce0a52011-06-16 21:08:19 +000040 Status playback(const void* data, size_t length, size_t* bytesRead = NULL,
41 bool readAtom = false);
reed@google.combb6992a2011-04-26 17:41:56 +000042private:
43 SkCanvas* fCanvas;
44 class SkGPipeState* fState;
45};
46
47///////////////////////////////////////////////////////////////////////////////
48
scroggo@google.com3cb969f2012-07-27 20:39:19 +000049class SkGPipeCanvas;
50
reed@google.comacd471f2011-05-03 21:26:46 +000051class SkGPipeController {
reed@google.comf3166342011-04-26 20:06:08 +000052public:
scroggo@google.com3cb969f2012-07-27 20:39:19 +000053 SkGPipeController() : fCanvas(NULL) {}
54 virtual ~SkGPipeController();
55
reed@google.comacd471f2011-05-03 21:26:46 +000056 /**
57 * Called periodically by the writer, to get a working buffer of RAM to
58 * write into. The actual size of the block is also returned, and must be
59 * actual >= minRequest. If NULL is returned, then actual is ignored and
60 * writing will stop.
61 *
62 * The returned block must be 4-byte aligned, and actual must be a
63 * multiple of 4.
64 * minRequest will always be a multiple of 4.
65 */
66 virtual void* requestBlock(size_t minRequest, size_t* actual) = 0;
reed@google.comf3166342011-04-26 20:06:08 +000067
68 /**
reed@google.comacd471f2011-05-03 21:26:46 +000069 * This is called each time some atomic portion of the data has been
70 * written to the block (most recently returned by requestBlock()).
71 * If bytes == 0, then the writer has finished.
reed@google.comf3166342011-04-26 20:06:08 +000072 *
reed@google.comacd471f2011-05-03 21:26:46 +000073 * bytes will always be a multiple of 4.
reed@google.comf3166342011-04-26 20:06:08 +000074 */
reed@google.comacd471f2011-05-03 21:26:46 +000075 virtual void notifyWritten(size_t bytes) = 0;
scroggo@google.com284bf502012-07-17 16:10:34 +000076 virtual int numberOfReaders() const { return 1; }
scroggo@google.com3cb969f2012-07-27 20:39:19 +000077
78private:
79 friend class SkGPipeWriter;
80 void setCanvas(SkGPipeCanvas*);
81
82 SkGPipeCanvas* fCanvas;
reed@google.comf3166342011-04-26 20:06:08 +000083};
84
reed@google.combb6992a2011-04-26 17:41:56 +000085class SkGPipeWriter {
86public:
87 SkGPipeWriter();
88 ~SkGPipeWriter();
89
90 bool isRecording() const { return NULL != fCanvas; }
reed@google.comdde09562011-05-23 12:21:05 +000091
92 enum Flags {
scroggo@google.com565254b2012-06-28 15:41:32 +000093 /**
94 * Tells the writer that the reader will be in a different process, so
95 * (for example) we cannot put function pointers in the stream.
96 */
97 kCrossProcess_Flag = 1 << 0,
98 /**
99 * Only meaningful if kCrossProcess_Flag is set. Tells the writer that
100 * in spite of being cross process, it will have shared address space
101 * with the reader, so the two can share large objects (like SkBitmaps)
102 */
scroggo@google.com15011ee2012-07-26 20:03:32 +0000103 kSharedAddressSpace_Flag = 1 << 1
reed@google.comdde09562011-05-23 12:21:05 +0000104 };
105
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000106 SkCanvas* startRecording(SkGPipeController*, uint32_t flags = 0,
107 uint32_t width = kDefaultRecordingCanvasSize,
108 uint32_t height = kDefaultRecordingCanvasSize);
reed@google.combb6992a2011-04-26 17:41:56 +0000109
reed@google.comacd471f2011-05-03 21:26:46 +0000110 // called in destructor, but can be called sooner once you know there
111 // should be no more drawing calls made into the recording canvas.
112 void endRecording();
reed@google.combb6992a2011-04-26 17:41:56 +0000113
junov@chromium.org77eec242012-07-18 17:54:45 +0000114 /**
115 * Tells the writer to commit all recorded draw commands to the
116 * controller immediately.
117 * @param detachCurrentBlock Set to true to request that the next draw
118 * command be recorded in a new block.
119 */
120 void flushRecording(bool detachCurrentBlock);
121
scroggo@google.com15011ee2012-07-26 20:03:32 +0000122 /**
123 * Return the amount of bytes being used for recording. Note that this
124 * does not include the amount of storage written to the stream, which is
125 * controlled by the SkGPipeController.
126 * Currently only returns the amount used for SkBitmaps, since they are
127 * potentially unbounded (if the client is not calling playback).
128 */
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000129 size_t storageAllocatedForRecording() const;
130
131 /**
132 * Attempt to reduce the storage allocated for recording by evicting
133 * cache resources.
134 * @param bytesToFree minimum number of bytes that should be attempted to
135 * be freed.
136 * @return number of bytes actually freed.
137 */
138 size_t freeMemoryIfPossible(size_t bytesToFree);
scroggo@google.com15011ee2012-07-26 20:03:32 +0000139
reed@google.combb6992a2011-04-26 17:41:56 +0000140private:
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000141 enum {
142 kDefaultRecordingCanvasSize = 32767,
143 };
144
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000145 SkGPipeCanvas* fCanvas;
scroggo@google.com3cb969f2012-07-27 20:39:19 +0000146 SkWriter32 fWriter;
reed@google.combb6992a2011-04-26 17:41:56 +0000147};
148
149#endif