blob: df2e8ff66eb86218a70cbe40582d5bf09665d2fd [file] [log] [blame]
halcanarydecb21e2015-12-10 07:52:45 -08001/*
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>
halcanaryf0270c32016-05-23 09:02:38 -07009#include <stdlib.h>
mtklein5dbd2742016-08-02 11:13:48 -070010#include "SkForceLinking.h"
11
12__SK_FORCE_IMAGE_DECODER_LINKING;
halcanarydecb21e2015-12-10 07:52:45 -080013
halcanarydecb21e2015-12-10 07:52:45 -080014#include "fiddle_main.h"
15
16// Globals externed in fiddle_main.h
17SkBitmap source;
halcanary7b8b2372016-04-18 08:17:56 -070018sk_sp<SkImage> image;
halcanarydecb21e2015-12-10 07:52:45 -080019
20static 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
halcanaryd0964592016-03-25 11:29:34 -070053static void dump_output(const sk_sp<SkData>& data,
54 const char* name, bool last = true) {
halcanarydecb21e2015-12-10 07:52:45 -080055 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
halcanaryd0964592016-03-25 11:29:34 -070062static SkData* encode_snapshot(const sk_sp<SkSurface>& surface) {
halcanary7b8b2372016-04-18 08:17:56 -070063 sk_sp<SkImage> img(surface->makeImageSnapshot());
halcanarydecb21e2015-12-10 07:52:45 -080064 return img ? img->encode() : nullptr;
65}
66
mtklein7d10b9f2016-07-27 11:17:18 -070067#if defined(__linux)
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 }
halcanarydecb21e2015-12-10 07:52:45 -080075
mtklein7d10b9f2016-07-27 11:17:18 -070076 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())));
halcanarya5598a42016-03-31 10:35:13 -070088 }
mtklein7d10b9f2016-07-27 11:17:18 -070089#else
90 static sk_sp<GrContext> create_grcontext() { return nullptr; }
91#endif
halcanarydecb21e2015-12-10 07:52:45 -080092
halcanarydecb21e2015-12-10 07:52:45 -080093int main() {
94 const DrawOptions options = GetDrawOptions();
halcanarydecb21e2015-12-10 07:52:45 -080095 if (options.source) {
halcanaryd0964592016-03-25 11:29:34 -070096 sk_sp<SkData> data(SkData::NewFromFileName(options.source));
halcanarydecb21e2015-12-10 07:52:45 -080097 if (!data) {
98 perror(options.source);
99 return 1;
100 } else {
halcanary7b8b2372016-04-18 08:17:56 -0700101 image = SkImage::MakeFromEncoded(std::move(data));
halcanarydecb21e2015-12-10 07:52:45 -0800102 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 }
halcanaryd0964592016-03-25 11:29:34 -0700110 sk_sp<SkData> rasterData, gpuData, pdfData, skpData;
halcanarydecb21e2015-12-10 07:52:45 -0800111 if (options.raster) {
halcanaryd0964592016-03-25 11:29:34 -0700112 auto rasterSurface =
113 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(options.size));
halcanaryf0270c32016-05-23 09:02:38 -0700114 srand(0);
halcanarydecb21e2015-12-10 07:52:45 -0800115 draw(rasterSurface->getCanvas());
116 rasterData.reset(encode_snapshot(rasterSurface));
117 }
118 if (options.gpu) {
mtklein7d10b9f2016-07-27 11:17:18 -0700119 auto grContext = create_grcontext();
halcanarydecb21e2015-12-10 07:52:45 -0800120 if (!grContext) {
mtklein7d10b9f2016-07-27 11:17:18 -0700121 fputs("Unable to get GrContext.\n", stderr);
halcanarydecb21e2015-12-10 07:52:45 -0800122 } else {
halcanaryd0964592016-03-25 11:29:34 -0700123 auto surface = SkSurface::MakeRenderTarget(
124 grContext.get(),
125 SkBudgeted::kNo,
126 SkImageInfo::MakeN32Premul(options.size));
halcanarydecb21e2015-12-10 07:52:45 -0800127 if (!surface) {
128 fputs("Unable to get render surface.\n", stderr);
129 exit(1);
130 }
halcanaryf0270c32016-05-23 09:02:38 -0700131 srand(0);
halcanarydecb21e2015-12-10 07:52:45 -0800132 draw(surface->getCanvas());
133 gpuData.reset(encode_snapshot(surface));
134 }
halcanarydecb21e2015-12-10 07:52:45 -0800135 }
136 if (options.pdf) {
137 SkDynamicMemoryWStream pdfStream;
halcanary676ab682016-05-03 12:10:04 -0700138 sk_sp<SkDocument> document(SkDocument::MakePDF(&pdfStream));
halcanaryf0270c32016-05-23 09:02:38 -0700139 srand(0);
halcanarydecb21e2015-12-10 07:52:45 -0800140 draw(document->beginPage(options.size.width(), options.size.height()));
141 document->close();
142 pdfData.reset(pdfStream.copyToData());
143 }
144 if (options.skp) {
145 SkSize size;
146 size = options.size;
147 SkPictureRecorder recorder;
halcanaryf0270c32016-05-23 09:02:38 -0700148 srand(0);
halcanarydecb21e2015-12-10 07:52:45 -0800149 draw(recorder.beginRecording(size.width(), size.height()));
halcanaryd0964592016-03-25 11:29:34 -0700150 auto picture = recorder.finishRecordingAsPicture();
halcanarydecb21e2015-12-10 07:52:45 -0800151 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
halcanarydecb21e2015-12-10 07:52:45 -0800163 return 0;
164}