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