| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * 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. | |
| 6 */ | |
| 7 | |
| 8 | |
| 9 | |
| 10 #ifndef SkGPipe_DEFINED | |
| 11 #define SkGPipe_DEFINED | |
| 12 | |
| 13 #include "SkFlattenable.h" | |
| 14 #include "SkPicture.h" | |
| 15 #include "SkWriter32.h" | |
| 16 | |
| 17 class SkCanvas; | |
| 18 | |
| 19 // XLib.h might have defined Status already (ugh) | |
| 20 #ifdef Status | |
| 21 #undef Status | |
| 22 #endif | |
| 23 | |
| 24 class SkGPipeReader { | |
| 25 public: | |
| 26 SkGPipeReader(); | |
| 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 | |
| 33 kError_Status, //!< encountered error | |
| 34 kReadAtom_Status//!< finished reading an atom | |
| 35 }; | |
| 36 | |
| 37 enum PlaybackFlags { | |
| 38 kReadAtom_PlaybackFlag = 0x1, //!< playback a single command from the st
ream | |
| 39 kSilent_PlaybackFlag = 0x2, //!< playback without drawing | |
| 40 }; | |
| 41 | |
| 42 void setCanvas(SkCanvas*); | |
| 43 | |
| 44 /** | |
| 45 * Set a function for decoding bitmaps that have encoded data. | |
| 46 */ | |
| 47 void setBitmapDecoder(SkPicture::InstallPixelRefProc proc) { fProc = proc; } | |
| 48 | |
| 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); | |
| 53 private: | |
| 54 SkCanvas* fCanvas; | |
| 55 class SkGPipeState* fState; | |
| 56 SkPicture::InstallPixelRefProc fProc; | |
| 57 }; | |
| 58 | |
| 59 /////////////////////////////////////////////////////////////////////////////// | |
| 60 | |
| 61 class SkGPipeCanvas; | |
| 62 | |
| 63 class SkGPipeController { | |
| 64 public: | |
| 65 SkGPipeController() : fCanvas(NULL) {} | |
| 66 virtual ~SkGPipeController(); | |
| 67 | |
| 68 /** | |
| 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; | |
| 79 | |
| 80 /** | |
| 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. | |
| 84 * | |
| 85 * bytes will always be a multiple of 4. | |
| 86 */ | |
| 87 virtual void notifyWritten(size_t bytes) = 0; | |
| 88 virtual int numberOfReaders() const { return 1; } | |
| 89 | |
| 90 /** | |
| 91 * Release resource references that are held in internal caches. | |
| 92 * This must only be called after the pipe has been completely flushed. | |
| 93 */ | |
| 94 void purgeCaches(); | |
| 95 | |
| 96 private: | |
| 97 friend class SkGPipeWriter; | |
| 98 void setCanvas(SkGPipeCanvas*); | |
| 99 | |
| 100 SkGPipeCanvas* fCanvas; | |
| 101 }; | |
| 102 | |
| 103 class SkGPipeWriter { | |
| 104 public: | |
| 105 SkGPipeWriter(); | |
| 106 ~SkGPipeWriter(); | |
| 107 | |
| 108 bool isRecording() const { return SkToBool(fCanvas); } | |
| 109 | |
| 110 enum Flags { | |
| 111 /** | |
| 112 * Tells the writer that the reader will be in a different process, so | |
| 113 * (for example) we cannot put function pointers in the stream. | |
| 114 */ | |
| 115 kCrossProcess_Flag = 1 << 0, | |
| 116 | |
| 117 /** | |
| 118 * Only meaningful if kCrossProcess_Flag is set. Tells the writer that | |
| 119 * in spite of being cross process, it will have shared address space | |
| 120 * with the reader, so the two can share large objects (like SkBitmaps)
. | |
| 121 */ | |
| 122 kSharedAddressSpace_Flag = 1 << 1, | |
| 123 | |
| 124 /** | |
| 125 * Tells the writer that there will be multiple threads reading the str
eam | |
| 126 * simultaneously. | |
| 127 */ | |
| 128 kSimultaneousReaders_Flag = 1 << 2, | |
| 129 }; | |
| 130 | |
| 131 SkCanvas* startRecording(SkGPipeController*, uint32_t flags = 0, | |
| 132 uint32_t width = kDefaultRecordingCanvasSize, | |
| 133 uint32_t height = kDefaultRecordingCanvasSize); | |
| 134 | |
| 135 // called in destructor, but can be called sooner once you know there | |
| 136 // should be no more drawing calls made into the recording canvas. | |
| 137 void endRecording(); | |
| 138 | |
| 139 /** | |
| 140 * Tells the writer to commit all recorded draw commands to the | |
| 141 * controller immediately. | |
| 142 * @param detachCurrentBlock Set to true to request that the next draw | |
| 143 * command be recorded in a new block. | |
| 144 */ | |
| 145 void flushRecording(bool detachCurrentBlock); | |
| 146 | |
| 147 /** | |
| 148 * Return the amount of bytes being used for recording. Note that this | |
| 149 * does not include the amount of storage written to the stream, which is | |
| 150 * controlled by the SkGPipeController. | |
| 151 * Currently only returns the amount used for SkBitmaps, since they are | |
| 152 * potentially unbounded (if the client is not calling playback). | |
| 153 */ | |
| 154 size_t storageAllocatedForRecording() const; | |
| 155 | |
| 156 /** | |
| 157 * Attempt to reduce the storage allocated for recording by evicting | |
| 158 * cache resources. | |
| 159 * @param bytesToFree minimum number of bytes that should be attempted to | |
| 160 * be freed. | |
| 161 * @return number of bytes actually freed. | |
| 162 */ | |
| 163 size_t freeMemoryIfPossible(size_t bytesToFree); | |
| 164 | |
| 165 private: | |
| 166 enum { | |
| 167 kDefaultRecordingCanvasSize = 32767, | |
| 168 }; | |
| 169 | |
| 170 SkGPipeCanvas* fCanvas; | |
| 171 SkWriter32 fWriter; | |
| 172 }; | |
| 173 | |
| 174 #endif | |
| OLD | NEW |