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