blob: d9b8f4c995175efde8d355de4a1ced3d9bd92a3d [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>
9
10#include <GL/osmesa.h>
11
12#include "fiddle_main.h"
13
14// Globals externed in fiddle_main.h
15SkBitmap source;
16SkImage* image(nullptr);
17
18static 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
halcanaryd0964592016-03-25 11:29:34 -070051static void dump_output(const sk_sp<SkData>& data,
52 const char* name, bool last = true) {
halcanarydecb21e2015-12-10 07:52:45 -080053 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
halcanaryd0964592016-03-25 11:29:34 -070060static SkData* encode_snapshot(const sk_sp<SkSurface>& surface) {
61 sk_sp<SkImage> img(surface->newImageSnapshot());
halcanarydecb21e2015-12-10 07:52:45 -080062 return img ? img->encode() : nullptr;
63}
64
65static 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
75static GrContext* create_mesa_grcontext() {
halcanaryd0964592016-03-25 11:29:34 -070076 sk_sp<const GrGLInterface> mesa(GrGLCreateMesaInterface());
halcanarydecb21e2015-12-10 07:52:45 -080077 intptr_t backend = reinterpret_cast<intptr_t>(mesa.get());
78 return backend ? GrContext::Create(kOpenGL_GrBackend, backend) : nullptr;
79}
80
81
82int main() {
83 const DrawOptions options = GetDrawOptions();
84 fprintf(stderr, "%s\n", options.source);
85 if (options.source) {
halcanaryd0964592016-03-25 11:29:34 -070086 sk_sp<SkData> data(SkData::NewFromFileName(options.source));
halcanarydecb21e2015-12-10 07:52:45 -080087 if (!data) {
88 perror(options.source);
89 return 1;
90 } else {
halcanaryd0964592016-03-25 11:29:34 -070091 image = SkImage::NewFromEncoded(data.get());
halcanarydecb21e2015-12-10 07:52:45 -080092 if (!image) {
93 perror("Unable to decode the source image.");
94 return 1;
95 }
96 SkAssertResult(image->asLegacyBitmap(
97 &source, SkImage::kRO_LegacyBitmapMode));
98 }
99 }
halcanaryd0964592016-03-25 11:29:34 -0700100 sk_sp<SkData> rasterData, gpuData, pdfData, skpData;
halcanarydecb21e2015-12-10 07:52:45 -0800101 if (options.raster) {
halcanaryd0964592016-03-25 11:29:34 -0700102 auto rasterSurface =
103 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(options.size));
halcanarydecb21e2015-12-10 07:52:45 -0800104 draw(rasterSurface->getCanvas());
105 rasterData.reset(encode_snapshot(rasterSurface));
106 }
107 if (options.gpu) {
108 OSMesaContext osMesaContext = create_osmesa_context();
halcanaryd0964592016-03-25 11:29:34 -0700109 sk_sp<GrContext> grContext(create_mesa_grcontext());
halcanarydecb21e2015-12-10 07:52:45 -0800110 if (!grContext) {
111 fputs("Unable to get Mesa GrContext.\n", stderr);
112 } else {
halcanaryd0964592016-03-25 11:29:34 -0700113 auto surface = SkSurface::MakeRenderTarget(
114 grContext.get(),
115 SkBudgeted::kNo,
116 SkImageInfo::MakeN32Premul(options.size));
halcanarydecb21e2015-12-10 07:52:45 -0800117 if (!surface) {
118 fputs("Unable to get render surface.\n", stderr);
119 exit(1);
120 }
121 draw(surface->getCanvas());
122 gpuData.reset(encode_snapshot(surface));
123 }
124 if (osMesaContext) {
125 OSMesaDestroyContext(osMesaContext);
126 }
127 }
128 if (options.pdf) {
129 SkDynamicMemoryWStream pdfStream;
halcanaryd0964592016-03-25 11:29:34 -0700130 sk_sp<SkDocument> document(SkDocument::CreatePDF(&pdfStream));
halcanarydecb21e2015-12-10 07:52:45 -0800131 draw(document->beginPage(options.size.width(), options.size.height()));
132 document->close();
133 pdfData.reset(pdfStream.copyToData());
134 }
135 if (options.skp) {
136 SkSize size;
137 size = options.size;
138 SkPictureRecorder recorder;
139 draw(recorder.beginRecording(size.width(), size.height()));
halcanaryd0964592016-03-25 11:29:34 -0700140 auto picture = recorder.finishRecordingAsPicture();
halcanarydecb21e2015-12-10 07:52:45 -0800141 SkDynamicMemoryWStream skpStream;
142 picture->serialize(&skpStream);
143 skpData.reset(skpStream.copyToData());
144 }
145
146 printf("{\n");
147 dump_output(rasterData, "Raster", !gpuData && !pdfData && !skpData);
148 dump_output(gpuData, "Gpu", !pdfData && !skpData);
149 dump_output(pdfData, "Pdf", !skpData);
150 dump_output(skpData, "Skp");
151 printf("}\n");
152
153 SkSafeSetNull(image);
154 return 0;
155}