blob: ae4e1cabdadd9b68f00de50de537363a46c1e9da [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"
18
19extern "C" {
20 #include "lua.h"
21 #include "lualib.h"
22 #include "lauxlib.h"
23}
24
reed@google.com2d516772013-05-21 16:05:53 +000025static const char gStartCanvasFunc[] = "sk_scrape_startcanvas";
26static const char gEndCanvasFunc[] = "sk_scrape_endcanvas";
27static const char gAccumulateFunc[] = "sk_scrape_accumulate";
28static const char gSummarizeFunc[] = "sk_scrape_summarize";
29
reed@google.comdff7e112013-05-15 19:34:20 +000030// PictureRenderingFlags.cpp
31extern bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap*);
32
33
34// Flags used by this file, alphabetically:
35DEFINE_string2(skpPath, r, "", "Read .skp files from this dir");
36DEFINE_string2(luaFile, l, "", "File containing lua script to run");
37
38static SkPicture* load_picture(const char path[]) {
39 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
40 SkPicture* pic = NULL;
41 if (stream.get()) {
42 bool success;
43 pic = SkNEW_ARGS(SkPicture, (stream.get(), &success,
44 &lazy_decode_bitmap));
45 if (!success) {
46 SkDELETE(pic);
47 pic = NULL;
48 }
49 }
50 return pic;
51}
52
53static SkData* read_into_data(const char file[]) {
54 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file));
55 if (!stream.get()) {
56 return SkData::NewEmpty();
57 }
58 size_t len = stream->getLength();
59 void* buffer = sk_malloc_throw(len);
60 stream->read(buffer, len);
61 return SkData::NewFromMalloc(buffer, len);
62}
63
reed@google.com2d516772013-05-21 16:05:53 +000064static void call_canvas(lua_State* L, SkLuaCanvas* canvas,
65 const char pictureFile[], const char funcName[]) {
66 lua_getglobal(L, funcName);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000067 if (!lua_isfunction(L, -1)) {
68 int t = lua_type(L, -1);
reed@google.com2d516772013-05-21 16:05:53 +000069 SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000070 lua_settop(L, -2);
71 } else {
72 canvas->pushThis();
reed@google.com2d516772013-05-21 16:05:53 +000073 lua_pushstring(L, pictureFile);
74 if (lua_pcall(L, 2, 0, 0) != LUA_OK) {
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000075 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
76 }
77 }
78}
79
reed@google.comdff7e112013-05-15 19:34:20 +000080int tool_main(int argc, char** argv);
81int tool_main(int argc, char** argv) {
82 SkCommandLineFlags::SetUsage("apply lua script to .skp files.");
83 SkCommandLineFlags::Parse(argc, argv);
84
85 if (FLAGS_skpPath.isEmpty()) {
86 SkDebugf(".skp files or directories are required.\n");
87 exit(-1);
88 }
89 if (FLAGS_luaFile.isEmpty()) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000090 SkDebugf("missing luaFile(s)\n");
reed@google.comdff7e112013-05-15 19:34:20 +000091 exit(-1);
92 }
93
reed@google.comdff7e112013-05-15 19:34:20 +000094 SkAutoGraphics ag;
reed@google.com3597b732013-05-22 20:12:50 +000095 SkLua L(gSummarizeFunc);
mike@reedtribe.org0e59b792013-05-21 03:24:37 +000096
97 for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
98 SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i]));
99 SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
reed@google.com3597b732013-05-22 20:12:50 +0000100 if (!L.runCode(data->data(), data->size())) {
mike@reedtribe.org0e59b792013-05-21 03:24:37 +0000101 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
102 exit(-1);
103 }
reed@google.comdff7e112013-05-15 19:34:20 +0000104 }
105
106 for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
107 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
108 SkString inputFilename;
109
110 while (iter.next(&inputFilename)) {
111 SkString inputPath;
112 SkString inputAsSkString(FLAGS_skpPath[i]);
113 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename);
skia.committer@gmail.com539f3642013-05-16 07:01:00 +0000114
reed@google.comdff7e112013-05-15 19:34:20 +0000115 const char* path = inputPath.c_str();
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000116 SkDebugf("scraping %s\n", path);
reed@google.comdff7e112013-05-15 19:34:20 +0000117
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000118 SkAutoTUnref<SkPicture> pic(load_picture(path));
reed@google.comdff7e112013-05-15 19:34:20 +0000119 if (pic.get()) {
reed@google.com2d516772013-05-21 16:05:53 +0000120 SkAutoTUnref<SkLuaCanvas> canvas(
121 new SkLuaCanvas(pic->width(), pic->height(),
122 L.get(), gAccumulateFunc));
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000123
reed@google.com2d516772013-05-21 16:05:53 +0000124 call_canvas(L.get(), canvas.get(), inputFilename.c_str(), gStartCanvasFunc);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000125 canvas->drawPicture(*pic);
reed@google.com2d516772013-05-21 16:05:53 +0000126 call_canvas(L.get(), canvas.get(), inputFilename.c_str(), gEndCanvasFunc);
127
reed@google.comdff7e112013-05-15 19:34:20 +0000128 } else {
mike@reedtribe.orgcef454e2013-05-21 01:49:06 +0000129 SkDebugf("failed to load\n");
reed@google.comdff7e112013-05-15 19:34:20 +0000130 }
131 }
132 }
133 return 0;
134}
135
136#if !defined SK_BUILD_FOR_IOS
137int main(int argc, char * const argv[]) {
138 return tool_main(argc, (char**) argv);
139}
140#endif