Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3370)

Unified Diff: experimental/fiddle/fiddle_main.cpp

Issue 1349163003: experiment/fiddle (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: experimental/fiddle/fiddle_main.cpp
diff --git a/experimental/fiddle/fiddle_main.cpp b/experimental/fiddle/fiddle_main.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..ca0302d9be95a63c85f1bbe52b4ed343316c4d55
--- /dev/null
+++ b/experimental/fiddle/fiddle_main.cpp
@@ -0,0 +1,145 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <stdio.h>
+
+#include "base64.h"
+#include "fiddle_main.h"
+#include "OSMesaContextHolder.h"
+
+// Globals externed in fiddle_main.h
+SkBitmap source;
+SkImage* image(nullptr);
mtklein 2015/09/17 13:58:22 Just curious: you seem to be managing this manuall
+
+const char* FLAGS_source(nullptr);
mtklein 2015/09/17 13:58:22 Any reason these FLAGS_ guys aren't static?
+int32_t FLAGS_width(256);
+int32_t FLAGS_height(256);
+bool FLAGS_gpu(false);
+bool FLAGS_noraster(false);
+bool FLAGS_pdf(false);
+
+static void help() {
+ const char options[] =
+ "\nOptions:\n"
+ " --width WIDTH Set the canvas width (default is 256).\n"
+ " --height HEIGHT Set the canvas height (default is 256).\n"
+ " --source PATH Set the source image file.\n"
+ " --noraster Disable the raster backend.\n"
mtklein 2015/09/17 13:58:22 Why are any of these options? Let's just run them
+ " --gpu Enable the GPU backend.\n"
+ " --pdf Enable the PDF backend.\n\n";
+ fputs(options, stderr);
mtklein 2015/09/17 13:58:22 If you use fprintf you can keep your (default is %
+ exit(2);
+}
+
+static void parse_flags(int, char** argv) {
+ ++argv; // skip argv[0]
+ while (*argv) { // argv is always NULL-terminated
+ if (0 == strcmp(*argv, "--width")) {
+ if (!*++argv) {
+ help();
+ }
+ FLAGS_width = atoi(*argv);
+ } else if (0 == strcmp(*argv, "--height")) {
+ if (!*++argv) {
+ help();
+ }
+ FLAGS_height = atoi(*argv);
+ } else if (0 == strcmp(*argv, "--source")) {
+ if (!*++argv) {
+ help();
+ }
+ FLAGS_source = *argv;
+ } else if (0 == strcmp(*argv, "--gpu")) {
+ FLAGS_gpu = true;
+ } else if (0 == strcmp(*argv, "--noraster")) {
+ FLAGS_noraster = true;
+ } else if (0 == strcmp(*argv, "--pdf")) {
+ FLAGS_pdf = true;
+ } else {
+ help();
+ }
+ ++argv;
+ }
+}
+
+static void dump_output(SkData* pngData, const char* name, bool last = true) {
+ if (pngData) {
+ printf("\t\"%s\": \"", name);
+ EncodeToBase64(pngData->data(), pngData->size(), stdout);
+ fputs(last ? "\"\n" : "\",\n", stdout);
+ }
+}
+
+static SkData* encode_snapshot(SkSurface* surface) {
+ SkAutoTUnref<SkImage> img(surface->newImageSnapshot());
+ return img ? img->encode() : nullptr;
+}
+
+static GrContext* create_mesa_grcontext() {
+ SkAutoTUnref<const GrGLInterface> mesa(GrGLCreateMesaInterface());
+ intptr_t backend = reinterpret_cast<intptr_t>(mesa.get());
+ return backend ? GrContext::Create(kOpenGL_GrBackend, backend) : nullptr;
+}
+
+int main(int argc, char** argv) {
+ parse_flags(argc, argv);
+ if (FLAGS_source) {
+ SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_source));
+ if (!data) {
+ perror("Unable to open the source image file.");
mtklein 2015/09/17 13:58:22 I don't think you mean to be using perror. perror
+ } else {
+ image = SkImage::NewFromEncoded(data);
+ bool success = SkInstallDiscardablePixelRef(data, &source);
+ if (!image || !success) {
+ perror("Unable to decode the source image.");
+ }
+ }
+ }
+ SkISize size = SkISize::Make(FLAGS_width, FLAGS_height);
+ SkAutoTUnref<SkData> rasterData, gpuData, pdfData;
+ if (!FLAGS_noraster) {
+ SkAutoTUnref<SkSurface> rasterSurface(
+ SkSurface::NewRaster(SkImageInfo::MakeN32Premul(size)));
+ draw(rasterSurface->getCanvas());
+ rasterData.reset(encode_snapshot(rasterSurface));
+ }
+ if (FLAGS_gpu) {
+ OSMesaContextHolder osMesaContextHolder;
+ SkAutoTUnref<GrContext> grContext(create_mesa_grcontext());
+ if (!grContext) {
+ fputs("Unable to get Mesa GrContext.\n", stderr);
+ } else {
+ SkAutoTUnref<SkSurface> surface(
+ SkSurface::NewRenderTarget(
+ grContext,
+ SkSurface::kNo_Budgeted,
+ SkImageInfo::MakeN32Premul(size)));
+ if (!surface) {
+ fputs("Unable to get render surface.\n", stderr);
+ exit(1);
+ }
+ draw(surface->getCanvas());
+ gpuData.reset(encode_snapshot(surface));
+ }
+ }
+ if (FLAGS_pdf) {
+ SkDynamicMemoryWStream pdfStream;
+ SkAutoTUnref<SkDocument> document(SkDocument::CreatePDF(&pdfStream));
+ draw(document->beginPage(FLAGS_width, FLAGS_height));
+ document->close();
+ pdfData.reset(pdfStream.copyToData());
+ }
+
+ printf("{\n");
+ dump_output(rasterData, "Raster", !gpuData && !pdfData);
+ dump_output(gpuData, "Gpu", !pdfData);
+ dump_output(pdfData, "Pdf");
+ printf("}\n");
+
+ SkSafeSetNull(image);
+ return 0;
+}

Powered by Google App Engine
This is Rietveld 408576698