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, |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 101 | }; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 102 | enum DstColorType { |
| 103 | kGetFromCanvas_DstColorType, |
| 104 | kIndex8_Always_DstColorType, |
| 105 | kGrayscale_Always_DstColorType, |
| 106 | }; |
| 107 | CodecSrc(Path, Mode, DstColorType); |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 108 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 109 | Error draw(SkCanvas*) const override; |
| 110 | SkISize size() const override; |
| 111 | Name name() const override; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 112 | private: |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 113 | Path fPath; |
| 114 | Mode fMode; |
| 115 | DstColorType fDstColorType; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 119 | class ImageSrc : public Src { |
| 120 | public: |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 121 | // divisor == 0 means decode the whole image |
| 122 | // divisor > 0 means decode in subsets, dividing into a divisor x divisor grid. |
| 123 | explicit ImageSrc(Path path, int divisor = 0); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 124 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 125 | Error draw(SkCanvas*) const override; |
| 126 | SkISize size() const override; |
| 127 | Name name() const override; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 128 | private: |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 129 | Path fPath; |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 130 | const int fDivisor; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | class SKPSrc : public Src { |
| 134 | public: |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 135 | explicit SKPSrc(Path path); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 136 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 137 | Error draw(SkCanvas*) const override; |
| 138 | SkISize size() const override; |
| 139 | Name name() const override; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 140 | private: |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 141 | Path fPath; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 145 | |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 146 | class NullSink : public Sink { |
| 147 | public: |
| 148 | NullSink() {} |
| 149 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 150 | Error draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const override; |
| 151 | int enclave() const override { return kAnyThread_Enclave; } |
| 152 | const char* fileExtension() const override { return ""; } |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 156 | class GPUSink : public Sink { |
| 157 | public: |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 158 | GPUSink(GrContextFactory::GLContextType, GrGLStandard, int samples, bool dfText, bool threaded); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 159 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 160 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 161 | int enclave() const override; |
| 162 | const char* fileExtension() const override { return "png"; } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 163 | private: |
| 164 | GrContextFactory::GLContextType fContextType; |
| 165 | GrGLStandard fGpuAPI; |
| 166 | int fSampleCount; |
| 167 | bool fUseDFText; |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 168 | bool fThreaded; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | class PDFSink : public Sink { |
| 172 | public: |
| 173 | PDFSink(); |
| 174 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 175 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 176 | int enclave() const override { return kAnyThread_Enclave; } |
| 177 | const char* fileExtension() const override { return "pdf"; } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 178 | }; |
| 179 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 180 | class XPSSink : public Sink { |
| 181 | public: |
| 182 | XPSSink(); |
| 183 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 184 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 185 | int enclave() const override { return kAnyThread_Enclave; } |
| 186 | const char* fileExtension() const override { return "xps"; } |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 187 | }; |
| 188 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 189 | class RasterSink : public Sink { |
| 190 | public: |
| 191 | explicit RasterSink(SkColorType); |
| 192 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 193 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 194 | int enclave() const override { return kAnyThread_Enclave; } |
| 195 | const char* fileExtension() const override { return "png"; } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 196 | private: |
| 197 | SkColorType fColorType; |
| 198 | }; |
| 199 | |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 200 | class SKPSink : public Sink { |
| 201 | public: |
| 202 | SKPSink(); |
| 203 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 204 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 205 | int enclave() const override { return kAnyThread_Enclave; } |
| 206 | const char* fileExtension() const override { return "skp"; } |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 207 | }; |
| 208 | |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 209 | class SVGSink : public Sink { |
| 210 | public: |
| 211 | SVGSink(); |
| 212 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 213 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 214 | int enclave() const override { return kAnyThread_Enclave; } |
| 215 | const char* fileExtension() const override { return "svg"; } |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 219 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 220 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 221 | class Via : public Sink { |
| 222 | public: |
| 223 | explicit Via(Sink* sink) : fSink(sink) {} |
| 224 | const char* fileExtension() const override { return fSink->fileExtension(); } |
| 225 | int enclave() const override { return fSink->enclave(); } |
| 226 | protected: |
| 227 | SkAutoTDelete<Sink> fSink; |
| 228 | }; |
| 229 | |
| 230 | class ViaMatrix : public Via { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 231 | public: |
| 232 | ViaMatrix(SkMatrix, Sink*); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 233 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 234 | private: |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 235 | const SkMatrix fMatrix; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 236 | }; |
| 237 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 238 | class ViaUpright : public Via { |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 239 | public: |
| 240 | ViaUpright(SkMatrix, Sink*); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 241 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 242 | private: |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 243 | const SkMatrix fMatrix; |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 244 | }; |
| 245 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 246 | class ViaPipe : public Via { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 247 | public: |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 248 | explicit ViaPipe(Sink* sink) : Via(sink) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 249 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 250 | }; |
| 251 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 252 | class ViaDeferred : public Via { |
reed | 06a22f6 | 2015-05-05 08:11:33 -0700 | [diff] [blame] | 253 | public: |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 254 | explicit ViaDeferred(Sink* sink) : Via(sink) {} |
reed | 06a22f6 | 2015-05-05 08:11:33 -0700 | [diff] [blame] | 255 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
reed | 06a22f6 | 2015-05-05 08:11:33 -0700 | [diff] [blame] | 256 | }; |
| 257 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 258 | class ViaSerialization : public Via { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 259 | public: |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 260 | explicit ViaSerialization(Sink* sink) : Via(sink) {} |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 261 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 262 | }; |
| 263 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 264 | class ViaTiles : public Via { |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 265 | public: |
| 266 | ViaTiles(int w, int h, SkBBHFactory*, Sink*); |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 267 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 268 | private: |
| 269 | const int fW, fH; |
| 270 | SkAutoTDelete<SkBBHFactory> fFactory; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 271 | }; |
| 272 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 273 | class ViaSecondPicture : public Via { |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 274 | public: |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 275 | explicit ViaSecondPicture(Sink* sink) : Via(sink) {} |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 276 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
mtklein | b7e8d69 | 2015-04-07 08:30:32 -0700 | [diff] [blame] | 277 | }; |
| 278 | |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 279 | class ViaSingletonPictures : public Via { |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 280 | public: |
mtklein | 7882924 | 2015-05-06 07:54:07 -0700 | [diff] [blame] | 281 | explicit ViaSingletonPictures(Sink* sink) : Via(sink) {} |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 282 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
mtklein | d31c13d | 2015-05-05 12:59:56 -0700 | [diff] [blame] | 283 | }; |
| 284 | |
mtklein | 6fbf4b3 | 2015-05-06 11:35:40 -0700 | [diff] [blame] | 285 | class ViaTwice : public Via { |
| 286 | public: |
| 287 | explicit ViaTwice(Sink* sink) : Via(sink) {} |
| 288 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 289 | }; |
| 290 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 291 | } // namespace DM |
| 292 | |
| 293 | #endif//DMSrcSink_DEFINED |