blob: b7c28edf006f17e1c32504364c7d2456fff8d97a [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"
msarett438b2ad2015-04-09 12:43:10 -070016#include "SkCodec.h"
mtklein748ca3b2015-01-15 10:56:12 -080017#include "SkData.h"
18#include "SkGPipe.h"
19#include "SkPicture.h"
mtklein748ca3b2015-01-15 10:56:12 -080020#include "gm.h"
21
22namespace DM {
23
24// This is just convenience. It lets you use either return "foo" or return SkStringPrintf(...).
25struct ImplicitString : public SkString {
26 template <typename T>
27 ImplicitString(const T& s) : SkString(s) {}
28};
mtklein748ca3b2015-01-15 10:56:12 -080029typedef ImplicitString Name;
mtklein8d17a132015-01-30 11:42:31 -080030typedef ImplicitString Path;
mtklein748ca3b2015-01-15 10:56:12 -080031
mtklein4089ef72015-03-05 08:40:28 -080032class Error {
33public:
34 Error(const SkString& s) : fMsg(s), fFatal(!this->isEmpty()) {}
35 Error(const char* s) : fMsg(s), fFatal(!this->isEmpty()) {}
36
37 Error(const Error&) = default;
38 Error& operator=(const Error&) = default;
39
40 static Error Nonfatal(const SkString& s) { return Nonfatal(s.c_str()); }
41 static Error Nonfatal(const char* s) {
42 Error e(s);
43 e.fFatal = false;
44 return e;
45 }
46
47 const char* c_str() const { return fMsg.c_str(); }
48 bool isEmpty() const { return fMsg.isEmpty(); }
49 bool isFatal() const { return fFatal; }
50
51private:
52 SkString fMsg;
53 bool fFatal;
54};
55
mtklein748ca3b2015-01-15 10:56:12 -080056struct Src {
57 // All Srcs must be thread safe.
58 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;
bsalomon4ee6bd82015-05-27 13:23:23 -070062 virtual void modifyGrContextOptions(GrContextOptions* options) const {}
mtklein748ca3b2015-01-15 10:56:12 -080063};
64
65struct Sink {
66 virtual ~Sink() {}
mtkleinb9eb4ac2015-02-02 18:26:03 -080067 // You may write to either the bitmap or stream. If you write to log, we'll print that out.
68 virtual Error SK_WARN_UNUSED_RESULT draw(const Src&, SkBitmap*, SkWStream*, SkString* log)
69 const = 0;
mtklein748ca3b2015-01-15 10:56:12 -080070 // Sinks in the same enclave (except kAnyThread_Enclave) will run serially on the same thread.
71 virtual int enclave() const = 0;
72
73 // File extension for the content draw() outputs, e.g. "png", "pdf".
74 virtual const char* fileExtension() const = 0;
75};
76
halcanary792c80f2015-02-20 07:21:05 -080077enum { kAnyThread_Enclave, kGPU_Enclave };
78static const int kNumEnclaves = kGPU_Enclave + 1;
mtklein748ca3b2015-01-15 10:56:12 -080079
80/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
81
mtklein748ca3b2015-01-15 10:56:12 -080082class GMSrc : public Src {
83public:
84 explicit GMSrc(skiagm::GMRegistry::Factory);
85
mtklein36352bf2015-03-25 18:17:31 -070086 Error draw(SkCanvas*) const override;
87 SkISize size() const override;
88 Name name() const override;
bsalomon4ee6bd82015-05-27 13:23:23 -070089 void modifyGrContextOptions(GrContextOptions* options) const override;
90
mtklein748ca3b2015-01-15 10:56:12 -080091private:
92 skiagm::GMRegistry::Factory fFactory;
93};
94
scroggo9b77ddd2015-03-19 06:03:39 -070095class CodecSrc : public Src {
96public:
scroggo9c59ebc2015-03-25 13:48:49 -070097 enum Mode {
98 kNormal_Mode,
99 kScanline_Mode,
emmaleer97002062015-05-27 12:36:10 -0700100 kScanline_Subset_Mode,
msarett0a242972015-06-11 14:27:27 -0700101 kStripe_Mode, // Tests the skipping of scanlines
scroggo9c59ebc2015-03-25 13:48:49 -0700102 };
msarett438b2ad2015-04-09 12:43:10 -0700103 enum DstColorType {
104 kGetFromCanvas_DstColorType,
105 kIndex8_Always_DstColorType,
106 kGrayscale_Always_DstColorType,
107 };
msarett0a242972015-06-11 14:27:27 -0700108 CodecSrc(Path, Mode, DstColorType, float);
scroggo9b77ddd2015-03-19 06:03:39 -0700109
mtklein36352bf2015-03-25 18:17:31 -0700110 Error draw(SkCanvas*) const override;
111 SkISize size() const override;
112 Name name() const override;
scroggo9b77ddd2015-03-19 06:03:39 -0700113private:
msarett438b2ad2015-04-09 12:43:10 -0700114 Path fPath;
115 Mode fMode;
116 DstColorType fDstColorType;
msarett0a242972015-06-11 14:27:27 -0700117 float fScale;
scroggo9b77ddd2015-03-19 06:03:39 -0700118};
119
120
mtklein748ca3b2015-01-15 10:56:12 -0800121class ImageSrc : public Src {
122public:
mtkleinedc93bc2015-01-30 13:22:23 -0800123 // divisor == 0 means decode the whole image
124 // divisor > 0 means decode in subsets, dividing into a divisor x divisor grid.
125 explicit ImageSrc(Path path, int divisor = 0);
mtklein748ca3b2015-01-15 10:56:12 -0800126
mtklein36352bf2015-03-25 18:17:31 -0700127 Error draw(SkCanvas*) const override;
128 SkISize size() const override;
129 Name name() const override;
mtklein748ca3b2015-01-15 10:56:12 -0800130private:
mtklein8d17a132015-01-30 11:42:31 -0800131 Path fPath;
mtkleinedc93bc2015-01-30 13:22:23 -0800132 const int fDivisor;
mtklein748ca3b2015-01-15 10:56:12 -0800133};
134
135class SKPSrc : public Src {
136public:
mtklein8d17a132015-01-30 11:42:31 -0800137 explicit SKPSrc(Path path);
mtklein748ca3b2015-01-15 10:56:12 -0800138
mtklein36352bf2015-03-25 18:17:31 -0700139 Error draw(SkCanvas*) const override;
140 SkISize size() const override;
141 Name name() const override;
mtklein748ca3b2015-01-15 10:56:12 -0800142private:
mtklein8d17a132015-01-30 11:42:31 -0800143 Path fPath;
mtklein748ca3b2015-01-15 10:56:12 -0800144};
145
146/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
147
mtkleinad66f9b2015-02-13 15:11:10 -0800148class NullSink : public Sink {
149public:
150 NullSink() {}
151
mtklein36352bf2015-03-25 18:17:31 -0700152 Error draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const override;
153 int enclave() const override { return kAnyThread_Enclave; }
154 const char* fileExtension() const override { return ""; }
mtkleinad66f9b2015-02-13 15:11:10 -0800155};
156
157
mtklein748ca3b2015-01-15 10:56:12 -0800158class GPUSink : public Sink {
159public:
mtklein82d28432015-01-15 12:46:02 -0800160 GPUSink(GrContextFactory::GLContextType, GrGLStandard, int samples, bool dfText, bool threaded);
mtklein748ca3b2015-01-15 10:56:12 -0800161
mtklein36352bf2015-03-25 18:17:31 -0700162 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
163 int enclave() const override;
164 const char* fileExtension() const override { return "png"; }
mtklein748ca3b2015-01-15 10:56:12 -0800165private:
166 GrContextFactory::GLContextType fContextType;
167 GrGLStandard fGpuAPI;
168 int fSampleCount;
169 bool fUseDFText;
mtklein82d28432015-01-15 12:46:02 -0800170 bool fThreaded;
mtklein748ca3b2015-01-15 10:56:12 -0800171};
172
173class PDFSink : public Sink {
174public:
175 PDFSink();
176
mtklein36352bf2015-03-25 18:17:31 -0700177 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
178 int enclave() const override { return kAnyThread_Enclave; }
179 const char* fileExtension() const override { return "pdf"; }
mtklein748ca3b2015-01-15 10:56:12 -0800180};
181
halcanary47ef4d52015-03-03 09:13:09 -0800182class XPSSink : public Sink {
183public:
184 XPSSink();
185
mtklein36352bf2015-03-25 18:17:31 -0700186 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
187 int enclave() const override { return kAnyThread_Enclave; }
188 const char* fileExtension() const override { return "xps"; }
halcanary47ef4d52015-03-03 09:13:09 -0800189};
190
mtklein748ca3b2015-01-15 10:56:12 -0800191class RasterSink : public Sink {
192public:
193 explicit RasterSink(SkColorType);
194
mtklein36352bf2015-03-25 18:17:31 -0700195 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
196 int enclave() const override { return kAnyThread_Enclave; }
197 const char* fileExtension() const override { return "png"; }
mtklein748ca3b2015-01-15 10:56:12 -0800198private:
199 SkColorType fColorType;
200};
201
mtklein9c3f17d2015-01-28 11:35:18 -0800202class SKPSink : public Sink {
203public:
204 SKPSink();
205
mtklein36352bf2015-03-25 18:17:31 -0700206 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
207 int enclave() const override { return kAnyThread_Enclave; }
208 const char* fileExtension() const override { return "skp"; }
mtklein9c3f17d2015-01-28 11:35:18 -0800209};
210
mtklein8a4527e2015-01-31 20:00:58 -0800211class SVGSink : public Sink {
212public:
213 SVGSink();
214
mtklein36352bf2015-03-25 18:17:31 -0700215 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
216 int enclave() const override { return kAnyThread_Enclave; }
217 const char* fileExtension() const override { return "svg"; }
mtklein8a4527e2015-01-31 20:00:58 -0800218};
219
220
mtklein748ca3b2015-01-15 10:56:12 -0800221/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
222
mtklein78829242015-05-06 07:54:07 -0700223class Via : public Sink {
224public:
225 explicit Via(Sink* sink) : fSink(sink) {}
226 const char* fileExtension() const override { return fSink->fileExtension(); }
227 int enclave() const override { return fSink->enclave(); }
228protected:
229 SkAutoTDelete<Sink> fSink;
230};
231
232class ViaMatrix : public Via {
mtklein748ca3b2015-01-15 10:56:12 -0800233public:
234 ViaMatrix(SkMatrix, Sink*);
mtklein36352bf2015-03-25 18:17:31 -0700235 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800236private:
mtklein78829242015-05-06 07:54:07 -0700237 const SkMatrix fMatrix;
mtklein748ca3b2015-01-15 10:56:12 -0800238};
239
mtklein78829242015-05-06 07:54:07 -0700240class ViaUpright : public Via {
mtkleind603b222015-02-17 11:13:33 -0800241public:
242 ViaUpright(SkMatrix, Sink*);
mtklein36352bf2015-03-25 18:17:31 -0700243 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtkleind603b222015-02-17 11:13:33 -0800244private:
mtklein78829242015-05-06 07:54:07 -0700245 const SkMatrix fMatrix;
mtkleind603b222015-02-17 11:13:33 -0800246};
247
mtklein78829242015-05-06 07:54:07 -0700248class ViaPipe : public Via {
mtklein748ca3b2015-01-15 10:56:12 -0800249public:
mtklein78829242015-05-06 07:54:07 -0700250 explicit ViaPipe(Sink* sink) : Via(sink) {}
mtklein36352bf2015-03-25 18:17:31 -0700251 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800252};
253
mtklein78829242015-05-06 07:54:07 -0700254class ViaDeferred : public Via {
reed06a22f62015-05-05 08:11:33 -0700255public:
mtklein78829242015-05-06 07:54:07 -0700256 explicit ViaDeferred(Sink* sink) : Via(sink) {}
reed06a22f62015-05-05 08:11:33 -0700257 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
reed06a22f62015-05-05 08:11:33 -0700258};
259
mtklein78829242015-05-06 07:54:07 -0700260class ViaSerialization : public Via {
mtklein748ca3b2015-01-15 10:56:12 -0800261public:
mtklein78829242015-05-06 07:54:07 -0700262 explicit ViaSerialization(Sink* sink) : Via(sink) {}
mtklein36352bf2015-03-25 18:17:31 -0700263 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800264};
265
mtklein78829242015-05-06 07:54:07 -0700266class ViaTiles : public Via {
mtklein748ca3b2015-01-15 10:56:12 -0800267public:
268 ViaTiles(int w, int h, SkBBHFactory*, Sink*);
mtklein36352bf2015-03-25 18:17:31 -0700269 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800270private:
271 const int fW, fH;
272 SkAutoTDelete<SkBBHFactory> fFactory;
mtklein748ca3b2015-01-15 10:56:12 -0800273};
274
mtklein78829242015-05-06 07:54:07 -0700275class ViaSecondPicture : public Via {
mtkleinb7e8d692015-04-07 08:30:32 -0700276public:
mtklein78829242015-05-06 07:54:07 -0700277 explicit ViaSecondPicture(Sink* sink) : Via(sink) {}
mtkleinb7e8d692015-04-07 08:30:32 -0700278 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtkleinb7e8d692015-04-07 08:30:32 -0700279};
280
mtklein78829242015-05-06 07:54:07 -0700281class ViaSingletonPictures : public Via {
mtkleind31c13d2015-05-05 12:59:56 -0700282public:
mtklein78829242015-05-06 07:54:07 -0700283 explicit ViaSingletonPictures(Sink* sink) : Via(sink) {}
mtkleind31c13d2015-05-05 12:59:56 -0700284 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtkleind31c13d2015-05-05 12:59:56 -0700285};
286
mtklein6fbf4b32015-05-06 11:35:40 -0700287class ViaTwice : public Via {
288public:
289 explicit ViaTwice(Sink* sink) : Via(sink) {}
290 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
291};
292
mtklein748ca3b2015-01-15 10:56:12 -0800293} // namespace DM
294
295#endif//DMSrcSink_DEFINED