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" |
| 16 | #include "SkData.h" |
| 17 | #include "SkGPipe.h" |
| 18 | #include "SkPicture.h" |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 19 | #include "gm.h" |
| 20 | |
| 21 | namespace DM { |
| 22 | |
| 23 | // This is just convenience. It lets you use either return "foo" or return SkStringPrintf(...). |
| 24 | struct ImplicitString : public SkString { |
| 25 | template <typename T> |
| 26 | ImplicitString(const T& s) : SkString(s) {} |
| 27 | }; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 28 | typedef ImplicitString Name; |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 29 | typedef ImplicitString Path; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 30 | |
mtklein | 4089ef7 | 2015-03-05 08:40:28 -0800 | [diff] [blame] | 31 | class Error { |
| 32 | public: |
| 33 | Error(const SkString& s) : fMsg(s), fFatal(!this->isEmpty()) {} |
| 34 | Error(const char* s) : fMsg(s), fFatal(!this->isEmpty()) {} |
| 35 | |
| 36 | Error(const Error&) = default; |
| 37 | Error& operator=(const Error&) = default; |
| 38 | |
| 39 | static Error Nonfatal(const SkString& s) { return Nonfatal(s.c_str()); } |
| 40 | static Error Nonfatal(const char* s) { |
| 41 | Error e(s); |
| 42 | e.fFatal = false; |
| 43 | return e; |
| 44 | } |
| 45 | |
| 46 | const char* c_str() const { return fMsg.c_str(); } |
| 47 | bool isEmpty() const { return fMsg.isEmpty(); } |
| 48 | bool isFatal() const { return fFatal; } |
| 49 | |
| 50 | private: |
| 51 | SkString fMsg; |
| 52 | bool fFatal; |
| 53 | }; |
| 54 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 55 | struct Src { |
| 56 | // All Srcs must be thread safe. |
| 57 | virtual ~Src() {} |
| 58 | virtual Error SK_WARN_UNUSED_RESULT draw(SkCanvas*) const = 0; |
| 59 | virtual SkISize size() const = 0; |
| 60 | virtual Name name() const = 0; |
| 61 | }; |
| 62 | |
| 63 | struct Sink { |
| 64 | virtual ~Sink() {} |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 65 | // You may write to either the bitmap or stream. If you write to log, we'll print that out. |
| 66 | virtual Error SK_WARN_UNUSED_RESULT draw(const Src&, SkBitmap*, SkWStream*, SkString* log) |
| 67 | const = 0; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 68 | // Sinks in the same enclave (except kAnyThread_Enclave) will run serially on the same thread. |
| 69 | virtual int enclave() const = 0; |
| 70 | |
| 71 | // File extension for the content draw() outputs, e.g. "png", "pdf". |
| 72 | virtual const char* fileExtension() const = 0; |
| 73 | }; |
| 74 | |
halcanary | 792c80f | 2015-02-20 07:21:05 -0800 | [diff] [blame] | 75 | enum { kAnyThread_Enclave, kGPU_Enclave }; |
| 76 | static const int kNumEnclaves = kGPU_Enclave + 1; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 77 | |
| 78 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 79 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 80 | class GMSrc : public Src { |
| 81 | public: |
| 82 | explicit GMSrc(skiagm::GMRegistry::Factory); |
| 83 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 84 | Error draw(SkCanvas*) const override; |
| 85 | SkISize size() const override; |
| 86 | Name name() const override; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 87 | private: |
| 88 | skiagm::GMRegistry::Factory fFactory; |
| 89 | }; |
| 90 | |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 91 | class CodecSrc : public Src { |
| 92 | public: |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 93 | enum Mode { |
| 94 | kNormal_Mode, |
| 95 | kScanline_Mode, |
| 96 | }; |
| 97 | CodecSrc(Path, Mode); |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 98 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 99 | Error draw(SkCanvas*) const override; |
| 100 | SkISize size() const override; |
| 101 | Name name() const override; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 102 | private: |
| 103 | Path fPath; |
scroggo | 9c59ebc | 2015-03-25 13:48:49 -0700 | [diff] [blame] | 104 | Mode fMode; |
scroggo | 9b77ddd | 2015-03-19 06:03:39 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 108 | class ImageSrc : public Src { |
| 109 | public: |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 110 | // divisor == 0 means decode the whole image |
| 111 | // divisor > 0 means decode in subsets, dividing into a divisor x divisor grid. |
| 112 | explicit ImageSrc(Path path, int divisor = 0); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 113 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 114 | Error draw(SkCanvas*) const override; |
| 115 | SkISize size() const override; |
| 116 | Name name() const override; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 117 | private: |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 118 | Path fPath; |
mtklein | edc93bc | 2015-01-30 13:22:23 -0800 | [diff] [blame] | 119 | const int fDivisor; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | class SKPSrc : public Src { |
| 123 | public: |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 124 | explicit SKPSrc(Path path); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 125 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 126 | Error draw(SkCanvas*) const override; |
| 127 | SkISize size() const override; |
| 128 | Name name() const override; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 129 | private: |
mtklein | 8d17a13 | 2015-01-30 11:42:31 -0800 | [diff] [blame] | 130 | Path fPath; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 134 | |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 135 | class NullSink : public Sink { |
| 136 | public: |
| 137 | NullSink() {} |
| 138 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 139 | Error draw(const Src& src, SkBitmap*, SkWStream*, SkString*) const override; |
| 140 | int enclave() const override { return kAnyThread_Enclave; } |
| 141 | const char* fileExtension() const override { return ""; } |
mtklein | ad66f9b | 2015-02-13 15:11:10 -0800 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 145 | class GPUSink : public Sink { |
| 146 | public: |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 147 | GPUSink(GrContextFactory::GLContextType, GrGLStandard, int samples, bool dfText, bool threaded); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 148 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 149 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 150 | int enclave() const override; |
| 151 | const char* fileExtension() const override { return "png"; } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 152 | private: |
| 153 | GrContextFactory::GLContextType fContextType; |
| 154 | GrGLStandard fGpuAPI; |
| 155 | int fSampleCount; |
| 156 | bool fUseDFText; |
mtklein | 82d2843 | 2015-01-15 12:46:02 -0800 | [diff] [blame] | 157 | bool fThreaded; |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | class PDFSink : public Sink { |
| 161 | public: |
| 162 | PDFSink(); |
| 163 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 164 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 165 | int enclave() const override { return kAnyThread_Enclave; } |
| 166 | const char* fileExtension() const override { return "pdf"; } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 167 | }; |
| 168 | |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 169 | class XPSSink : public Sink { |
| 170 | public: |
| 171 | XPSSink(); |
| 172 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 173 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 174 | int enclave() const override { return kAnyThread_Enclave; } |
| 175 | const char* fileExtension() const override { return "xps"; } |
halcanary | 47ef4d5 | 2015-03-03 09:13:09 -0800 | [diff] [blame] | 176 | }; |
| 177 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 178 | class RasterSink : public Sink { |
| 179 | public: |
| 180 | explicit RasterSink(SkColorType); |
| 181 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 182 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 183 | int enclave() const override { return kAnyThread_Enclave; } |
| 184 | const char* fileExtension() const override { return "png"; } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 185 | private: |
| 186 | SkColorType fColorType; |
| 187 | }; |
| 188 | |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 189 | class SKPSink : public Sink { |
| 190 | public: |
| 191 | SKPSink(); |
| 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 "skp"; } |
mtklein | 9c3f17d | 2015-01-28 11:35:18 -0800 | [diff] [blame] | 196 | }; |
| 197 | |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 198 | class SVGSink : public Sink { |
| 199 | public: |
| 200 | SVGSink(); |
| 201 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 202 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 203 | int enclave() const override { return kAnyThread_Enclave; } |
| 204 | const char* fileExtension() const override { return "svg"; } |
mtklein | 8a4527e | 2015-01-31 20:00:58 -0800 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 208 | /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ |
| 209 | |
| 210 | class ViaMatrix : public Sink { |
| 211 | public: |
| 212 | ViaMatrix(SkMatrix, Sink*); |
| 213 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 214 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 215 | int enclave() const override { return fSink->enclave(); } |
| 216 | const char* fileExtension() const override { return fSink->fileExtension(); } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 217 | private: |
| 218 | SkMatrix fMatrix; |
| 219 | SkAutoTDelete<Sink> fSink; |
| 220 | }; |
| 221 | |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 222 | class ViaUpright : public Sink { |
| 223 | public: |
| 224 | ViaUpright(SkMatrix, Sink*); |
| 225 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 226 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 227 | int enclave() const override { return fSink->enclave(); } |
| 228 | const char* fileExtension() const override { return fSink->fileExtension(); } |
mtklein | d603b22 | 2015-02-17 11:13:33 -0800 | [diff] [blame] | 229 | private: |
| 230 | SkMatrix fMatrix; |
| 231 | SkAutoTDelete<Sink> fSink; |
| 232 | }; |
| 233 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 234 | class ViaPipe : public Sink { |
| 235 | public: |
mtklein | 7edca21 | 2015-01-21 13:18:51 -0800 | [diff] [blame] | 236 | explicit ViaPipe(Sink*); |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 237 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 238 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 239 | int enclave() const override { return fSink->enclave(); } |
| 240 | const char* fileExtension() const override { return fSink->fileExtension(); } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 241 | private: |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 242 | SkAutoTDelete<Sink> fSink; |
| 243 | }; |
| 244 | |
| 245 | class ViaSerialization : public Sink { |
| 246 | public: |
| 247 | explicit ViaSerialization(Sink*); |
| 248 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 249 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 250 | int enclave() const override { return fSink->enclave(); } |
| 251 | const char* fileExtension() const override { return fSink->fileExtension(); } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 252 | private: |
| 253 | SkAutoTDelete<Sink> fSink; |
| 254 | }; |
| 255 | |
| 256 | class ViaTiles : public Sink { |
| 257 | public: |
| 258 | ViaTiles(int w, int h, SkBBHFactory*, Sink*); |
| 259 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 260 | Error draw(const Src&, SkBitmap*, SkWStream*, SkString*) const override; |
| 261 | int enclave() const override { return fSink->enclave(); } |
| 262 | const char* fileExtension() const override { return fSink->fileExtension(); } |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 263 | private: |
| 264 | const int fW, fH; |
| 265 | SkAutoTDelete<SkBBHFactory> fFactory; |
| 266 | SkAutoTDelete<Sink> fSink; |
| 267 | }; |
| 268 | |
mtklein | 748ca3b | 2015-01-15 10:56:12 -0800 | [diff] [blame] | 269 | } // namespace DM |
| 270 | |
| 271 | #endif//DMSrcSink_DEFINED |