1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/pipe/SkGPipe.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,168 @@ 1.4 +/* 1.5 + * Copyright 2011 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 + 1.12 + 1.13 +#ifndef SkGPipe_DEFINED 1.14 +#define SkGPipe_DEFINED 1.15 + 1.16 +#include "SkFlattenable.h" 1.17 +#include "SkPicture.h" 1.18 +#include "SkWriter32.h" 1.19 + 1.20 +class SkCanvas; 1.21 + 1.22 +// XLib.h might have defined Status already (ugh) 1.23 +#ifdef Status 1.24 + #undef Status 1.25 +#endif 1.26 + 1.27 +class SkGPipeReader { 1.28 +public: 1.29 + SkGPipeReader(); 1.30 + SkGPipeReader(SkCanvas* target); 1.31 + ~SkGPipeReader(); 1.32 + 1.33 + enum Status { 1.34 + kDone_Status, //!< no more data expected from reader 1.35 + kEOF_Status, //!< need more data from reader 1.36 + kError_Status, //!< encountered error 1.37 + kReadAtom_Status//!< finished reading an atom 1.38 + }; 1.39 + 1.40 + enum PlaybackFlags { 1.41 + kReadAtom_PlaybackFlag = 0x1, //!< playback a single command from the stream 1.42 + kSilent_PlaybackFlag = 0x2, //!< playback without drawing 1.43 + }; 1.44 + 1.45 + void setCanvas(SkCanvas*); 1.46 + 1.47 + /** 1.48 + * Set a function for decoding bitmaps that have encoded data. 1.49 + */ 1.50 + void setBitmapDecoder(SkPicture::InstallPixelRefProc proc) { fProc = proc; } 1.51 + 1.52 + // data must be 4-byte aligned 1.53 + // length must be a multiple of 4 1.54 + Status playback(const void* data, size_t length, uint32_t playbackFlags = 0, 1.55 + size_t* bytesRead = NULL); 1.56 +private: 1.57 + SkCanvas* fCanvas; 1.58 + class SkGPipeState* fState; 1.59 + SkPicture::InstallPixelRefProc fProc; 1.60 +}; 1.61 + 1.62 +/////////////////////////////////////////////////////////////////////////////// 1.63 + 1.64 +class SkGPipeCanvas; 1.65 + 1.66 +class SkGPipeController { 1.67 +public: 1.68 + SkGPipeController() : fCanvas(NULL) {} 1.69 + virtual ~SkGPipeController(); 1.70 + 1.71 + /** 1.72 + * Called periodically by the writer, to get a working buffer of RAM to 1.73 + * write into. The actual size of the block is also returned, and must be 1.74 + * actual >= minRequest. If NULL is returned, then actual is ignored and 1.75 + * writing will stop. 1.76 + * 1.77 + * The returned block must be 4-byte aligned, and actual must be a 1.78 + * multiple of 4. 1.79 + * minRequest will always be a multiple of 4. 1.80 + */ 1.81 + virtual void* requestBlock(size_t minRequest, size_t* actual) = 0; 1.82 + 1.83 + /** 1.84 + * This is called each time some atomic portion of the data has been 1.85 + * written to the block (most recently returned by requestBlock()). 1.86 + * If bytes == 0, then the writer has finished. 1.87 + * 1.88 + * bytes will always be a multiple of 4. 1.89 + */ 1.90 + virtual void notifyWritten(size_t bytes) = 0; 1.91 + virtual int numberOfReaders() const { return 1; } 1.92 + 1.93 +private: 1.94 + friend class SkGPipeWriter; 1.95 + void setCanvas(SkGPipeCanvas*); 1.96 + 1.97 + SkGPipeCanvas* fCanvas; 1.98 +}; 1.99 + 1.100 +class SkGPipeWriter { 1.101 +public: 1.102 + SkGPipeWriter(); 1.103 + ~SkGPipeWriter(); 1.104 + 1.105 + bool isRecording() const { return NULL != fCanvas; } 1.106 + 1.107 + enum Flags { 1.108 + /** 1.109 + * Tells the writer that the reader will be in a different process, so 1.110 + * (for example) we cannot put function pointers in the stream. 1.111 + */ 1.112 + kCrossProcess_Flag = 1 << 0, 1.113 + 1.114 + /** 1.115 + * Only meaningful if kCrossProcess_Flag is set. Tells the writer that 1.116 + * in spite of being cross process, it will have shared address space 1.117 + * with the reader, so the two can share large objects (like SkBitmaps). 1.118 + */ 1.119 + kSharedAddressSpace_Flag = 1 << 1, 1.120 + 1.121 + /** 1.122 + * Tells the writer that there will be multiple threads reading the stream 1.123 + * simultaneously. 1.124 + */ 1.125 + kSimultaneousReaders_Flag = 1 << 2, 1.126 + }; 1.127 + 1.128 + SkCanvas* startRecording(SkGPipeController*, uint32_t flags = 0, 1.129 + uint32_t width = kDefaultRecordingCanvasSize, 1.130 + uint32_t height = kDefaultRecordingCanvasSize); 1.131 + 1.132 + // called in destructor, but can be called sooner once you know there 1.133 + // should be no more drawing calls made into the recording canvas. 1.134 + void endRecording(); 1.135 + 1.136 + /** 1.137 + * Tells the writer to commit all recorded draw commands to the 1.138 + * controller immediately. 1.139 + * @param detachCurrentBlock Set to true to request that the next draw 1.140 + * command be recorded in a new block. 1.141 + */ 1.142 + void flushRecording(bool detachCurrentBlock); 1.143 + 1.144 + /** 1.145 + * Return the amount of bytes being used for recording. Note that this 1.146 + * does not include the amount of storage written to the stream, which is 1.147 + * controlled by the SkGPipeController. 1.148 + * Currently only returns the amount used for SkBitmaps, since they are 1.149 + * potentially unbounded (if the client is not calling playback). 1.150 + */ 1.151 + size_t storageAllocatedForRecording() const; 1.152 + 1.153 + /** 1.154 + * Attempt to reduce the storage allocated for recording by evicting 1.155 + * cache resources. 1.156 + * @param bytesToFree minimum number of bytes that should be attempted to 1.157 + * be freed. 1.158 + * @return number of bytes actually freed. 1.159 + */ 1.160 + size_t freeMemoryIfPossible(size_t bytesToFree); 1.161 + 1.162 +private: 1.163 + enum { 1.164 + kDefaultRecordingCanvasSize = 32767, 1.165 + }; 1.166 + 1.167 + SkGPipeCanvas* fCanvas; 1.168 + SkWriter32 fWriter; 1.169 +}; 1.170 + 1.171 +#endif