blob: 2c21e87ef60c017926fa4dc841667d2f1d94588e [file] [log] [blame]
scroggo478652e2015-03-25 07:11:02 -07001/*
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
mtklein748ca3b2015-01-15 10:56:12 -08008#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 "SkData.h"
17#include "SkGPipe.h"
18#include "SkPicture.h"
mtklein748ca3b2015-01-15 10:56:12 -080019#include "gm.h"
20
21namespace DM {
22
23// This is just convenience. It lets you use either return "foo" or return SkStringPrintf(...).
24struct ImplicitString : public SkString {
25 template <typename T>
26 ImplicitString(const T& s) : SkString(s) {}
27};
mtklein748ca3b2015-01-15 10:56:12 -080028typedef ImplicitString Name;
mtklein8d17a132015-01-30 11:42:31 -080029typedef ImplicitString Path;
mtklein748ca3b2015-01-15 10:56:12 -080030
mtklein4089ef72015-03-05 08:40:28 -080031class Error {
32public:
33 Error(const SkString& s) : fMsg(s), fFatal(!this->isEmpty()) {}
34 Error(const char* s) : fMsg(s), fFatal(!this->isEmpty()) {}
35
36 Error(const Error&) = default;
37 Error& operator=(const Error&) = default;
38
39 static Error Nonfatal(const SkString& s) { return Nonfatal(s.c_str()); }
40 static Error Nonfatal(const char* s) {
41 Error e(s);
42 e.fFatal = false;
43 return e;
44 }
45
46 const char* c_str() const { return fMsg.c_str(); }
47 bool isEmpty() const { return fMsg.isEmpty(); }
48 bool isFatal() const { return fFatal; }
49
50private:
51 SkString fMsg;
52 bool fFatal;
53};
54
mtklein748ca3b2015-01-15 10:56:12 -080055struct Src {
56 // All Srcs must be thread safe.
57 virtual ~Src() {}
58 virtual Error SK_WARN_UNUSED_RESULT draw(SkCanvas*) const = 0;
59 virtual SkISize size() const = 0;
60 virtual Name name() const = 0;
61};
62
63struct Sink {
64 virtual ~Sink() {}
mtkleinb9eb4ac2015-02-02 18:26:03 -080065 // 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)
67 const = 0;
mtklein748ca3b2015-01-15 10:56:12 -080068 // Sinks in the same enclave (except kAnyThread_Enclave) will run serially on the same thread.
69 virtual int enclave() const = 0;
70
71 // File extension for the content draw() outputs, e.g. "png", "pdf".
72 virtual const char* fileExtension() const = 0;
73};
74
halcanary792c80f2015-02-20 07:21:05 -080075enum { kAnyThread_Enclave, kGPU_Enclave };
76static const int kNumEnclaves = kGPU_Enclave + 1;
mtklein748ca3b2015-01-15 10:56:12 -080077
78/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
79
mtklein748ca3b2015-01-15 10:56:12 -080080class GMSrc : public Src {
81public:
82 explicit GMSrc(skiagm::GMRegistry::Factory);
83
mtklein36352bf2015-03-25 18:17:31 -070084 Error draw(SkCanvas*) const override;
85 SkISize size() const override;
86 Name name() const override;
mtklein748ca3b2015-01-15 10:56:12 -080087private:
88 skiagm::GMRegistry::Factory fFactory;
89};
90
scroggo9b77ddd2015-03-19 06:03:39 -070091class CodecSrc : public Src {
92public:
scroggo9c59ebc2015-03-25 13:48:49 -070093 enum Mode {
94 kNormal_Mode,
95 kScanline_Mode,
96 };
97 CodecSrc(Path, Mode);
scroggo9b77ddd2015-03-19 06:03:39 -070098
mtklein36352bf2015-03-25 18:17:31 -070099 Error draw(SkCanvas*) const override;
100 SkISize size() const override;
101 Name name() const override;
scroggo9b77ddd2015-03-19 06:03:39 -0700102private:
103 Path fPath;
scroggo9c59ebc2015-03-25 13:48:49 -0700104 Mode fMode;
scroggo9b77ddd2015-03-19 06:03:39 -0700105};
106
107
mtklein748ca3b2015-01-15 10:56:12 -0800108class ImageSrc : public Src {
109public:
mtkleinedc93bc2015-01-30 13:22:23 -0800110 // 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);
mtklein748ca3b2015-01-15 10:56:12 -0800113
mtklein36352bf2015-03-25 18:17:31 -0700114 Error draw(SkCanvas*) const override;
115 SkISize size() const override;
116 Name name() const override;
mtklein748ca3b2015-01-15 10:56:12 -0800117private:
mtklein8d17a132015-01-30 11:42:31 -0800118 Path fPath;
mtkleinedc93bc2015-01-30 13:22:23 -0800119 const int fDivisor;
mtklein748ca3b2015-01-15 10:56:12 -0800120};
121
122class SKPSrc : public Src {
123public:
mtklein8d17a132015-01-30 11:42:31 -0800124 explicit SKPSrc(Path path);
mtklein748ca3b2015-01-15 10:56:12 -0800125
mtklein36352bf2015-03-25 18:17:31 -0700126 Error draw(SkCanvas*) const override;
127 SkISize size() const override;
128 Name name() const override;
mtklein748ca3b2015-01-15 10:56:12 -0800129private:
mtklein8d17a132015-01-30 11:42:31 -0800130 Path fPath;
mtklein748ca3b2015-01-15 10:56:12 -0800131};
132
133/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
134
mtkleinad66f9b2015-02-13 15:11:10 -0800135class NullSink : public Sink {
136public:
137 NullSink() {}
138
mtklein36352bf2015-03-25 18:17:31 -0700139 Error draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const override;
140 int enclave() const override { return kAnyThread_Enclave; }
141 const char* fileExtension() const override { return ""; }
mtkleinad66f9b2015-02-13 15:11:10 -0800142};
143
144
mtklein748ca3b2015-01-15 10:56:12 -0800145class GPUSink : public Sink {
146public:
mtklein82d28432015-01-15 12:46:02 -0800147 GPUSink(GrContextFactory::GLContextType, GrGLStandard, int samples, bool dfText, bool threaded);
mtklein748ca3b2015-01-15 10:56:12 -0800148
mtklein36352bf2015-03-25 18:17:31 -0700149 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
150 int enclave() const override;
151 const char* fileExtension() const override { return "png"; }
mtklein748ca3b2015-01-15 10:56:12 -0800152private:
153 GrContextFactory::GLContextType fContextType;
154 GrGLStandard fGpuAPI;
155 int fSampleCount;
156 bool fUseDFText;
mtklein82d28432015-01-15 12:46:02 -0800157 bool fThreaded;
mtklein748ca3b2015-01-15 10:56:12 -0800158};
159
160class PDFSink : public Sink {
161public:
162 PDFSink();
163
mtklein36352bf2015-03-25 18:17:31 -0700164 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
165 int enclave() const override { return kAnyThread_Enclave; }
166 const char* fileExtension() const override { return "pdf"; }
mtklein748ca3b2015-01-15 10:56:12 -0800167};
168
halcanary47ef4d52015-03-03 09:13:09 -0800169class XPSSink : public Sink {
170public:
171 XPSSink();
172
mtklein36352bf2015-03-25 18:17:31 -0700173 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
174 int enclave() const override { return kAnyThread_Enclave; }
175 const char* fileExtension() const override { return "xps"; }
halcanary47ef4d52015-03-03 09:13:09 -0800176};
177
mtklein748ca3b2015-01-15 10:56:12 -0800178class RasterSink : public Sink {
179public:
180 explicit RasterSink(SkColorType);
181
mtklein36352bf2015-03-25 18:17:31 -0700182 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
183 int enclave() const override { return kAnyThread_Enclave; }
184 const char* fileExtension() const override { return "png"; }
mtklein748ca3b2015-01-15 10:56:12 -0800185private:
186 SkColorType fColorType;
187};
188
mtklein9c3f17d2015-01-28 11:35:18 -0800189class SKPSink : public Sink {
190public:
191 SKPSink();
192
mtklein36352bf2015-03-25 18:17:31 -0700193 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
194 int enclave() const override { return kAnyThread_Enclave; }
195 const char* fileExtension() const override { return "skp"; }
mtklein9c3f17d2015-01-28 11:35:18 -0800196};
197
mtklein8a4527e2015-01-31 20:00:58 -0800198class SVGSink : public Sink {
199public:
200 SVGSink();
201
mtklein36352bf2015-03-25 18:17:31 -0700202 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
203 int enclave() const override { return kAnyThread_Enclave; }
204 const char* fileExtension() const override { return "svg"; }
mtklein8a4527e2015-01-31 20:00:58 -0800205};
206
207
mtklein748ca3b2015-01-15 10:56:12 -0800208/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
209
210class ViaMatrix : public Sink {
211public:
212 ViaMatrix(SkMatrix, Sink*);
213
mtklein36352bf2015-03-25 18:17:31 -0700214 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
215 int enclave() const override { return fSink->enclave(); }
216 const char* fileExtension() const override { return fSink->fileExtension(); }
mtklein748ca3b2015-01-15 10:56:12 -0800217private:
218 SkMatrix fMatrix;
219 SkAutoTDelete<Sink> fSink;
220};
221
mtkleind603b222015-02-17 11:13:33 -0800222class ViaUpright : public Sink {
223public:
224 ViaUpright(SkMatrix, Sink*);
225
mtklein36352bf2015-03-25 18:17:31 -0700226 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
227 int enclave() const override { return fSink->enclave(); }
228 const char* fileExtension() const override { return fSink->fileExtension(); }
mtkleind603b222015-02-17 11:13:33 -0800229private:
230 SkMatrix fMatrix;
231 SkAutoTDelete<Sink> fSink;
232};
233
mtklein748ca3b2015-01-15 10:56:12 -0800234class ViaPipe : public Sink {
235public:
mtklein7edca212015-01-21 13:18:51 -0800236 explicit ViaPipe(Sink*);
mtklein748ca3b2015-01-15 10:56:12 -0800237
mtklein36352bf2015-03-25 18:17:31 -0700238 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
239 int enclave() const override { return fSink->enclave(); }
240 const char* fileExtension() const override { return fSink->fileExtension(); }
mtklein748ca3b2015-01-15 10:56:12 -0800241private:
mtklein748ca3b2015-01-15 10:56:12 -0800242 SkAutoTDelete<Sink> fSink;
243};
244
245class ViaSerialization : public Sink {
246public:
247 explicit ViaSerialization(Sink*);
248
mtklein36352bf2015-03-25 18:17:31 -0700249 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
250 int enclave() const override { return fSink->enclave(); }
251 const char* fileExtension() const override { return fSink->fileExtension(); }
mtklein748ca3b2015-01-15 10:56:12 -0800252private:
253 SkAutoTDelete<Sink> fSink;
254};
255
256class ViaTiles : public Sink {
257public:
258 ViaTiles(int w, int h, SkBBHFactory*, Sink*);
259
mtklein36352bf2015-03-25 18:17:31 -0700260 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
261 int enclave() const override { return fSink->enclave(); }
262 const char* fileExtension() const override { return fSink->fileExtension(); }
mtklein748ca3b2015-01-15 10:56:12 -0800263private:
264 const int fW, fH;
265 SkAutoTDelete<SkBBHFactory> fFactory;
266 SkAutoTDelete<Sink> fSink;
267};
268
mtklein748ca3b2015-01-15 10:56:12 -0800269} // namespace DM
270
271#endif//DMSrcSink_DEFINED