| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkLuaCanvas.h" | 8 #include "SkLuaCanvas.h" |
| 9 #include "SkPicture.h" | 9 #include "SkPicture.h" |
| 10 #include "SkCommandLineFlags.h" | 10 #include "SkCommandLineFlags.h" |
| 11 #include "SkGraphics.h" | 11 #include "SkGraphics.h" |
| 12 #include "SkStream.h" | 12 #include "SkStream.h" |
| 13 #include "SkData.h" | 13 #include "SkData.h" |
| 14 #include "picture_utils.h" | 14 #include "picture_utils.h" |
| 15 #include "SkOSFile.h" | 15 #include "SkOSFile.h" |
| 16 #include "SkImageDecoder.h" | 16 #include "SkImageDecoder.h" |
| 17 | 17 |
| 18 extern "C" { | 18 extern "C" { |
| 19 #include "lua.h" | 19 #include "lua.h" |
| 20 #include "lualib.h" | 20 #include "lualib.h" |
| 21 #include "lauxlib.h" | 21 #include "lauxlib.h" |
| 22 } | 22 } |
| 23 | 23 |
| 24 static const char gStartCanvasFunc[] = "sk_scrape_startcanvas"; |
| 25 static const char gEndCanvasFunc[] = "sk_scrape_endcanvas"; |
| 26 static const char gAccumulateFunc[] = "sk_scrape_accumulate"; |
| 27 static const char gSummarizeFunc[] = "sk_scrape_summarize"; |
| 28 |
| 24 // PictureRenderingFlags.cpp | 29 // PictureRenderingFlags.cpp |
| 25 extern bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap*); | 30 extern bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap*); |
| 26 | 31 |
| 27 | 32 |
| 28 // Flags used by this file, alphabetically: | 33 // Flags used by this file, alphabetically: |
| 29 DEFINE_string2(skpPath, r, "", "Read .skp files from this dir"); | 34 DEFINE_string2(skpPath, r, "", "Read .skp files from this dir"); |
| 30 DEFINE_string2(luaFile, l, "", "File containing lua script to run"); | 35 DEFINE_string2(luaFile, l, "", "File containing lua script to run"); |
| 31 | 36 |
| 32 static SkPicture* load_picture(const char path[]) { | 37 static SkPicture* load_picture(const char path[]) { |
| 33 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); | 38 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path)); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 SkDebugf("--- lua failed\n"); | 97 SkDebugf("--- lua failed\n"); |
| 93 return false; | 98 return false; |
| 94 } | 99 } |
| 95 return true; | 100 return true; |
| 96 } | 101 } |
| 97 private: | 102 private: |
| 98 lua_State* fL; | 103 lua_State* fL; |
| 99 SkString fTermCode; | 104 SkString fTermCode; |
| 100 }; | 105 }; |
| 101 | 106 |
| 102 static void call_setcanvas(lua_State* L, SkLuaCanvas* canvas) { | 107 static void call_canvas(lua_State* L, SkLuaCanvas* canvas, |
| 103 lua_getglobal(L, "setcanvas"); | 108 const char pictureFile[], const char funcName[]) { |
| 109 lua_getglobal(L, funcName); |
| 104 if (!lua_isfunction(L, -1)) { | 110 if (!lua_isfunction(L, -1)) { |
| 105 int t = lua_type(L, -1); | 111 int t = lua_type(L, -1); |
| 106 SkDebugf("--- expected setcanvas function %d\n", t); | 112 SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t); |
| 107 lua_settop(L, -2); | 113 lua_settop(L, -2); |
| 108 } else { | 114 } else { |
| 109 canvas->pushThis(); | 115 canvas->pushThis(); |
| 110 if (lua_pcall(L, 1, 0, 0) != LUA_OK) { | 116 lua_pushstring(L, pictureFile); |
| 117 if (lua_pcall(L, 2, 0, 0) != LUA_OK) { |
| 111 SkDebugf("lua err: %s\n", lua_tostring(L, -1)); | 118 SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 112 } | 119 } |
| 113 } | 120 } |
| 114 } | 121 } |
| 115 | 122 |
| 116 int tool_main(int argc, char** argv); | 123 int tool_main(int argc, char** argv); |
| 117 int tool_main(int argc, char** argv) { | 124 int tool_main(int argc, char** argv) { |
| 118 SkCommandLineFlags::SetUsage("apply lua script to .skp files."); | 125 SkCommandLineFlags::SetUsage("apply lua script to .skp files."); |
| 119 SkCommandLineFlags::Parse(argc, argv); | 126 SkCommandLineFlags::Parse(argc, argv); |
| 120 | 127 |
| 121 if (FLAGS_skpPath.isEmpty()) { | 128 if (FLAGS_skpPath.isEmpty()) { |
| 122 SkDebugf(".skp files or directories are required.\n"); | 129 SkDebugf(".skp files or directories are required.\n"); |
| 123 exit(-1); | 130 exit(-1); |
| 124 } | 131 } |
| 125 if (FLAGS_luaFile.isEmpty()) { | 132 if (FLAGS_luaFile.isEmpty()) { |
| 126 SkDebugf("missing luaFile(s)\n"); | 133 SkDebugf("missing luaFile(s)\n"); |
| 127 exit(-1); | 134 exit(-1); |
| 128 } | 135 } |
| 129 | 136 |
| 130 SkAutoGraphics ag; | 137 SkAutoGraphics ag; |
| 131 SkAutoLua L("summarize"); | 138 SkAutoLua L(gSummarizeFunc); |
| 132 | 139 |
| 133 for (int i = 0; i < FLAGS_luaFile.count(); ++i) { | 140 for (int i = 0; i < FLAGS_luaFile.count(); ++i) { |
| 134 SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i])); | 141 SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i])); |
| 135 SkDebugf("loading %s...\n", FLAGS_luaFile[i]); | 142 SkDebugf("loading %s...\n", FLAGS_luaFile[i]); |
| 136 if (!L.load(data->data(), data->size())) { | 143 if (!L.load(data->data(), data->size())) { |
| 137 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]); | 144 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]); |
| 138 exit(-1); | 145 exit(-1); |
| 139 } | 146 } |
| 140 } | 147 } |
| 141 | 148 |
| 142 for (int i = 0; i < FLAGS_skpPath.count(); i ++) { | 149 for (int i = 0; i < FLAGS_skpPath.count(); i ++) { |
| 143 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp"); | 150 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp"); |
| 144 SkString inputFilename; | 151 SkString inputFilename; |
| 145 | 152 |
| 146 while (iter.next(&inputFilename)) { | 153 while (iter.next(&inputFilename)) { |
| 147 SkString inputPath; | 154 SkString inputPath; |
| 148 SkString inputAsSkString(FLAGS_skpPath[i]); | 155 SkString inputAsSkString(FLAGS_skpPath[i]); |
| 149 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename); | 156 sk_tools::make_filepath(&inputPath, inputAsSkString, inputFilename); |
| 150 | 157 |
| 151 const char* path = inputPath.c_str(); | 158 const char* path = inputPath.c_str(); |
| 152 SkDebugf("scraping %s\n", path); | 159 SkDebugf("scraping %s\n", path); |
| 153 | 160 |
| 154 SkAutoTUnref<SkPicture> pic(load_picture(path)); | 161 SkAutoTUnref<SkPicture> pic(load_picture(path)); |
| 155 if (pic.get()) { | 162 if (pic.get()) { |
| 156 SkAutoTUnref<SkLuaCanvas> canvas(new SkLuaCanvas(pic->width(), p
ic->height(), L.get(), "accumulate")); | 163 SkAutoTUnref<SkLuaCanvas> canvas( |
| 164 new SkLuaCanvas(pic->width(), pic->height(), |
| 165 L.get(), gAccumulateFunc)); |
| 157 | 166 |
| 158 call_setcanvas(L.get(), canvas.get()); | 167 call_canvas(L.get(), canvas.get(), inputFilename.c_str(), gStart
CanvasFunc); |
| 168 canvas->drawPicture(*pic); |
| 169 call_canvas(L.get(), canvas.get(), inputFilename.c_str(), gEndCa
nvasFunc); |
| 159 | 170 |
| 160 canvas->drawPicture(*pic); | |
| 161 } else { | 171 } else { |
| 162 SkDebugf("failed to load\n"); | 172 SkDebugf("failed to load\n"); |
| 163 } | 173 } |
| 164 } | 174 } |
| 165 } | 175 } |
| 166 return 0; | 176 return 0; |
| 167 } | 177 } |
| 168 | 178 |
| 169 #if !defined SK_BUILD_FOR_IOS | 179 #if !defined SK_BUILD_FOR_IOS |
| 170 int main(int argc, char * const argv[]) { | 180 int main(int argc, char * const argv[]) { |
| 171 return tool_main(argc, (char**) argv); | 181 return tool_main(argc, (char**) argv); |
| 172 } | 182 } |
| 173 #endif | 183 #endif |
| OLD | NEW |