blob: 4ba84f38ca00077116bff8d026120244dd2dd662 [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
Hal Canary169f37f2017-02-15 10:20:30 -05008#include <cstdio>
9#include <cstdlib>
10#include <sstream>
Joe Gregorio1fd18232017-02-13 11:39:43 -050011#include <string>
halcanarydecb21e2015-12-10 07:52:45 -080012
Brian Osman250e6e82017-12-12 16:24:41 -050013#include "SkAutoPixmapStorage.h"
Joe Gregorio1e735c02017-04-19 14:05:14 -040014#include "SkCommandLineFlags.h"
Robert Phillipsc34aa9d2017-11-17 14:59:43 -050015#include "SkMipMap.h"
Robert Phillips57e08282017-11-16 14:59:48 -050016#include "SkUtils.h"
Joe Gregorio1e735c02017-04-19 14:05:14 -040017
halcanarydecb21e2015-12-10 07:52:45 -080018#include "fiddle_main.h"
19
Joe Gregorio1e735c02017-04-19 14:05:14 -040020DEFINE_double(duration, 1.0, "The total duration, in seconds, of the animation we are drawing.");
21DEFINE_double(frame, 1.0, "A double value in [0, 1] that specifies the point in animation to draw.");
22
Robert Phillips57e08282017-11-16 14:59:48 -050023#include "GrBackendSurface.h"
24#include "GrContextPriv.h"
25#include "GrGpu.h"
26
27#include "GrTest.h"
28
29
halcanarydecb21e2015-12-10 07:52:45 -080030// Globals externed in fiddle_main.h
Robert Phillips57e08282017-11-16 14:59:48 -050031sk_sp<GrTexture> backingTexture; // not externed
32GrBackendTexture backEndTexture;
33
34sk_sp<GrRenderTarget> backingRenderTarget; // not externed
35GrBackendRenderTarget backEndRenderTarget;
36
37sk_sp<GrTexture> backingTextureRenderTarget; // not externed
38GrBackendTexture backEndTextureRenderTarget;
39
halcanarydecb21e2015-12-10 07:52:45 -080040SkBitmap source;
halcanary7b8b2372016-04-18 08:17:56 -070041sk_sp<SkImage> image;
Joe Gregorio1e735c02017-04-19 14:05:14 -040042double duration; // The total duration of the animation in seconds.
43double frame; // A value in [0, 1] of where we are in the animation.
halcanarydecb21e2015-12-10 07:52:45 -080044
Hal Canary169f37f2017-02-15 10:20:30 -050045// Global used by the local impl of SkDebugf.
46std::ostringstream gTextOutput;
Joe Gregorio1fd18232017-02-13 11:39:43 -050047
Joe Gregorio97b10ac2017-06-01 13:24:11 -040048// Global to record the GL driver info via create_grcontext().
49std::ostringstream gGLDriverInfo;
50
Joe Gregorio1fd18232017-02-13 11:39:43 -050051void SkDebugf(const char * fmt, ...) {
Joe Gregorio1fd18232017-02-13 11:39:43 -050052 va_list args;
53 va_start(args, fmt);
Hal Canary169f37f2017-02-15 10:20:30 -050054 char formatbuffer[1024];
55 int n = vsnprintf(formatbuffer, sizeof(formatbuffer), fmt, args);
Joe Gregorio1fd18232017-02-13 11:39:43 -050056 va_end(args);
57 if (n>=0 && n<=int(sizeof(formatbuffer))) {
Hal Canary169f37f2017-02-15 10:20:30 -050058 gTextOutput.write(formatbuffer, n);
Joe Gregorio1fd18232017-02-13 11:39:43 -050059 }
60}
61
halcanarydecb21e2015-12-10 07:52:45 -080062static void encode_to_base64(const void* data, size_t size, FILE* out) {
63 const uint8_t* input = reinterpret_cast<const uint8_t*>(data);
64 const uint8_t* end = &input[size];
65 static const char codes[] =
66 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
67 "abcdefghijklmnopqrstuvwxyz0123456789+/";
68 while (input != end) {
69 uint8_t b = (*input & 0xFC) >> 2;
70 fputc(codes[b], out);
71 b = (*input & 0x03) << 4;
72 ++input;
73 if (input == end) {
74 fputc(codes[b], out);
75 fputs("==", out);
76 return;
77 }
78 b |= (*input & 0xF0) >> 4;
79 fputc(codes[b], out);
80 b = (*input & 0x0F) << 2;
81 ++input;
82 if (input == end) {
83 fputc(codes[b], out);
84 fputc('=', out);
85 return;
86 }
87 b |= (*input & 0xC0) >> 6;
88 fputc(codes[b], out);
89 b = *input & 0x3F;
90 fputc(codes[b], out);
91 ++input;
92 }
93}
94
Hal Canary169f37f2017-02-15 10:20:30 -050095
96static void dump_output(const void* data, size_t size,
97 const char* name, bool last = true) {
98 printf("\t\"%s\": \"", name);
99 encode_to_base64(data, size, stdout);
100 fputs(last ? "\"\n" : "\",\n", stdout);
101}
102
halcanaryd0964592016-03-25 11:29:34 -0700103static void dump_output(const sk_sp<SkData>& data,
104 const char* name, bool last = true) {
halcanarydecb21e2015-12-10 07:52:45 -0800105 if (data) {
Hal Canary169f37f2017-02-15 10:20:30 -0500106 dump_output(data->data(), data->size(), name, last);
halcanarydecb21e2015-12-10 07:52:45 -0800107 }
108}
109
Mike Reed6409f842017-07-11 16:03:13 -0400110static sk_sp<SkData> encode_snapshot(const sk_sp<SkSurface>& surface) {
halcanary7b8b2372016-04-18 08:17:56 -0700111 sk_sp<SkImage> img(surface->makeImageSnapshot());
Mike Reed6409f842017-07-11 16:03:13 -0400112 return img ? img->encodeToData() : nullptr;
halcanarydecb21e2015-12-10 07:52:45 -0800113}
114
Joe Gregorio4f12f1e2017-06-14 15:54:32 -0400115static SkCanvas* prepare_canvas(SkCanvas * canvas) {
116 canvas->clear(SK_ColorWHITE);
117 return canvas;
118}
119
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500120static bool setup_backend_objects(GrContext* context,
121 const SkBitmap& bm,
122 const DrawOptions& options) {
Robert Phillips57e08282017-11-16 14:59:48 -0500123 if (!context) {
124 return false;
125 }
126
Robert Phillips6be756b2018-01-16 15:07:54 -0500127 auto resourceProvider = context->contextPriv().resourceProvider();
128
Robert Phillips57e08282017-11-16 14:59:48 -0500129 GrSurfaceDesc backingDesc;
130 backingDesc.fFlags = kNone_GrSurfaceFlags;
131 backingDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
132 backingDesc.fWidth = bm.width();
133 backingDesc.fHeight = bm.height();
Greg Danielfaa095e2017-12-19 13:15:02 -0500134 // This config must match the SkColorType used in draw.cpp in the SkImage and Surface factories
Robert Phillipsb67821d2017-12-13 15:00:45 -0500135 backingDesc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomon3a2cc2c2018-02-03 00:25:12 +0000136 backingDesc.fSampleCnt = 0;
Robert Phillips57e08282017-11-16 14:59:48 -0500137
138 if (!bm.empty()) {
Brian Osman250e6e82017-12-12 16:24:41 -0500139 SkPixmap originalPixmap;
140 SkPixmap* pixmap = &originalPixmap;
141 if (!bm.peekPixels(&originalPixmap)) {
142 return false;
143 }
144
145 SkAutoPixmapStorage rgbaPixmap;
146 if (kN32_SkColorType != kRGBA_8888_SkColorType) {
147 if (!rgbaPixmap.tryAlloc(bm.info().makeColorType(kRGBA_8888_SkColorType))) {
148 return false;
149 }
150 if (!bm.readPixels(rgbaPixmap)) {
151 return false;
152 }
153 pixmap = &rgbaPixmap;
154 }
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500155 int mipLevelCount = GrMipMapped::kYes == options.fMipMapping
156 ? SkMipMap::ComputeLevelCount(bm.width(), bm.height())
157 : 1;
158 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
159
Brian Osman250e6e82017-12-12 16:24:41 -0500160 texels[0].fPixels = pixmap->addr();
161 texels[0].fRowBytes = pixmap->rowBytes();
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500162
163 for (int i = 1; i < mipLevelCount; i++) {
164 texels[i].fPixels = nullptr;
165 texels[i].fRowBytes = 0;
166 }
Robert Phillips57e08282017-11-16 14:59:48 -0500167
Robert Phillips6be756b2018-01-16 15:07:54 -0500168 backingTexture = resourceProvider->createTexture(backingDesc, SkBudgeted::kNo,
169 texels.get(), mipLevelCount,
170 SkDestinationSurfaceColorMode::kLegacy);
Robert Phillips57e08282017-11-16 14:59:48 -0500171 if (!backingTexture) {
172 return false;
173 }
174
Robert Phillipsb67821d2017-12-13 15:00:45 -0500175 backEndTexture = backingTexture->getBackendTexture();
Robert Phillips57e08282017-11-16 14:59:48 -0500176 if (!backEndTexture.isValid()) {
177 return false;
178 }
179 }
180
181 backingDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500182 backingDesc.fWidth = options.fOffScreenWidth;
183 backingDesc.fHeight = options.fOffScreenHeight;
184 backingDesc.fSampleCnt = options.fOffScreenSampleCount;
Robert Phillips57e08282017-11-16 14:59:48 -0500185
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500186 SkAutoTMalloc<uint32_t> data(backingDesc.fWidth * backingDesc.fHeight);
187 sk_memset32(data.get(), 0, backingDesc.fWidth * backingDesc.fHeight);
Robert Phillips57e08282017-11-16 14:59:48 -0500188
Robert Phillips57e08282017-11-16 14:59:48 -0500189
190 {
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500191 // This backend object should be renderable but not textureable. Given the limitations
192 // of how we're creating it though it will wind up being secretly textureable.
193 // We use this fact to initialize it with data but don't allow mipmaps
194 GrMipLevel level0 = { data.get(), backingDesc.fWidth*sizeof(uint32_t) };
195
Robert Phillips6be756b2018-01-16 15:07:54 -0500196 sk_sp<GrTexture> tmp = resourceProvider->createTexture(
Robert Phillips57e08282017-11-16 14:59:48 -0500197 backingDesc, SkBudgeted::kNo,
198 &level0, 1,
199 SkDestinationSurfaceColorMode::kLegacy);
200 if (!tmp || !tmp->asRenderTarget()) {
201 return false;
202 }
203
204 backingRenderTarget = sk_ref_sp(tmp->asRenderTarget());
205
Robert Phillipsb67821d2017-12-13 15:00:45 -0500206 backEndRenderTarget = backingRenderTarget->getBackendRenderTarget();
Robert Phillips57e08282017-11-16 14:59:48 -0500207 if (!backEndRenderTarget.isValid()) {
208 return false;
209 }
210 }
211
212 {
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500213 int mipLevelCount = GrMipMapped::kYes == options.fOffScreenMipMapping
214 ? SkMipMap::ComputeLevelCount(backingDesc.fWidth, backingDesc.fHeight)
215 : 1;
216 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
217
218 texels[0].fPixels = data.get();
219 texels[0].fRowBytes = backingDesc.fWidth*sizeof(uint32_t);
220
221 for (int i = 1; i < mipLevelCount; i++) {
222 texels[i].fPixels = nullptr;
223 texels[i].fRowBytes = 0;
224 }
225
Robert Phillips6be756b2018-01-16 15:07:54 -0500226 backingTextureRenderTarget = resourceProvider->createTexture(
Robert Phillips57e08282017-11-16 14:59:48 -0500227 backingDesc, SkBudgeted::kNo,
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500228 texels.get(), mipLevelCount,
Robert Phillips57e08282017-11-16 14:59:48 -0500229 SkDestinationSurfaceColorMode::kLegacy);
230 if (!backingTextureRenderTarget || !backingTextureRenderTarget->asRenderTarget()) {
231 return false;
232 }
233
Robert Phillipsb67821d2017-12-13 15:00:45 -0500234 backEndTextureRenderTarget = backingTextureRenderTarget->getBackendTexture();
Robert Phillips57e08282017-11-16 14:59:48 -0500235 if (!backEndTextureRenderTarget.isValid()) {
236 return false;
237 }
238 }
239
240
241 return true;
242}
243
Joe Gregorio1e735c02017-04-19 14:05:14 -0400244int main(int argc, char** argv) {
245 SkCommandLineFlags::Parse(argc, argv);
246 duration = FLAGS_duration;
247 frame = FLAGS_frame;
Joe Gregorio1fd18232017-02-13 11:39:43 -0500248 DrawOptions options = GetDrawOptions();
249 // If textOnly then only do one type of image, otherwise the text
250 // output is duplicated for each type.
251 if (options.textOnly) {
252 options.raster = true;
253 options.gpu = false;
254 options.pdf = false;
255 options.skp = false;
256 }
halcanarydecb21e2015-12-10 07:52:45 -0800257 if (options.source) {
bungeman38d909e2016-08-02 14:40:46 -0700258 sk_sp<SkData> data(SkData::MakeFromFileName(options.source));
halcanarydecb21e2015-12-10 07:52:45 -0800259 if (!data) {
260 perror(options.source);
261 return 1;
262 } else {
halcanary7b8b2372016-04-18 08:17:56 -0700263 image = SkImage::MakeFromEncoded(std::move(data));
halcanarydecb21e2015-12-10 07:52:45 -0800264 if (!image) {
265 perror("Unable to decode the source image.");
266 return 1;
267 }
Robert Phillips57e08282017-11-16 14:59:48 -0500268 SkAssertResult(image->asLegacyBitmap(&source, SkImage::kRO_LegacyBitmapMode));
halcanarydecb21e2015-12-10 07:52:45 -0800269 }
270 }
halcanaryd0964592016-03-25 11:29:34 -0700271 sk_sp<SkData> rasterData, gpuData, pdfData, skpData;
Matt Saretta2f71262016-11-29 17:14:58 -0500272 SkColorType colorType = kN32_SkColorType;
273 sk_sp<SkColorSpace> colorSpace = nullptr;
274 if (options.f16) {
275 SkASSERT(options.srgb);
276 colorType = kRGBA_F16_SkColorType;
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500277 colorSpace = SkColorSpace::MakeSRGBLinear();
Matt Saretta2f71262016-11-29 17:14:58 -0500278 } else if (options.srgb) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500279 colorSpace = SkColorSpace::MakeSRGB();
Matt Saretta2f71262016-11-29 17:14:58 -0500280 }
281 SkImageInfo info = SkImageInfo::Make(options.size.width(), options.size.height(), colorType,
282 kPremul_SkAlphaType, colorSpace);
halcanarydecb21e2015-12-10 07:52:45 -0800283 if (options.raster) {
Matt Saretta2f71262016-11-29 17:14:58 -0500284 auto rasterSurface = SkSurface::MakeRaster(info);
halcanaryf0270c32016-05-23 09:02:38 -0700285 srand(0);
Joe Gregorio4f12f1e2017-06-14 15:54:32 -0400286 draw(prepare_canvas(rasterSurface->getCanvas()));
Mike Reed6409f842017-07-11 16:03:13 -0400287 rasterData = encode_snapshot(rasterSurface);
halcanarydecb21e2015-12-10 07:52:45 -0800288 }
289 if (options.gpu) {
Robert Phillips57e08282017-11-16 14:59:48 -0500290 sk_sp<GrContext> grContext = create_grcontext(gGLDriverInfo);
halcanarydecb21e2015-12-10 07:52:45 -0800291 if (!grContext) {
mtklein7d10b9f2016-07-27 11:17:18 -0700292 fputs("Unable to get GrContext.\n", stderr);
halcanarydecb21e2015-12-10 07:52:45 -0800293 } else {
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500294 if (!setup_backend_objects(grContext.get(), source, options)) {
Robert Phillips57e08282017-11-16 14:59:48 -0500295 fputs("Unable to create backend objects.\n", stderr);
296 exit(1);
297 }
298
Matt Saretta2f71262016-11-29 17:14:58 -0500299 auto surface = SkSurface::MakeRenderTarget(grContext.get(), SkBudgeted::kNo, info);
halcanarydecb21e2015-12-10 07:52:45 -0800300 if (!surface) {
301 fputs("Unable to get render surface.\n", stderr);
302 exit(1);
303 }
halcanaryf0270c32016-05-23 09:02:38 -0700304 srand(0);
Joe Gregorio4f12f1e2017-06-14 15:54:32 -0400305 draw(prepare_canvas(surface->getCanvas()));
Mike Reed6409f842017-07-11 16:03:13 -0400306 gpuData = encode_snapshot(surface);
halcanarydecb21e2015-12-10 07:52:45 -0800307 }
halcanarydecb21e2015-12-10 07:52:45 -0800308 }
309 if (options.pdf) {
310 SkDynamicMemoryWStream pdfStream;
halcanary676ab682016-05-03 12:10:04 -0700311 sk_sp<SkDocument> document(SkDocument::MakePDF(&pdfStream));
mtkleincd01b032016-08-31 04:58:19 -0700312 if (document) {
313 srand(0);
Joe Gregorio4f12f1e2017-06-14 15:54:32 -0400314 draw(prepare_canvas(document->beginPage(options.size.width(), options.size.height())));
mtkleincd01b032016-08-31 04:58:19 -0700315 document->close();
reed42943c82016-09-12 12:01:44 -0700316 pdfData = pdfStream.detachAsData();
mtkleincd01b032016-08-31 04:58:19 -0700317 }
halcanarydecb21e2015-12-10 07:52:45 -0800318 }
319 if (options.skp) {
320 SkSize size;
321 size = options.size;
322 SkPictureRecorder recorder;
halcanaryf0270c32016-05-23 09:02:38 -0700323 srand(0);
Joe Gregorio4f12f1e2017-06-14 15:54:32 -0400324 draw(prepare_canvas(recorder.beginRecording(size.width(), size.height())));
halcanaryd0964592016-03-25 11:29:34 -0700325 auto picture = recorder.finishRecordingAsPicture();
halcanarydecb21e2015-12-10 07:52:45 -0800326 SkDynamicMemoryWStream skpStream;
327 picture->serialize(&skpStream);
reed42943c82016-09-12 12:01:44 -0700328 skpData = skpStream.detachAsData();
halcanarydecb21e2015-12-10 07:52:45 -0800329 }
330
331 printf("{\n");
Joe Gregorio0236cba2017-02-13 13:55:17 -0500332 if (!options.textOnly) {
Joe Gregorio97b10ac2017-06-01 13:24:11 -0400333 dump_output(rasterData, "Raster", false);
334 dump_output(gpuData, "Gpu", false);
335 dump_output(pdfData, "Pdf", false);
336 dump_output(skpData, "Skp", false);
Joe Gregorio0236cba2017-02-13 13:55:17 -0500337 } else {
Hal Canary169f37f2017-02-15 10:20:30 -0500338 std::string textoutput = gTextOutput.str();
Joe Gregorio97b10ac2017-06-01 13:24:11 -0400339 dump_output(textoutput.c_str(), textoutput.length(), "Text", false);
Joe Gregorio0236cba2017-02-13 13:55:17 -0500340 }
Joe Gregorio97b10ac2017-06-01 13:24:11 -0400341 std::string glinfo = gGLDriverInfo.str();
342 dump_output(glinfo.c_str(), glinfo.length(), "GLInfo", true);
halcanarydecb21e2015-12-10 07:52:45 -0800343 printf("}\n");
344
halcanarydecb21e2015-12-10 07:52:45 -0800345 return 0;
346}