Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: tools/lua/lua_pictures.cpp

Issue 15742009: expand SkLua to handle creation of its own State (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« samplecode/SampleLua.cpp ('K') | « src/utils/SkLuaCanvas.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "SkLua.h"
8 #include "SkLuaCanvas.h" 9 #include "SkLuaCanvas.h"
9 #include "SkPicture.h" 10 #include "SkPicture.h"
10 #include "SkCommandLineFlags.h" 11 #include "SkCommandLineFlags.h"
11 #include "SkGraphics.h" 12 #include "SkGraphics.h"
12 #include "SkStream.h" 13 #include "SkStream.h"
13 #include "SkData.h" 14 #include "SkData.h"
14 #include "picture_utils.h" 15 #include "picture_utils.h"
15 #include "SkOSFile.h" 16 #include "SkOSFile.h"
16 #include "SkImageDecoder.h" 17 #include "SkImageDecoder.h"
17 18
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file)); 54 SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(file));
54 if (!stream.get()) { 55 if (!stream.get()) {
55 return SkData::NewEmpty(); 56 return SkData::NewEmpty();
56 } 57 }
57 size_t len = stream->getLength(); 58 size_t len = stream->getLength();
58 void* buffer = sk_malloc_throw(len); 59 void* buffer = sk_malloc_throw(len);
59 stream->read(buffer, len); 60 stream->read(buffer, len);
60 return SkData::NewFromMalloc(buffer, len); 61 return SkData::NewFromMalloc(buffer, len);
61 } 62 }
62 63
63 class SkAutoLua {
64 public:
65 SkAutoLua(const char termCode[] = NULL) : fTermCode(termCode) {
66 fL = luaL_newstate();
67 luaL_openlibs(fL);
68 }
69 ~SkAutoLua() {
70 if (fTermCode.size() > 0) {
71 lua_getglobal(fL, fTermCode.c_str());
72 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
73 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
74 }
75 }
76 lua_close(fL);
77 }
78
79 lua_State* get() const { return fL; }
80 lua_State* operator*() const { return fL; }
81 lua_State* operator->() const { return fL; }
82
83 bool load(const char code[]) {
84 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
85 if (err) {
86 SkDebugf("--- lua failed\n");
87 return false;
88 }
89 return true;
90 }
91 bool load(const void* code, size_t size) {
92 SkString str((const char*)code, size);
93 return load(str.c_str());
94 int err = luaL_loadbufferx(fL, (const char*)code, size, NULL, NULL)
95 || lua_pcall(fL, 0, 0, 0);
96 if (err) {
97 SkDebugf("--- lua failed\n");
98 return false;
99 }
100 return true;
101 }
102 private:
103 lua_State* fL;
104 SkString fTermCode;
105 };
106
107 static void call_canvas(lua_State* L, SkLuaCanvas* canvas, 64 static void call_canvas(lua_State* L, SkLuaCanvas* canvas,
108 const char pictureFile[], const char funcName[]) { 65 const char pictureFile[], const char funcName[]) {
109 lua_getglobal(L, funcName); 66 lua_getglobal(L, funcName);
110 if (!lua_isfunction(L, -1)) { 67 if (!lua_isfunction(L, -1)) {
111 int t = lua_type(L, -1); 68 int t = lua_type(L, -1);
112 SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t); 69 SkDebugf("--- expected %s function %d, ignoring.\n", funcName, t);
113 lua_settop(L, -2); 70 lua_settop(L, -2);
114 } else { 71 } else {
115 canvas->pushThis(); 72 canvas->pushThis();
116 lua_pushstring(L, pictureFile); 73 lua_pushstring(L, pictureFile);
(...skipping 11 matching lines...) Expand all
128 if (FLAGS_skpPath.isEmpty()) { 85 if (FLAGS_skpPath.isEmpty()) {
129 SkDebugf(".skp files or directories are required.\n"); 86 SkDebugf(".skp files or directories are required.\n");
130 exit(-1); 87 exit(-1);
131 } 88 }
132 if (FLAGS_luaFile.isEmpty()) { 89 if (FLAGS_luaFile.isEmpty()) {
133 SkDebugf("missing luaFile(s)\n"); 90 SkDebugf("missing luaFile(s)\n");
134 exit(-1); 91 exit(-1);
135 } 92 }
136 93
137 SkAutoGraphics ag; 94 SkAutoGraphics ag;
138 SkAutoLua L(gSummarizeFunc); 95 SkLua L(gSummarizeFunc);
139 96
140 for (int i = 0; i < FLAGS_luaFile.count(); ++i) { 97 for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
141 SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i])); 98 SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i]));
142 SkDebugf("loading %s...\n", FLAGS_luaFile[i]); 99 SkDebugf("loading %s...\n", FLAGS_luaFile[i]);
143 if (!L.load(data->data(), data->size())) { 100 if (!L.runCode(data->data(), data->size())) {
144 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]); 101 SkDebugf("failed to load luaFile %s\n", FLAGS_luaFile[i]);
145 exit(-1); 102 exit(-1);
146 } 103 }
147 } 104 }
148 105
149 for (int i = 0; i < FLAGS_skpPath.count(); i ++) { 106 for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
150 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp"); 107 SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
151 SkString inputFilename; 108 SkString inputFilename;
152 109
153 while (iter.next(&inputFilename)) { 110 while (iter.next(&inputFilename)) {
(...skipping 20 matching lines...) Expand all
174 } 131 }
175 } 132 }
176 return 0; 133 return 0;
177 } 134 }
178 135
179 #if !defined SK_BUILD_FOR_IOS 136 #if !defined SK_BUILD_FOR_IOS
180 int main(int argc, char * const argv[]) { 137 int main(int argc, char * const argv[]) {
181 return tool_main(argc, (char**) argv); 138 return tool_main(argc, (char**) argv);
182 } 139 }
183 #endif 140 #endif
OLDNEW
« samplecode/SampleLua.cpp ('K') | « src/utils/SkLuaCanvas.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698