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" |
| 18 | |
| 19 | extern "C" { |
| 20 | #include "lua.h" |
| 21 | #include "lualib.h" |
| 22 | #include "lauxlib.h" |
| 23 | } |
| 24 | |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 25 | static const char gStartCanvasFunc[] = "sk_scrape_startcanvas"; |
| 26 | static const char gEndCanvasFunc[] = "sk_scrape_endcanvas"; |
| 27 | static const char gAccumulateFunc[] = "sk_scrape_accumulate"; |
| 28 | static const char gSummarizeFunc[] = "sk_scrape_summarize"; |
| 29 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 30 | // PictureRenderingFlags.cpp |
| 31 | extern bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap*); |
| 32 | |
| 33 | |
| 34 | // Flags used by this file, alphabetically: |
| 35 | DEFINE_string2(skpPath, r, "", "Read .skp files from this dir"); |
| 36 | DEFINE_string2(luaFile, l, "", "File containing lua script to run"); |
| 37 | |
| 38 | static 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 | |
| 53 | static 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.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 64 | static void call_canvas(lua_State* L, SkLuaCanvas* canvas, |
| 65 | const char pictureFile[], const char funcName[]) { |
| 66 | lua_getglobal(L, funcName); |
mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 67 | if (!lua_isfunction(L, -1)) { |
| 68 | int t = lua_type(L, -1); |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 69 | SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t); |
mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 70 | lua_settop(L, -2); |
| 71 | } else { |
| 72 | canvas->pushThis(); |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 73 | lua_pushstring(L, pictureFile); |
| 74 | if (lua_pcall(L, 2, 0, 0) != LUA_OK) { |
mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 75 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 80 | int tool_main(int argc, char** argv); |
| 81 | int 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.org | 0e59b79 | 2013-05-21 03:24:37 +0000 | [diff] [blame] | 90 | SkDebugf("missing luaFile(s)\n"); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 91 | exit(-1); |
| 92 | } |
| 93 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 94 | SkAutoGraphics ag; |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 95 | SkLua L(gSummarizeFunc); |
mike@reedtribe.org | 0e59b79 | 2013-05-21 03:24:37 +0000 | [diff] [blame] | 96 | |
| 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.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 100 | if (!L.runCode(data->data(), data->size())) { |
mike@reedtribe.org | 0e59b79 | 2013-05-21 03:24:37 +0000 | [diff] [blame] | 101 | SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]); |
| 102 | exit(-1); |
| 103 | } |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 104 | } |
| 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.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 114 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 115 | const char* path = inputPath.c_str(); |
mike@reedtribe.org | cef454e | 2013-05-21 01:49:06 +0000 | [diff] [blame] | 116 | SkDebugf("scraping %s\n", path); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 117 | |
mike@reedtribe.org | cef454e | 2013-05-21 01:49:06 +0000 | [diff] [blame] | 118 | SkAutoTUnref<SkPicture> pic(load_picture(path)); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 119 | if (pic.get()) { |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 120 | SkAutoTUnref<SkLuaCanvas> canvas( |
| 121 | new SkLuaCanvas(pic->width(), pic->height(), |
| 122 | L.get(), gAccumulateFunc)); |
mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 123 | |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 124 | call_canvas(L.get(), canvas.get(), inputFilename.c_str(), gStartCanvasFunc); |
mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 125 | canvas->drawPicture(*pic); |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 126 | call_canvas(L.get(), canvas.get(), inputFilename.c_str(), gEndCanvasFunc); |
| 127 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 128 | } else { |
mike@reedtribe.org | cef454e | 2013-05-21 01:49:06 +0000 | [diff] [blame] | 129 | SkDebugf("failed to load\n"); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | #if !defined SK_BUILD_FOR_IOS |
| 137 | int main(int argc, char * const argv[]) { |
| 138 | return tool_main(argc, (char**) argv); |
| 139 | } |
| 140 | #endif |