halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [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 | |
Hal Canary | 169f37f | 2017-02-15 10:20:30 -0500 | [diff] [blame] | 8 | #include <cstdio> |
| 9 | #include <cstdlib> |
| 10 | #include <sstream> |
Joe Gregorio | 1fd1823 | 2017-02-13 11:39:43 -0500 | [diff] [blame] | 11 | #include <string> |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 12 | |
Joe Gregorio | 1e735c0 | 2017-04-19 14:05:14 -0400 | [diff] [blame] | 13 | #include "SkCommandLineFlags.h" |
| 14 | |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 15 | #include "fiddle_main.h" |
| 16 | |
Joe Gregorio | 1e735c0 | 2017-04-19 14:05:14 -0400 | [diff] [blame] | 17 | DEFINE_double(duration, 1.0, "The total duration, in seconds, of the animation we are drawing."); |
| 18 | DEFINE_double(frame, 1.0, "A double value in [0, 1] that specifies the point in animation to draw."); |
| 19 | |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 20 | // Globals externed in fiddle_main.h |
| 21 | SkBitmap source; |
halcanary | 7b8b237 | 2016-04-18 08:17:56 -0700 | [diff] [blame] | 22 | sk_sp<SkImage> image; |
Joe Gregorio | 1e735c0 | 2017-04-19 14:05:14 -0400 | [diff] [blame] | 23 | double duration; // The total duration of the animation in seconds. |
| 24 | double frame; // A value in [0, 1] of where we are in the animation. |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 25 | |
Hal Canary | 169f37f | 2017-02-15 10:20:30 -0500 | [diff] [blame] | 26 | // Global used by the local impl of SkDebugf. |
| 27 | std::ostringstream gTextOutput; |
Joe Gregorio | 1fd1823 | 2017-02-13 11:39:43 -0500 | [diff] [blame] | 28 | |
Joe Gregorio | 97b10ac | 2017-06-01 13:24:11 -0400 | [diff] [blame] | 29 | // Global to record the GL driver info via create_grcontext(). |
| 30 | std::ostringstream gGLDriverInfo; |
| 31 | |
Joe Gregorio | 1fd1823 | 2017-02-13 11:39:43 -0500 | [diff] [blame] | 32 | void SkDebugf(const char * fmt, ...) { |
Joe Gregorio | 1fd1823 | 2017-02-13 11:39:43 -0500 | [diff] [blame] | 33 | va_list args; |
| 34 | va_start(args, fmt); |
Hal Canary | 169f37f | 2017-02-15 10:20:30 -0500 | [diff] [blame] | 35 | char formatbuffer[1024]; |
| 36 | int n = vsnprintf(formatbuffer, sizeof(formatbuffer), fmt, args); |
Joe Gregorio | 1fd1823 | 2017-02-13 11:39:43 -0500 | [diff] [blame] | 37 | va_end(args); |
| 38 | if (n>=0 && n<=int(sizeof(formatbuffer))) { |
Hal Canary | 169f37f | 2017-02-15 10:20:30 -0500 | [diff] [blame] | 39 | gTextOutput.write(formatbuffer, n); |
Joe Gregorio | 1fd1823 | 2017-02-13 11:39:43 -0500 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 43 | static void encode_to_base64(const void* data, size_t size, FILE* out) { |
| 44 | const uint8_t* input = reinterpret_cast<const uint8_t*>(data); |
| 45 | const uint8_t* end = &input[size]; |
| 46 | static const char codes[] = |
| 47 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 48 | "abcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 49 | while (input != end) { |
| 50 | uint8_t b = (*input & 0xFC) >> 2; |
| 51 | fputc(codes[b], out); |
| 52 | b = (*input & 0x03) << 4; |
| 53 | ++input; |
| 54 | if (input == end) { |
| 55 | fputc(codes[b], out); |
| 56 | fputs("==", out); |
| 57 | return; |
| 58 | } |
| 59 | b |= (*input & 0xF0) >> 4; |
| 60 | fputc(codes[b], out); |
| 61 | b = (*input & 0x0F) << 2; |
| 62 | ++input; |
| 63 | if (input == end) { |
| 64 | fputc(codes[b], out); |
| 65 | fputc('=', out); |
| 66 | return; |
| 67 | } |
| 68 | b |= (*input & 0xC0) >> 6; |
| 69 | fputc(codes[b], out); |
| 70 | b = *input & 0x3F; |
| 71 | fputc(codes[b], out); |
| 72 | ++input; |
| 73 | } |
| 74 | } |
| 75 | |
Hal Canary | 169f37f | 2017-02-15 10:20:30 -0500 | [diff] [blame] | 76 | |
| 77 | static void dump_output(const void* data, size_t size, |
| 78 | const char* name, bool last = true) { |
| 79 | printf("\t\"%s\": \"", name); |
| 80 | encode_to_base64(data, size, stdout); |
| 81 | fputs(last ? "\"\n" : "\",\n", stdout); |
| 82 | } |
| 83 | |
halcanary | d096459 | 2016-03-25 11:29:34 -0700 | [diff] [blame] | 84 | static void dump_output(const sk_sp<SkData>& data, |
| 85 | const char* name, bool last = true) { |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 86 | if (data) { |
Hal Canary | 169f37f | 2017-02-15 10:20:30 -0500 | [diff] [blame] | 87 | dump_output(data->data(), data->size(), name, last); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 91 | static sk_sp<SkData> encode_snapshot(const sk_sp<SkSurface>& surface) { |
halcanary | 7b8b237 | 2016-04-18 08:17:56 -0700 | [diff] [blame] | 92 | sk_sp<SkImage> img(surface->makeImageSnapshot()); |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 93 | return img ? img->encodeToData() : nullptr; |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Joe Gregorio | 4f12f1e | 2017-06-14 15:54:32 -0400 | [diff] [blame] | 96 | static SkCanvas* prepare_canvas(SkCanvas * canvas) { |
| 97 | canvas->clear(SK_ColorWHITE); |
| 98 | return canvas; |
| 99 | } |
| 100 | |
Joe Gregorio | 1e735c0 | 2017-04-19 14:05:14 -0400 | [diff] [blame] | 101 | int main(int argc, char** argv) { |
| 102 | SkCommandLineFlags::Parse(argc, argv); |
| 103 | duration = FLAGS_duration; |
| 104 | frame = FLAGS_frame; |
Joe Gregorio | 1fd1823 | 2017-02-13 11:39:43 -0500 | [diff] [blame] | 105 | DrawOptions options = GetDrawOptions(); |
| 106 | // If textOnly then only do one type of image, otherwise the text |
| 107 | // output is duplicated for each type. |
| 108 | if (options.textOnly) { |
| 109 | options.raster = true; |
| 110 | options.gpu = false; |
| 111 | options.pdf = false; |
| 112 | options.skp = false; |
| 113 | } |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 114 | if (options.source) { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 115 | sk_sp<SkData> data(SkData::MakeFromFileName(options.source)); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 116 | if (!data) { |
| 117 | perror(options.source); |
| 118 | return 1; |
| 119 | } else { |
halcanary | 7b8b237 | 2016-04-18 08:17:56 -0700 | [diff] [blame] | 120 | image = SkImage::MakeFromEncoded(std::move(data)); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 121 | if (!image) { |
| 122 | perror("Unable to decode the source image."); |
| 123 | return 1; |
| 124 | } |
| 125 | SkAssertResult(image->asLegacyBitmap( |
| 126 | &source, SkImage::kRO_LegacyBitmapMode)); |
| 127 | } |
| 128 | } |
halcanary | d096459 | 2016-03-25 11:29:34 -0700 | [diff] [blame] | 129 | sk_sp<SkData> rasterData, gpuData, pdfData, skpData; |
Matt Sarett | a2f7126 | 2016-11-29 17:14:58 -0500 | [diff] [blame] | 130 | SkColorType colorType = kN32_SkColorType; |
| 131 | sk_sp<SkColorSpace> colorSpace = nullptr; |
| 132 | if (options.f16) { |
| 133 | SkASSERT(options.srgb); |
| 134 | colorType = kRGBA_F16_SkColorType; |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 135 | colorSpace = SkColorSpace::MakeSRGBLinear(); |
Matt Sarett | a2f7126 | 2016-11-29 17:14:58 -0500 | [diff] [blame] | 136 | } else if (options.srgb) { |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 137 | colorSpace = SkColorSpace::MakeSRGB(); |
Matt Sarett | a2f7126 | 2016-11-29 17:14:58 -0500 | [diff] [blame] | 138 | } |
| 139 | SkImageInfo info = SkImageInfo::Make(options.size.width(), options.size.height(), colorType, |
| 140 | kPremul_SkAlphaType, colorSpace); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 141 | if (options.raster) { |
Matt Sarett | a2f7126 | 2016-11-29 17:14:58 -0500 | [diff] [blame] | 142 | auto rasterSurface = SkSurface::MakeRaster(info); |
halcanary | f0270c3 | 2016-05-23 09:02:38 -0700 | [diff] [blame] | 143 | srand(0); |
Joe Gregorio | 4f12f1e | 2017-06-14 15:54:32 -0400 | [diff] [blame] | 144 | draw(prepare_canvas(rasterSurface->getCanvas())); |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 145 | rasterData = encode_snapshot(rasterSurface); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 146 | } |
| 147 | if (options.gpu) { |
Joe Gregorio | 97b10ac | 2017-06-01 13:24:11 -0400 | [diff] [blame] | 148 | auto grContext = create_grcontext(gGLDriverInfo); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 149 | if (!grContext) { |
mtklein | 7d10b9f | 2016-07-27 11:17:18 -0700 | [diff] [blame] | 150 | fputs("Unable to get GrContext.\n", stderr); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 151 | } else { |
Matt Sarett | a2f7126 | 2016-11-29 17:14:58 -0500 | [diff] [blame] | 152 | auto surface = SkSurface::MakeRenderTarget(grContext.get(), SkBudgeted::kNo, info); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 153 | if (!surface) { |
| 154 | fputs("Unable to get render surface.\n", stderr); |
| 155 | exit(1); |
| 156 | } |
halcanary | f0270c3 | 2016-05-23 09:02:38 -0700 | [diff] [blame] | 157 | srand(0); |
Joe Gregorio | 4f12f1e | 2017-06-14 15:54:32 -0400 | [diff] [blame] | 158 | draw(prepare_canvas(surface->getCanvas())); |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 159 | gpuData = encode_snapshot(surface); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 160 | } |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 161 | } |
| 162 | if (options.pdf) { |
| 163 | SkDynamicMemoryWStream pdfStream; |
halcanary | 676ab68 | 2016-05-03 12:10:04 -0700 | [diff] [blame] | 164 | sk_sp<SkDocument> document(SkDocument::MakePDF(&pdfStream)); |
mtklein | cd01b03 | 2016-08-31 04:58:19 -0700 | [diff] [blame] | 165 | if (document) { |
| 166 | srand(0); |
Joe Gregorio | 4f12f1e | 2017-06-14 15:54:32 -0400 | [diff] [blame] | 167 | draw(prepare_canvas(document->beginPage(options.size.width(), options.size.height()))); |
mtklein | cd01b03 | 2016-08-31 04:58:19 -0700 | [diff] [blame] | 168 | document->close(); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 169 | pdfData = pdfStream.detachAsData(); |
mtklein | cd01b03 | 2016-08-31 04:58:19 -0700 | [diff] [blame] | 170 | } |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 171 | } |
| 172 | if (options.skp) { |
| 173 | SkSize size; |
| 174 | size = options.size; |
| 175 | SkPictureRecorder recorder; |
halcanary | f0270c3 | 2016-05-23 09:02:38 -0700 | [diff] [blame] | 176 | srand(0); |
Joe Gregorio | 4f12f1e | 2017-06-14 15:54:32 -0400 | [diff] [blame] | 177 | draw(prepare_canvas(recorder.beginRecording(size.width(), size.height()))); |
halcanary | d096459 | 2016-03-25 11:29:34 -0700 | [diff] [blame] | 178 | auto picture = recorder.finishRecordingAsPicture(); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 179 | SkDynamicMemoryWStream skpStream; |
| 180 | picture->serialize(&skpStream); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 181 | skpData = skpStream.detachAsData(); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | printf("{\n"); |
Joe Gregorio | 0236cba | 2017-02-13 13:55:17 -0500 | [diff] [blame] | 185 | if (!options.textOnly) { |
Joe Gregorio | 97b10ac | 2017-06-01 13:24:11 -0400 | [diff] [blame] | 186 | dump_output(rasterData, "Raster", false); |
| 187 | dump_output(gpuData, "Gpu", false); |
| 188 | dump_output(pdfData, "Pdf", false); |
| 189 | dump_output(skpData, "Skp", false); |
Joe Gregorio | 0236cba | 2017-02-13 13:55:17 -0500 | [diff] [blame] | 190 | } else { |
Hal Canary | 169f37f | 2017-02-15 10:20:30 -0500 | [diff] [blame] | 191 | std::string textoutput = gTextOutput.str(); |
Joe Gregorio | 97b10ac | 2017-06-01 13:24:11 -0400 | [diff] [blame] | 192 | dump_output(textoutput.c_str(), textoutput.length(), "Text", false); |
Joe Gregorio | 0236cba | 2017-02-13 13:55:17 -0500 | [diff] [blame] | 193 | } |
Joe Gregorio | 97b10ac | 2017-06-01 13:24:11 -0400 | [diff] [blame] | 194 | std::string glinfo = gGLDriverInfo.str(); |
| 195 | dump_output(glinfo.c_str(), glinfo.length(), "GLInfo", true); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 196 | printf("}\n"); |
| 197 | |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 198 | return 0; |
| 199 | } |