Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
fiddle_main.cpp
Go to the documentation of this file.
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 <cstdio>
9#include <cstdlib>
10#include <sstream>
11#include <string>
12
14#include "src/core/SkMemset.h"
15#include "src/core/SkMipmap.h"
17
19
21 "The total duration, in seconds, of the animation we are drawing.");
22static DEFINE_double(frame, 1.0,
23 "A double value in [0, 1] that specifies the point in animation to draw.");
24
35
36#if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE)
38#endif
39
40using namespace skia_private;
41
42// Globals externed in fiddle_main.h
48double duration; // The total duration of the animation in seconds.
49double frame; // A value in [0, 1] of where we are in the animation.
51
52// Global used by the local impl of SkDebugf.
53std::ostringstream gTextOutput;
54
55// Global to record the GL driver info via create_direct_context().
56std::ostringstream gGLDriverInfo;
57
61
62void SkDebugf(const char * fmt, ...) {
63 va_list args;
64 va_start(args, fmt);
65 char formatbuffer[1024];
66 int n = vsnprintf(formatbuffer, sizeof(formatbuffer), fmt, args);
67 va_end(args);
68 if (n>=0 && n<=int(sizeof(formatbuffer))) {
69 gTextOutput.write(formatbuffer, n);
70 }
71}
72
73static void encode_to_base64(const void* data, size_t size, FILE* out) {
74 const uint8_t* input = reinterpret_cast<const uint8_t*>(data);
75 const uint8_t* end = &input[size];
76 static const char codes[] =
77 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
78 "abcdefghijklmnopqrstuvwxyz0123456789+/";
79 while (input != end) {
80 uint8_t b = (*input & 0xFC) >> 2;
81 fputc(codes[b], out);
82 b = (*input & 0x03) << 4;
83 ++input;
84 if (input == end) {
85 fputc(codes[b], out);
86 fputs("==", out);
87 return;
88 }
89 b |= (*input & 0xF0) >> 4;
90 fputc(codes[b], out);
91 b = (*input & 0x0F) << 2;
92 ++input;
93 if (input == end) {
94 fputc(codes[b], out);
95 fputc('=', out);
96 return;
97 }
98 b |= (*input & 0xC0) >> 6;
99 fputc(codes[b], out);
100 b = *input & 0x3F;
101 fputc(codes[b], out);
102 ++input;
103 }
104}
105
106
107static void dump_output(const void* data, size_t size,
108 const char* name, bool last = true) {
109 printf("\t\"%s\": \"", name);
110 encode_to_base64(data, size, stdout);
111 fputs(last ? "\"\n" : "\",\n", stdout);
112}
113
114static void dump_output(const sk_sp<SkData>& data,
115 const char* name, bool last = true) {
116 if (data) {
117 dump_output(data->data(), data->size(), name, last);
118 }
119}
120
122 sk_sp<SkImage> img(surface->makeImageSnapshot());
123 return SkPngEncoder::Encode(ctx, img.get(), {});
124}
125
127 canvas->clear(SK_ColorWHITE);
128 return canvas;
129}
130
131#ifdef SK_GL
132static bool setup_backend_objects(GrDirectContext* dContext,
133 const SkBitmap& bm,
134 const DrawOptions& options) {
135 if (!dContext) {
136 fputs("Context is null.\n", stderr);
137 return false;
138 }
139
140 // This config must match the SkColorType used in draw.cpp in the SkImage and Surface factories
142 GrRenderable::kYes);
143
144 if (!bm.empty()) {
145 SkPixmap originalPixmap;
146 SkPixmap* pixmap = &originalPixmap;
147 if (!bm.peekPixels(&originalPixmap)) {
148 fputs("Unable to peekPixels.\n", stderr);
149 return false;
150 }
151
152 SkAutoPixmapStorage rgbaPixmap;
153 constexpr bool kRGBAIsNative = kN32_SkColorType == kRGBA_8888_SkColorType;
154 if ((!kRGBAIsNative)) {
155 if (!rgbaPixmap.tryAlloc(bm.info().makeColorType(kRGBA_8888_SkColorType))) {
156 fputs("Unable to alloc rgbaPixmap.\n", stderr);
157 return false;
158 }
159 if (!bm.readPixels(rgbaPixmap)) {
160 fputs("Unable to read rgbaPixmap.\n", stderr);
161 return false;
162 }
163 pixmap = &rgbaPixmap;
164 }
165
166 managedBackendTexture = sk_gpu_test::ManagedBackendTexture::MakeFromPixmap(
167 dContext,
168 *pixmap,
169 options.fMipMapping,
170 GrRenderable::kNo,
171 GrProtected::kNo);
173 fputs("Failed to create backEndTexture.\n", stderr);
174 return false;
175 }
177 }
178
179 {
180 auto resourceProvider = dContext->priv().resourceProvider();
181
182 SkISize offscreenDims = {options.fOffScreenWidth, options.fOffScreenHeight};
183 AutoTMalloc<uint32_t> data(offscreenDims.area());
184 SkOpts::memset32(data.get(), 0, offscreenDims.area());
185
186 // This backend object should be renderable but not textureable. Given the limitations
187 // of how we're creating it though it will wind up being secretly textureable.
188 // We use this fact to initialize it with data but don't allow mipmaps
189 GrMipLevel level0 = {data.get(), offscreenDims.width()*sizeof(uint32_t), nullptr};
190
191 constexpr int kSampleCnt = 1;
192 sk_sp<GrTexture> tmp =
193 resourceProvider->createTexture(offscreenDims,
194 renderableFormat,
197 GrRenderable::kYes,
198 kSampleCnt,
200 skgpu::Mipmapped::kNo,
201 GrProtected::kNo,
202 &level0,
203 /*label=*/"Fiddle_SetupBackendObjects");
204 if (!tmp || !tmp->asRenderTarget()) {
205 fputs("GrTexture is invalid.\n", stderr);
206 return false;
207 }
208
209 backingRenderTarget = sk_ref_sp(tmp->asRenderTarget());
210
211 backEndRenderTarget = backingRenderTarget->getBackendRenderTarget();
213 fputs("BackEndRenderTarget is invalid.\n", stderr);
214 return false;
215 }
216 }
217
218 {
219 managedBackendTextureRenderTarget = sk_gpu_test::ManagedBackendTexture::MakeWithData(
220 dContext,
221 options.fOffScreenWidth,
222 options.fOffScreenHeight,
223 renderableFormat,
225 options.fOffScreenMipMapping,
226 GrRenderable::kYes,
227 GrProtected::kNo);
229 fputs("Failed to create backendTextureRenderTarget.\n", stderr);
230 return false;
231 }
233 }
234
235 return true;
236}
237#endif
238
239int main(int argc, char** argv) {
241 duration = FLAGS_duration;
242 frame = FLAGS_frame;
244 // If textOnly then only do one type of image, otherwise the text
245 // output is duplicated for each type.
246 if (options.textOnly) {
247 options.raster = true;
248 options.gpu = false;
249 options.pdf = false;
250 options.skp = false;
251 }
252#if defined(SK_FONTMGR_FONTCONFIG_AVAILABLE)
254#else
256#endif
257 if (options.source) {
259 if (!data) {
260 perror(options.source);
261 return 1;
262 } else {
263 image = SkImages::DeferredFromEncodedData(std::move(data));
264 if (!image) {
265 perror("Unable to decode the source image.");
266 return 1;
267 }
269 }
270 }
271 sk_sp<SkData> rasterData, gpuData, pdfData, skpData;
272 SkColorType colorType = kN32_SkColorType;
273 sk_sp<SkColorSpace> colorSpace = nullptr;
274 if (options.f16) {
275 SkASSERT(options.srgb);
277 colorSpace = SkColorSpace::MakeSRGBLinear();
278 } else if (options.srgb) {
279 colorSpace = SkColorSpace::MakeSRGB();
280 }
281 SkImageInfo info = SkImageInfo::Make(options.size.width(), options.size.height(), colorType,
282 kPremul_SkAlphaType, colorSpace);
283 if (options.raster) {
284 auto rasterSurface = SkSurfaces::Raster(info);
285 srand(0);
286 draw(prepare_canvas(rasterSurface->getCanvas()));
287 rasterData = encode_snapshot(nullptr, rasterSurface);
288 }
289#ifdef SK_GL
290 if (options.gpu) {
291 std::unique_ptr<sk_gpu_test::GLTestContext> glContext;
293 if (!direct) {
294 fputs("Unable to get GrContext.\n", stderr);
295 } else {
296 if (!setup_backend_objects(direct.get(), source, options)) {
297 fputs("Unable to create backend objects.\n", stderr);
298 exit(1);
299 }
300
302 if (!surface) {
303 fputs("Unable to get render surface.\n", stderr);
304 exit(1);
305 }
306 srand(0);
307 draw(prepare_canvas(surface->getCanvas()));
308 gpuData = encode_snapshot(direct.get(), surface);
309 }
310 }
311#endif
312
313#ifdef SK_SUPPORT_PDF
314 if (options.pdf) {
315 SkDynamicMemoryWStream pdfStream;
316 auto document = SkPDF::MakeDocument(&pdfStream);
317 if (document) {
318 srand(0);
319 draw(prepare_canvas(document->beginPage(options.size.width(), options.size.height())));
320 document->close();
321 pdfData = pdfStream.detachAsData();
322 }
323 }
324#endif
325
326 if (options.skp) {
327 auto size = SkSize::Make(options.size);
328 SkPictureRecorder recorder;
329 srand(0);
330 draw(prepare_canvas(recorder.beginRecording(size.width(), size.height())));
331 auto picture = recorder.finishRecordingAsPicture();
332 SkDynamicMemoryWStream skpStream;
333 picture->serialize(&skpStream);
334 skpData = skpStream.detachAsData();
335 }
336
337 printf("{\n");
338 if (!options.textOnly) {
339 dump_output(rasterData, "Raster", false);
340 dump_output(gpuData, "Gpu", false);
341 dump_output(pdfData, "Pdf", false);
342 dump_output(skpData, "Skp", false);
343 } else {
344 std::string textoutput = gTextOutput.str();
345 dump_output(textoutput.c_str(), textoutput.length(), "Text", false);
346 }
347 std::string glinfo = gGLDriverInfo.str();
348 dump_output(glinfo.c_str(), glinfo.length(), "GLInfo", true);
349 printf("}\n");
350
351 return 0;
352}
#define DEFINE_double(name, defaultValue, helpString)
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
#define SkAssertResult(cond)
Definition SkAssert.h:123
#define SkASSERT(cond)
Definition SkAssert.h:116
SkColorType
Definition SkColorType.h:19
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition SkColorType.h:38
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
SK_API sk_sp< SkFontMgr > SkFontMgr_New_FontConfig(FcConfig *fc)
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
sk_sp< T > sk_ref_sp(T *obj)
Definition SkRefCnt.h:381
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition aaclip.cpp:27
static void Parse(int argc, const char *const *argv)
SK_API GrBackendFormat defaultBackendFormat(SkColorType, GrRenderable) const
GrResourceProvider * resourceProvider()
GrDirectContextPriv priv()
bool tryAlloc(const SkImageInfo &)
bool empty() const
Definition SkBitmap.h:210
const SkImageInfo & info() const
Definition SkBitmap.h:139
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY) const
Definition SkBitmap.cpp:488
bool peekPixels(SkPixmap *pixmap) const
Definition SkBitmap.cpp:635
void clear(SkColor color)
Definition SkCanvas.h:1199
static sk_sp< SkColorSpace > MakeSRGB()
static sk_sp< SkColorSpace > MakeSRGBLinear()
static sk_sp< SkData > MakeFromFileName(const char path[])
Definition SkData.cpp:148
sk_sp< SkData > detachAsData()
Definition SkStream.cpp:707
static sk_sp< SkFontMgr > RefEmpty()
bool asLegacyBitmap(SkBitmap *bitmap, LegacyBitmapMode legacyBitmapMode=kRO_LegacyBitmapMode) const
Definition SkImage.cpp:233
SkCanvas * beginRecording(const SkRect &bounds, sk_sp< SkBBoxHierarchy > bbh)
sk_sp< SkPicture > finishRecordingAsPicture()
sk_sp< SkData > serialize(const SkSerialProcs *procs=nullptr) const
T * get() const
Definition SkRefCnt.h:303
DrawOptions GetDrawOptions()
Definition draw.cpp:16
sk_sp< GrDirectContext > create_direct_context(std::ostringstream &driverinfo, std::unique_ptr< sk_gpu_test::GLTestContext > *glContext)
VkSurfaceKHR surface
Definition main.cc:49
static bool b
sk_sp< SkFontMgr > fontMgr
GrBackendTexture backEndTextureRenderTarget
static sk_sp< SkData > encode_snapshot(GrDirectContext *ctx, const sk_sp< SkSurface > &surface)
static SkCanvas * prepare_canvas(SkCanvas *canvas)
static void dump_output(const void *data, size_t size, const char *name, bool last=true)
sk_sp< SkImage > image
std::ostringstream gTextOutput
sk_sp< sk_gpu_test::ManagedBackendTexture > managedBackendTexture
SkBitmap source
void SkDebugf(const char *fmt,...)
GrBackendRenderTarget backEndRenderTarget
sk_sp< GrRenderTarget > backingRenderTarget
GrBackendTexture backEndTexture
sk_sp< sk_gpu_test::ManagedBackendTexture > managedBackendTextureRenderTarget
double duration
std::ostringstream gGLDriverInfo
double frame
static void encode_to_base64(const void *data, size_t size, FILE *out)
glong glong end
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
const char * name
Definition fuchsia.cc:50
char ** argv
Definition library.h:9
constexpr SkColor4f kTransparent
Definition SkColor.h:434
SK_API sk_sp< SkImage > DeferredFromEncodedData(sk_sp< SkData > encoded, std::optional< SkAlphaType > alphaType=std::nullopt)
void(* memset32)(uint32_t[], uint32_t, int)
SK_API sk_sp< SkDocument > MakeDocument(SkWStream *stream, const Metadata &metadata)
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switches.h:41
Definition main.py:1
static SkString fmt(SkColor4f c)
Definition p3.cpp:43
constexpr int32_t width() const
Definition SkSize.h:36
constexpr int64_t area() const
Definition SkSize.h:39
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
SkImageInfo makeColorType(SkColorType newColorType) const
static constexpr SkSize Make(SkScalar w, SkScalar h)
Definition SkSize.h:56