blob: 106948a8d3449e2867feeca8de1f8da9230e3c78 [file] [log] [blame]
reed@google.comdff7e112013-05-15 19:34:20 +00001/*
2 * Copyright 2013 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
reed@google.com3597b732013-05-22 20:12:50 +00008#include "SkLua.h"
reed@google.comdff7e112013-05-15 19:34:20 +00009#include "SkLuaCanvas.h"
10#include "SkPicture.h"
11#include "SkCommandLineFlags.h"
12#include "SkGraphics.h"
13#include "SkStream.h"
14#include "SkData.h"
15#include "picture_utils.h"
16#include "SkOSFile.h"
17#include "SkImageDecoder.h"
reed@google.comb8b3d902013-06-12 20:07:10 +000018#include "SkForceLinking.h"
19
20__SK_FORCE_IMAGE_DECODER_LINKING;
reed@google.comdff7e112013-05-15 19:34:20 +000021
22extern "C" {
23 #include "lua.h"
24 #include "lualib.h"
25 #include "lauxlib.h"
26}
27
reed@google.com2d516772013-05-21 16:05:53 +000028static const char gStartCanvasFunc[] = "sk_scrape_startcanvas";
29static const char gEndCanvasFunc[] = "sk_scrape_endcanvas";
30static const char gAccumulateFunc[] = "sk_scrape_accumulate";
31static const char gSummarizeFunc[] = "sk_scrape_summarize";
32
reed@google.comdff7e112013-05-15 19:34:20 +000033// PictureRenderingFlags.cpp
34extern bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap*);
35
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +000036DEFINE_string2(skpPath, r, "", "Read this .skp file or .skp files from this dir");
reed@google.comdff7e112013-05-15 19:34:20 +000037DEFINE_string2(luaFile, l, "", "File containing lua script to run");
reed@google.come2aad272013-05-31 19:46:02 +000038DEFINE_string2(headCode, s, "", "Optional lua code to call at beginning");
39DEFINE_string2(tailFunc, s, "", "Optional lua function to call at end");
reed@google.comdff7e112013-05-15 19:34:20 +000040
41static SkPicture* load_picture(const char path[]) {
42 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
43 SkPicture* pic = NULL;
44 if (stream.get()) {
45 bool success;
46 pic = SkNEW_ARGS(SkPicture, (stream.get(), &success,
47 &lazy_decode_bitmap));
48 if (!success) {
49 SkDELETE(pic);
50 pic = NULL;
51 }
52 }
53 return pic;
54}
55
56static SkData* read_into_data(const char file[]) {
57 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file));
58 if (!stream.get()) {
59 return SkData::NewEmpty();
60 }
61 size_t len = stream->getLength();
62 void* buffer = sk_malloc_throw(len);
63 stream->read(buffer, len);
64 return SkData::NewFromMalloc(buffer, len);
65}
66
reed@google.com2d516772013-05-21 16:05:53 +000067static void call_canvas(lua_State* L, SkLuaCanvas* canvas,
68 const char pictureFile[], const char funcName[]) {
69 lua_getglobal(L, funcName);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000070 if (!lua_isfunction(L, -1)) {
71 int t = lua_type(L, -1);
reed@google.com2d516772013-05-21 16:05:53 +000072 SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000073 lua_settop(L, -2);
74 } else {
75 canvas->pushThis();
reed@google.com2d516772013-05-21 16:05:53 +000076 lua_pushstring(L, pictureFile);
77 if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000078 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
79 }
80 }
81}
82
reed@google.comdff7e112013-05-15 19:34:20 +000083int tool_main(int argc, char** argv);
84int tool_main(int argc, char** argv) {
85 SkCommandLineFlags::SetUsage("apply lua script to .skp files.");
86 SkCommandLineFlags::Parse(argc, argv);
87
88 if (FLAGS_skpPath.isEmpty()) {
89 SkDebugf(".skp files or directories are required.\n");
90 exit(-1);
91 }
92 if (FLAGS_luaFile.isEmpty()) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000093 SkDebugf("missing luaFile(s)\n");
reed@google.comdff7e112013-05-15 19:34:20 +000094 exit(-1);
95 }
96
reed@google.come2aad272013-05-31 19:46:02 +000097 const char* summary = gSummarizeFunc;
98 if (!FLAGS_tailFunc.isEmpty()) {
99 summary = FLAGS_tailFunc[0];
100 }
101
reed@google.comdff7e112013-05-15 19:34:20 +0000102 SkAutoGraphics ag;
reed@google.come2aad272013-05-31 19:46:02 +0000103 SkLua L(summary);
mike@reedtribe.org0e59b792013-05-21 03:24:37 +0000104
105 for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
106 SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i]));
107 SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
reed@google.com3597b732013-05-22 20:12:50 +0000108 if (!L.runCode(data->data(), data->size())) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +0000109 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
110 exit(-1);
111 }
reed@google.comdff7e112013-05-15 19:34:20 +0000112 }
113
reed@google.come2aad272013-05-31 19:46:02 +0000114 if (!FLAGS_headCode.isEmpty()) {
115 L.runCode(FLAGS_headCode[0]);
116 }
skia.committer@gmail.com26da7f02013-06-01 07:01:39 +0000117
reed@google.comdff7e112013-05-15 19:34:20 +0000118 for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000119 SkTArray<SkString> paths;
120 if (sk_isdir(FLAGS_skpPath[i])) {
121 // Add all .skp in this directory.
122 const SkString directory(FLAGS_skpPath[i]);
123 SkString filename;
124 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
125 while(iter.next(&filename)) {
126 sk_tools::make_filepath(&paths.push_back(), directory, filename);
127 }
128 } else {
129 // Add this as an .skp itself.
130 paths.push_back() = FLAGS_skpPath[i];
131 }
reed@google.comdff7e112013-05-15 19:34:20 +0000132
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000133 for (int i = 0; i < paths.count(); i++) {
134 const char* path = paths[i].c_str();
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000135 SkDebugf("scraping %s\n", path);
reed@google.comdff7e112013-05-15 19:34:20 +0000136
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000137 SkAutoTUnref<SkPicture> pic(load_picture(path));
reed@google.comdff7e112013-05-15 19:34:20 +0000138 if (pic.get()) {
reed@google.com2d516772013-05-21 16:05:53 +0000139 SkAutoTUnref<SkLuaCanvas> canvas(
140 new SkLuaCanvas(pic->width(), pic->height(),
141 L.get(), gAccumulateFunc));
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000142
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000143 call_canvas(L.get(), canvas.get(), path, gStartCanvasFunc);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000144 canvas->drawPicture(*pic);
commit-bot@chromium.org3da6c562013-06-18 18:35:58 +0000145 call_canvas(L.get(), canvas.get(), path, gEndCanvasFunc);
reed@google.com2d516772013-05-21 16:05:53 +0000146
reed@google.comdff7e112013-05-15 19:34:20 +0000147 } else {
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000148 SkDebugf("failed to load\n");
reed@google.comdff7e112013-05-15 19:34:20 +0000149 }
150 }
151 }
152 return 0;
153}
154
155#if !defined SK_BUILD_FOR_IOS
156int main(int argc, char * const argv[]) {
157 return tool_main(argc, (char**) argv);
158}
159#endif