blob: c5561c0c6fd271ca4f894045183eb0ecc6688571 [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();
halcanarydecb21e2015-12-10 07:52:45 -080084 if (options.source) {
halcanaryd0964592016-03-25 11:29:34 -070085 sk_sp<SkData> data(SkData::NewFromFileName(options.source));
halcanarydecb21e2015-12-10 07:52:45 -080086 if (!data) {
87 perror(options.source);
88 return 1;
89 } else {
halcanaryd0964592016-03-25 11:29:34 -070090 image = SkImage::NewFromEncoded(data.get());
halcanarydecb21e2015-12-10 07:52:45 -080091 if (!image) {
92 perror("Unable to decode the source image.");
93 return 1;
94 }
95 SkAssertResult(image->asLegacyBitmap(
96 &source, SkImage::kRO_LegacyBitmapMode));
97 }
98 }
halcanaryd0964592016-03-25 11:29:34 -070099 sk_sp<SkData> rasterData, gpuData, pdfData, skpData;
halcanarydecb21e2015-12-10 07:52:45 -0800100 if (options.raster) {
halcanaryd0964592016-03-25 11:29:34 -0700101 auto rasterSurface =
102 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(options.size));
halcanarydecb21e2015-12-10 07:52:45 -0800103 draw(rasterSurface->getCanvas());
104 rasterData.reset(encode_snapshot(rasterSurface));
105 }
106 if (options.gpu) {
107 OSMesaContext osMesaContext = create_osmesa_context();
halcanaryd0964592016-03-25 11:29:34 -0700108 sk_sp<GrContext> grContext(create_mesa_grcontext());
halcanarydecb21e2015-12-10 07:52:45 -0800109 if (!grContext) {
110 fputs("Unable to get Mesa GrContext.\n", stderr);
111 } else {
halcanaryd0964592016-03-25 11:29:34 -0700112 auto surface = SkSurface::MakeRenderTarget(
113 grContext.get(),
114 SkBudgeted::kNo,
115 SkImageInfo::MakeN32Premul(options.size));
halcanarydecb21e2015-12-10 07:52:45 -0800116 if (!surface) {
117 fputs("Unable to get render surface.\n", stderr);
118 exit(1);
119 }
120 draw(surface->getCanvas());
121 gpuData.reset(encode_snapshot(surface));
122 }
123 if (osMesaContext) {
124 OSMesaDestroyContext(osMesaContext);
125 }
126 }
127 if (options.pdf) {
128 SkDynamicMemoryWStream pdfStream;
halcanaryd0964592016-03-25 11:29:34 -0700129 sk_sp<SkDocument> document(SkDocument::CreatePDF(&pdfStream));
halcanarydecb21e2015-12-10 07:52:45 -0800130 draw(document->beginPage(options.size.width(), options.size.height()));
131 document->close();
132 pdfData.reset(pdfStream.copyToData());
133 }
134 if (options.skp) {
135 SkSize size;
136 size = options.size;
137 SkPictureRecorder recorder;
138 draw(recorder.beginRecording(size.width(), size.height()));
halcanaryd0964592016-03-25 11:29:34 -0700139 auto picture = recorder.finishRecordingAsPicture();
halcanarydecb21e2015-12-10 07:52:45 -0800140 SkDynamicMemoryWStream skpStream;
141 picture->serialize(&skpStream);
142 skpData.reset(skpStream.copyToData());
143 }
144
145 printf("{\n");
146 dump_output(rasterData, "Raster", !gpuData && !pdfData && !skpData);
147 dump_output(gpuData, "Gpu", !pdfData && !skpData);
148 dump_output(pdfData, "Pdf", !skpData);
149 dump_output(skpData, "Skp");
150 printf("}\n");
151
152 SkSafeSetNull(image);
153 return 0;
154}