epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * 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.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 11 | #ifndef SkGPipe_DEFINED |
| 12 | #define SkGPipe_DEFINED |
| 13 | |
| 14 | #include "SkWriter32.h" |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 15 | #include "SkFlattenable.h" |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 16 | |
| 17 | class SkCanvas; |
| 18 | |
reed@google.com | bc75483 | 2011-08-12 14:49:55 +0000 | [diff] [blame] | 19 | // XLib.h might have defined Status already (ugh) |
| 20 | #ifdef Status |
| 21 | #undef Status |
| 22 | #endif |
| 23 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 24 | class SkGPipeReader { |
| 25 | public: |
scroggo@google.com | 72c9672 | 2012-06-06 21:07:10 +0000 | [diff] [blame] | 26 | SkGPipeReader(); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 27 | 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.com | 1bce0a5 | 2011-06-16 21:08:19 +0000 | [diff] [blame] | 33 | kError_Status, //!< encountered error |
| 34 | kReadAtom_Status//!< finished reading an atom |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
scroggo@google.com | 72c9672 | 2012-06-06 21:07:10 +0000 | [diff] [blame] | 37 | void setCanvas(SkCanvas*); |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 38 | // data must be 4-byte aligned |
| 39 | // length must be a multiple of 4 |
yangsu@google.com | 1bce0a5 | 2011-06-16 21:08:19 +0000 | [diff] [blame] | 40 | Status playback(const void* data, size_t length, size_t* bytesRead = NULL, |
| 41 | bool readAtom = false); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 42 | private: |
| 43 | SkCanvas* fCanvas; |
| 44 | class SkGPipeState* fState; |
| 45 | }; |
| 46 | |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | |
scroggo@google.com | 3cb969f | 2012-07-27 20:39:19 +0000 | [diff] [blame] | 49 | class SkGPipeCanvas; |
| 50 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 51 | class SkGPipeController { |
reed@google.com | f316634 | 2011-04-26 20:06:08 +0000 | [diff] [blame] | 52 | public: |
scroggo@google.com | 3cb969f | 2012-07-27 20:39:19 +0000 | [diff] [blame] | 53 | SkGPipeController() : fCanvas(NULL) {} |
| 54 | virtual ~SkGPipeController(); |
| 55 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 56 | /** |
| 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.com | f316634 | 2011-04-26 20:06:08 +0000 | [diff] [blame] | 67 | |
| 68 | /** |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 69 | * 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.com | f316634 | 2011-04-26 20:06:08 +0000 | [diff] [blame] | 72 | * |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 73 | * bytes will always be a multiple of 4. |
reed@google.com | f316634 | 2011-04-26 20:06:08 +0000 | [diff] [blame] | 74 | */ |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 75 | virtual void notifyWritten(size_t bytes) = 0; |
scroggo@google.com | 284bf50 | 2012-07-17 16:10:34 +0000 | [diff] [blame] | 76 | virtual int numberOfReaders() const { return 1; } |
scroggo@google.com | 3cb969f | 2012-07-27 20:39:19 +0000 | [diff] [blame] | 77 | |
| 78 | private: |
| 79 | friend class SkGPipeWriter; |
| 80 | void setCanvas(SkGPipeCanvas*); |
| 81 | |
| 82 | SkGPipeCanvas* fCanvas; |
reed@google.com | f316634 | 2011-04-26 20:06:08 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 85 | class SkGPipeWriter { |
| 86 | public: |
| 87 | SkGPipeWriter(); |
| 88 | ~SkGPipeWriter(); |
| 89 | |
| 90 | bool isRecording() const { return NULL != fCanvas; } |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 91 | |
| 92 | enum Flags { |
scroggo@google.com | 565254b | 2012-06-28 15:41:32 +0000 | [diff] [blame] | 93 | /** |
| 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.com | 15011ee | 2012-07-26 20:03:32 +0000 | [diff] [blame] | 103 | kSharedAddressSpace_Flag = 1 << 1 |
reed@google.com | dde0956 | 2011-05-23 12:21:05 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | SkCanvas* startRecording(SkGPipeController*, uint32_t flags = 0); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 107 | |
reed@google.com | acd471f | 2011-05-03 21:26:46 +0000 | [diff] [blame] | 108 | // called in destructor, but can be called sooner once you know there |
| 109 | // should be no more drawing calls made into the recording canvas. |
| 110 | void endRecording(); |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 111 | |
junov@chromium.org | 77eec24 | 2012-07-18 17:54:45 +0000 | [diff] [blame] | 112 | /** |
| 113 | * Tells the writer to commit all recorded draw commands to the |
| 114 | * controller immediately. |
| 115 | * @param detachCurrentBlock Set to true to request that the next draw |
| 116 | * command be recorded in a new block. |
| 117 | */ |
| 118 | void flushRecording(bool detachCurrentBlock); |
| 119 | |
scroggo@google.com | 15011ee | 2012-07-26 20:03:32 +0000 | [diff] [blame] | 120 | /** |
| 121 | * Return the amount of bytes being used for recording. Note that this |
| 122 | * does not include the amount of storage written to the stream, which is |
| 123 | * controlled by the SkGPipeController. |
| 124 | * Currently only returns the amount used for SkBitmaps, since they are |
| 125 | * potentially unbounded (if the client is not calling playback). |
| 126 | */ |
| 127 | size_t storageAllocatedForRecording(); |
| 128 | |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 129 | private: |
scroggo@google.com | 3cb969f | 2012-07-27 20:39:19 +0000 | [diff] [blame] | 130 | SkGPipeCanvas* fCanvas; |
scroggo@google.com | 3cb969f | 2012-07-27 20:39:19 +0000 | [diff] [blame] | 131 | SkWriter32 fWriter; |
reed@google.com | bb6992a | 2011-04-26 17:41:56 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | #endif |