blob: 578f7cbf0c1722b9840fd29de857259f40de69f9 [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;
Robert Phillips57e08282017-11-16 14:59:48 -0500131 backingDesc.fWidth = bm.width();
132 backingDesc.fHeight = bm.height();
Greg Danielfaa095e2017-12-19 13:15:02 -0500133 // This config must match the SkColorType used in draw.cpp in the SkImage and Surface factories
Robert Phillipsb67821d2017-12-13 15:00:45 -0500134 backingDesc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500135 backingDesc.fSampleCnt = 1;
Robert Phillips57e08282017-11-16 14:59:48 -0500136
137 if (!bm.empty()) {
Brian Osman250e6e82017-12-12 16:24:41 -0500138 SkPixmap originalPixmap;
139 SkPixmap* pixmap = &originalPixmap;
140 if (!bm.peekPixels(&originalPixmap)) {
141 return false;
142 }
143
144 SkAutoPixmapStorage rgbaPixmap;
145 if (kN32_SkColorType != kRGBA_8888_SkColorType) {
146 if (!rgbaPixmap.tryAlloc(bm.info().makeColorType(kRGBA_8888_SkColorType))) {
147 return false;
148 }
149 if (!bm.readPixels(rgbaPixmap)) {
150 return false;
151 }
152 pixmap = &rgbaPixmap;
153 }
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500154 int mipLevelCount = GrMipMapped::kYes == options.fMipMapping
155 ? SkMipMap::ComputeLevelCount(bm.width(), bm.height())
156 : 1;
157 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
158
Brian Osman250e6e82017-12-12 16:24:41 -0500159 texels[0].fPixels = pixmap->addr();
160 texels[0].fRowBytes = pixmap->rowBytes();
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500161
162 for (int i = 1; i < mipLevelCount; i++) {
163 texels[i].fPixels = nullptr;
164 texels[i].fRowBytes = 0;
165 }
Robert Phillips57e08282017-11-16 14:59:48 -0500166
Brian Salomon58389b92018-03-07 13:01:25 -0500167 backingTexture = resourceProvider->createTexture(backingDesc, SkBudgeted::kNo, texels.get(),
168 mipLevelCount,
169 SkDestinationSurfaceColorMode::kLegacy);
Robert Phillips57e08282017-11-16 14:59:48 -0500170 if (!backingTexture) {
171 return false;
172 }
173
Robert Phillipsb67821d2017-12-13 15:00:45 -0500174 backEndTexture = backingTexture->getBackendTexture();
Robert Phillips57e08282017-11-16 14:59:48 -0500175 if (!backEndTexture.isValid()) {
176 return false;
177 }
178 }
179
180 backingDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500181 backingDesc.fWidth = options.fOffScreenWidth;
182 backingDesc.fHeight = options.fOffScreenHeight;
183 backingDesc.fSampleCnt = options.fOffScreenSampleCount;
Robert Phillips57e08282017-11-16 14:59:48 -0500184
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500185 SkAutoTMalloc<uint32_t> data(backingDesc.fWidth * backingDesc.fHeight);
186 sk_memset32(data.get(), 0, backingDesc.fWidth * backingDesc.fHeight);
Robert Phillips57e08282017-11-16 14:59:48 -0500187
Robert Phillips57e08282017-11-16 14:59:48 -0500188
189 {
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500190 // This backend object should be renderable but not textureable. Given the limitations
191 // of how we're creating it though it will wind up being secretly textureable.
192 // We use this fact to initialize it with data but don't allow mipmaps
193 GrMipLevel level0 = { data.get(), backingDesc.fWidth*sizeof(uint32_t) };
194
Robert Phillips6be756b2018-01-16 15:07:54 -0500195 sk_sp<GrTexture> tmp = resourceProvider->createTexture(
Brian Salomon58389b92018-03-07 13:01:25 -0500196 backingDesc, SkBudgeted::kNo, &level0, 1, SkDestinationSurfaceColorMode::kLegacy);
Robert Phillips57e08282017-11-16 14:59:48 -0500197 if (!tmp || !tmp->asRenderTarget()) {
198 return false;
199 }
200
201 backingRenderTarget = sk_ref_sp(tmp->asRenderTarget());
202
Robert Phillipsb67821d2017-12-13 15:00:45 -0500203 backEndRenderTarget = backingRenderTarget->getBackendRenderTarget();
Robert Phillips57e08282017-11-16 14:59:48 -0500204 if (!backEndRenderTarget.isValid()) {
205 return false;
206 }
207 }
208
209 {
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500210 int mipLevelCount = GrMipMapped::kYes == options.fOffScreenMipMapping
211 ? SkMipMap::ComputeLevelCount(backingDesc.fWidth, backingDesc.fHeight)
212 : 1;
213 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
214
215 texels[0].fPixels = data.get();
216 texels[0].fRowBytes = backingDesc.fWidth*sizeof(uint32_t);
217
218 for (int i = 1; i < mipLevelCount; i++) {
219 texels[i].fPixels = nullptr;
220 texels[i].fRowBytes = 0;
221 }
222
Robert Phillips6be756b2018-01-16 15:07:54 -0500223 backingTextureRenderTarget = resourceProvider->createTexture(
Brian Salomon58389b92018-03-07 13:01:25 -0500224 backingDesc, SkBudgeted::kNo, texels.get(), mipLevelCount,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500225 SkDestinationSurfaceColorMode::kLegacy);
Robert Phillips57e08282017-11-16 14:59:48 -0500226 if (!backingTextureRenderTarget || !backingTextureRenderTarget->asRenderTarget()) {
227 return false;
228 }
229
Robert Phillipsb67821d2017-12-13 15:00:45 -0500230 backEndTextureRenderTarget = backingTextureRenderTarget->getBackendTexture();
Robert Phillips57e08282017-11-16 14:59:48 -0500231 if (!backEndTextureRenderTarget.isValid()) {
232 return false;
233 }
234 }
235
236
237 return true;
238}
239
Joe Gregorio1e735c02017-04-19 14:05:14 -0400240int main(int argc, char** argv) {
241 SkCommandLineFlags::Parse(argc, argv);
242 duration = FLAGS_duration;
243 frame = FLAGS_frame;
Joe Gregorio1fd18232017-02-13 11:39:43 -0500244 DrawOptions options = GetDrawOptions();
245 // If textOnly then only do one type of image, otherwise the text
246 // output is duplicated for each type.
247 if (options.textOnly) {
248 options.raster = true;
249 options.gpu = false;
250 options.pdf = false;
251 options.skp = false;
252 }
halcanarydecb21e2015-12-10 07:52:45 -0800253 if (options.source) {
bungeman38d909e2016-08-02 14:40:46 -0700254 sk_sp<SkData> data(SkData::MakeFromFileName(options.source));
halcanarydecb21e2015-12-10 07:52:45 -0800255 if (!data) {
256 perror(options.source);
257 return 1;
258 } else {
halcanary7b8b2372016-04-18 08:17:56 -0700259 image = SkImage::MakeFromEncoded(std::move(data));
halcanarydecb21e2015-12-10 07:52:45 -0800260 if (!image) {
261 perror("Unable to decode the source image.");
262 return 1;
263 }
Cary Clark4f5a79c2018-02-07 15:51:00 -0500264 SkAssertResult(image->asLegacyBitmap(&source));
halcanarydecb21e2015-12-10 07:52:45 -0800265 }
266 }
halcanaryd0964592016-03-25 11:29:34 -0700267 sk_sp<SkData> rasterData, gpuData, pdfData, skpData;
Matt Saretta2f71262016-11-29 17:14:58 -0500268 SkColorType colorType = kN32_SkColorType;
269 sk_sp<SkColorSpace> colorSpace = nullptr;
270 if (options.f16) {
271 SkASSERT(options.srgb);
272 colorType = kRGBA_F16_SkColorType;
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500273 colorSpace = SkColorSpace::MakeSRGBLinear();
Matt Saretta2f71262016-11-29 17:14:58 -0500274 } else if (options.srgb) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500275 colorSpace = SkColorSpace::MakeSRGB();
Matt Saretta2f71262016-11-29 17:14:58 -0500276 }
277 SkImageInfo info = SkImageInfo::Make(options.size.width(), options.size.height(), colorType,
278 kPremul_SkAlphaType, colorSpace);
halcanarydecb21e2015-12-10 07:52:45 -0800279 if (options.raster) {
Matt Saretta2f71262016-11-29 17:14:58 -0500280 auto rasterSurface = SkSurface::MakeRaster(info);
halcanaryf0270c32016-05-23 09:02:38 -0700281 srand(0);
Joe Gregorio4f12f1e2017-06-14 15:54:32 -0400282 draw(prepare_canvas(rasterSurface->getCanvas()));
Mike Reed6409f842017-07-11 16:03:13 -0400283 rasterData = encode_snapshot(rasterSurface);
halcanarydecb21e2015-12-10 07:52:45 -0800284 }
285 if (options.gpu) {
Robert Phillips57e08282017-11-16 14:59:48 -0500286 sk_sp<GrContext> grContext = create_grcontext(gGLDriverInfo);
halcanarydecb21e2015-12-10 07:52:45 -0800287 if (!grContext) {
mtklein7d10b9f2016-07-27 11:17:18 -0700288 fputs("Unable to get GrContext.\n", stderr);
halcanarydecb21e2015-12-10 07:52:45 -0800289 } else {
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500290 if (!setup_backend_objects(grContext.get(), source, options)) {
Robert Phillips57e08282017-11-16 14:59:48 -0500291 fputs("Unable to create backend objects.\n", stderr);
292 exit(1);
293 }
294
Matt Saretta2f71262016-11-29 17:14:58 -0500295 auto surface = SkSurface::MakeRenderTarget(grContext.get(), SkBudgeted::kNo, info);
halcanarydecb21e2015-12-10 07:52:45 -0800296 if (!surface) {
297 fputs("Unable to get render surface.\n", stderr);
298 exit(1);
299 }
halcanaryf0270c32016-05-23 09:02:38 -0700300 srand(0);
Joe Gregorio4f12f1e2017-06-14 15:54:32 -0400301 draw(prepare_canvas(surface->getCanvas()));
Mike Reed6409f842017-07-11 16:03:13 -0400302 gpuData = encode_snapshot(surface);
halcanarydecb21e2015-12-10 07:52:45 -0800303 }
halcanarydecb21e2015-12-10 07:52:45 -0800304 }
305 if (options.pdf) {
306 SkDynamicMemoryWStream pdfStream;
halcanary676ab682016-05-03 12:10:04 -0700307 sk_sp<SkDocument> document(SkDocument::MakePDF(&pdfStream));
mtkleincd01b032016-08-31 04:58:19 -0700308 if (document) {
309 srand(0);
Joe Gregorio4f12f1e2017-06-14 15:54:32 -0400310 draw(prepare_canvas(document->beginPage(options.size.width(), options.size.height())));
mtkleincd01b032016-08-31 04:58:19 -0700311 document->close();
reed42943c82016-09-12 12:01:44 -0700312 pdfData = pdfStream.detachAsData();
mtkleincd01b032016-08-31 04:58:19 -0700313 }
halcanarydecb21e2015-12-10 07:52:45 -0800314 }
315 if (options.skp) {
316 SkSize size;
317 size = options.size;
318 SkPictureRecorder recorder;
halcanaryf0270c32016-05-23 09:02:38 -0700319 srand(0);
Joe Gregorio4f12f1e2017-06-14 15:54:32 -0400320 draw(prepare_canvas(recorder.beginRecording(size.width(), size.height())));
halcanaryd0964592016-03-25 11:29:34 -0700321 auto picture = recorder.finishRecordingAsPicture();
halcanarydecb21e2015-12-10 07:52:45 -0800322 SkDynamicMemoryWStream skpStream;
323 picture->serialize(&skpStream);
reed42943c82016-09-12 12:01:44 -0700324 skpData = skpStream.detachAsData();
halcanarydecb21e2015-12-10 07:52:45 -0800325 }
326
327 printf("{\n");
Joe Gregorio0236cba2017-02-13 13:55:17 -0500328 if (!options.textOnly) {
Joe Gregorio97b10ac2017-06-01 13:24:11 -0400329 dump_output(rasterData, "Raster", false);
330 dump_output(gpuData, "Gpu", false);
331 dump_output(pdfData, "Pdf", false);
332 dump_output(skpData, "Skp", false);
Joe Gregorio0236cba2017-02-13 13:55:17 -0500333 } else {
Hal Canary169f37f2017-02-15 10:20:30 -0500334 std::string textoutput = gTextOutput.str();
Joe Gregorio97b10ac2017-06-01 13:24:11 -0400335 dump_output(textoutput.c_str(), textoutput.length(), "Text", false);
Joe Gregorio0236cba2017-02-13 13:55:17 -0500336 }
Joe Gregorio97b10ac2017-06-01 13:24:11 -0400337 std::string glinfo = gGLDriverInfo.str();
338 dump_output(glinfo.c_str(), glinfo.length(), "GLInfo", true);
halcanarydecb21e2015-12-10 07:52:45 -0800339 printf("}\n");
340
halcanarydecb21e2015-12-10 07:52:45 -0800341 return 0;
342}