OLD | NEW |
| (Empty) |
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; | |
16 sk_sp<SkImage> image; | |
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 | |
51 static void dump_output(const sk_sp<SkData>& data, | |
52 const char* name, bool last = true) { | |
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 | |
60 static SkData* encode_snapshot(const sk_sp<SkSurface>& surface) { | |
61 sk_sp<SkImage> img(surface->makeImageSnapshot()); | |
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 | |
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()))); | |
91 } | |
92 | |
93 int main() { | |
94 const DrawOptions options = GetDrawOptions(); | |
95 if (options.source) { | |
96 sk_sp<SkData> data(SkData::NewFromFileName(options.source)); | |
97 if (!data) { | |
98 perror(options.source); | |
99 return 1; | |
100 } else { | |
101 image = SkImage::MakeFromEncoded(std::move(data)); | |
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 } | |
110 sk_sp<SkData> rasterData, gpuData, pdfData, skpData; | |
111 if (options.raster) { | |
112 auto rasterSurface = | |
113 SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(options.size)); | |
114 draw(rasterSurface->getCanvas()); | |
115 rasterData.reset(encode_snapshot(rasterSurface)); | |
116 } | |
117 if (options.gpu) { | |
118 OSMesaContext osMesaContext = create_osmesa_context(); | |
119 auto grContext = create_mesa_grcontext(); | |
120 if (!grContext) { | |
121 fputs("Unable to get Mesa GrContext.\n", stderr); | |
122 } else { | |
123 auto surface = SkSurface::MakeRenderTarget( | |
124 grContext.get(), | |
125 SkBudgeted::kNo, | |
126 SkImageInfo::MakeN32Premul(options.size)); | |
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; | |
140 sk_sp<SkDocument> document(SkDocument::CreatePDF(&pdfStream)); | |
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())); | |
150 auto picture = recorder.finishRecordingAsPicture(); | |
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 | |
163 return 0; | |
164 } | |
OLD | NEW |