blob: f6270daaa59ae3935a4e095ed53742ff01a3d854 [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>
halcanarydecb21e2015-12-10 07:52:45 -080010
11#include <GL/osmesa.h>
12
13#include "fiddle_main.h"
14
15// Globals externed in fiddle_main.h
16SkBitmap source;
halcanary7b8b2372016-04-18 08:17:56 -070017sk_sp<SkImage> image;
halcanarydecb21e2015-12-10 07:52:45 -080018
19static 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
halcanaryd0964592016-03-25 11:29:34 -070052static void dump_output(const sk_sp<SkData>& data,
53 const char* name, bool last = true) {
halcanarydecb21e2015-12-10 07:52:45 -080054 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
halcanaryd0964592016-03-25 11:29:34 -070061static SkData* encode_snapshot(const sk_sp<SkSurface>& surface) {
halcanary7b8b2372016-04-18 08:17:56 -070062 sk_sp<SkImage> img(surface->makeImageSnapshot());
halcanarydecb21e2015-12-10 07:52:45 -080063 return img ? img->encode() : nullptr;
64}
65
66static 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
halcanarya5598a42016-03-31 10:35:13 -070076static 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())));
halcanarydecb21e2015-12-10 07:52:45 -080092}
93
halcanarydecb21e2015-12-10 07:52:45 -080094int main() {
95 const DrawOptions options = GetDrawOptions();
halcanarydecb21e2015-12-10 07:52:45 -080096 if (options.source) {
halcanaryd0964592016-03-25 11:29:34 -070097 sk_sp<SkData> data(SkData::NewFromFileName(options.source));
halcanarydecb21e2015-12-10 07:52:45 -080098 if (!data) {
99 perror(options.source);
100 return 1;
101 } else {
halcanary7b8b2372016-04-18 08:17:56 -0700102 image = SkImage::MakeFromEncoded(std::move(data));
halcanarydecb21e2015-12-10 07:52:45 -0800103 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 }
halcanaryd0964592016-03-25 11:29:34 -0700111 sk_sp<SkData> rasterData, gpuData, pdfData, skpData;
halcanarydecb21e2015-12-10 07:52:45 -0800112 if (options.raster) {
halcanaryd0964592016-03-25 11:29:34 -0700113 auto rasterSurface =
114 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(options.size));
halcanaryf0270c32016-05-23 09:02:38 -0700115 srand(0);
halcanarydecb21e2015-12-10 07:52:45 -0800116 draw(rasterSurface->getCanvas());
117 rasterData.reset(encode_snapshot(rasterSurface));
118 }
119 if (options.gpu) {
120 OSMesaContext osMesaContext = create_osmesa_context();
halcanarya5598a42016-03-31 10:35:13 -0700121 auto grContext = create_mesa_grcontext();
halcanarydecb21e2015-12-10 07:52:45 -0800122 if (!grContext) {
123 fputs("Unable to get Mesa GrContext.\n", stderr);
124 } else {
halcanaryd0964592016-03-25 11:29:34 -0700125 auto surface = SkSurface::MakeRenderTarget(
126 grContext.get(),
127 SkBudgeted::kNo,
128 SkImageInfo::MakeN32Premul(options.size));
halcanarydecb21e2015-12-10 07:52:45 -0800129 if (!surface) {
130 fputs("Unable to get render surface.\n", stderr);
131 exit(1);
132 }
halcanaryf0270c32016-05-23 09:02:38 -0700133 srand(0);
halcanarydecb21e2015-12-10 07:52:45 -0800134 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;
halcanary676ab682016-05-03 12:10:04 -0700143 sk_sp<SkDocument> document(SkDocument::MakePDF(&pdfStream));
halcanaryf0270c32016-05-23 09:02:38 -0700144 srand(0);
halcanarydecb21e2015-12-10 07:52:45 -0800145 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;
halcanaryf0270c32016-05-23 09:02:38 -0700153 srand(0);
halcanarydecb21e2015-12-10 07:52:45 -0800154 draw(recorder.beginRecording(size.width(), size.height()));
halcanaryd0964592016-03-25 11:29:34 -0700155 auto picture = recorder.finishRecordingAsPicture();
halcanarydecb21e2015-12-10 07:52:45 -0800156 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
halcanarydecb21e2015-12-10 07:52:45 -0800168 return 0;
169}