blob: 3190576c680d4936e3e46cf8c5965ad3c7ae0f08 [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"
mtklein748ca3b2015-01-15 10:56:12 -080018#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) {}
msarett9e707a02015-09-01 14:57:57 -070027 ImplicitString() : SkString("") {}
mtklein748ca3b2015-01-15 10:56:12 -080028};
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
mtklein99cab4e2015-07-31 06:43:04 -070056struct SinkFlags {
57 enum { kNull, kGPU, kVector, kRaster } type;
58 enum { kDirect, kIndirect } approach;
59};
mtkleine0effd62015-07-29 06:37:28 -070060
mtklein748ca3b2015-01-15 10:56:12 -080061struct Src {
mtklein748ca3b2015-01-15 10:56:12 -080062 virtual ~Src() {}
63 virtual Error SK_WARN_UNUSED_RESULT draw(SkCanvas*) const = 0;
64 virtual SkISize size() const = 0;
65 virtual Name name() const = 0;
bsalomon4ee6bd82015-05-27 13:23:23 -070066 virtual void modifyGrContextOptions(GrContextOptions* options) const {}
mtklein99cab4e2015-07-31 06:43:04 -070067 virtual bool veto(SinkFlags) const { return false; }
mtklein21eaf3b2016-02-08 12:39:59 -080068
69 // Force Tasks using this Src to run on the main thread?
70 virtual bool serial() const { return false; }
mtklein748ca3b2015-01-15 10:56:12 -080071};
72
73struct Sink {
74 virtual ~Sink() {}
mtkleinb9eb4ac2015-02-02 18:26:03 -080075 // You may write to either the bitmap or stream. If you write to log, we'll print that out.
76 virtual Error SK_WARN_UNUSED_RESULT draw(const Src&, SkBitmap*, SkWStream*, SkString* log)
77 const = 0;
mtklein21eaf3b2016-02-08 12:39:59 -080078
79 // Force Tasks using this Sink to run on the main thread?
80 virtual bool serial() const { return false; }
mtklein748ca3b2015-01-15 10:56:12 -080081
82 // File extension for the content draw() outputs, e.g. "png", "pdf".
83 virtual const char* fileExtension() const = 0;
mtklein99cab4e2015-07-31 06:43:04 -070084
85 virtual SinkFlags flags() const = 0;
mtklein748ca3b2015-01-15 10:56:12 -080086};
87
mtklein748ca3b2015-01-15 10:56:12 -080088/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
89
mtklein748ca3b2015-01-15 10:56:12 -080090class GMSrc : public Src {
91public:
92 explicit GMSrc(skiagm::GMRegistry::Factory);
93
mtklein36352bf2015-03-25 18:17:31 -070094 Error draw(SkCanvas*) const override;
95 SkISize size() const override;
96 Name name() const override;
bsalomon4ee6bd82015-05-27 13:23:23 -070097 void modifyGrContextOptions(GrContextOptions* options) const override;
98
mtklein748ca3b2015-01-15 10:56:12 -080099private:
100 skiagm::GMRegistry::Factory fFactory;
101};
102
scroggo9b77ddd2015-03-19 06:03:39 -0700103class CodecSrc : public Src {
104public:
scroggo9c59ebc2015-03-25 13:48:49 -0700105 enum Mode {
msarett9e707a02015-09-01 14:57:57 -0700106 kCodec_Mode,
msarettbb25b532016-01-13 09:31:39 -0800107 // We choose to test only one mode with zero initialized memory.
108 // This will exercise all of the interesting cases in SkSwizzler
109 // without doubling the size of our test suite.
110 kCodecZeroInit_Mode,
scroggo9c59ebc2015-03-25 13:48:49 -0700111 kScanline_Mode,
msarett0a242972015-06-11 14:27:27 -0700112 kStripe_Mode, // Tests the skipping of scanlines
msarett91c22b22016-02-22 12:27:46 -0800113 kCroppedScanline_Mode, // Tests (jpeg) cropped scanline optimization
scroggob636b452015-07-22 07:16:20 -0700114 kSubset_Mode, // For codecs that support subsets directly.
msarettb714fb02016-01-22 14:46:42 -0800115 kGen_Mode, // Test SkCodecImageGenerator (includes YUV)
scroggo9c59ebc2015-03-25 13:48:49 -0700116 };
msarett438b2ad2015-04-09 12:43:10 -0700117 enum DstColorType {
118 kGetFromCanvas_DstColorType,
119 kIndex8_Always_DstColorType,
120 kGrayscale_Always_DstColorType,
121 };
scroggoc5560be2016-02-03 09:42:42 -0800122 CodecSrc(Path, Mode, DstColorType, SkAlphaType, float);
scroggo9b77ddd2015-03-19 06:03:39 -0700123
mtklein36352bf2015-03-25 18:17:31 -0700124 Error draw(SkCanvas*) const override;
125 SkISize size() const override;
126 Name name() const override;
mtklein99cab4e2015-07-31 06:43:04 -0700127 bool veto(SinkFlags) const override;
scroggo3ac66e92016-02-08 15:09:48 -0800128 bool serial() const override { return fRunSerially; }
scroggo9b77ddd2015-03-19 06:03:39 -0700129private:
msarett9e707a02015-09-01 14:57:57 -0700130 Path fPath;
131 Mode fMode;
132 DstColorType fDstColorType;
scroggoc5560be2016-02-03 09:42:42 -0800133 SkAlphaType fDstAlphaType;
msarett9e707a02015-09-01 14:57:57 -0700134 float fScale;
scroggo3ac66e92016-02-08 15:09:48 -0800135 bool fRunSerially;
scroggo9b77ddd2015-03-19 06:03:39 -0700136};
137
msarett3d9d7a72015-10-21 10:27:10 -0700138class AndroidCodecSrc : public Src {
139public:
140 enum Mode {
141 kFullImage_Mode,
142 // Splits the image into multiple subsets using a divisor and decodes the subsets
143 // separately.
144 kDivisor_Mode,
145 };
146
scroggoc5560be2016-02-03 09:42:42 -0800147 AndroidCodecSrc(Path, Mode, CodecSrc::DstColorType, SkAlphaType, int sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700148
149 Error draw(SkCanvas*) const override;
150 SkISize size() const override;
151 Name name() const override;
152 bool veto(SinkFlags) const override;
scroggo3ac66e92016-02-08 15:09:48 -0800153 bool serial() const override { return fRunSerially; }
msarett3d9d7a72015-10-21 10:27:10 -0700154private:
155 Path fPath;
156 Mode fMode;
157 CodecSrc::DstColorType fDstColorType;
scroggoc5560be2016-02-03 09:42:42 -0800158 SkAlphaType fDstAlphaType;
msarett3d9d7a72015-10-21 10:27:10 -0700159 int fSampleSize;
scroggo3ac66e92016-02-08 15:09:48 -0800160 bool fRunSerially;
msarett3d9d7a72015-10-21 10:27:10 -0700161};
162
msaretta5783ae2015-09-08 15:35:32 -0700163// Allows for testing of various implementations of Android's BitmapRegionDecoder
164class BRDSrc : public Src {
165public:
166 enum Mode {
167 // Decode the entire image as one region.
168 kFullImage_Mode,
169 // Splits the image into multiple regions using a divisor and decodes the regions
170 // separately. Also, this test adds a border of a few pixels to each of the regions
171 // that it is decoding. This tests the behavior when a client asks for a region that
172 // does not fully fit in the image.
173 kDivisor_Mode,
174 };
175
msarett5cb48852015-11-06 08:56:32 -0800176 BRDSrc(Path, SkBitmapRegionDecoder::Strategy, Mode, CodecSrc::DstColorType, uint32_t);
msaretta5783ae2015-09-08 15:35:32 -0700177
msaretta5783ae2015-09-08 15:35:32 -0700178 Error draw(SkCanvas*) const override;
179 SkISize size() const override;
180 Name name() const override;
181 bool veto(SinkFlags) const override;
182private:
183 Path fPath;
msarett5cb48852015-11-06 08:56:32 -0800184 SkBitmapRegionDecoder::Strategy fStrategy;
msaretta5783ae2015-09-08 15:35:32 -0700185 Mode fMode;
186 CodecSrc::DstColorType fDstColorType;
187 uint32_t fSampleSize;
188};
scroggo9b77ddd2015-03-19 06:03:39 -0700189
mtklein748ca3b2015-01-15 10:56:12 -0800190class SKPSrc : public Src {
191public:
mtklein8d17a132015-01-30 11:42:31 -0800192 explicit SKPSrc(Path path);
mtklein748ca3b2015-01-15 10:56:12 -0800193
mtklein36352bf2015-03-25 18:17:31 -0700194 Error draw(SkCanvas*) const override;
195 SkISize size() const override;
196 Name name() const override;
mtklein748ca3b2015-01-15 10:56:12 -0800197private:
mtklein8d17a132015-01-30 11:42:31 -0800198 Path fPath;
mtklein748ca3b2015-01-15 10:56:12 -0800199};
200
201/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
202
mtkleinad66f9b2015-02-13 15:11:10 -0800203class NullSink : public Sink {
204public:
205 NullSink() {}
206
mtklein36352bf2015-03-25 18:17:31 -0700207 Error draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const override;
mtklein36352bf2015-03-25 18:17:31 -0700208 const char* fileExtension() const override { return ""; }
mtklein99cab4e2015-07-31 06:43:04 -0700209 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kNull, SinkFlags::kDirect }; }
mtkleinad66f9b2015-02-13 15:11:10 -0800210};
211
212
mtklein748ca3b2015-01-15 10:56:12 -0800213class GPUSink : public Sink {
214public:
kkinnunen5219fd92015-12-10 06:28:13 -0800215 GPUSink(GrContextFactory::GLContextType, GrContextFactory::GLContextOptions,
kkinnunen3e980c32015-12-23 01:33:00 -0800216 int samples, bool diText, bool threaded);
mtklein748ca3b2015-01-15 10:56:12 -0800217
mtklein36352bf2015-03-25 18:17:31 -0700218 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein21eaf3b2016-02-08 12:39:59 -0800219 bool serial() const override { return !fThreaded; }
mtklein36352bf2015-03-25 18:17:31 -0700220 const char* fileExtension() const override { return "png"; }
mtklein99cab4e2015-07-31 06:43:04 -0700221 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kGPU, SinkFlags::kDirect }; }
mtklein748ca3b2015-01-15 10:56:12 -0800222private:
kkinnunen5219fd92015-12-10 06:28:13 -0800223 GrContextFactory::GLContextType fContextType;
224 GrContextFactory::GLContextOptions fContextOptions;
kkinnunen5219fd92015-12-10 06:28:13 -0800225 int fSampleCount;
226 bool fUseDIText;
227 bool fThreaded;
mtklein748ca3b2015-01-15 10:56:12 -0800228};
229
230class PDFSink : public Sink {
231public:
halcanaryc11c62f2015-09-28 11:51:54 -0700232 PDFSink(const char* rasterizer);
mtklein748ca3b2015-01-15 10:56:12 -0800233
mtklein36352bf2015-03-25 18:17:31 -0700234 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein36352bf2015-03-25 18:17:31 -0700235 const char* fileExtension() const override { return "pdf"; }
mtklein99cab4e2015-07-31 06:43:04 -0700236 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kVector, SinkFlags::kDirect }; }
halcanaryc11c62f2015-09-28 11:51:54 -0700237private:
238 const char* fRasterizer;
mtklein748ca3b2015-01-15 10:56:12 -0800239};
240
halcanary47ef4d52015-03-03 09:13:09 -0800241class XPSSink : public Sink {
242public:
243 XPSSink();
244
mtklein36352bf2015-03-25 18:17:31 -0700245 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein36352bf2015-03-25 18:17:31 -0700246 const char* fileExtension() const override { return "xps"; }
mtklein99cab4e2015-07-31 06:43:04 -0700247 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kVector, SinkFlags::kDirect }; }
halcanary47ef4d52015-03-03 09:13:09 -0800248};
249
mtklein748ca3b2015-01-15 10:56:12 -0800250class RasterSink : public Sink {
251public:
252 explicit RasterSink(SkColorType);
253
mtklein36352bf2015-03-25 18:17:31 -0700254 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein36352bf2015-03-25 18:17:31 -0700255 const char* fileExtension() const override { return "png"; }
mtklein99cab4e2015-07-31 06:43:04 -0700256 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kRaster, SinkFlags::kDirect }; }
mtklein748ca3b2015-01-15 10:56:12 -0800257private:
258 SkColorType fColorType;
259};
260
mtklein9c3f17d2015-01-28 11:35:18 -0800261class SKPSink : public Sink {
262public:
263 SKPSink();
264
mtklein36352bf2015-03-25 18:17:31 -0700265 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein36352bf2015-03-25 18:17:31 -0700266 const char* fileExtension() const override { return "skp"; }
mtklein99cab4e2015-07-31 06:43:04 -0700267 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kVector, SinkFlags::kDirect }; }
mtklein9c3f17d2015-01-28 11:35:18 -0800268};
269
mtklein8a4527e2015-01-31 20:00:58 -0800270class SVGSink : public Sink {
271public:
272 SVGSink();
273
mtklein36352bf2015-03-25 18:17:31 -0700274 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein36352bf2015-03-25 18:17:31 -0700275 const char* fileExtension() const override { return "svg"; }
mtklein99cab4e2015-07-31 06:43:04 -0700276 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kVector, SinkFlags::kDirect }; }
mtklein8a4527e2015-01-31 20:00:58 -0800277};
278
279
mtklein748ca3b2015-01-15 10:56:12 -0800280/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
281
mtklein78829242015-05-06 07:54:07 -0700282class Via : public Sink {
283public:
msarett62d3b102015-12-10 15:14:27 -0800284 explicit Via(Sink* sink) : fSink(sink) {}
mtklein78829242015-05-06 07:54:07 -0700285 const char* fileExtension() const override { return fSink->fileExtension(); }
mtklein21eaf3b2016-02-08 12:39:59 -0800286 bool serial() const override { return fSink->serial(); }
mtklein99cab4e2015-07-31 06:43:04 -0700287 SinkFlags flags() const override {
288 SinkFlags flags = fSink->flags();
289 flags.approach = SinkFlags::kIndirect;
290 return flags;
291 }
mtklein78829242015-05-06 07:54:07 -0700292protected:
293 SkAutoTDelete<Sink> fSink;
294};
295
296class ViaMatrix : public Via {
mtklein748ca3b2015-01-15 10:56:12 -0800297public:
msarett62d3b102015-12-10 15:14:27 -0800298 ViaMatrix(SkMatrix, Sink*);
mtklein36352bf2015-03-25 18:17:31 -0700299 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800300private:
mtklein78829242015-05-06 07:54:07 -0700301 const SkMatrix fMatrix;
mtklein748ca3b2015-01-15 10:56:12 -0800302};
303
mtklein78829242015-05-06 07:54:07 -0700304class ViaUpright : public Via {
mtkleind603b222015-02-17 11:13:33 -0800305public:
msarett62d3b102015-12-10 15:14:27 -0800306 ViaUpright(SkMatrix, Sink*);
mtklein36352bf2015-03-25 18:17:31 -0700307 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtkleind603b222015-02-17 11:13:33 -0800308private:
mtklein78829242015-05-06 07:54:07 -0700309 const SkMatrix fMatrix;
mtkleind603b222015-02-17 11:13:33 -0800310};
311
mtklein2e2ea382015-10-16 10:29:41 -0700312class ViaRemote : public Via {
reed06a22f62015-05-05 08:11:33 -0700313public:
msarett62d3b102015-12-10 15:14:27 -0800314 ViaRemote(bool cache, Sink* sink) : Via(sink), fCache(cache) {}
reed06a22f62015-05-05 08:11:33 -0700315 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein2e2ea382015-10-16 10:29:41 -0700316private:
317 bool fCache;
reed06a22f62015-05-05 08:11:33 -0700318};
319
mtklein78829242015-05-06 07:54:07 -0700320class ViaSerialization : public Via {
mtklein748ca3b2015-01-15 10:56:12 -0800321public:
msarett62d3b102015-12-10 15:14:27 -0800322 explicit ViaSerialization(Sink* sink) : Via(sink) {}
mtklein36352bf2015-03-25 18:17:31 -0700323 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800324};
325
mtklein4a34ecb2016-01-08 10:19:35 -0800326class ViaPicture : public Via {
327public:
328 explicit ViaPicture(Sink* sink) : Via(sink) {}
329 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
330};
331
mtklein78829242015-05-06 07:54:07 -0700332class ViaTiles : public Via {
mtklein748ca3b2015-01-15 10:56:12 -0800333public:
msarett62d3b102015-12-10 15:14:27 -0800334 ViaTiles(int w, int h, SkBBHFactory*, Sink*);
mtklein36352bf2015-03-25 18:17:31 -0700335 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtklein748ca3b2015-01-15 10:56:12 -0800336private:
337 const int fW, fH;
338 SkAutoTDelete<SkBBHFactory> fFactory;
mtklein748ca3b2015-01-15 10:56:12 -0800339};
340
mtklein78829242015-05-06 07:54:07 -0700341class ViaSecondPicture : public Via {
mtkleinb7e8d692015-04-07 08:30:32 -0700342public:
msarett62d3b102015-12-10 15:14:27 -0800343 explicit ViaSecondPicture(Sink* sink) : Via(sink) {}
mtkleinb7e8d692015-04-07 08:30:32 -0700344 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtkleinb7e8d692015-04-07 08:30:32 -0700345};
346
mtklein78829242015-05-06 07:54:07 -0700347class ViaSingletonPictures : public Via {
mtkleind31c13d2015-05-05 12:59:56 -0700348public:
msarett62d3b102015-12-10 15:14:27 -0800349 explicit ViaSingletonPictures(Sink* sink) : Via(sink) {}
mtkleind31c13d2015-05-05 12:59:56 -0700350 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
mtkleind31c13d2015-05-05 12:59:56 -0700351};
352
mtklein6fbf4b32015-05-06 11:35:40 -0700353class ViaTwice : public Via {
354public:
msarett62d3b102015-12-10 15:14:27 -0800355 explicit ViaTwice(Sink* sink) : Via(sink) {}
mtklein6fbf4b32015-05-06 11:35:40 -0700356 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
357};
358
halcanary7a76f9c2016-02-03 11:53:18 -0800359class ViaMojo : public Via {
360public:
361 explicit ViaMojo(Sink* sink) : Via(sink) {}
362 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
363};
364
mtklein748ca3b2015-01-15 10:56:12 -0800365} // namespace DM
366
367#endif//DMSrcSink_DEFINED