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