2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef DMSrcSink_DEFINED
9 #define DMSrcSink_DEFINED
11 #include "DMGpuSupport.h"
12 #include "SkBBHFactory.h"
13 #include "SkBBoxHierarchy.h"
18 #include "SkPicture.h"
23 // This is just convenience. It lets you use either return "foo" or return SkStringPrintf(...).
24 struct ImplicitString : public SkString {
26 ImplicitString(const T& s) : SkString(s) {}
28 typedef ImplicitString Name;
29 typedef ImplicitString Path;
33 Error(const SkString& s) : fMsg(s), fFatal(!this->isEmpty()) {}
34 Error(const char* s) : fMsg(s), fFatal(!this->isEmpty()) {}
36 Error(const Error&) = default;
37 Error& operator=(const Error&) = default;
39 static Error Nonfatal(const SkString& s) { return Nonfatal(s.c_str()); }
40 static Error Nonfatal(const char* s) {
46 const char* c_str() const { return fMsg.c_str(); }
47 bool isEmpty() const { return fMsg.isEmpty(); }
48 bool isFatal() const { return fFatal; }
56 // All Srcs must be thread safe.
58 virtual Error SK_WARN_UNUSED_RESULT draw(SkCanvas*) const = 0;
59 virtual SkISize size() const = 0;
60 virtual Name name() const = 0;
65 // You may write to either the bitmap or stream. If you write to log, we'll print that out.
66 virtual Error SK_WARN_UNUSED_RESULT draw(const Src&, SkBitmap*, SkWStream*, SkString* log)
68 // Sinks in the same enclave (except kAnyThread_Enclave) will run serially on the same thread.
69 virtual int enclave() const = 0;
71 // File extension for the content draw() outputs, e.g. "png", "pdf".
72 virtual const char* fileExtension() const = 0;
75 enum { kAnyThread_Enclave, kGPU_Enclave };
76 static const int kNumEnclaves = kGPU_Enclave + 1;
78 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
80 class GMSrc : public Src {
82 explicit GMSrc(skiagm::GMRegistry::Factory);
84 Error draw(SkCanvas*) const SK_OVERRIDE;
85 SkISize size() const SK_OVERRIDE;
86 Name name() const SK_OVERRIDE;
88 skiagm::GMRegistry::Factory fFactory;
91 class CodecSrc : public Src {
99 Error draw(SkCanvas*) const SK_OVERRIDE;
100 SkISize size() const SK_OVERRIDE;
101 Name name() const SK_OVERRIDE;
108 class ImageSrc : public Src {
110 // divisor == 0 means decode the whole image
111 // divisor > 0 means decode in subsets, dividing into a divisor x divisor grid.
112 explicit ImageSrc(Path path, int divisor = 0);
114 Error draw(SkCanvas*) const SK_OVERRIDE;
115 SkISize size() const SK_OVERRIDE;
116 Name name() const SK_OVERRIDE;
122 class SKPSrc : public Src {
124 explicit SKPSrc(Path path);
126 Error draw(SkCanvas*) const SK_OVERRIDE;
127 SkISize size() const SK_OVERRIDE;
128 Name name() const SK_OVERRIDE;
133 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
135 class NullSink : public Sink {
139 Error draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
140 int enclave() const SK_OVERRIDE { return kAnyThread_Enclave; }
141 const char* fileExtension() const SK_OVERRIDE { return ""; }
145 class GPUSink : public Sink {
147 GPUSink(GrContextFactory::GLContextType, GrGLStandard, int samples, bool dfText, bool threaded);
149 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
150 int enclave() const SK_OVERRIDE;
151 const char* fileExtension() const SK_OVERRIDE { return "png"; }
153 GrContextFactory::GLContextType fContextType;
154 GrGLStandard fGpuAPI;
160 class PDFSink : public Sink {
164 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
165 int enclave() const SK_OVERRIDE { return kAnyThread_Enclave; }
166 const char* fileExtension() const SK_OVERRIDE { return "pdf"; }
169 class XPSSink : public Sink {
173 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
174 int enclave() const SK_OVERRIDE { return kAnyThread_Enclave; }
175 const char* fileExtension() const SK_OVERRIDE { return "xps"; }
178 class RasterSink : public Sink {
180 explicit RasterSink(SkColorType);
182 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
183 int enclave() const SK_OVERRIDE { return kAnyThread_Enclave; }
184 const char* fileExtension() const SK_OVERRIDE { return "png"; }
186 SkColorType fColorType;
189 class SKPSink : public Sink {
193 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
194 int enclave() const SK_OVERRIDE { return kAnyThread_Enclave; }
195 const char* fileExtension() const SK_OVERRIDE { return "skp"; }
198 class SVGSink : public Sink {
202 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
203 int enclave() const SK_OVERRIDE { return kAnyThread_Enclave; }
204 const char* fileExtension() const SK_OVERRIDE { return "svg"; }
208 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
210 class ViaMatrix : public Sink {
212 ViaMatrix(SkMatrix, Sink*);
214 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
215 int enclave() const SK_OVERRIDE { return fSink->enclave(); }
216 const char* fileExtension() const SK_OVERRIDE { return fSink->fileExtension(); }
219 SkAutoTDelete<Sink> fSink;
222 class ViaUpright : public Sink {
224 ViaUpright(SkMatrix, Sink*);
226 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
227 int enclave() const SK_OVERRIDE { return fSink->enclave(); }
228 const char* fileExtension() const SK_OVERRIDE { return fSink->fileExtension(); }
231 SkAutoTDelete<Sink> fSink;
234 class ViaPipe : public Sink {
236 explicit ViaPipe(Sink*);
238 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
239 int enclave() const SK_OVERRIDE { return fSink->enclave(); }
240 const char* fileExtension() const SK_OVERRIDE { return fSink->fileExtension(); }
242 SkAutoTDelete<Sink> fSink;
245 class ViaSerialization : public Sink {
247 explicit ViaSerialization(Sink*);
249 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
250 int enclave() const SK_OVERRIDE { return fSink->enclave(); }
251 const char* fileExtension() const SK_OVERRIDE { return fSink->fileExtension(); }
253 SkAutoTDelete<Sink> fSink;
256 class ViaTiles : public Sink {
258 ViaTiles(int w, int h, SkBBHFactory*, Sink*);
260 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const SK_OVERRIDE;
261 int enclave() const SK_OVERRIDE { return fSink->enclave(); }
262 const char* fileExtension() const SK_OVERRIDE { return fSink->fileExtension(); }
265 SkAutoTDelete<SkBBHFactory> fFactory;
266 SkAutoTDelete<Sink> fSink;
271 #endif//DMSrcSink_DEFINED