OLD | NEW |
(Empty) | |
| 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 } |
| 73 |
| 74 lua_State* get() const { return fL; } |
| 75 lua_State* operator*() const { return fL; } |
| 76 lua_State* operator->() const { return fL; } |
| 77 |
| 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()) { |
| 112 SkDebugf("missing luaFile\n"); |
| 113 exit(-1); |
| 114 } |
| 115 |
| 116 SkAutoDataUnref data(read_into_data(FLAGS_luaFile[0])); |
| 117 |
| 118 SkAutoGraphics ag; |
| 119 SkAutoLua L("summarize"); |
| 120 if (!L.load(data->data(), data->size())) { |
| 121 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[0]); |
| 122 exit(-1); |
| 123 } |
| 124 |
| 125 for (int i = 0; i < FLAGS_skpPath.count(); i ++) { |
| 126 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp"); |
| 127 SkString inputFilename; |
| 128 |
| 129 while (iter.next(&inputFilename)) { |
| 130 SkString inputPath; |
| 131 SkString inputAsSkString(FLAGS_skpPath[i]); |
| 132 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename); |
| 133 |
| 134 const char* path = inputPath.c_str(); |
| 135 SkAutoTUnref<SkPicture> pic(load_picture(path)); |
| 136 |
| 137 if (pic.get()) { |
| 138 SkDebugf("scraping %s\n", path); |
| 139 SkLuaCanvas canvas(pic->width(), pic->height(), L.get(), "accumu
late"); |
| 140 canvas.drawPicture(*pic); |
| 141 } else { |
| 142 SkDebugf("failed to load %s\n", path); |
| 143 } |
| 144 } |
| 145 } |
| 146 return 0; |
| 147 } |
| 148 |
| 149 #if !defined SK_BUILD_FOR_IOS |
| 150 int main(int argc, char * const argv[]) { |
| 151 return tool_main(argc, (char**) argv); |
| 152 } |
| 153 #endif |
OLD | NEW |