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 | |
| 8 | #include "SkLuaCanvas.h" |
| 9 | #include "SkPicture.h" |
| 10 | #include "SkCommandLineFlags.h" |
| 11 | #include "SkGraphics.h" |
| 12 | #include "SkStream.h" |
| 13 | #include "SkData.h" |
| 14 | #include "picture_utils.h" |
| 15 | #include "SkOSFile.h" |
| 16 | #include "SkImageDecoder.h" |
| 17 | |
| 18 | extern "C" { |
| 19 | #include "lua.h" |
| 20 | #include "lualib.h" |
| 21 | #include "lauxlib.h" |
| 22 | } |
| 23 | |
| 24 | // PictureRenderingFlags.cpp |
| 25 | extern bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap*); |
| 26 | |
| 27 | |
| 28 | // Flags used by this file, alphabetically: |
| 29 | DEFINE_string2(skpPath, r, "", "Read .skp files from this dir"); |
| 30 | DEFINE_string2(luaFile, l, "", "File containing lua script to run"); |
| 31 | |
| 32 | static SkPicture* load_picture(const char path[]) { |
| 33 | SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); |
| 34 | SkPicture* pic = NULL; |
| 35 | if (stream.get()) { |
| 36 | bool success; |
| 37 | pic = SkNEW_ARGS(SkPicture, (stream.get(), &success, |
| 38 | &lazy_decode_bitmap)); |
| 39 | if (!success) { |
| 40 | SkDELETE(pic); |
| 41 | pic = NULL; |
| 42 | } |
| 43 | } |
| 44 | return pic; |
| 45 | } |
| 46 | |
| 47 | static SkData* read_into_data(const char file[]) { |
| 48 | SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file)); |
| 49 | if (!stream.get()) { |
| 50 | return SkData::NewEmpty(); |
| 51 | } |
| 52 | size_t len = stream->getLength(); |
| 53 | void* buffer = sk_malloc_throw(len); |
| 54 | stream->read(buffer, len); |
| 55 | return SkData::NewFromMalloc(buffer, len); |
| 56 | } |
| 57 | |
| 58 | class SkAutoLua { |
| 59 | public: |
| 60 | SkAutoLua(const char termCode[] = NULL) : fTermCode(termCode) { |
| 61 | fL = luaL_newstate(); |
| 62 | luaL_openlibs(fL); |
| 63 | } |
| 64 | ~SkAutoLua() { |
| 65 | if (fTermCode.size() > 0) { |
| 66 | lua_getglobal(fL, fTermCode.c_str()); |
| 67 | if (lua_pcall(fL, 0, 0, 0) != LUA_OK) { |
| 68 | SkDebugf("lua err: %s\n", lua_tostring(fL, -1)); |
| 69 | } |
| 70 | } |
| 71 | lua_close(fL); |
| 72 | } |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 73 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 74 | lua_State* get() const { return fL; } |
| 75 | lua_State* operator*() const { return fL; } |
| 76 | lua_State* operator->() const { return fL; } |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 77 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 78 | bool load(const char code[]) { |
| 79 | int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0); |
| 80 | if (err) { |
| 81 | SkDebugf("--- lua failed\n"); |
| 82 | return false; |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | bool load(const void* code, size_t size) { |
| 87 | SkString str((const char*)code, size); |
| 88 | return load(str.c_str()); |
| 89 | int err = luaL_loadbufferx(fL, (const char*)code, size, NULL, NULL) |
| 90 | || lua_pcall(fL, 0, 0, 0); |
| 91 | if (err) { |
| 92 | SkDebugf("--- lua failed\n"); |
| 93 | return false; |
| 94 | } |
| 95 | return true; |
| 96 | } |
| 97 | private: |
| 98 | lua_State* fL; |
| 99 | SkString fTermCode; |
| 100 | }; |
| 101 | |
| 102 | int tool_main(int argc, char** argv); |
| 103 | int tool_main(int argc, char** argv) { |
| 104 | SkCommandLineFlags::SetUsage("apply lua script to .skp files."); |
| 105 | SkCommandLineFlags::Parse(argc, argv); |
| 106 | |
| 107 | if (FLAGS_skpPath.isEmpty()) { |
| 108 | SkDebugf(".skp files or directories are required.\n"); |
| 109 | exit(-1); |
| 110 | } |
| 111 | if (FLAGS_luaFile.isEmpty()) { |
mike@reedtribe.org | 0e59b79 | 2013-05-21 03:24:37 +0000 | [diff] [blame^] | 112 | SkDebugf("missing luaFile(s)\n"); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 113 | exit(-1); |
| 114 | } |
| 115 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 116 | SkAutoGraphics ag; |
| 117 | SkAutoLua L("summarize"); |
mike@reedtribe.org | 0e59b79 | 2013-05-21 03:24:37 +0000 | [diff] [blame^] | 118 | |
| 119 | for (int i = 0; i < FLAGS_luaFile.count(); ++i) { |
| 120 | SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i])); |
| 121 | SkDebugf("loading %s...\n", FLAGS_luaFile[i]); |
| 122 | if (!L.load(data->data(), data->size())) { |
| 123 | SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]); |
| 124 | exit(-1); |
| 125 | } |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | for (int i = 0; i < FLAGS_skpPath.count(); i ++) { |
| 129 | SkOSFile::Iter iter(FLAGS_skpPath[i], "skp"); |
| 130 | SkString inputFilename; |
| 131 | |
| 132 | while (iter.next(&inputFilename)) { |
| 133 | SkString inputPath; |
| 134 | SkString inputAsSkString(FLAGS_skpPath[i]); |
| 135 | sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename); |
skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 136 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 137 | const char* path = inputPath.c_str(); |
mike@reedtribe.org | cef454e | 2013-05-21 01:49:06 +0000 | [diff] [blame] | 138 | SkDebugf("scraping %s\n", path); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 139 | |
mike@reedtribe.org | cef454e | 2013-05-21 01:49:06 +0000 | [diff] [blame] | 140 | SkAutoTUnref<SkPicture> pic(load_picture(path)); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 141 | if (pic.get()) { |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 142 | SkLuaCanvas canvas(pic->width(), pic->height(), L.get(), "accumulate"); |
| 143 | canvas.drawPicture(*pic); |
| 144 | } else { |
mike@reedtribe.org | cef454e | 2013-05-21 01:49:06 +0000 | [diff] [blame] | 145 | SkDebugf("failed to load\n"); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | #if !defined SK_BUILD_FOR_IOS |
| 153 | int main(int argc, char * const argv[]) { |
| 154 | return tool_main(argc, (char**) argv); |
| 155 | } |
| 156 | #endif |