blob: 86de9bacf0b8c1be0996de1e018c283d67aaa86a [file] [log] [blame]
junov@chromium.org777442d2012-06-12 14:56:36 +00001/*
2 * Copyright 2012 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
junov@chromium.org777442d2012-06-12 14:56:36 +00008#include "SkBitmap.h"
9#include "SkCanvas.h"
10#include "SkColorPriv.h"
keyar@chromium.org472b3792012-07-20 22:34:27 +000011#include "SkDevice.h"
junov@chromium.org777442d2012-06-12 14:56:36 +000012#include "SkImageEncoder.h"
13#include "SkOSFile.h"
14#include "SkPicture.h"
15#include "SkStream.h"
16#include "SkString.h"
senorblanco@chromium.org3cbbb542012-07-13 18:55:53 +000017#include "SkTArray.h"
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000018#include "PictureRenderer.h"
twiz@google.coma31b8bb2012-06-22 18:24:56 +000019#include "picture_utils.h"
junov@chromium.org777442d2012-06-12 14:56:36 +000020
junov@chromium.org777442d2012-06-12 14:56:36 +000021static void usage(const char* argv0) {
22 SkDebugf("SkPicture rendering tool\n");
23 SkDebugf("\n"
24"Usage: \n"
keyar@chromium.org472b3792012-07-20 22:34:27 +000025" %s <input>... <outputDir> \n"
keyar@chromium.org795cd472012-08-02 18:57:53 +000026" [--mode pipe | simple | tile width[%] height[%]]"
junov@chromium.org777442d2012-06-12 14:56:36 +000027, argv0);
keyar@chromium.org472b3792012-07-20 22:34:27 +000028 SkDebugf("\n\n");
junov@chromium.org777442d2012-06-12 14:56:36 +000029 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000030" input: A list of directories and files to use as input. Files are\n"
31" expected to have the .skp extension.\n\n");
junov@chromium.org777442d2012-06-12 14:56:36 +000032 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000033" outputDir: directory to write the rendered images.\n\n");
keyar@chromium.org472b3792012-07-20 22:34:27 +000034 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000035" --mode pipe | simple | tile width[%] height[%]: Run in the\n"
36" corresponding mode. Default is simple.\n");
keyar@chromium.org472b3792012-07-20 22:34:27 +000037 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +000038" pipe, Render using a SkGPipe.\n");
39 SkDebugf(
40" simple, Render using the default rendering method.\n");
41 SkDebugf(
42" tile width[%] height[%], Do a simple render using tiles\n"
43" with the given dimensions.\n");
junov@chromium.org777442d2012-06-12 14:56:36 +000044}
45
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000046static void make_output_filepath(SkString* path, const SkString& dir,
junov@chromium.org777442d2012-06-12 14:56:36 +000047 const SkString& name) {
twiz@google.coma31b8bb2012-06-22 18:24:56 +000048 sk_tools::make_filepath(path, dir, name);
junov@chromium.org777442d2012-06-12 14:56:36 +000049 path->remove(path->size() - 3, 3);
50 path->append("png");
51}
52
junov@chromium.org777442d2012-06-12 14:56:36 +000053/* since PNG insists on unpremultiplying our alpha, we take no precision chances
54 and force all pixels to be 100% opaque, otherwise on compare we may not get
55 a perfect match.
56 */
57static void force_all_opaque(const SkBitmap& bitmap) {
58 SkAutoLockPixels lock(bitmap);
59 for (int y = 0; y < bitmap.height(); y++) {
60 for (int x = 0; x < bitmap.width(); x++) {
61 *bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
62 }
63 }
64}
65
66static bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
67 SkBitmap copy;
68 bitmap.copyTo(&copy, SkBitmap::kARGB_8888_Config);
69 force_all_opaque(copy);
70 return SkImageEncoder::EncodeFile(path.c_str(), copy,
71 SkImageEncoder::kPNG_Type, 100);
72}
73
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000074static void write_output(const SkString& outputDir, const SkString& inputFilename,
junov@chromium.org777442d2012-06-12 14:56:36 +000075 const SkBitmap& bitmap) {
76 SkString outputPath;
77 make_output_filepath(&outputPath, outputDir, inputFilename);
78 bool isWritten = write_bitmap(outputPath, bitmap);
79 if (!isWritten) {
80 SkDebugf("Could not write to file %s\n", outputPath.c_str());
81 }
82}
83
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +000084static void render_picture(const SkString& inputPath, const SkString& outputDir,
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000085 sk_tools::PictureRenderer& renderer) {
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000086 SkString inputFilename;
87 sk_tools::get_basename(&inputFilename, inputPath);
twiz@google.coma31b8bb2012-06-22 18:24:56 +000088
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +000089 SkFILEStream inputStream;
twiz@google.coma31b8bb2012-06-22 18:24:56 +000090 inputStream.setPath(inputPath.c_str());
91 if (!inputStream.isValid()) {
92 SkDebugf("Could not open file %s\n", inputPath.c_str());
93 return;
94 }
95
junov@chromium.org777442d2012-06-12 14:56:36 +000096 SkPicture picture(&inputStream);
97 SkBitmap bitmap;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000098 sk_tools::setup_bitmap(&bitmap, picture.width(), picture.height());
keyar@chromium.org451bb9f2012-07-26 17:27:57 +000099
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000100 renderer.init(&picture);
101
102 renderer.render();
103
104 renderer.getCanvas()->readPixels(&bitmap, 0, 0);
junov@chromium.org777442d2012-06-12 14:56:36 +0000105 write_output(outputDir, inputFilename, bitmap);
keyar@chromium.org9d696c02012-08-07 17:11:33 +0000106
107 renderer.end();
junov@chromium.org777442d2012-06-12 14:56:36 +0000108}
109
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000110static void process_input(const SkString& input, const SkString& outputDir,
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000111 sk_tools::PictureRenderer& renderer) {
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000112 SkOSFile::Iter iter(input.c_str(), "skp");
junov@chromium.org777442d2012-06-12 14:56:36 +0000113 SkString inputFilename;
114
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000115 if (iter.next(&inputFilename)) {
116 do {
117 SkString inputPath;
118 sk_tools::make_filepath(&inputPath, input, inputFilename);
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000119 render_picture(inputPath, outputDir, renderer);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000120 } while(iter.next(&inputFilename));
121 } else {
122 SkString inputPath(input);
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000123 render_picture(inputPath, outputDir, renderer);
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000124 }
125}
126
127static void parse_commandline(int argc, char* const argv[], SkTArray<SkString>* inputs,
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000128 sk_tools::PictureRenderer*& renderer){
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000129 const char* argv0 = argv[0];
130 char* const* stop = argv + argc;
131
132 for (++argv; argv < stop; ++argv) {
keyar@chromium.org795cd472012-08-02 18:57:53 +0000133 if (0 == strcmp(*argv, "--mode")) {
134 SkDELETE(renderer);
135
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000136 ++argv;
keyar@chromium.org795cd472012-08-02 18:57:53 +0000137 if (argv >= stop) {
138 SkDebugf("Missing mode for --mode\n");
139 usage(argv0);
140 exit(-1);
141 }
142
143 if (0 == strcmp(*argv, "pipe")) {
144 renderer = SkNEW(sk_tools::PipePictureRenderer);
145 } else if (0 == strcmp(*argv, "simple")) {
146 renderer = SkNEW(sk_tools::SimplePictureRenderer);
147 } else if (0 == strcmp(*argv, "tile")) {
148 sk_tools::TiledPictureRenderer* tileRenderer =
149 SkNEW(sk_tools::TiledPictureRenderer);
150 ++argv;
151 if (argv >= stop) {
152 SkDELETE(tileRenderer);
153 SkDebugf("Missing width for --mode tile\n");
154 usage(argv0);
155 exit(-1);
156 }
157
keyar@chromium.org163b5672012-08-01 17:53:29 +0000158 if (sk_tools::is_percentage(*argv)) {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000159 tileRenderer->setTileWidthPercentage(atof(*argv));
160 if (!(tileRenderer->getTileWidthPercentage() > 0)) {
161 SkDELETE(tileRenderer);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000162 SkDebugf("--mode tile must be given a width percentage > 0\n");
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000163 exit(-1);
164 }
165 } else {
166 tileRenderer->setTileWidth(atoi(*argv));
167 if (!(tileRenderer->getTileWidth() > 0)) {
168 SkDELETE(tileRenderer);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000169 SkDebugf("--mode tile must be given a width > 0\n");
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000170 exit(-1);
171 }
172 }
keyar@chromium.org795cd472012-08-02 18:57:53 +0000173
174 ++argv;
175 if (argv >= stop) {
176 SkDELETE(tileRenderer);
177 SkDebugf("Missing height for --mode tile\n");
178 usage(argv0);
179 exit(-1);
180 }
181
keyar@chromium.org163b5672012-08-01 17:53:29 +0000182 if (sk_tools::is_percentage(*argv)) {
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000183 tileRenderer->setTileHeightPercentage(atof(*argv));
184 if (!(tileRenderer->getTileHeightPercentage() > 0)) {
185 SkDELETE(tileRenderer);
186 SkDebugf(
keyar@chromium.org795cd472012-08-02 18:57:53 +0000187 "--mode tile must be given a height percentage > 0\n");
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000188 exit(-1);
189 }
190 } else {
191 tileRenderer->setTileHeight(atoi(*argv));
192 if (!(tileRenderer->getTileHeight() > 0)) {
193 SkDELETE(tileRenderer);
keyar@chromium.org795cd472012-08-02 18:57:53 +0000194 SkDebugf("--mode tile must be given a height > 0\n");
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000195 exit(-1);
196 }
197 }
keyar@chromium.org795cd472012-08-02 18:57:53 +0000198
199 renderer = tileRenderer;
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000200 } else {
keyar@chromium.org795cd472012-08-02 18:57:53 +0000201 SkDebugf("%s is not a valid mode for --mode\n", *argv);
keyar@chromium.orgcc6e5ef2012-07-27 20:09:26 +0000202 usage(argv0);
203 exit(-1);
204 }
keyar@chromium.org472b3792012-07-20 22:34:27 +0000205 } else if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000206 SkDELETE(renderer);
keyar@chromium.org472b3792012-07-20 22:34:27 +0000207 usage(argv0);
208 exit(-1);
keyar@chromium.orga2333d92012-07-16 17:29:16 +0000209 } else {
210 inputs->push_back(SkString(*argv));
211 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000212 }
keyar@chromium.org472b3792012-07-20 22:34:27 +0000213
214 if (inputs->count() < 2) {
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000215 SkDELETE(renderer);
keyar@chromium.org472b3792012-07-20 22:34:27 +0000216 usage(argv0);
217 exit(-1);
218 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000219
220 if (NULL == renderer) {
221 renderer = SkNEW(sk_tools::SimplePictureRenderer);
222 }
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000223}
224
225int main(int argc, char* const argv[]) {
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000226 SkTArray<SkString> inputs;
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000227 sk_tools::PictureRenderer* renderer = NULL;
keyar@chromium.org472b3792012-07-20 22:34:27 +0000228
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000229 parse_commandline(argc, argv, &inputs, renderer);
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000230 SkString outputDir = inputs[inputs.count() - 1];
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000231 SkASSERT(renderer);
keyar@chromium.orgd1dc9202012-07-09 18:32:08 +0000232
keyar@chromium.org1cbd47c2012-07-13 18:22:59 +0000233 for (int i = 0; i < inputs.count() - 1; i ++) {
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000234 process_input(inputs[i], outputDir, *renderer);
junov@chromium.org777442d2012-06-12 14:56:36 +0000235 }
keyar@chromium.org451bb9f2012-07-26 17:27:57 +0000236
237 SkDELETE(renderer);
junov@chromium.org777442d2012-06-12 14:56:36 +0000238}