blob: e8d258d765b3b8e1f9a545fecbfea3286e840417 [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"
Joe Gregorio8e9810c2018-05-29 13:56:27 -040026#include "gl/GLTestContext.h"
Robert Phillips57e08282017-11-16 14:59:48 -050027
halcanarydecb21e2015-12-10 07:52:45 -080028// Globals externed in fiddle_main.h
Robert Phillips57e08282017-11-16 14:59:48 -050029sk_sp<GrTexture> backingTexture; // not externed
30GrBackendTexture backEndTexture;
31
32sk_sp<GrRenderTarget> backingRenderTarget; // not externed
33GrBackendRenderTarget backEndRenderTarget;
34
35sk_sp<GrTexture> backingTextureRenderTarget; // not externed
36GrBackendTexture backEndTextureRenderTarget;
37
halcanarydecb21e2015-12-10 07:52:45 -080038SkBitmap source;
halcanary7b8b2372016-04-18 08:17:56 -070039sk_sp<SkImage> image;
Joe Gregorio1e735c02017-04-19 14:05:14 -040040double duration; // The total duration of the animation in seconds.
41double frame; // A value in [0, 1] of where we are in the animation.
halcanarydecb21e2015-12-10 07:52:45 -080042
Hal Canary169f37f2017-02-15 10:20:30 -050043// Global used by the local impl of SkDebugf.
44std::ostringstream gTextOutput;
Joe Gregorio1fd18232017-02-13 11:39:43 -050045
Joe Gregorio97b10ac2017-06-01 13:24:11 -040046// Global to record the GL driver info via create_grcontext().
47std::ostringstream gGLDriverInfo;
48
Joe Gregorio1fd18232017-02-13 11:39:43 -050049void SkDebugf(const char * fmt, ...) {
Joe Gregorio1fd18232017-02-13 11:39:43 -050050 va_list args;
51 va_start(args, fmt);
Hal Canary169f37f2017-02-15 10:20:30 -050052 char formatbuffer[1024];
53 int n = vsnprintf(formatbuffer, sizeof(formatbuffer), fmt, args);
Joe Gregorio1fd18232017-02-13 11:39:43 -050054 va_end(args);
55 if (n>=0 && n<=int(sizeof(formatbuffer))) {
Hal Canary169f37f2017-02-15 10:20:30 -050056 gTextOutput.write(formatbuffer, n);
Joe Gregorio1fd18232017-02-13 11:39:43 -050057 }
58}
59
halcanarydecb21e2015-12-10 07:52:45 -080060static void encode_to_base64(const void* data, size_t size, FILE* out) {
61 const uint8_t* input = reinterpret_cast<const uint8_t*>(data);
62 const uint8_t* end = &input[size];
63 static const char codes[] =
64 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
65 "abcdefghijklmnopqrstuvwxyz0123456789+/";
66 while (input != end) {
67 uint8_t b = (*input & 0xFC) >> 2;
68 fputc(codes[b], out);
69 b = (*input & 0x03) << 4;
70 ++input;
71 if (input == end) {
72 fputc(codes[b], out);
73 fputs("==", out);
74 return;
75 }
76 b |= (*input & 0xF0) >> 4;
77 fputc(codes[b], out);
78 b = (*input & 0x0F) << 2;
79 ++input;
80 if (input == end) {
81 fputc(codes[b], out);
82 fputc('=', out);
83 return;
84 }
85 b |= (*input & 0xC0) >> 6;
86 fputc(codes[b], out);
87 b = *input & 0x3F;
88 fputc(codes[b], out);
89 ++input;
90 }
91}
92
Hal Canary169f37f2017-02-15 10:20:30 -050093
94static void dump_output(const void* data, size_t size,
95 const char* name, bool last = true) {
96 printf("\t\"%s\": \"", name);
97 encode_to_base64(data, size, stdout);
98 fputs(last ? "\"\n" : "\",\n", stdout);
99}
100
halcanaryd0964592016-03-25 11:29:34 -0700101static void dump_output(const sk_sp<SkData>& data,
102 const char* name, bool last = true) {
halcanarydecb21e2015-12-10 07:52:45 -0800103 if (data) {
Hal Canary169f37f2017-02-15 10:20:30 -0500104 dump_output(data->data(), data->size(), name, last);
halcanarydecb21e2015-12-10 07:52:45 -0800105 }
106}
107
Mike Reed6409f842017-07-11 16:03:13 -0400108static sk_sp<SkData> encode_snapshot(const sk_sp<SkSurface>& surface) {
halcanary7b8b2372016-04-18 08:17:56 -0700109 sk_sp<SkImage> img(surface->makeImageSnapshot());
Mike Reed6409f842017-07-11 16:03:13 -0400110 return img ? img->encodeToData() : nullptr;
halcanarydecb21e2015-12-10 07:52:45 -0800111}
112
Joe Gregorio4f12f1e2017-06-14 15:54:32 -0400113static SkCanvas* prepare_canvas(SkCanvas * canvas) {
114 canvas->clear(SK_ColorWHITE);
115 return canvas;
116}
117
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500118static bool setup_backend_objects(GrContext* context,
119 const SkBitmap& bm,
120 const DrawOptions& options) {
Robert Phillips57e08282017-11-16 14:59:48 -0500121 if (!context) {
122 return false;
123 }
124
Robert Phillips9da87e02019-02-04 13:26:26 -0500125 auto resourceProvider = context->priv().resourceProvider();
Robert Phillips6be756b2018-01-16 15:07:54 -0500126
Robert Phillips57e08282017-11-16 14:59:48 -0500127 GrSurfaceDesc backingDesc;
128 backingDesc.fFlags = kNone_GrSurfaceFlags;
Robert Phillips57e08282017-11-16 14:59:48 -0500129 backingDesc.fWidth = bm.width();
130 backingDesc.fHeight = bm.height();
Greg Danielfaa095e2017-12-19 13:15:02 -0500131 // This config must match the SkColorType used in draw.cpp in the SkImage and Surface factories
Robert Phillipsb67821d2017-12-13 15:00:45 -0500132 backingDesc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500133 backingDesc.fSampleCnt = 1;
Robert Phillips57e08282017-11-16 14:59:48 -0500134
135 if (!bm.empty()) {
Brian Osman250e6e82017-12-12 16:24:41 -0500136 SkPixmap originalPixmap;
137 SkPixmap* pixmap = &originalPixmap;
138 if (!bm.peekPixels(&originalPixmap)) {
139 return false;
140 }
141
142 SkAutoPixmapStorage rgbaPixmap;
143 if (kN32_SkColorType != kRGBA_8888_SkColorType) {
144 if (!rgbaPixmap.tryAlloc(bm.info().makeColorType(kRGBA_8888_SkColorType))) {
145 return false;
146 }
147 if (!bm.readPixels(rgbaPixmap)) {
148 return false;
149 }
150 pixmap = &rgbaPixmap;
151 }
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500152 int mipLevelCount = GrMipMapped::kYes == options.fMipMapping
153 ? SkMipMap::ComputeLevelCount(bm.width(), bm.height())
154 : 1;
155 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
156
Brian Osman250e6e82017-12-12 16:24:41 -0500157 texels[0].fPixels = pixmap->addr();
158 texels[0].fRowBytes = pixmap->rowBytes();
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500159
160 for (int i = 1; i < mipLevelCount; i++) {
161 texels[i].fPixels = nullptr;
162 texels[i].fRowBytes = 0;
163 }
Robert Phillips57e08282017-11-16 14:59:48 -0500164
Brian Salomon58389b92018-03-07 13:01:25 -0500165 backingTexture = resourceProvider->createTexture(backingDesc, SkBudgeted::kNo, texels.get(),
Brian Osman2b23c4b2018-06-01 12:25:08 -0400166 mipLevelCount);
Robert Phillips57e08282017-11-16 14:59:48 -0500167 if (!backingTexture) {
168 return false;
169 }
170
Robert Phillipsb67821d2017-12-13 15:00:45 -0500171 backEndTexture = backingTexture->getBackendTexture();
Robert Phillips57e08282017-11-16 14:59:48 -0500172 if (!backEndTexture.isValid()) {
173 return false;
174 }
175 }
176
177 backingDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500178 backingDesc.fWidth = options.fOffScreenWidth;
179 backingDesc.fHeight = options.fOffScreenHeight;
180 backingDesc.fSampleCnt = options.fOffScreenSampleCount;
Robert Phillips57e08282017-11-16 14:59:48 -0500181
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500182 SkAutoTMalloc<uint32_t> data(backingDesc.fWidth * backingDesc.fHeight);
183 sk_memset32(data.get(), 0, backingDesc.fWidth * backingDesc.fHeight);
Robert Phillips57e08282017-11-16 14:59:48 -0500184
Robert Phillips57e08282017-11-16 14:59:48 -0500185
186 {
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500187 // This backend object should be renderable but not textureable. Given the limitations
188 // of how we're creating it though it will wind up being secretly textureable.
189 // We use this fact to initialize it with data but don't allow mipmaps
190 GrMipLevel level0 = { data.get(), backingDesc.fWidth*sizeof(uint32_t) };
191
Brian Osman2b23c4b2018-06-01 12:25:08 -0400192 sk_sp<GrTexture> tmp = resourceProvider->createTexture(backingDesc, SkBudgeted::kNo,
193 &level0, 1);
Robert Phillips57e08282017-11-16 14:59:48 -0500194 if (!tmp || !tmp->asRenderTarget()) {
195 return false;
196 }
197
198 backingRenderTarget = sk_ref_sp(tmp->asRenderTarget());
199
Robert Phillipsb67821d2017-12-13 15:00:45 -0500200 backEndRenderTarget = backingRenderTarget->getBackendRenderTarget();
Robert Phillips57e08282017-11-16 14:59:48 -0500201 if (!backEndRenderTarget.isValid()) {
202 return false;
203 }
204 }
205
206 {
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500207 int mipLevelCount = GrMipMapped::kYes == options.fOffScreenMipMapping
208 ? SkMipMap::ComputeLevelCount(backingDesc.fWidth, backingDesc.fHeight)
209 : 1;
210 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipLevelCount]);
211
212 texels[0].fPixels = data.get();
213 texels[0].fRowBytes = backingDesc.fWidth*sizeof(uint32_t);
214
215 for (int i = 1; i < mipLevelCount; i++) {
216 texels[i].fPixels = nullptr;
217 texels[i].fRowBytes = 0;
218 }
219
Brian Osman2b23c4b2018-06-01 12:25:08 -0400220 backingTextureRenderTarget = resourceProvider->createTexture(backingDesc, SkBudgeted::kNo,
221 texels.get(), mipLevelCount);
Robert Phillips57e08282017-11-16 14:59:48 -0500222 if (!backingTextureRenderTarget || !backingTextureRenderTarget->asRenderTarget()) {
223 return false;
224 }
225
Robert Phillipsb67821d2017-12-13 15:00:45 -0500226 backEndTextureRenderTarget = backingTextureRenderTarget->getBackendTexture();
Robert Phillips57e08282017-11-16 14:59:48 -0500227 if (!backEndTextureRenderTarget.isValid()) {
228 return false;
229 }
230 }
231
232
233 return true;
234}
235
Joe Gregorio1e735c02017-04-19 14:05:14 -0400236int main(int argc, char** argv) {
237 SkCommandLineFlags::Parse(argc, argv);
238 duration = FLAGS_duration;
239 frame = FLAGS_frame;
Joe Gregorio1fd18232017-02-13 11:39:43 -0500240 DrawOptions options = GetDrawOptions();
241 // If textOnly then only do one type of image, otherwise the text
242 // output is duplicated for each type.
243 if (options.textOnly) {
244 options.raster = true;
245 options.gpu = false;
246 options.pdf = false;
247 options.skp = false;
248 }
halcanarydecb21e2015-12-10 07:52:45 -0800249 if (options.source) {
bungeman38d909e2016-08-02 14:40:46 -0700250 sk_sp<SkData> data(SkData::MakeFromFileName(options.source));
halcanarydecb21e2015-12-10 07:52:45 -0800251 if (!data) {
252 perror(options.source);
253 return 1;
254 } else {
halcanary7b8b2372016-04-18 08:17:56 -0700255 image = SkImage::MakeFromEncoded(std::move(data));
halcanarydecb21e2015-12-10 07:52:45 -0800256 if (!image) {
257 perror("Unable to decode the source image.");
258 return 1;
259 }
Cary Clark4f5a79c2018-02-07 15:51:00 -0500260 SkAssertResult(image->asLegacyBitmap(&source));
halcanarydecb21e2015-12-10 07:52:45 -0800261 }
262 }
halcanaryd0964592016-03-25 11:29:34 -0700263 sk_sp<SkData> rasterData, gpuData, pdfData, skpData;
Matt Saretta2f71262016-11-29 17:14:58 -0500264 SkColorType colorType = kN32_SkColorType;
265 sk_sp<SkColorSpace> colorSpace = nullptr;
266 if (options.f16) {
267 SkASSERT(options.srgb);
268 colorType = kRGBA_F16_SkColorType;
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500269 colorSpace = SkColorSpace::MakeSRGBLinear();
Matt Saretta2f71262016-11-29 17:14:58 -0500270 } else if (options.srgb) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500271 colorSpace = SkColorSpace::MakeSRGB();
Matt Saretta2f71262016-11-29 17:14:58 -0500272 }
273 SkImageInfo info = SkImageInfo::Make(options.size.width(), options.size.height(), colorType,
274 kPremul_SkAlphaType, colorSpace);
halcanarydecb21e2015-12-10 07:52:45 -0800275 if (options.raster) {
Matt Saretta2f71262016-11-29 17:14:58 -0500276 auto rasterSurface = SkSurface::MakeRaster(info);
halcanaryf0270c32016-05-23 09:02:38 -0700277 srand(0);
Joe Gregorio4f12f1e2017-06-14 15:54:32 -0400278 draw(prepare_canvas(rasterSurface->getCanvas()));
Mike Reed6409f842017-07-11 16:03:13 -0400279 rasterData = encode_snapshot(rasterSurface);
halcanarydecb21e2015-12-10 07:52:45 -0800280 }
281 if (options.gpu) {
Joe Gregorio8e9810c2018-05-29 13:56:27 -0400282 std::unique_ptr<sk_gpu_test::GLTestContext> glContext;
283 sk_sp<GrContext> grContext = create_grcontext(gGLDriverInfo, &glContext);
halcanarydecb21e2015-12-10 07:52:45 -0800284 if (!grContext) {
mtklein7d10b9f2016-07-27 11:17:18 -0700285 fputs("Unable to get GrContext.\n", stderr);
halcanarydecb21e2015-12-10 07:52:45 -0800286 } else {
Robert Phillipsc34aa9d2017-11-17 14:59:43 -0500287 if (!setup_backend_objects(grContext.get(), source, options)) {
Robert Phillips57e08282017-11-16 14:59:48 -0500288 fputs("Unable to create backend objects.\n", stderr);
289 exit(1);
290 }
291
Matt Saretta2f71262016-11-29 17:14:58 -0500292 auto surface = SkSurface::MakeRenderTarget(grContext.get(), SkBudgeted::kNo, info);
halcanarydecb21e2015-12-10 07:52:45 -0800293 if (!surface) {
294 fputs("Unable to get render surface.\n", stderr);
295 exit(1);
296 }
halcanaryf0270c32016-05-23 09:02:38 -0700297 srand(0);
Joe Gregorio4f12f1e2017-06-14 15:54:32 -0400298 draw(prepare_canvas(surface->getCanvas()));
Mike Reed6409f842017-07-11 16:03:13 -0400299 gpuData = encode_snapshot(surface);
halcanarydecb21e2015-12-10 07:52:45 -0800300 }
halcanarydecb21e2015-12-10 07:52:45 -0800301 }
302 if (options.pdf) {
303 SkDynamicMemoryWStream pdfStream;
Hal Canary3026d4b2019-01-07 10:00:48 -0500304 auto document = SkPDF::MakeDocument(&pdfStream);
mtkleincd01b032016-08-31 04:58:19 -0700305 if (document) {
306 srand(0);
Joe Gregorio4f12f1e2017-06-14 15:54:32 -0400307 draw(prepare_canvas(document->beginPage(options.size.width(), options.size.height())));
mtkleincd01b032016-08-31 04:58:19 -0700308 document->close();
reed42943c82016-09-12 12:01:44 -0700309 pdfData = pdfStream.detachAsData();
mtkleincd01b032016-08-31 04:58:19 -0700310 }
halcanarydecb21e2015-12-10 07:52:45 -0800311 }
312 if (options.skp) {
313 SkSize size;
314 size = options.size;
315 SkPictureRecorder recorder;
halcanaryf0270c32016-05-23 09:02:38 -0700316 srand(0);
Joe Gregorio4f12f1e2017-06-14 15:54:32 -0400317 draw(prepare_canvas(recorder.beginRecording(size.width(), size.height())));
halcanaryd0964592016-03-25 11:29:34 -0700318 auto picture = recorder.finishRecordingAsPicture();
halcanarydecb21e2015-12-10 07:52:45 -0800319 SkDynamicMemoryWStream skpStream;
320 picture->serialize(&skpStream);
reed42943c82016-09-12 12:01:44 -0700321 skpData = skpStream.detachAsData();
halcanarydecb21e2015-12-10 07:52:45 -0800322 }
323
324 printf("{\n");
Joe Gregorio0236cba2017-02-13 13:55:17 -0500325 if (!options.textOnly) {
Joe Gregorio97b10ac2017-06-01 13:24:11 -0400326 dump_output(rasterData, "Raster", false);
327 dump_output(gpuData, "Gpu", false);
328 dump_output(pdfData, "Pdf", false);
329 dump_output(skpData, "Skp", false);
Joe Gregorio0236cba2017-02-13 13:55:17 -0500330 } else {
Hal Canary169f37f2017-02-15 10:20:30 -0500331 std::string textoutput = gTextOutput.str();
Joe Gregorio97b10ac2017-06-01 13:24:11 -0400332 dump_output(textoutput.c_str(), textoutput.length(), "Text", false);
Joe Gregorio0236cba2017-02-13 13:55:17 -0500333 }
Joe Gregorio97b10ac2017-06-01 13:24:11 -0400334 std::string glinfo = gGLDriverInfo.str();
335 dump_output(glinfo.c_str(), glinfo.length(), "GLInfo", true);
halcanarydecb21e2015-12-10 07:52:45 -0800336 printf("}\n");
337
halcanarydecb21e2015-12-10 07:52:45 -0800338 return 0;
339}