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" |
fmalita | d067ae7 | 2016-11-09 09:15:05 -0800 | [diff] [blame] | 17 | #include "SkOSPath.h" |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 18 | |
bungeman | 60e0fee | 2015-08-26 05:15:46 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 21 | extern "C" { |
| 22 | #include "lua.h" |
| 23 | #include "lualib.h" |
| 24 | #include "lauxlib.h" |
| 25 | } |
| 26 | |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 27 | static const char gStartCanvasFunc[] = "sk_scrape_startcanvas"; |
| 28 | static const char gEndCanvasFunc[] = "sk_scrape_endcanvas"; |
| 29 | static const char gAccumulateFunc[] = "sk_scrape_accumulate"; |
| 30 | static const char gSummarizeFunc[] = "sk_scrape_summarize"; |
| 31 | |
commit-bot@chromium.org | aaeb87d | 2013-06-21 21:45:20 +0000 | [diff] [blame] | 32 | // Example usage for the modulo flag: |
| 33 | // for i in {0..5}; do lua_pictures --skpPath SKP_PATH -l YOUR_SCRIPT --modulo $i 6 &; done |
Mike Klein | 38af943 | 2016-11-11 11:39:44 -0500 | [diff] [blame] | 34 | static DEFINE_string(modulo, "", "[--modulo <remainder> <divisor>]: only run tests for which " |
commit-bot@chromium.org | aaeb87d | 2013-06-21 21:45:20 +0000 | [diff] [blame] | 35 | "testIndex %% divisor == remainder."); |
Mike Klein | 38af943 | 2016-11-11 11:39:44 -0500 | [diff] [blame] | 36 | static DEFINE_string2(skpPath, r, "", "Read this .skp file or .skp files from this dir"); |
| 37 | static DEFINE_string2(luaFile, l, "", "File containing lua script to run"); |
| 38 | static DEFINE_string2(headCode, s, "", "Optional lua code to call at beginning"); |
| 39 | static DEFINE_string2(tailFunc, s, "", "Optional lua function to call at end"); |
| 40 | static DEFINE_bool2(quiet, q, false, "Silence all non-error related output"); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 41 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 42 | static sk_sp<SkPicture> load_picture(const char path[]) { |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 43 | std::unique_ptr<SkStream> stream = SkStream::MakeFromFile(path); |
| 44 | if (stream) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 45 | return SkPicture::MakeFromStream(stream.get()); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 46 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 47 | return nullptr; |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 48 | } |
| 49 | |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 50 | static void call_canvas(lua_State* L, SkLuaCanvas* canvas, |
| 51 | const char pictureFile[], const char funcName[]) { |
| 52 | lua_getglobal(L, funcName); |
mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 53 | if (!lua_isfunction(L, -1)) { |
| 54 | int t = lua_type(L, -1); |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 55 | SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t); |
mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 56 | lua_settop(L, -2); |
| 57 | } else { |
| 58 | canvas->pushThis(); |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 59 | lua_pushstring(L, pictureFile); |
| 60 | if (lua_pcall(L, 2, 0, 0) != LUA_OK) { |
mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 61 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
Mike Klein | be28ee2 | 2017-02-06 12:46:20 -0500 | [diff] [blame] | 66 | int main(int argc, char** argv) { |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 67 | SkCommandLineFlags::SetUsage("apply lua script to .skp files."); |
| 68 | SkCommandLineFlags::Parse(argc, argv); |
| 69 | |
| 70 | if (FLAGS_skpPath.isEmpty()) { |
| 71 | SkDebugf(".skp files or directories are required.\n"); |
| 72 | exit(-1); |
| 73 | } |
| 74 | if (FLAGS_luaFile.isEmpty()) { |
mike@reedtribe.org | 0e59b79 | 2013-05-21 03:24:37 +0000 | [diff] [blame] | 75 | SkDebugf("missing luaFile(s)\n"); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 76 | exit(-1); |
| 77 | } |
| 78 | |
reed@google.com | e2aad27 | 2013-05-31 19:46:02 +0000 | [diff] [blame] | 79 | const char* summary = gSummarizeFunc; |
| 80 | if (!FLAGS_tailFunc.isEmpty()) { |
| 81 | summary = FLAGS_tailFunc[0]; |
| 82 | } |
| 83 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 84 | SkAutoGraphics ag; |
reed@google.com | e2aad27 | 2013-05-31 19:46:02 +0000 | [diff] [blame] | 85 | SkLua L(summary); |
mike@reedtribe.org | 0e59b79 | 2013-05-21 03:24:37 +0000 | [diff] [blame] | 86 | |
| 87 | for (int i = 0; i < FLAGS_luaFile.count(); ++i) { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 88 | sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_luaFile[i])); |
| 89 | if (!data) { |
| 90 | data = SkData::MakeEmpty(); |
reed | 9594da1 | 2014-09-12 12:12:27 -0700 | [diff] [blame] | 91 | } |
commit-bot@chromium.org | bdc772e | 2014-03-19 19:23:17 +0000 | [diff] [blame] | 92 | if (!FLAGS_quiet) { |
| 93 | SkDebugf("loading %s...\n", FLAGS_luaFile[i]); |
| 94 | } |
reed@google.com | 3597b73 | 2013-05-22 20:12:50 +0000 | [diff] [blame] | 95 | if (!L.runCode(data->data(), data->size())) { |
mike@reedtribe.org | 0e59b79 | 2013-05-21 03:24:37 +0000 | [diff] [blame] | 96 | SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]); |
| 97 | exit(-1); |
| 98 | } |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 99 | } |
| 100 | |
reed@google.com | e2aad27 | 2013-05-31 19:46:02 +0000 | [diff] [blame] | 101 | if (!FLAGS_headCode.isEmpty()) { |
| 102 | L.runCode(FLAGS_headCode[0]); |
| 103 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 104 | |
commit-bot@chromium.org | aaeb87d | 2013-06-21 21:45:20 +0000 | [diff] [blame] | 105 | int moduloRemainder = -1; |
| 106 | int moduloDivisor = -1; |
| 107 | SkString moduloStr; |
| 108 | |
| 109 | if (FLAGS_modulo.count() == 2) { |
| 110 | moduloRemainder = atoi(FLAGS_modulo[0]); |
| 111 | moduloDivisor = atoi(FLAGS_modulo[1]); |
| 112 | if (moduloRemainder < 0 || moduloDivisor <= 0 || moduloRemainder >= moduloDivisor) { |
| 113 | SkDebugf("invalid modulo values.\n"); |
| 114 | return -1; |
| 115 | } |
| 116 | } |
| 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)) { |
tfarina | a8e2e15 | 2014-07-28 19:26:58 -0700 | [diff] [blame] | 126 | paths.push_back() = SkOSPath::Join(directory.c_str(), filename.c_str()); |
commit-bot@chromium.org | 3da6c56 | 2013-06-18 18:35:58 +0000 | [diff] [blame] | 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++) { |
commit-bot@chromium.org | aaeb87d | 2013-06-21 21:45:20 +0000 | [diff] [blame] | 134 | if (moduloRemainder >= 0) { |
| 135 | if ((i % moduloDivisor) != moduloRemainder) { |
| 136 | continue; |
| 137 | } |
| 138 | moduloStr.printf("[%d.%d] ", i, moduloDivisor); |
| 139 | } |
commit-bot@chromium.org | 3da6c56 | 2013-06-18 18:35:58 +0000 | [diff] [blame] | 140 | const char* path = paths[i].c_str(); |
commit-bot@chromium.org | bdc772e | 2014-03-19 19:23:17 +0000 | [diff] [blame] | 141 | if (!FLAGS_quiet) { |
| 142 | SkDebugf("scraping %s %s\n", path, moduloStr.c_str()); |
| 143 | } |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 144 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 145 | auto pic(load_picture(path)); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 146 | if (pic.get()) { |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 147 | std::unique_ptr<SkLuaCanvas> canvas( |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 148 | new SkLuaCanvas(SkScalarCeilToInt(pic->cullRect().width()), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 149 | SkScalarCeilToInt(pic->cullRect().height()), |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 150 | L.get(), gAccumulateFunc)); |
mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 151 | |
commit-bot@chromium.org | 3da6c56 | 2013-06-18 18:35:58 +0000 | [diff] [blame] | 152 | call_canvas(L.get(), canvas.get(), path, gStartCanvasFunc); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 153 | canvas->drawPicture(pic); |
commit-bot@chromium.org | 3da6c56 | 2013-06-18 18:35:58 +0000 | [diff] [blame] | 154 | call_canvas(L.get(), canvas.get(), path, gEndCanvasFunc); |
reed@google.com | 2d51677 | 2013-05-21 16:05:53 +0000 | [diff] [blame] | 155 | |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 156 | } else { |
mike@reedtribe.org | cef454e | 2013-05-21 01:49:06 +0000 | [diff] [blame] | 157 | SkDebugf("failed to load\n"); |
reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | return 0; |
| 162 | } |