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 | |
| 8 | #include <stdio.h> |
halcanary | f0270c3 | 2016-05-23 09:02:38 -0700 | [diff] [blame] | 9 | #include <stdlib.h> |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 10 | |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 11 | #include "fiddle_main.h" |
| 12 | |
| 13 | // Globals externed in fiddle_main.h |
| 14 | SkBitmap source; |
halcanary | 7b8b237 | 2016-04-18 08:17:56 -0700 | [diff] [blame] | 15 | sk_sp<SkImage> image; |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 16 | |
| 17 | static void encode_to_base64(const void* data, size_t size, FILE* out) { |
| 18 | const uint8_t* input = reinterpret_cast<const uint8_t*>(data); |
| 19 | const uint8_t* end = &input[size]; |
| 20 | static const char codes[] = |
| 21 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 22 | "abcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 23 | while (input != end) { |
| 24 | uint8_t b = (*input & 0xFC) >> 2; |
| 25 | fputc(codes[b], out); |
| 26 | b = (*input & 0x03) << 4; |
| 27 | ++input; |
| 28 | if (input == end) { |
| 29 | fputc(codes[b], out); |
| 30 | fputs("==", out); |
| 31 | return; |
| 32 | } |
| 33 | b |= (*input & 0xF0) >> 4; |
| 34 | fputc(codes[b], out); |
| 35 | b = (*input & 0x0F) << 2; |
| 36 | ++input; |
| 37 | if (input == end) { |
| 38 | fputc(codes[b], out); |
| 39 | fputc('=', out); |
| 40 | return; |
| 41 | } |
| 42 | b |= (*input & 0xC0) >> 6; |
| 43 | fputc(codes[b], out); |
| 44 | b = *input & 0x3F; |
| 45 | fputc(codes[b], out); |
| 46 | ++input; |
| 47 | } |
| 48 | } |
| 49 | |
halcanary | d096459 | 2016-03-25 11:29:34 -0700 | [diff] [blame] | 50 | static void dump_output(const sk_sp<SkData>& data, |
| 51 | const char* name, bool last = true) { |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 52 | if (data) { |
| 53 | printf("\t\"%s\": \"", name); |
| 54 | encode_to_base64(data->data(), data->size(), stdout); |
| 55 | fputs(last ? "\"\n" : "\",\n", stdout); |
| 56 | } |
| 57 | } |
| 58 | |
halcanary | d096459 | 2016-03-25 11:29:34 -0700 | [diff] [blame] | 59 | static SkData* encode_snapshot(const sk_sp<SkSurface>& surface) { |
halcanary | 7b8b237 | 2016-04-18 08:17:56 -0700 | [diff] [blame] | 60 | sk_sp<SkImage> img(surface->makeImageSnapshot()); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 61 | return img ? img->encode() : nullptr; |
| 62 | } |
| 63 | |
mtklein | f419781 | 2016-08-25 08:44:49 -0700 | [diff] [blame] | 64 | #if defined(__linux) && !defined(__ANDROID__) |
mtklein | 7d10b9f | 2016-07-27 11:17:18 -0700 | [diff] [blame] | 65 | #include <GL/osmesa.h> |
| 66 | static sk_sp<GrContext> create_grcontext() { |
| 67 | // We just leak the OSMesaContext... the process will die soon anyway. |
| 68 | if (OSMesaContext osMesaContext = OSMesaCreateContextExt(OSMESA_BGRA, 0, 0, 0, nullptr)) { |
| 69 | static uint32_t buffer[16 * 16]; |
| 70 | OSMesaMakeCurrent(osMesaContext, &buffer, GL_UNSIGNED_BYTE, 16, 16); |
| 71 | } |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 72 | |
mtklein | 7d10b9f | 2016-07-27 11:17:18 -0700 | [diff] [blame] | 73 | auto osmesa_get = [](void* ctx, const char name[]) { |
| 74 | SkASSERT(nullptr == ctx); |
| 75 | SkASSERT(OSMesaGetCurrentContext()); |
| 76 | return OSMesaGetProcAddress(name); |
| 77 | }; |
| 78 | sk_sp<const GrGLInterface> mesa(GrGLAssembleInterface(nullptr, osmesa_get)); |
| 79 | if (!mesa) { |
| 80 | return nullptr; |
| 81 | } |
| 82 | return sk_sp<GrContext>(GrContext::Create( |
| 83 | kOpenGL_GrBackend, |
| 84 | reinterpret_cast<intptr_t>(mesa.get()))); |
halcanary | a5598a4 | 2016-03-31 10:35:13 -0700 | [diff] [blame] | 85 | } |
mtklein | 7d10b9f | 2016-07-27 11:17:18 -0700 | [diff] [blame] | 86 | #else |
| 87 | static sk_sp<GrContext> create_grcontext() { return nullptr; } |
| 88 | #endif |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 89 | |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 90 | int main() { |
| 91 | const DrawOptions options = GetDrawOptions(); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 92 | if (options.source) { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 93 | sk_sp<SkData> data(SkData::MakeFromFileName(options.source)); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 94 | if (!data) { |
| 95 | perror(options.source); |
| 96 | return 1; |
| 97 | } else { |
halcanary | 7b8b237 | 2016-04-18 08:17:56 -0700 | [diff] [blame] | 98 | image = SkImage::MakeFromEncoded(std::move(data)); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 99 | if (!image) { |
| 100 | perror("Unable to decode the source image."); |
| 101 | return 1; |
| 102 | } |
| 103 | SkAssertResult(image->asLegacyBitmap( |
| 104 | &source, SkImage::kRO_LegacyBitmapMode)); |
| 105 | } |
| 106 | } |
halcanary | d096459 | 2016-03-25 11:29:34 -0700 | [diff] [blame] | 107 | sk_sp<SkData> rasterData, gpuData, pdfData, skpData; |
Matt Sarett | a2f7126 | 2016-11-29 17:14:58 -0500 | [diff] [blame^] | 108 | SkColorType colorType = kN32_SkColorType; |
| 109 | sk_sp<SkColorSpace> colorSpace = nullptr; |
| 110 | if (options.f16) { |
| 111 | SkASSERT(options.srgb); |
| 112 | colorType = kRGBA_F16_SkColorType; |
| 113 | colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGBLinear_Named); |
| 114 | } else if (options.srgb) { |
| 115 | colorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named); |
| 116 | } |
| 117 | SkImageInfo info = SkImageInfo::Make(options.size.width(), options.size.height(), colorType, |
| 118 | kPremul_SkAlphaType, colorSpace); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 119 | if (options.raster) { |
Matt Sarett | a2f7126 | 2016-11-29 17:14:58 -0500 | [diff] [blame^] | 120 | auto rasterSurface = SkSurface::MakeRaster(info); |
halcanary | f0270c3 | 2016-05-23 09:02:38 -0700 | [diff] [blame] | 121 | srand(0); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 122 | draw(rasterSurface->getCanvas()); |
| 123 | rasterData.reset(encode_snapshot(rasterSurface)); |
| 124 | } |
| 125 | if (options.gpu) { |
mtklein | 7d10b9f | 2016-07-27 11:17:18 -0700 | [diff] [blame] | 126 | auto grContext = create_grcontext(); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 127 | if (!grContext) { |
mtklein | 7d10b9f | 2016-07-27 11:17:18 -0700 | [diff] [blame] | 128 | fputs("Unable to get GrContext.\n", stderr); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 129 | } else { |
Matt Sarett | a2f7126 | 2016-11-29 17:14:58 -0500 | [diff] [blame^] | 130 | auto surface = SkSurface::MakeRenderTarget(grContext.get(), SkBudgeted::kNo, info); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 131 | if (!surface) { |
| 132 | fputs("Unable to get render surface.\n", stderr); |
| 133 | exit(1); |
| 134 | } |
halcanary | f0270c3 | 2016-05-23 09:02:38 -0700 | [diff] [blame] | 135 | srand(0); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 136 | draw(surface->getCanvas()); |
| 137 | gpuData.reset(encode_snapshot(surface)); |
| 138 | } |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 139 | } |
| 140 | if (options.pdf) { |
| 141 | SkDynamicMemoryWStream pdfStream; |
halcanary | 676ab68 | 2016-05-03 12:10:04 -0700 | [diff] [blame] | 142 | sk_sp<SkDocument> document(SkDocument::MakePDF(&pdfStream)); |
mtklein | cd01b03 | 2016-08-31 04:58:19 -0700 | [diff] [blame] | 143 | if (document) { |
| 144 | srand(0); |
| 145 | draw(document->beginPage(options.size.width(), options.size.height())); |
| 146 | document->close(); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 147 | pdfData = pdfStream.detachAsData(); |
mtklein | cd01b03 | 2016-08-31 04:58:19 -0700 | [diff] [blame] | 148 | } |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 149 | } |
| 150 | if (options.skp) { |
| 151 | SkSize size; |
| 152 | size = options.size; |
| 153 | SkPictureRecorder recorder; |
halcanary | f0270c3 | 2016-05-23 09:02:38 -0700 | [diff] [blame] | 154 | srand(0); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 155 | draw(recorder.beginRecording(size.width(), size.height())); |
halcanary | d096459 | 2016-03-25 11:29:34 -0700 | [diff] [blame] | 156 | auto picture = recorder.finishRecordingAsPicture(); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 157 | SkDynamicMemoryWStream skpStream; |
| 158 | picture->serialize(&skpStream); |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 159 | skpData = skpStream.detachAsData(); |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | printf("{\n"); |
| 163 | dump_output(rasterData, "Raster", !gpuData && !pdfData && !skpData); |
| 164 | dump_output(gpuData, "Gpu", !pdfData && !skpData); |
| 165 | dump_output(pdfData, "Pdf", !skpData); |
| 166 | dump_output(skpData, "Skp"); |
| 167 | printf("}\n"); |
| 168 | |
halcanary | decb21e | 2015-12-10 07:52:45 -0800 | [diff] [blame] | 169 | return 0; |
| 170 | } |