1 /* 2 * Copyright 2015 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 #ifndef DMSrcSink_DEFINED 9 #define DMSrcSink_DEFINED 10 11 #include "DMGpuSupport.h" 12 #include "SkBBHFactory.h" 13 #include "SkBBoxHierarchy.h" 14 #include "SkBitmap.h" 15 #include "SkCanvas.h" 16 #include "SkCodec.h" 17 #include "SkData.h" 18 #include "SkGPipe.h" 19 #include "SkPicture.h" 20 #include "gm.h" 21 22 namespace DM { 23 24 // This is just convenience. It lets you use either return "foo" or return SkStringPrintf(...). 25 struct ImplicitString : public SkString { 26 template <typename T> ImplicitStringImplicitString27 ImplicitString(const T& s) : SkString(s) {} 28 }; 29 typedef ImplicitString Name; 30 typedef ImplicitString Path; 31 32 class Error { 33 public: Error(const SkString & s)34 Error(const SkString& s) : fMsg(s), fFatal(!this->isEmpty()) {} Error(const char * s)35 Error(const char* s) : fMsg(s), fFatal(!this->isEmpty()) {} 36 37 Error(const Error&) = default; 38 Error& operator=(const Error&) = default; 39 Nonfatal(const SkString & s)40 static Error Nonfatal(const SkString& s) { return Nonfatal(s.c_str()); } Nonfatal(const char * s)41 static Error Nonfatal(const char* s) { 42 Error e(s); 43 e.fFatal = false; 44 return e; 45 } 46 c_str()47 const char* c_str() const { return fMsg.c_str(); } isEmpty()48 bool isEmpty() const { return fMsg.isEmpty(); } isFatal()49 bool isFatal() const { return fFatal; } 50 51 private: 52 SkString fMsg; 53 bool fFatal; 54 }; 55 56 struct Src { 57 // All Srcs must be thread safe. ~SrcSrc58 virtual ~Src() {} 59 virtual Error SK_WARN_UNUSED_RESULT draw(SkCanvas*) const = 0; 60 virtual SkISize size() const = 0; 61 virtual Name name() const = 0; 62 }; 63 64 struct Sink { ~SinkSink65 virtual ~Sink() {} 66 // You may write to either the bitmap or stream. If you write to log, we'll print that out. 67 virtual Error SK_WARN_UNUSED_RESULT draw(const Src&, SkBitmap*, SkWStream*, SkString* log) 68 const = 0; 69 // Sinks in the same enclave (except kAnyThread_Enclave) will run serially on the same thread. 70 virtual int enclave() const = 0; 71 72 // File extension for the content draw() outputs, e.g. "png", "pdf". 73 virtual const char* fileExtension() const = 0; 74 }; 75 76 enum { kAnyThread_Enclave, kGPU_Enclave }; 77 static const int kNumEnclaves = kGPU_Enclave + 1; 78 79 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 80 81 class GMSrc : public Src { 82 public: 83 explicit GMSrc(skiagm::GMRegistry::Factory); 84 85 Error draw(SkCanvas*) const override; 86 SkISize size() const override; 87 Name name() const override; 88 private: 89 skiagm::GMRegistry::Factory fFactory; 90 }; 91 92 class CodecSrc : public Src { 93 public: 94 enum Mode { 95 kNormal_Mode, 96 kScanline_Mode, 97 }; 98 enum DstColorType { 99 kGetFromCanvas_DstColorType, 100 kIndex8_Always_DstColorType, 101 kGrayscale_Always_DstColorType, 102 }; 103 CodecSrc(Path, Mode, DstColorType); 104 105 Error draw(SkCanvas*) const override; 106 SkISize size() const override; 107 Name name() const override; 108 private: 109 Path fPath; 110 Mode fMode; 111 DstColorType fDstColorType; 112 }; 113 114 115 class ImageSrc : public Src { 116 public: 117 // divisor == 0 means decode the whole image 118 // divisor > 0 means decode in subsets, dividing into a divisor x divisor grid. 119 explicit ImageSrc(Path path, int divisor = 0); 120 121 Error draw(SkCanvas*) const override; 122 SkISize size() const override; 123 Name name() const override; 124 private: 125 Path fPath; 126 const int fDivisor; 127 }; 128 129 class SKPSrc : public Src { 130 public: 131 explicit SKPSrc(Path path); 132 133 Error draw(SkCanvas*) const override; 134 SkISize size() const override; 135 Name name() const override; 136 private: 137 Path fPath; 138 }; 139 140 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 141 142 class NullSink : public Sink { 143 public: NullSink()144 NullSink() {} 145 146 Error draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const override; enclave()147 int enclave() const override { return kAnyThread_Enclave; } fileExtension()148 const char* fileExtension() const override { return ""; } 149 }; 150 151 152 class GPUSink : public Sink { 153 public: 154 GPUSink(GrContextFactory::GLContextType, GrGLStandard, int samples, bool dfText, bool threaded); 155 156 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; 157 int enclave() const override; fileExtension()158 const char* fileExtension() const override { return "png"; } 159 private: 160 GrContextFactory::GLContextType fContextType; 161 GrGLStandard fGpuAPI; 162 int fSampleCount; 163 bool fUseDFText; 164 bool fThreaded; 165 }; 166 167 class PDFSink : public Sink { 168 public: 169 PDFSink(); 170 171 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; enclave()172 int enclave() const override { return kAnyThread_Enclave; } fileExtension()173 const char* fileExtension() const override { return "pdf"; } 174 }; 175 176 class XPSSink : public Sink { 177 public: 178 XPSSink(); 179 180 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; enclave()181 int enclave() const override { return kAnyThread_Enclave; } fileExtension()182 const char* fileExtension() const override { return "xps"; } 183 }; 184 185 class RasterSink : public Sink { 186 public: 187 explicit RasterSink(SkColorType); 188 189 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; enclave()190 int enclave() const override { return kAnyThread_Enclave; } fileExtension()191 const char* fileExtension() const override { return "png"; } 192 private: 193 SkColorType fColorType; 194 }; 195 196 class SKPSink : public Sink { 197 public: 198 SKPSink(); 199 200 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; enclave()201 int enclave() const override { return kAnyThread_Enclave; } fileExtension()202 const char* fileExtension() const override { return "skp"; } 203 }; 204 205 class SVGSink : public Sink { 206 public: 207 SVGSink(); 208 209 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; enclave()210 int enclave() const override { return kAnyThread_Enclave; } fileExtension()211 const char* fileExtension() const override { return "svg"; } 212 }; 213 214 215 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 216 217 class Via : public Sink { 218 public: Via(Sink * sink)219 explicit Via(Sink* sink) : fSink(sink) {} fileExtension()220 const char* fileExtension() const override { return fSink->fileExtension(); } enclave()221 int enclave() const override { return fSink->enclave(); } 222 protected: 223 SkAutoTDelete<Sink> fSink; 224 }; 225 226 class ViaMatrix : public Via { 227 public: 228 ViaMatrix(SkMatrix, Sink*); 229 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; 230 private: 231 const SkMatrix fMatrix; 232 }; 233 234 class ViaUpright : public Via { 235 public: 236 ViaUpright(SkMatrix, Sink*); 237 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; 238 private: 239 const SkMatrix fMatrix; 240 }; 241 242 class ViaPipe : public Via { 243 public: ViaPipe(Sink * sink)244 explicit ViaPipe(Sink* sink) : Via(sink) {} 245 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; 246 }; 247 248 class ViaDeferred : public Via { 249 public: ViaDeferred(Sink * sink)250 explicit ViaDeferred(Sink* sink) : Via(sink) {} 251 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; 252 }; 253 254 class ViaSerialization : public Via { 255 public: ViaSerialization(Sink * sink)256 explicit ViaSerialization(Sink* sink) : Via(sink) {} 257 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; 258 }; 259 260 class ViaTiles : public Via { 261 public: 262 ViaTiles(int w, int h, SkBBHFactory*, Sink*); 263 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; 264 private: 265 const int fW, fH; 266 SkAutoTDelete<SkBBHFactory> fFactory; 267 }; 268 269 class ViaSecondPicture : public Via { 270 public: ViaSecondPicture(Sink * sink)271 explicit ViaSecondPicture(Sink* sink) : Via(sink) {} 272 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; 273 }; 274 275 class ViaSingletonPictures : public Via { 276 public: ViaSingletonPictures(Sink * sink)277 explicit ViaSingletonPictures(Sink* sink) : Via(sink) {} 278 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; 279 }; 280 281 class ViaTwice : public Via { 282 public: ViaTwice(Sink * sink)283 explicit ViaTwice(Sink* sink) : Via(sink) {} 284 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; 285 }; 286 287 } // namespace DM 288 289 #endif//DMSrcSink_DEFINED 290