scroggo | 478652e | 2015-03-25 07:11:02 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 8 | #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" |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 16 | #include "SkCodec.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 17 | #include "SkData.h" |
| 18 | #include "SkGPipe.h" |
| 19 | #include "SkPicture.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 20 | #include "gm.h" |
| 21 | |
| 22 | namespace DM { |
| 23 | |
| 24 | // This is just convenience. It lets you use either return "foo" or return SkStringPrintf(...). |
| 25 | struct ImplicitString : public SkString { |
| 26 | template <typename T> |
| 27 | ImplicitString(const T& s) : SkString(s) {} |
| 28 | }; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 29 | typedef ImplicitString Name; |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 30 | typedef ImplicitString Path; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 31 | |
mtklein | 4089ef7 | 2015-03-05 08:40:28 -0800 | [diff] [blame] | 32 | class Error { |
| 33 | public: |
| 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 | |
| 51 | private: |
| 52 | SkString fMsg; |
| 53 | bool fFatal; |
| 54 | }; |
| 55 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 56 | struct 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; |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 62 | virtual void modifyGrContextOptions(GrContextOptions* options) const {} |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | struct Sink { |
| 66 | virtual ~Sink() {} |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 67 | // 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; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 70 | // 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 | |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 77 | enum { kAnyThread_Enclave, kGPU_Enclave }; |
| 78 | static const int kNumEnclaves = kGPU_Enclave + 1; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 79 | |
| 80 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 81 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 82 | class GMSrc : public Src { |
| 83 | public: |
| 84 | explicit GMSrc(skiagm::GMRegistry::Factory); |
| 85 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 86 | Error draw(SkCanvas*) const override; |
| 87 | SkISize size() const override; |
| 88 | Name name() const override; |
bsalomon | 4ee6bd8 | 2015-05-27 13:23:23 -0700 | [diff] [blame] | 89 | void modifyGrContextOptions(GrContextOptions* options) const override; |
| 90 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 91 | private: |
| 92 | skiagm::GMRegistry::Factory fFactory; |
| 93 | }; |
| 94 | |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 95 | class CodecSrc : public Src { |
| 96 | public: |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 97 | enum Mode { |
| 98 | kNormal_Mode, |
| 99 | kScanline_Mode, |
emmaleer | 9700206 | 2015-05-27 12:36:10 -0700 | [diff] [blame] | 100 | kScanline_Subset_Mode, |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 101 | kStripe_Mode, // Tests the skipping of scanlines |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 102 | }; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 103 | enum DstColorType { |
| 104 | kGetFromCanvas_DstColorType, |
| 105 | kIndex8_Always_DstColorType, |
| 106 | kGrayscale_Always_DstColorType, |
| 107 | }; |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 108 | CodecSrc(Path, Mode, DstColorType, float); |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 109 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 110 | Error draw(SkCanvas*) const override; |
| 111 | SkISize size() const override; |
| 112 | Name name() const override; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 113 | private: |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 114 | Path fPath; |
| 115 | Mode fMode; |
| 116 | DstColorType fDstColorType; |
msarett | 0a24297 | 2015-06-11 14:27:27 -0700 | [diff] [blame] | 117 | float fScale; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 121 | class ImageSrc : public Src { |
| 122 | public: |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 123 | // 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); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 126 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 127 | Error draw(SkCanvas*) const override; |
| 128 | SkISize size() const override; |
| 129 | Name name() const override; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 130 | private: |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 131 | Path fPath; |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 132 | const int fDivisor; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | class SKPSrc : public Src { |
| 136 | public: |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 137 | explicit SKPSrc(Path path); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 138 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 139 | Error draw(SkCanvas*) const override; |
| 140 | SkISize size() const override; |
| 141 | Name name() const override; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 142 | private: |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 143 | Path fPath; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 147 | |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 148 | class NullSink : public Sink { |
| 149 | public: |
| 150 | NullSink() {} |
| 151 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 152 | 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 ""; } |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 158 | class GPUSink : public Sink { |
| 159 | public: |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 160 | GPUSink(GrContextFactory::GLContextType, GrGLStandard, int samples, bool dfText, bool threaded); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 161 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 162 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 163 | int enclave() const override; |
| 164 | const char* fileExtension() const override { return "png"; } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 165 | private: |
| 166 | GrContextFactory::GLContextType fContextType; |
| 167 | GrGLStandard fGpuAPI; |
| 168 | int fSampleCount; |
| 169 | bool fUseDFText; |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 170 | bool fThreaded; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | class PDFSink : public Sink { |
| 174 | public: |
| 175 | PDFSink(); |
| 176 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 177 | 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"; } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 182 | class XPSSink : public Sink { |
| 183 | public: |
| 184 | XPSSink(); |
| 185 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 186 | 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"; } |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 189 | }; |
| 190 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 191 | class RasterSink : public Sink { |
| 192 | public: |
| 193 | explicit RasterSink(SkColorType); |
| 194 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 195 | 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"; } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 198 | private: |
| 199 | SkColorType fColorType; |
| 200 | }; |
| 201 | |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 202 | class SKPSink : public Sink { |
| 203 | public: |
| 204 | SKPSink(); |
| 205 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 206 | 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"; } |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 209 | }; |
| 210 | |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 211 | class SVGSink : public Sink { |
| 212 | public: |
| 213 | SVGSink(); |
| 214 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 215 | 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"; } |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 221 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 222 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 223 | class Via : public Sink { |
| 224 | public: |
| 225 | explicit Via(Sink* sink) : fSink(sink) {} |
| 226 | const char* fileExtension() const override { return fSink->fileExtension(); } |
| 227 | int enclave() const override { return fSink->enclave(); } |
| 228 | protected: |
| 229 | SkAutoTDelete<Sink> fSink; |
| 230 | }; |
| 231 | |
| 232 | class ViaMatrix : public Via { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 233 | public: |
| 234 | ViaMatrix(SkMatrix, Sink*); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 235 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 236 | private: |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 237 | const SkMatrix fMatrix; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 238 | }; |
| 239 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 240 | class ViaUpright : public Via { |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 241 | public: |
| 242 | ViaUpright(SkMatrix, Sink*); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 243 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 244 | private: |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 245 | const SkMatrix fMatrix; |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 246 | }; |
| 247 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 248 | class ViaPipe : public Via { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 249 | public: |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 250 | explicit ViaPipe(Sink* sink) : Via(sink) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 251 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 252 | }; |
| 253 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 254 | class ViaDeferred : public Via { |
reed | 06a22f6 | 2015-05-05 08:11:33 -0700 | [diff] [blame] | 255 | public: |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 256 | explicit ViaDeferred(Sink* sink) : Via(sink) {} |
reed | 06a22f6 | 2015-05-05 08:11:33 -0700 | [diff] [blame] | 257 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
reed | 06a22f6 | 2015-05-05 08:11:33 -0700 | [diff] [blame] | 258 | }; |
| 259 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 260 | class ViaSerialization : public Via { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 261 | public: |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 262 | explicit ViaSerialization(Sink* sink) : Via(sink) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 263 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 264 | }; |
| 265 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 266 | class ViaTiles : public Via { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 267 | public: |
| 268 | ViaTiles(int w, int h, SkBBHFactory*, Sink*); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 269 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 270 | private: |
| 271 | const int fW, fH; |
| 272 | SkAutoTDelete<SkBBHFactory> fFactory; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 273 | }; |
| 274 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 275 | class ViaSecondPicture : public Via { |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 276 | public: |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 277 | explicit ViaSecondPicture(Sink* sink) : Via(sink) {} |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 278 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 279 | }; |
| 280 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 281 | class ViaSingletonPictures : public Via { |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 282 | public: |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 283 | explicit ViaSingletonPictures(Sink* sink) : Via(sink) {} |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 284 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 285 | }; |
| 286 | |
mtklein | 6fbf4b3 | 2015-05-06 11:35:40 -0700 | [diff] [blame] | 287 | class ViaTwice : public Via { |
| 288 | public: |
| 289 | explicit ViaTwice(Sink* sink) : Via(sink) {} |
| 290 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 291 | }; |
| 292 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 293 | } // namespace DM |
| 294 | |
| 295 | #endif//DMSrcSink_DEFINED |