blob: 5e053b1155cfd57106bbc96e665bb53c8d3d720a [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"
msarett5cb48852015-11-06 08:56:32 -080015#include "SkBitmapRegionDecoder.h"
mtklein748ca3b2015-01-15 10:56:12 -080016#include "SkCanvas.h"
17#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) {}
msarett9e707a02015-09-01 14:57:57 -070028 ImplicitString() : SkString("") {}
mtklein748ca3b2015-01-15 10:56:12 -080029};
mtklein748ca3b2015-01-15 10:56:12 -080030typedef ImplicitString Name;
mtklein8d17a132015-01-30 11:42:31 -080031typedef ImplicitString Path;
mtklein748ca3b2015-01-15 10:56:12 -080032
mtklein4089ef72015-03-05 08:40:28 -080033class Error {
34public:
35 Error(const SkString& s) : fMsg(s), fFatal(!this->isEmpty()) {}
36 Error(const char* s) : fMsg(s), fFatal(!this->isEmpty()) {}
37
38 Error(const Error&) = default;
39 Error& operator=(const Error&) = default;
40
41 static Error Nonfatal(const SkString& s) { return Nonfatal(s.c_str()); }
42 static Error Nonfatal(const char* s) {
43 Error e(s);
44 e.fFatal = false;
45 return e;
46 }
47
48 const char* c_str() const { return fMsg.c_str(); }
49 bool isEmpty() const { return fMsg.isEmpty(); }
50 bool isFatal() const { return fFatal; }
51
52private:
53 SkString fMsg;
54 bool fFatal;
55};
56
mtklein99cab4e2015-07-31 06:43:04 -070057struct SinkFlags {
58 enum { kNull, kGPU, kVector, kRaster } type;
59 enum { kDirect, kIndirect } approach;
60};
mtkleine0effd62015-07-29 06:37:28 -070061
mtklein748ca3b2015-01-15 10:56:12 -080062struct Src {
63 // All Srcs must be thread safe.
64 virtual ~Src() {}
65 virtual Error SK_WARN_UNUSED_RESULT draw(SkCanvas*) const = 0;
66 virtual SkISize size() const = 0;
67 virtual Name name() const = 0;
bsalomon4ee6bd82015-05-27 13:23:23 -070068 virtual void modifyGrContextOptions(GrContextOptions* options) const {}
mtklein99cab4e2015-07-31 06:43:04 -070069 virtual bool veto(SinkFlags) const { return false; }
mtklein748ca3b2015-01-15 10:56:12 -080070};
71
72struct Sink {
73 virtual ~Sink() {}
mtkleinb9eb4ac2015-02-02 18:26:03 -080074 // You may write to either the bitmap or stream. If you write to log, we'll print that out.
75 virtual Error SK_WARN_UNUSED_RESULT draw(const Src&, SkBitmap*, SkWStream*, SkString* log)
76 const = 0;
mtklein748ca3b2015-01-15 10:56:12 -080077 // Sinks in the same enclave (except kAnyThread_Enclave) will run serially on the same thread.
78 virtual int enclave() const = 0;
79
80 // File extension for the content draw() outputs, e.g. "png", "pdf".
81 virtual const char* fileExtension() const = 0;
mtklein99cab4e2015-07-31 06:43:04 -070082
83 virtual SinkFlags flags() const = 0;
mtklein748ca3b2015-01-15 10:56:12 -080084};
85
halcanary792c80f2015-02-20 07:21:05 -080086enum { kAnyThread_Enclave, kGPU_Enclave };
87static const int kNumEnclaves = kGPU_Enclave + 1;
mtklein748ca3b2015-01-15 10:56:12 -080088
89/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
90
mtklein748ca3b2015-01-15 10:56:12 -080091class GMSrc : public Src {
92public:
93 explicit GMSrc(skiagm::GMRegistry::Factory);
94
mtklein36352bf2015-03-25 18:17:31 -070095 Error draw(SkCanvas*) const override;
96 SkISize size() const override;
97 Name name() const override;
bsalomon4ee6bd82015-05-27 13:23:23 -070098 void modifyGrContextOptions(GrContextOptions* options) const override;
99
mtklein748ca3b2015-01-15 10:56:12 -0800100private:
101 skiagm::GMRegistry::Factory fFactory;
102};
103
scroggo9b77ddd2015-03-19 06:03:39 -0700104class CodecSrc : public Src {
105public:
scroggo9c59ebc2015-03-25 13:48:49 -0700106 enum Mode {
msarett9e707a02015-09-01 14:57:57 -0700107 kCodec_Mode,
scroggo9c59ebc2015-03-25 13:48:49 -0700108 kScanline_Mode,
msarett0a242972015-06-11 14:27:27 -0700109 kStripe_Mode, // Tests the skipping of scanlines
scroggob636b452015-07-22 07:16:20 -0700110 kSubset_Mode, // For codecs that support subsets directly.
scroggo9c59ebc2015-03-25 13:48:49 -0700111 };
msarett438b2ad2015-04-09 12:43:10 -0700112 enum DstColorType {
113 kGetFromCanvas_DstColorType,
114 kIndex8_Always_DstColorType,
115 kGrayscale_Always_DstColorType,
116 };
msarett0a242972015-06-11 14:27:27 -0700117 CodecSrc(Path, Mode, DstColorType, float);
scroggo9b77ddd2015-03-19 06:03:39 -0700118
mtklein36352bf2015-03-25 18:17:31 -0700119 Error draw(SkCanvas*) const override;
120 SkISize size() const override;
121 Name name() const override;
mtklein99cab4e2015-07-31 06:43:04 -0700122 bool veto(SinkFlags) const override;
scroggo9b77ddd2015-03-19 06:03:39 -0700123private:
msarett9e707a02015-09-01 14:57:57 -0700124 Path fPath;
125 Mode fMode;
126 DstColorType fDstColorType;
127 float fScale;
scroggo9b77ddd2015-03-19 06:03:39 -0700128};
129
msarett3d9d7a72015-10-21 10:27:10 -0700130class AndroidCodecSrc : public Src {
131public:
132 enum Mode {
133 kFullImage_Mode,
134 // Splits the image into multiple subsets using a divisor and decodes the subsets
135 // separately.
136 kDivisor_Mode,
137 };
138
139 AndroidCodecSrc(Path, Mode, CodecSrc::DstColorType, int sampleSize);
140
141 Error draw(SkCanvas*) const override;
142 SkISize size() const override;
143 Name name() const override;
144 bool veto(SinkFlags) const override;
145private:
146 Path fPath;
147 Mode fMode;
148 CodecSrc::DstColorType fDstColorType;
149 int fSampleSize;
150};
151
msaretta5783ae2015-09-08 15:35:32 -0700152// Allows for testing of various implementations of Android's BitmapRegionDecoder
153class BRDSrc : public Src {
154public:
155 enum Mode {
156 // Decode the entire image as one region.
157 kFullImage_Mode,
158 // Splits the image into multiple regions using a divisor and decodes the regions
159 // separately. Also, this test adds a border of a few pixels to each of the regions
160 // that it is decoding. This tests the behavior when a client asks for a region that
161 // does not fully fit in the image.
162 kDivisor_Mode,
163 };
164
msarett5cb48852015-11-06 08:56:32 -0800165 BRDSrc(Path, SkBitmapRegionDecoder::Strategy, Mode, CodecSrc::DstColorType, uint32_t);
msaretta5783ae2015-09-08 15:35:32 -0700166
msaretta5783ae2015-09-08 15:35:32 -0700167 Error draw(SkCanvas*) const override;
168 SkISize size() const override;
169 Name name() const override;
170 bool veto(SinkFlags) const override;
171private:
172 Path fPath;
msarett5cb48852015-11-06 08:56:32 -0800173 SkBitmapRegionDecoder::Strategy fStrategy;
msaretta5783ae2015-09-08 15:35:32 -0700174 Mode fMode;
175 CodecSrc::DstColorType fDstColorType;
176 uint32_t fSampleSize;
177};
scroggo9b77ddd2015-03-19 06:03:39 -0700178
mtklein748ca3b2015-01-15 10:56:12 -0800179class ImageSrc : public Src {
180public:
msarett164d3022015-11-10 15:09:03 -0800181 explicit ImageSrc(Path path);
mtklein748ca3b2015-01-15 10:56:12 -0800182
mtklein36352bf2015-03-25 18:17:31 -0700183 Error draw(SkCanvas*) const override;
184 SkISize size() const override;
185 Name name() const override;
mtklein99cab4e2015-07-31 06:43:04 -0700186 bool veto(SinkFlags) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800187private:
mtklein8d17a132015-01-30 11:42:31 -0800188 Path fPath;
mtklein748ca3b2015-01-15 10:56:12 -0800189};
190
191class SKPSrc : public Src {
192public:
mtklein8d17a132015-01-30 11:42:31 -0800193 explicit SKPSrc(Path path);
mtklein748ca3b2015-01-15 10:56:12 -0800194
mtklein36352bf2015-03-25 18:17:31 -0700195 Error draw(SkCanvas*) const override;
196 SkISize size() const override;
197 Name name() const override;
mtklein748ca3b2015-01-15 10:56:12 -0800198private:
mtklein8d17a132015-01-30 11:42:31 -0800199 Path fPath;
mtklein748ca3b2015-01-15 10:56:12 -0800200};
201
202/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
203
mtkleinad66f9b2015-02-13 15:11:10 -0800204class NullSink : public Sink {
205public:
206 NullSink() {}
207
mtklein36352bf2015-03-25 18:17:31 -0700208 Error draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const override;
209 int enclave() const override { return kAnyThread_Enclave; }
210 const char* fileExtension() const override { return ""; }
mtklein99cab4e2015-07-31 06:43:04 -0700211 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kNull, SinkFlags::kDirect }; }
mtkleinad66f9b2015-02-13 15:11:10 -0800212};
213
214
mtklein748ca3b2015-01-15 10:56:12 -0800215class GPUSink : public Sink {
216public:
kkinnunen5219fd92015-12-10 06:28:13 -0800217 GPUSink(GrContextFactory::GLContextType, GrContextFactory::GLContextOptions,
kkinnunenc8b43362015-12-18 03:27:32 -0800218 int samples, bool diText, bool threaded);
mtklein748ca3b2015-01-15 10:56:12 -0800219
mtklein36352bf2015-03-25 18:17:31 -0700220 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
221 int enclave() const override;
222 const char* fileExtension() const override { return "png"; }
mtklein99cab4e2015-07-31 06:43:04 -0700223 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kGPU, SinkFlags::kDirect }; }
mtklein748ca3b2015-01-15 10:56:12 -0800224private:
kkinnunen5219fd92015-12-10 06:28:13 -0800225 GrContextFactory::GLContextType fContextType;
226 GrContextFactory::GLContextOptions fContextOptions;
kkinnunen5219fd92015-12-10 06:28:13 -0800227 int fSampleCount;
228 bool fUseDIText;
229 bool fThreaded;
mtklein748ca3b2015-01-15 10:56:12 -0800230};
231
232class PDFSink : public Sink {
233public:
halcanaryc11c62f2015-09-28 11:51:54 -0700234 PDFSink(const char* rasterizer);
mtklein748ca3b2015-01-15 10:56:12 -0800235
mtklein36352bf2015-03-25 18:17:31 -0700236 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
237 int enclave() const override { return kAnyThread_Enclave; }
238 const char* fileExtension() const override { return "pdf"; }
mtklein99cab4e2015-07-31 06:43:04 -0700239 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kVector, SinkFlags::kDirect }; }
halcanaryc11c62f2015-09-28 11:51:54 -0700240private:
241 const char* fRasterizer;
mtklein748ca3b2015-01-15 10:56:12 -0800242};
243
halcanary47ef4d52015-03-03 09:13:09 -0800244class XPSSink : public Sink {
245public:
246 XPSSink();
247
mtklein36352bf2015-03-25 18:17:31 -0700248 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
249 int enclave() const override { return kAnyThread_Enclave; }
250 const char* fileExtension() const override { return "xps"; }
mtklein99cab4e2015-07-31 06:43:04 -0700251 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kVector, SinkFlags::kDirect }; }
halcanary47ef4d52015-03-03 09:13:09 -0800252};
253
mtklein748ca3b2015-01-15 10:56:12 -0800254class RasterSink : public Sink {
255public:
256 explicit RasterSink(SkColorType);
257
mtklein36352bf2015-03-25 18:17:31 -0700258 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
259 int enclave() const override { return kAnyThread_Enclave; }
260 const char* fileExtension() const override { return "png"; }
mtklein99cab4e2015-07-31 06:43:04 -0700261 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kRaster, SinkFlags::kDirect }; }
mtklein748ca3b2015-01-15 10:56:12 -0800262private:
263 SkColorType fColorType;
264};
265
mtklein9c3f17d2015-01-28 11:35:18 -0800266class SKPSink : public Sink {
267public:
268 SKPSink();
269
mtklein36352bf2015-03-25 18:17:31 -0700270 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
271 int enclave() const override { return kAnyThread_Enclave; }
272 const char* fileExtension() const override { return "skp"; }
mtklein99cab4e2015-07-31 06:43:04 -0700273 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kVector, SinkFlags::kDirect }; }
mtklein9c3f17d2015-01-28 11:35:18 -0800274};
275
mtklein8a4527e2015-01-31 20:00:58 -0800276class SVGSink : public Sink {
277public:
278 SVGSink();
279
mtklein36352bf2015-03-25 18:17:31 -0700280 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
281 int enclave() const override { return kAnyThread_Enclave; }
282 const char* fileExtension() const override { return "svg"; }
mtklein99cab4e2015-07-31 06:43:04 -0700283 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kVector, SinkFlags::kDirect }; }
mtklein8a4527e2015-01-31 20:00:58 -0800284};
285
286
mtklein748ca3b2015-01-15 10:56:12 -0800287/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
288
mtklein78829242015-05-06 07:54:07 -0700289class Via : public Sink {
290public:
msarett62d3b102015-12-10 15:14:27 -0800291 explicit Via(Sink* sink) : fSink(sink) {}
mtklein78829242015-05-06 07:54:07 -0700292 const char* fileExtension() const override { return fSink->fileExtension(); }
293 int enclave() const override { return fSink->enclave(); }
mtklein99cab4e2015-07-31 06:43:04 -0700294 SinkFlags flags() const override {
295 SinkFlags flags = fSink->flags();
296 flags.approach = SinkFlags::kIndirect;
297 return flags;
298 }
mtklein78829242015-05-06 07:54:07 -0700299protected:
300 SkAutoTDelete<Sink> fSink;
301};
302
303class ViaMatrix : public Via {
mtklein748ca3b2015-01-15 10:56:12 -0800304public:
msarett62d3b102015-12-10 15:14:27 -0800305 ViaMatrix(SkMatrix, Sink*);
mtklein36352bf2015-03-25 18:17:31 -0700306 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800307private:
mtklein78829242015-05-06 07:54:07 -0700308 const SkMatrix fMatrix;
mtklein748ca3b2015-01-15 10:56:12 -0800309};
310
mtklein78829242015-05-06 07:54:07 -0700311class ViaUpright : public Via {
mtkleind603b222015-02-17 11:13:33 -0800312public:
msarett62d3b102015-12-10 15:14:27 -0800313 ViaUpright(SkMatrix, Sink*);
mtklein36352bf2015-03-25 18:17:31 -0700314 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtkleind603b222015-02-17 11:13:33 -0800315private:
mtklein78829242015-05-06 07:54:07 -0700316 const SkMatrix fMatrix;
mtkleind603b222015-02-17 11:13:33 -0800317};
318
mtklein78829242015-05-06 07:54:07 -0700319class ViaPipe : public Via {
mtklein748ca3b2015-01-15 10:56:12 -0800320public:
msarett62d3b102015-12-10 15:14:27 -0800321 explicit ViaPipe(Sink* sink) : Via(sink) {}
mtklein36352bf2015-03-25 18:17:31 -0700322 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800323};
324
mtklein2e2ea382015-10-16 10:29:41 -0700325class ViaRemote : public Via {
reed06a22f62015-05-05 08:11:33 -0700326public:
msarett62d3b102015-12-10 15:14:27 -0800327 ViaRemote(bool cache, Sink* sink) : Via(sink), fCache(cache) {}
reed06a22f62015-05-05 08:11:33 -0700328 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein2e2ea382015-10-16 10:29:41 -0700329private:
330 bool fCache;
reed06a22f62015-05-05 08:11:33 -0700331};
332
mtklein78829242015-05-06 07:54:07 -0700333class ViaSerialization : public Via {
mtklein748ca3b2015-01-15 10:56:12 -0800334public:
msarett62d3b102015-12-10 15:14:27 -0800335 explicit ViaSerialization(Sink* sink) : Via(sink) {}
mtklein36352bf2015-03-25 18:17:31 -0700336 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800337};
338
mtklein78829242015-05-06 07:54:07 -0700339class ViaTiles : public Via {
mtklein748ca3b2015-01-15 10:56:12 -0800340public:
msarett62d3b102015-12-10 15:14:27 -0800341 ViaTiles(int w, int h, SkBBHFactory*, Sink*);
mtklein36352bf2015-03-25 18:17:31 -0700342 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800343private:
344 const int fW, fH;
345 SkAutoTDelete<SkBBHFactory> fFactory;
mtklein748ca3b2015-01-15 10:56:12 -0800346};
347
mtklein78829242015-05-06 07:54:07 -0700348class ViaSecondPicture : public Via {
mtkleinb7e8d692015-04-07 08:30:32 -0700349public:
msarett62d3b102015-12-10 15:14:27 -0800350 explicit ViaSecondPicture(Sink* sink) : Via(sink) {}
mtkleinb7e8d692015-04-07 08:30:32 -0700351 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtkleinb7e8d692015-04-07 08:30:32 -0700352};
353
mtklein78829242015-05-06 07:54:07 -0700354class ViaSingletonPictures : public Via {
mtkleind31c13d2015-05-05 12:59:56 -0700355public:
msarett62d3b102015-12-10 15:14:27 -0800356 explicit ViaSingletonPictures(Sink* sink) : Via(sink) {}
mtkleind31c13d2015-05-05 12:59:56 -0700357 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtkleind31c13d2015-05-05 12:59:56 -0700358};
359
mtklein6fbf4b32015-05-06 11:35:40 -0700360class ViaTwice : public Via {
361public:
msarett62d3b102015-12-10 15:14:27 -0800362 explicit ViaTwice(Sink* sink) : Via(sink) {}
mtklein6fbf4b32015-05-06 11:35:40 -0700363 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
364};
365
mtklein748ca3b2015-01-15 10:56:12 -0800366} // namespace DM
367
368#endif//DMSrcSink_DEFINED