blob: 5b7338b4400416cdb9dbc8949471800c3b3c6ab7 [file] [log] [blame]
Andrew Top200ce4b2018-01-29 13:43:50 -08001/*
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
8#ifndef DMSrcSink_DEFINED
9#define DMSrcSink_DEFINED
10
Xiaoming Shi73dfa202020-03-12 11:31:35 -070011#include "gm/gm.h"
12#include "include/android/SkBitmapRegionDecoder.h"
13#include "include/core/SkBBHFactory.h"
14#include "include/core/SkBitmap.h"
15#include "include/core/SkCanvas.h"
16#include "include/core/SkData.h"
17#include "include/core/SkPicture.h"
18#include "src/core/SkBBoxHierarchy.h"
19#include "src/utils/SkMultiPictureDocument.h"
20#include "tools/flags/CommonFlagsConfig.h"
21#include "tools/gpu/MemoryCache.h"
22
23#include <functional>
Andrew Top200ce4b2018-01-29 13:43:50 -080024
25//#define TEST_VIA_SVG
26
27namespace DM {
28
29// This is just convenience. It lets you use either return "foo" or return SkStringPrintf(...).
30struct ImplicitString : public SkString {
31 template <typename T>
32 ImplicitString(const T& s) : SkString(s) {}
33 ImplicitString() : SkString("") {}
34};
35typedef ImplicitString Name;
36typedef ImplicitString Path;
37
38class Error {
39public:
40 Error(const SkString& s) : fMsg(s), fFatal(!this->isEmpty()) {}
41 Error(const char* s) : fMsg(s), fFatal(!this->isEmpty()) {}
42
43 Error(const Error&) = default;
44 Error& operator=(const Error&) = default;
45
46 static Error Nonfatal(const SkString& s) { return Nonfatal(s.c_str()); }
47 static Error Nonfatal(const char* s) {
48 Error e(s);
49 e.fFatal = false;
50 return e;
51 }
52
53 const char* c_str() const { return fMsg.c_str(); }
54 bool isEmpty() const { return fMsg.isEmpty(); }
55 bool isFatal() const { return fFatal; }
56
57private:
58 SkString fMsg;
59 bool fFatal;
60};
61
62struct SinkFlags {
63 enum Type { kNull, kGPU, kVector, kRaster } type;
64 enum Approach { kDirect, kIndirect } approach;
65 enum Multisampled { kNotMultisampled, kMultisampled } multisampled;
66 SinkFlags(Type t, Approach a, Multisampled ms = kNotMultisampled)
67 : type(t), approach(a), multisampled(ms) {}
68};
69
70struct Src {
71 virtual ~Src() {}
72 virtual Error SK_WARN_UNUSED_RESULT draw(SkCanvas*) const = 0;
73 virtual SkISize size() const = 0;
74 virtual Name name() const = 0;
75 virtual void modifyGrContextOptions(GrContextOptions* options) const {}
76 virtual bool veto(SinkFlags) const { return false; }
77
78 virtual int pageCount() const { return 1; }
79 virtual Error SK_WARN_UNUSED_RESULT draw(int, SkCanvas* canvas) const {
80 return this->draw(canvas);
81 }
82 virtual SkISize size(int) const { return this->size(); }
83 // Force Tasks using this Src to run on the main thread?
84 virtual bool serial() const { return false; }
85};
86
87struct Sink {
88 virtual ~Sink() {}
89 // You may write to either the bitmap or stream. If you write to log, we'll print that out.
90 virtual Error SK_WARN_UNUSED_RESULT draw(const Src&, SkBitmap*, SkWStream*, SkString* log)
91 const = 0;
92
93 // Force Tasks using this Sink to run on the main thread?
94 virtual bool serial() const { return false; }
95
96 // File extension for the content draw() outputs, e.g. "png", "pdf".
97 virtual const char* fileExtension() const = 0;
98
99 virtual SinkFlags flags() const = 0;
100};
101
102/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
103
104class GMSrc : public Src {
105public:
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700106 explicit GMSrc(skiagm::GMFactory);
Andrew Top200ce4b2018-01-29 13:43:50 -0800107
108 Error draw(SkCanvas*) const override;
109 SkISize size() const override;
110 Name name() const override;
111 void modifyGrContextOptions(GrContextOptions* options) const override;
112
113private:
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700114 skiagm::GMFactory fFactory;
Andrew Top200ce4b2018-01-29 13:43:50 -0800115};
116
117class CodecSrc : public Src {
118public:
119 enum Mode {
120 kCodec_Mode,
121 // We choose to test only one mode with zero initialized memory.
122 // This will exercise all of the interesting cases in SkSwizzler
123 // without doubling the size of our test suite.
124 kCodecZeroInit_Mode,
125 kScanline_Mode,
126 kStripe_Mode, // Tests the skipping of scanlines
127 kCroppedScanline_Mode, // Tests (jpeg) cropped scanline optimization
128 kSubset_Mode, // For codecs that support subsets directly.
129 kAnimated_Mode, // For codecs that support animation.
130 };
131 enum DstColorType {
132 kGetFromCanvas_DstColorType,
133 kGrayscale_Always_DstColorType,
134 kNonNative8888_Always_DstColorType,
135 };
136 CodecSrc(Path, Mode, DstColorType, SkAlphaType, float);
137
138 Error draw(SkCanvas*) const override;
139 SkISize size() const override;
140 Name name() const override;
141 bool veto(SinkFlags) const override;
142 bool serial() const override { return fRunSerially; }
143private:
144 Path fPath;
145 Mode fMode;
146 DstColorType fDstColorType;
147 SkAlphaType fDstAlphaType;
148 float fScale;
149 bool fRunSerially;
150};
151
152class AndroidCodecSrc : public Src {
153public:
154 AndroidCodecSrc(Path, CodecSrc::DstColorType, SkAlphaType, int sampleSize);
155
156 Error draw(SkCanvas*) const override;
157 SkISize size() const override;
158 Name name() const override;
159 bool veto(SinkFlags) const override;
160 bool serial() const override { return fRunSerially; }
161private:
162 Path fPath;
163 CodecSrc::DstColorType fDstColorType;
164 SkAlphaType fDstAlphaType;
165 int fSampleSize;
166 bool fRunSerially;
167};
168
169// Allows for testing of various implementations of Android's BitmapRegionDecoder
170class BRDSrc : public Src {
171public:
172 enum Mode {
173 // Decode the entire image as one region.
174 kFullImage_Mode,
175 // Splits the image into multiple regions using a divisor and decodes the regions
176 // separately. Also, this test adds a border of a few pixels to each of the regions
177 // that it is decoding. This tests the behavior when a client asks for a region that
178 // does not fully fit in the image.
179 kDivisor_Mode,
180 };
181
182 BRDSrc(Path, Mode, CodecSrc::DstColorType, uint32_t);
183
184 Error draw(SkCanvas*) const override;
185 SkISize size() const override;
186 Name name() const override;
187 bool veto(SinkFlags) const override;
188private:
189 Path fPath;
190 Mode fMode;
191 CodecSrc::DstColorType fDstColorType;
192 uint32_t fSampleSize;
193};
194
195class ImageGenSrc : public Src {
196public:
197 enum Mode {
198 kCodec_Mode, // Use CodecImageGenerator
199 kPlatform_Mode, // Uses CG or WIC
200 };
201 ImageGenSrc(Path, Mode, SkAlphaType, bool);
202
203 Error draw(SkCanvas*) const override;
204 SkISize size() const override;
205 Name name() const override;
206 bool veto(SinkFlags) const override;
207 bool serial() const override { return fRunSerially; }
208private:
209 Path fPath;
210 Mode fMode;
211 SkAlphaType fDstAlphaType;
212 bool fIsGpu;
213 bool fRunSerially;
214};
215
216class ColorCodecSrc : public Src {
217public:
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700218 ColorCodecSrc(Path, bool decode_to_dst);
Andrew Top200ce4b2018-01-29 13:43:50 -0800219
220 Error draw(SkCanvas*) const override;
221 SkISize size() const override;
222 Name name() const override;
223 bool veto(SinkFlags) const override;
224private:
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700225 Path fPath;
226 bool fDecodeToDst;
Andrew Top200ce4b2018-01-29 13:43:50 -0800227};
228
229class SKPSrc : public Src {
230public:
231 explicit SKPSrc(Path path);
232
233 Error draw(SkCanvas*) const override;
234 SkISize size() const override;
235 Name name() const override;
236private:
237 Path fPath;
238};
239
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700240// This class extracts all the paths from an SKP and then removes unwanted paths according to the
241// provided l/r trail. It then just draws the remaining paths. (Non-path draws are thrown out.) It
242// is useful for finding a reduced repo case for path drawing bugs.
243class BisectSrc : public SKPSrc {
244public:
245 explicit BisectSrc(Path path, const char* trail);
246
247 Error draw(SkCanvas*) const override;
248
249private:
250 SkString fTrail;
251
252 typedef SKPSrc INHERITED;
253};
254
255
256#if defined(SK_ENABLE_SKOTTIE)
257class SkottieSrc final : public Src {
258public:
259 explicit SkottieSrc(Path path);
260
261 Error draw(SkCanvas*) const override;
262 SkISize size() const override;
263 Name name() const override;
264 bool veto(SinkFlags) const override;
265
266private:
267 // Generates a kTileCount x kTileCount filmstrip with evenly distributed frames.
268 static constexpr int kTileCount = 5;
269
270 // Fit kTileCount x kTileCount frames to a 1000x1000 film strip.
271 static constexpr SkScalar kTargetSize = 1000;
272 static constexpr SkScalar kTileSize = kTargetSize / kTileCount;
273
274 Path fPath;
275};
276#endif
277
Andrew Top200ce4b2018-01-29 13:43:50 -0800278#if defined(SK_XML)
279} // namespace DM
280
281class SkSVGDOM;
282
283namespace DM {
284
285class SVGSrc : public Src {
286public:
287 explicit SVGSrc(Path path);
288
289 Error draw(SkCanvas*) const override;
290 SkISize size() const override;
291 Name name() const override;
292 bool veto(SinkFlags) const override;
293
294private:
295 Name fName;
296 sk_sp<SkSVGDOM> fDom;
297 SkScalar fScale;
298
299 typedef Src INHERITED;
300};
301#endif // SK_XML
302/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
303
304class MSKPSrc : public Src {
305public:
306 explicit MSKPSrc(Path path);
307
308 int pageCount() const override;
309 Error draw(SkCanvas* c) const override;
310 Error draw(int, SkCanvas*) const override;
311 SkISize size() const override;
312 SkISize size(int) const override;
313 Name name() const override;
314
315private:
316 Path fPath;
317 mutable SkTArray<SkDocumentPage> fPages;
318};
319
320/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
321
322class NullSink : public Sink {
323public:
324 NullSink() {}
325
326 Error draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const override;
327 const char* fileExtension() const override { return ""; }
328 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kNull, SinkFlags::kDirect }; }
329};
330
Andrew Top200ce4b2018-01-29 13:43:50 -0800331class GPUSink : public Sink {
332public:
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700333 GPUSink(const SkCommandLineConfigGpu*, const GrContextOptions&);
Andrew Top200ce4b2018-01-29 13:43:50 -0800334
335 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700336 Error onDraw(const Src&, SkBitmap*, SkWStream*, SkString*,
337 const GrContextOptions& baseOptions,
338 std::function<void(GrContext*)> initContext = nullptr) const;
339
340 sk_gpu_test::GrContextFactory::ContextType contextType() const { return fContextType; }
341 const sk_gpu_test::GrContextFactory::ContextOverrides& contextOverrides() {
342 return fContextOverrides;
343 }
344 SkCommandLineConfigGpu::SurfType surfType() const { return fSurfType; }
345 bool useDIText() const { return fUseDIText; }
346 bool serial() const override { return true; }
Andrew Top200ce4b2018-01-29 13:43:50 -0800347 const char* fileExtension() const override { return "png"; }
348 SinkFlags flags() const override {
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700349 SinkFlags::Multisampled ms = fSampleCount > 1 ? SinkFlags::kMultisampled
Andrew Top200ce4b2018-01-29 13:43:50 -0800350 : SinkFlags::kNotMultisampled;
351 return SinkFlags{ SinkFlags::kGPU, SinkFlags::kDirect, ms };
352 }
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700353 const GrContextOptions& baseContextOptions() const { return fBaseContextOptions; }
354
Andrew Top200ce4b2018-01-29 13:43:50 -0800355private:
356 sk_gpu_test::GrContextFactory::ContextType fContextType;
357 sk_gpu_test::GrContextFactory::ContextOverrides fContextOverrides;
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700358 SkCommandLineConfigGpu::SurfType fSurfType;
Andrew Top200ce4b2018-01-29 13:43:50 -0800359 int fSampleCount;
360 bool fUseDIText;
361 SkColorType fColorType;
362 SkAlphaType fAlphaType;
363 sk_sp<SkColorSpace> fColorSpace;
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700364 GrContextOptions fBaseContextOptions;
365 sk_gpu_test::MemoryCache fMemoryCache;
366};
367
368class GPUThreadTestingSink : public GPUSink {
369public:
370 GPUThreadTestingSink(const SkCommandLineConfigGpu*, const GrContextOptions&);
371
372 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
373
374 const char* fileExtension() const override {
375 // Suppress writing out results from this config - we just want to do our matching test
376 return nullptr;
377 }
378
379private:
380 std::unique_ptr<SkExecutor> fExecutor;
381
382 typedef GPUSink INHERITED;
383};
384
385class GPUPersistentCacheTestingSink : public GPUSink {
386public:
387 GPUPersistentCacheTestingSink(const SkCommandLineConfigGpu*, const GrContextOptions&);
388
389 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
390
391 const char* fileExtension() const override {
392 // Suppress writing out results from this config - we just want to do our matching test
393 return nullptr;
394 }
395
396private:
397 int fCacheType;
398
399 typedef GPUSink INHERITED;
400};
401
402class GPUPrecompileTestingSink : public GPUSink {
403public:
404 GPUPrecompileTestingSink(const SkCommandLineConfigGpu*, const GrContextOptions&);
405
406 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
407
408 const char* fileExtension() const override {
409 // Suppress writing out results from this config - we just want to do our matching test
410 return nullptr;
411 }
412
413private:
414 typedef GPUSink INHERITED;
Andrew Top200ce4b2018-01-29 13:43:50 -0800415};
416
417class PDFSink : public Sink {
418public:
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700419 PDFSink(bool pdfa, SkScalar rasterDpi) : fPDFA(pdfa), fRasterDpi(rasterDpi) {}
Andrew Top200ce4b2018-01-29 13:43:50 -0800420 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
421 const char* fileExtension() const override { return "pdf"; }
422 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kVector, SinkFlags::kDirect }; }
423 bool fPDFA;
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700424 SkScalar fRasterDpi;
Andrew Top200ce4b2018-01-29 13:43:50 -0800425};
426
427class XPSSink : public Sink {
428public:
429 XPSSink();
430
431 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
432 const char* fileExtension() const override { return "xps"; }
433 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kVector, SinkFlags::kDirect }; }
434};
435
Andrew Top200ce4b2018-01-29 13:43:50 -0800436class RasterSink : public Sink {
437public:
438 explicit RasterSink(SkColorType, sk_sp<SkColorSpace> = nullptr);
439
440 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
441 const char* fileExtension() const override { return "png"; }
442 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kRaster, SinkFlags::kDirect }; }
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700443
Andrew Top200ce4b2018-01-29 13:43:50 -0800444private:
445 SkColorType fColorType;
446 sk_sp<SkColorSpace> fColorSpace;
447};
448
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700449class ThreadedSink : public RasterSink {
450public:
451 explicit ThreadedSink(SkColorType, sk_sp<SkColorSpace> = nullptr);
452 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
453};
454
Andrew Top200ce4b2018-01-29 13:43:50 -0800455class SKPSink : public Sink {
456public:
457 SKPSink();
458
459 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
460 const char* fileExtension() const override { return "skp"; }
461 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kVector, SinkFlags::kDirect }; }
462};
463
464class DebugSink : public Sink {
465public:
466 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
467 const char* fileExtension() const override { return "json"; }
468 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kVector, SinkFlags::kDirect }; }
469};
470
471class SVGSink : public Sink {
472public:
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700473 SVGSink(int pageIndex = 0);
Andrew Top200ce4b2018-01-29 13:43:50 -0800474
475 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
476 const char* fileExtension() const override { return "svg"; }
477 SinkFlags flags() const override { return SinkFlags{ SinkFlags::kVector, SinkFlags::kDirect }; }
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700478
479private:
480 int fPageIndex;
Andrew Top200ce4b2018-01-29 13:43:50 -0800481};
482
483
484/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
485
486class Via : public Sink {
487public:
488 explicit Via(Sink* sink) : fSink(sink) {}
489 const char* fileExtension() const override { return fSink->fileExtension(); }
490 bool serial() const override { return fSink->serial(); }
491 SinkFlags flags() const override {
492 SinkFlags flags = fSink->flags();
493 flags.approach = SinkFlags::kIndirect;
494 return flags;
495 }
496protected:
497 std::unique_ptr<Sink> fSink;
498};
499
500class ViaMatrix : public Via {
501public:
502 ViaMatrix(SkMatrix, Sink*);
503 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
504private:
505 const SkMatrix fMatrix;
506};
507
508class ViaUpright : public Via {
509public:
510 ViaUpright(SkMatrix, Sink*);
511 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
512private:
513 const SkMatrix fMatrix;
514};
515
516class ViaSerialization : public Via {
517public:
518 explicit ViaSerialization(Sink* sink) : Via(sink) {}
519 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
520};
521
522class ViaPicture : public Via {
523public:
524 explicit ViaPicture(Sink* sink) : Via(sink) {}
525 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
526};
527
Andrew Top200ce4b2018-01-29 13:43:50 -0800528class ViaTiles : public Via {
529public:
530 ViaTiles(int w, int h, SkBBHFactory*, Sink*);
531 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
532private:
533 const int fW, fH;
534 std::unique_ptr<SkBBHFactory> fFactory;
535};
536
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700537class ViaDDL : public Via {
Andrew Top200ce4b2018-01-29 13:43:50 -0800538public:
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700539 ViaDDL(int numReplays, int numDivisions, Sink* sink);
Andrew Top200ce4b2018-01-29 13:43:50 -0800540 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
Xiaoming Shi73dfa202020-03-12 11:31:35 -0700541private:
542 const int fNumReplays;
543 const int fNumDivisions;
Andrew Top200ce4b2018-01-29 13:43:50 -0800544};
545
546class ViaSVG : public Via {
547public:
548 explicit ViaSVG(Sink* sink) : Via(sink) {}
549 Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override;
550};
551
Andrew Top200ce4b2018-01-29 13:43:50 -0800552} // namespace DM
553
554#endif//DMSrcSink_DEFINED