blob: 3b180eacf591625fe0c780fe680ca655827edd25 [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
mtkleine0effd62015-07-29 06:37:28 -070056enum SinkType { kGPU_SinkType, kVector_SinkType, kRaster_SinkType };
57
mtklein748ca3b2015-01-15 10:56:12 -080058struct Src {
59 // All Srcs must be thread safe.
60 virtual ~Src() {}
61 virtual Error SK_WARN_UNUSED_RESULT draw(SkCanvas*) const = 0;
62 virtual SkISize size() const = 0;
63 virtual Name name() const = 0;
bsalomon4ee6bd82015-05-27 13:23:23 -070064 virtual void modifyGrContextOptions(GrContextOptions* options) const {}
mtkleine0effd62015-07-29 06:37:28 -070065 virtual bool veto(SinkType) const { return false; }
mtklein748ca3b2015-01-15 10:56:12 -080066};
67
68struct Sink {
69 virtual ~Sink() {}
mtkleinb9eb4ac2015-02-02 18:26:03 -080070 // You may write to either the bitmap or stream. If you write to log, we'll print that out.
71 virtual Error SK_WARN_UNUSED_RESULT draw(const Src&, SkBitmap*, SkWStream*, SkString* log)
72 const = 0;
mtklein748ca3b2015-01-15 10:56:12 -080073 // Sinks in the same enclave (except kAnyThread_Enclave) will run serially on the same thread.
74 virtual int enclave() const = 0;
75
76 // File extension for the content draw() outputs, e.g. "png", "pdf".
77 virtual const char* fileExtension() const = 0;
78};
79
halcanary792c80f2015-02-20 07:21:05 -080080enum { kAnyThread_Enclave, kGPU_Enclave };
81static const int kNumEnclaves = kGPU_Enclave + 1;
mtklein748ca3b2015-01-15 10:56:12 -080082
83/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
84
mtklein748ca3b2015-01-15 10:56:12 -080085class GMSrc : public Src {
86public:
87 explicit GMSrc(skiagm::GMRegistry::Factory);
88
mtklein36352bf2015-03-25 18:17:31 -070089 Error draw(SkCanvas*) const override;
90 SkISize size() const override;
91 Name name() const override;
bsalomon4ee6bd82015-05-27 13:23:23 -070092 void modifyGrContextOptions(GrContextOptions* options) const override;
93
mtklein748ca3b2015-01-15 10:56:12 -080094private:
95 skiagm::GMRegistry::Factory fFactory;
96};
97
scroggo9b77ddd2015-03-19 06:03:39 -070098class CodecSrc : public Src {
99public:
scroggo9c59ebc2015-03-25 13:48:49 -0700100 enum Mode {
101 kNormal_Mode,
102 kScanline_Mode,
emmaleer97002062015-05-27 12:36:10 -0700103 kScanline_Subset_Mode,
msarett0a242972015-06-11 14:27:27 -0700104 kStripe_Mode, // Tests the skipping of scanlines
scroggob636b452015-07-22 07:16:20 -0700105 kSubset_Mode, // For codecs that support subsets directly.
scroggo9c59ebc2015-03-25 13:48:49 -0700106 };
msarett438b2ad2015-04-09 12:43:10 -0700107 enum DstColorType {
108 kGetFromCanvas_DstColorType,
109 kIndex8_Always_DstColorType,
110 kGrayscale_Always_DstColorType,
111 };
msarett0a242972015-06-11 14:27:27 -0700112 CodecSrc(Path, Mode, DstColorType, float);
scroggo9b77ddd2015-03-19 06:03:39 -0700113
mtklein36352bf2015-03-25 18:17:31 -0700114 Error draw(SkCanvas*) const override;
115 SkISize size() const override;
116 Name name() const override;
mtkleine0effd62015-07-29 06:37:28 -0700117 bool veto(SinkType) const override;
scroggo9b77ddd2015-03-19 06:03:39 -0700118private:
msarett438b2ad2015-04-09 12:43:10 -0700119 Path fPath;
120 Mode fMode;
121 DstColorType fDstColorType;
msarett0a242972015-06-11 14:27:27 -0700122 float fScale;
scroggo9b77ddd2015-03-19 06:03:39 -0700123};
124
125
mtklein748ca3b2015-01-15 10:56:12 -0800126class ImageSrc : public Src {
127public:
mtkleinedc93bc2015-01-30 13:22:23 -0800128 // divisor == 0 means decode the whole image
129 // divisor > 0 means decode in subsets, dividing into a divisor x divisor grid.
130 explicit ImageSrc(Path path, int divisor = 0);
mtklein748ca3b2015-01-15 10:56:12 -0800131
mtklein36352bf2015-03-25 18:17:31 -0700132 Error draw(SkCanvas*) const override;
133 SkISize size() const override;
134 Name name() const override;
mtkleine0effd62015-07-29 06:37:28 -0700135 bool veto(SinkType) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800136private:
mtklein8d17a132015-01-30 11:42:31 -0800137 Path fPath;
mtkleinedc93bc2015-01-30 13:22:23 -0800138 const int fDivisor;
mtklein748ca3b2015-01-15 10:56:12 -0800139};
140
141class SKPSrc : public Src {
142public:
mtklein8d17a132015-01-30 11:42:31 -0800143 explicit SKPSrc(Path path);
mtklein748ca3b2015-01-15 10:56:12 -0800144
mtklein36352bf2015-03-25 18:17:31 -0700145 Error draw(SkCanvas*) const override;
146 SkISize size() const override;
147 Name name() const override;
mtklein748ca3b2015-01-15 10:56:12 -0800148private:
mtklein8d17a132015-01-30 11:42:31 -0800149 Path fPath;
mtklein748ca3b2015-01-15 10:56:12 -0800150};
151
152/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
153
mtkleinad66f9b2015-02-13 15:11:10 -0800154class NullSink : public Sink {
155public:
156 NullSink() {}
157
mtklein36352bf2015-03-25 18:17:31 -0700158 Error draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const override;
159 int enclave() const override { return kAnyThread_Enclave; }
160 const char* fileExtension() const override { return ""; }
mtkleinad66f9b2015-02-13 15:11:10 -0800161};
162
163
mtklein748ca3b2015-01-15 10:56:12 -0800164class GPUSink : public Sink {
165public:
mtklein82d28432015-01-15 12:46:02 -0800166 GPUSink(GrContextFactory::GLContextType, GrGLStandard, int samples, bool dfText, bool threaded);
mtklein748ca3b2015-01-15 10:56:12 -0800167
mtklein36352bf2015-03-25 18:17:31 -0700168 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
169 int enclave() const override;
170 const char* fileExtension() const override { return "png"; }
mtklein748ca3b2015-01-15 10:56:12 -0800171private:
172 GrContextFactory::GLContextType fContextType;
173 GrGLStandard fGpuAPI;
174 int fSampleCount;
175 bool fUseDFText;
mtklein82d28432015-01-15 12:46:02 -0800176 bool fThreaded;
mtklein748ca3b2015-01-15 10:56:12 -0800177};
178
179class PDFSink : public Sink {
180public:
181 PDFSink();
182
mtklein36352bf2015-03-25 18:17:31 -0700183 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
184 int enclave() const override { return kAnyThread_Enclave; }
185 const char* fileExtension() const override { return "pdf"; }
mtklein748ca3b2015-01-15 10:56:12 -0800186};
187
halcanary47ef4d52015-03-03 09:13:09 -0800188class XPSSink : public Sink {
189public:
190 XPSSink();
191
mtklein36352bf2015-03-25 18:17:31 -0700192 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
193 int enclave() const override { return kAnyThread_Enclave; }
194 const char* fileExtension() const override { return "xps"; }
halcanary47ef4d52015-03-03 09:13:09 -0800195};
196
mtklein748ca3b2015-01-15 10:56:12 -0800197class RasterSink : public Sink {
198public:
199 explicit RasterSink(SkColorType);
200
mtklein36352bf2015-03-25 18:17:31 -0700201 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
202 int enclave() const override { return kAnyThread_Enclave; }
203 const char* fileExtension() const override { return "png"; }
mtklein748ca3b2015-01-15 10:56:12 -0800204private:
205 SkColorType fColorType;
206};
207
mtklein9c3f17d2015-01-28 11:35:18 -0800208class SKPSink : public Sink {
209public:
210 SKPSink();
211
mtklein36352bf2015-03-25 18:17:31 -0700212 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
213 int enclave() const override { return kAnyThread_Enclave; }
214 const char* fileExtension() const override { return "skp"; }
mtklein9c3f17d2015-01-28 11:35:18 -0800215};
216
mtklein8a4527e2015-01-31 20:00:58 -0800217class SVGSink : public Sink {
218public:
219 SVGSink();
220
mtklein36352bf2015-03-25 18:17:31 -0700221 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
222 int enclave() const override { return kAnyThread_Enclave; }
223 const char* fileExtension() const override { return "svg"; }
mtklein8a4527e2015-01-31 20:00:58 -0800224};
225
226
mtklein748ca3b2015-01-15 10:56:12 -0800227/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
228
mtklein78829242015-05-06 07:54:07 -0700229class Via : public Sink {
230public:
231 explicit Via(Sink* sink) : fSink(sink) {}
232 const char* fileExtension() const override { return fSink->fileExtension(); }
233 int enclave() const override { return fSink->enclave(); }
234protected:
235 SkAutoTDelete<Sink> fSink;
236};
237
238class ViaMatrix : public Via {
mtklein748ca3b2015-01-15 10:56:12 -0800239public:
240 ViaMatrix(SkMatrix, Sink*);
mtklein36352bf2015-03-25 18:17:31 -0700241 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800242private:
mtklein78829242015-05-06 07:54:07 -0700243 const SkMatrix fMatrix;
mtklein748ca3b2015-01-15 10:56:12 -0800244};
245
mtklein78829242015-05-06 07:54:07 -0700246class ViaUpright : public Via {
mtkleind603b222015-02-17 11:13:33 -0800247public:
248 ViaUpright(SkMatrix, Sink*);
mtklein36352bf2015-03-25 18:17:31 -0700249 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtkleind603b222015-02-17 11:13:33 -0800250private:
mtklein78829242015-05-06 07:54:07 -0700251 const SkMatrix fMatrix;
mtkleind603b222015-02-17 11:13:33 -0800252};
253
mtklein78829242015-05-06 07:54:07 -0700254class ViaPipe : public Via {
mtklein748ca3b2015-01-15 10:56:12 -0800255public:
mtklein78829242015-05-06 07:54:07 -0700256 explicit ViaPipe(Sink* sink) : Via(sink) {}
mtklein36352bf2015-03-25 18:17:31 -0700257 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800258};
259
mtklein78829242015-05-06 07:54:07 -0700260class ViaDeferred : public Via {
reed06a22f62015-05-05 08:11:33 -0700261public:
mtklein78829242015-05-06 07:54:07 -0700262 explicit ViaDeferred(Sink* sink) : Via(sink) {}
reed06a22f62015-05-05 08:11:33 -0700263 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
reed06a22f62015-05-05 08:11:33 -0700264};
265
mtklein78829242015-05-06 07:54:07 -0700266class ViaSerialization : public Via {
mtklein748ca3b2015-01-15 10:56:12 -0800267public:
mtklein78829242015-05-06 07:54:07 -0700268 explicit ViaSerialization(Sink* sink) : Via(sink) {}
mtklein36352bf2015-03-25 18:17:31 -0700269 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800270};
271
mtklein78829242015-05-06 07:54:07 -0700272class ViaTiles : public Via {
mtklein748ca3b2015-01-15 10:56:12 -0800273public:
274 ViaTiles(int w, int h, SkBBHFactory*, Sink*);
mtklein36352bf2015-03-25 18:17:31 -0700275 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800276private:
277 const int fW, fH;
278 SkAutoTDelete<SkBBHFactory> fFactory;
mtklein748ca3b2015-01-15 10:56:12 -0800279};
280
mtklein78829242015-05-06 07:54:07 -0700281class ViaSecondPicture : public Via {
mtkleinb7e8d692015-04-07 08:30:32 -0700282public:
mtklein78829242015-05-06 07:54:07 -0700283 explicit ViaSecondPicture(Sink* sink) : Via(sink) {}
mtkleinb7e8d692015-04-07 08:30:32 -0700284 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtkleinb7e8d692015-04-07 08:30:32 -0700285};
286
mtklein78829242015-05-06 07:54:07 -0700287class ViaSingletonPictures : public Via {
mtkleind31c13d2015-05-05 12:59:56 -0700288public:
mtklein78829242015-05-06 07:54:07 -0700289 explicit ViaSingletonPictures(Sink* sink) : Via(sink) {}
mtkleind31c13d2015-05-05 12:59:56 -0700290 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtkleind31c13d2015-05-05 12:59:56 -0700291};
292
mtklein6fbf4b32015-05-06 11:35:40 -0700293class ViaTwice : public Via {
294public:
295 explicit ViaTwice(Sink* sink) : Via(sink) {}
296 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
297};
298
mtklein748ca3b2015-01-15 10:56:12 -0800299} // namespace DM
300
301#endif//DMSrcSink_DEFINED