Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 <stdio.h> | |
| 9 | |
| 10 #include "base64.h" | |
| 11 #include "fiddle_main.h" | |
| 12 #include "OSMesaContextHolder.h" | |
| 13 | |
| 14 // Globals externed in fiddle_main.h | |
| 15 SkBitmap source; | |
| 16 SkImage* image(nullptr); | |
|
mtklein
2015/09/17 13:58:22
Just curious: you seem to be managing this manuall
| |
| 17 | |
| 18 const char* FLAGS_source(nullptr); | |
|
mtklein
2015/09/17 13:58:22
Any reason these FLAGS_ guys aren't static?
| |
| 19 int32_t FLAGS_width(256); | |
| 20 int32_t FLAGS_height(256); | |
| 21 bool FLAGS_gpu(false); | |
| 22 bool FLAGS_noraster(false); | |
| 23 bool FLAGS_pdf(false); | |
| 24 | |
| 25 static void help() { | |
| 26 const char options[] = | |
| 27 "\nOptions:\n" | |
| 28 " --width WIDTH Set the canvas width (default is 256).\n" | |
| 29 " --height HEIGHT Set the canvas height (default is 256).\n" | |
| 30 " --source PATH Set the source image file.\n" | |
| 31 " --noraster Disable the raster backend.\n" | |
|
mtklein
2015/09/17 13:58:22
Why are any of these options? Let's just run them
| |
| 32 " --gpu Enable the GPU backend.\n" | |
| 33 " --pdf Enable the PDF backend.\n\n"; | |
| 34 fputs(options, stderr); | |
|
mtklein
2015/09/17 13:58:22
If you use fprintf you can keep your (default is %
| |
| 35 exit(2); | |
| 36 } | |
| 37 | |
| 38 static void parse_flags(int, char** argv) { | |
| 39 ++argv; // skip argv[0] | |
| 40 while (*argv) { // argv is always NULL-terminated | |
| 41 if (0 == strcmp(*argv, "--width")) { | |
| 42 if (!*++argv) { | |
| 43 help(); | |
| 44 } | |
| 45 FLAGS_width = atoi(*argv); | |
| 46 } else if (0 == strcmp(*argv, "--height")) { | |
| 47 if (!*++argv) { | |
| 48 help(); | |
| 49 } | |
| 50 FLAGS_height = atoi(*argv); | |
| 51 } else if (0 == strcmp(*argv, "--source")) { | |
| 52 if (!*++argv) { | |
| 53 help(); | |
| 54 } | |
| 55 FLAGS_source = *argv; | |
| 56 } else if (0 == strcmp(*argv, "--gpu")) { | |
| 57 FLAGS_gpu = true; | |
| 58 } else if (0 == strcmp(*argv, "--noraster")) { | |
| 59 FLAGS_noraster = true; | |
| 60 } else if (0 == strcmp(*argv, "--pdf")) { | |
| 61 FLAGS_pdf = true; | |
| 62 } else { | |
| 63 help(); | |
| 64 } | |
| 65 ++argv; | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 static void dump_output(SkData* pngData, const char* name, bool last = true) { | |
| 70 if (pngData) { | |
| 71 printf("\t\"%s\": \"", name); | |
| 72 EncodeToBase64(pngData->data(), pngData->size(), stdout); | |
| 73 fputs(last ? "\"\n" : "\",\n", stdout); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 static SkData* encode_snapshot(SkSurface* surface) { | |
| 78 SkAutoTUnref<SkImage> img(surface->newImageSnapshot()); | |
| 79 return img ? img->encode() : nullptr; | |
| 80 } | |
| 81 | |
| 82 static GrContext* create_mesa_grcontext() { | |
| 83 SkAutoTUnref<const GrGLInterface> mesa(GrGLCreateMesaInterface()); | |
| 84 intptr_t backend = reinterpret_cast<intptr_t>(mesa.get()); | |
| 85 return backend ? GrContext::Create(kOpenGL_GrBackend, backend) : nullptr; | |
| 86 } | |
| 87 | |
| 88 int main(int argc, char** argv) { | |
| 89 parse_flags(argc, argv); | |
| 90 if (FLAGS_source) { | |
| 91 SkAutoTUnref<SkData> data(SkData::NewFromFileName(FLAGS_source)); | |
| 92 if (!data) { | |
| 93 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
| |
| 94 } else { | |
| 95 image = SkImage::NewFromEncoded(data); | |
| 96 bool success = SkInstallDiscardablePixelRef(data, &source); | |
| 97 if (!image || !success) { | |
| 98 perror("Unable to decode the source image."); | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 SkISize size = SkISize::Make(FLAGS_width, FLAGS_height); | |
| 103 SkAutoTUnref<SkData> rasterData, gpuData, pdfData; | |
| 104 if (!FLAGS_noraster) { | |
| 105 SkAutoTUnref<SkSurface> rasterSurface( | |
| 106 SkSurface::NewRaster(SkImageInfo::MakeN32Premul(size))); | |
| 107 draw(rasterSurface->getCanvas()); | |
| 108 rasterData.reset(encode_snapshot(rasterSurface)); | |
| 109 } | |
| 110 if (FLAGS_gpu) { | |
| 111 OSMesaContextHolder osMesaContextHolder; | |
| 112 SkAutoTUnref<GrContext> grContext(create_mesa_grcontext()); | |
| 113 if (!grContext) { | |
| 114 fputs("Unable to get Mesa GrContext.\n", stderr); | |
| 115 } else { | |
| 116 SkAutoTUnref<SkSurface> surface( | |
| 117 SkSurface::NewRenderTarget( | |
| 118 grContext, | |
| 119 SkSurface::kNo_Budgeted, | |
| 120 SkImageInfo::MakeN32Premul(size))); | |
| 121 if (!surface) { | |
| 122 fputs("Unable to get render surface.\n", stderr); | |
| 123 exit(1); | |
| 124 } | |
| 125 draw(surface->getCanvas()); | |
| 126 gpuData.reset(encode_snapshot(surface)); | |
| 127 } | |
| 128 } | |
| 129 if (FLAGS_pdf) { | |
| 130 SkDynamicMemoryWStream pdfStream; | |
| 131 SkAutoTUnref<SkDocument> document(SkDocument::CreatePDF(&pdfStream)); | |
| 132 draw(document->beginPage(FLAGS_width, FLAGS_height)); | |
| 133 document->close(); | |
| 134 pdfData.reset(pdfStream.copyToData()); | |
| 135 } | |
| 136 | |
| 137 printf("{\n"); | |
| 138 dump_output(rasterData, "Raster", !gpuData && !pdfData); | |
| 139 dump_output(gpuData, "Gpu", !pdfData); | |
| 140 dump_output(pdfData, "Pdf"); | |
| 141 printf("}\n"); | |
| 142 | |
| 143 SkSafeSetNull(image); | |
| 144 return 0; | |
| 145 } | |
| OLD | NEW |