reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 8 | #include "SkLua.h" |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 9 | #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.com | b8b3d90 | 2013-06-12 20:07:10 +0000 | [diff] [blame] | 18 | #include "SkForceLinking.h" |
| 19 | |
| 20 | __SK_FORCE_IMAGE_DECODER_LINKING; |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 21 | |
| 22 | extern "C" { |
| 23 | #include "lua.h" |
| 24 | #include "lualib.h" |
| 25 | #include "lauxlib.h" |
| 26 | } |
| 27 | |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 28 | static const char gStartCanvasFunc[] = "sk_scrape_startcanvas"; |
| 29 | static const char gEndCanvasFunc[] = "sk_scrape_endcanvas"; |
| 30 | static const char gAccumulateFunc[] = "sk_scrape_accumulate"; |
| 31 | static const char gSummarizeFunc[] = "sk_scrape_summarize"; |
| 32 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 33 | // PictureRenderingFlags.cpp |
| 34 | extern bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap*); |
| 35 | |
commit-bot@chromium.org | 3da6c56 | 2013-06-18 18:35:58 +0000 | [diff] [blame^] | 36 | DEFINE_string2(skpPath, r, "", "Read this .skp file or .skp files from this dir"); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 37 | DEFINE_string2(luaFile, l, "", "File containing lua script to run"); |
reed@google.com | e2aad27 | 2013-05-31 19:46:02 +0000 | [diff] [blame] | 38 | DEFINE_string2(headCode, s, "", "Optional lua code to call at beginning"); |
| 39 | DEFINE_string2(tailFunc, s, "", "Optional lua function to call at end"); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 40 | |
| 41 | static 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 | |
| 56 | static 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.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 67 | static void call_canvas(lua_State* L, SkLuaCanvas* canvas, |
| 68 | const char pictureFile[], const char funcName[]) { |
| 69 | lua_getglobal(L, funcName); |
mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 70 | if (!lua_isfunction(L, -1)) { |
| 71 | int t = lua_type(L, -1); |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 72 | SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t); |
mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 73 | lua_settop(L, -2); |
| 74 | } else { |
| 75 | canvas->pushThis(); |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 76 | lua_pushstring(L, pictureFile); |
| 77 | if (lua_pcall(L, 2, 0, 0) != LUA_OK) { |
mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 78 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 83 | int tool_main(int argc, char** argv); |
| 84 | int 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.org | 0e59b79 | 2013-05-21 03:24:37 +0000 | [diff] [blame] | 93 | SkDebugf("missing luaFile(s)\n"); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 94 | exit(-1); |
| 95 | } |
| 96 | |
reed@google.com | e2aad27 | 2013-05-31 19:46:02 +0000 | [diff] [blame] | 97 | const char* summary = gSummarizeFunc; |
| 98 | if (!FLAGS_tailFunc.isEmpty()) { |
| 99 | summary = FLAGS_tailFunc[0]; |
| 100 | } |
| 101 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 102 | SkAutoGraphics ag; |
reed@google.com | e2aad27 | 2013-05-31 19:46:02 +0000 | [diff] [blame] | 103 | SkLua L(summary); |
mike@reedtribe.org | 0e59b79 | 2013-05-21 03:24:37 +0000 | [diff] [blame] | 104 | |
| 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.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 108 | if (!L.runCode(data->data(), data->size())) { |
mike@reedtribe.org | 0e59b79 | 2013-05-21 03:24:37 +0000 | [diff] [blame] | 109 | SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]); |
| 110 | exit(-1); |
| 111 | } |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 112 | } |
| 113 | |
reed@google.com | e2aad27 | 2013-05-31 19:46:02 +0000 | [diff] [blame] | 114 | if (!FLAGS_headCode.isEmpty()) { |
| 115 | L.runCode(FLAGS_headCode[0]); |
| 116 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 117 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 118 | for (int i = 0; i < FLAGS_skpPath.count(); i ++) { |
commit-bot@chromium.org | 3da6c56 | 2013-06-18 18:35:58 +0000 | [diff] [blame^] | 119 | 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.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 132 | |
commit-bot@chromium.org | 3da6c56 | 2013-06-18 18:35:58 +0000 | [diff] [blame^] | 133 | for (int i = 0; i < paths.count(); i++) { |
| 134 | const char* path = paths[i].c_str(); |
mike@reedtribe.org | cef454e | 2013-05-21 01:49:06 +0000 | [diff] [blame] | 135 | SkDebugf("scraping %s\n", path); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 136 | |
mike@reedtribe.org | cef454e | 2013-05-21 01:49:06 +0000 | [diff] [blame] | 137 | SkAutoTUnref<SkPicture> pic(load_picture(path)); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 138 | if (pic.get()) { |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 139 | SkAutoTUnref<SkLuaCanvas> canvas( |
| 140 | new SkLuaCanvas(pic->width(), pic->height(), |
| 141 | L.get(), gAccumulateFunc)); |
mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 142 | |
commit-bot@chromium.org | 3da6c56 | 2013-06-18 18:35:58 +0000 | [diff] [blame^] | 143 | call_canvas(L.get(), canvas.get(), path, gStartCanvasFunc); |
mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 144 | canvas->drawPicture(*pic); |
commit-bot@chromium.org | 3da6c56 | 2013-06-18 18:35:58 +0000 | [diff] [blame^] | 145 | call_canvas(L.get(), canvas.get(), path, gEndCanvasFunc); |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 146 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 147 | } else { |
mike@reedtribe.org | cef454e | 2013-05-21 01:49:06 +0000 | [diff] [blame] | 148 | SkDebugf("failed to load\n"); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | #if !defined SK_BUILD_FOR_IOS |
| 156 | int main(int argc, char * const argv[]) { |
| 157 | return tool_main(argc, (char**) argv); |
| 158 | } |
| 159 | #endif |