Smart gpu selection system

This commit is contained in:
Łukasz Magiera 2016-02-28 14:31:55 +01:00
parent 872775d9e4
commit 3b89f0b59c
10 changed files with 112 additions and 36 deletions

View File

@ -8,6 +8,7 @@ extern char lua_eeprom[];
extern char lua_fbgpu[]; extern char lua_fbgpu[];
extern char lua_filesystem[]; extern char lua_filesystem[];
extern char lua_gpio[]; extern char lua_gpio[];
extern char lua_gpudetect[];
extern char lua_init[]; extern char lua_init[];
extern char lua_internet[]; extern char lua_internet[];
extern char lua_sandbox[]; extern char lua_sandbox[];

View File

@ -20,7 +20,7 @@ typedef unsigned short ushort;
lua_State* getL(); lua_State* getL();
void run_init(); void run_init(int argc, char **argv);
void lupi_init(); void lupi_init();
void luanative_start(lua_State *L); void luanative_start(lua_State *L);
void fb_start(lua_State *L); void fb_start(lua_State *L);

View File

@ -1,5 +1,6 @@
#ifndef RES_H #ifndef RES_H
#define RES_H #define RES_H
extern char res_eepromDefault[]; extern char res_eepromDefault[];
extern char res_help[];
extern char res_unifont[]; extern char res_unifont[];
#endif #endif

5
resources/help Normal file
View File

@ -0,0 +1,5 @@
Usage: lupi [options]
-h, --help This help
-f, --fb Force framebuffer rendering
-t, --text Force text mode rendering

View File

@ -429,6 +429,19 @@ static int l_platform (lua_State *L) { /* returns platform identifiers separated
return 1; return 1;
} }
static int l_isinit (lua_State *L) {
#ifndef _WIN32
lua_pushboolean(L, 0);
#else
if(getpid() == 1) {
lua_pushboolean(L, 1);
} else {
lua_pushboolean(L, 0);
}
#endif
return 1;
}
#ifdef DEBUG #ifdef DEBUG
static int l_debug (lua_State *L) { static int l_debug (lua_State *L) {
return 0; return 0;
@ -464,6 +477,7 @@ void luanative_start(lua_State *L) {
{"freeMemory", l_freeMemory}, {"freeMemory", l_freeMemory},
{"pull", l_pull}, {"pull", l_pull},
{"platform", l_platform}, {"platform", l_platform},
{"isinit", l_isinit},
#ifdef DEBUG #ifdef DEBUG
{"debug", l_debug}, {"debug", l_debug},
#endif #endif

View File

@ -1,9 +1,9 @@
#include <stdio.h> #include <stdio.h>
#include "lupi.h" #include "lupi.h"
int main (void) { int main (int argc, char **argv) {
puts("LuPI L0 INIT"); puts("LuPI L0 INIT");
lupi_init(); lupi_init();
run_init(); run_init(argc, argv);
return 0; return 0;
} }

View File

@ -7,25 +7,27 @@
#include <stdlib.h> #include <stdlib.h>
void setup_modules(lua_State *L) { void setup_modules(lua_State *L) {
lua_createtable (L, 0, 1); lua_createtable (L, 0, 32);
pushstuple(L, "boot", lua_boot); pushstuple(L, "boot", lua_boot);
pushstuple(L, "color", lua_util_color); pushstuple(L, "color", lua_util_color);
pushstuple(L, "component", lua_component); pushstuple(L, "component", lua_component);
pushstuple(L, "computer", lua_computer); pushstuple(L, "computer", lua_computer);
pushstuple(L, "debug", lua_debug); pushstuple(L, "debug", lua_debug);
pushstuple(L, "eeprom", lua_eeprom); pushstuple(L, "eeprom", lua_eeprom);
pushstuple(L, "filesystem", lua_filesystem); pushstuple(L, "filesystem", lua_filesystem);
pushstuple(L, "gpio", lua_gpio); pushstuple(L, "gpio", lua_gpio);
pushstuple(L, "internet", lua_internet); pushstuple(L, "gpudetect", lua_gpudetect);
pushstuple(L, "sandbox", lua_sandbox); pushstuple(L, "internet", lua_internet);
pushstuple(L, "textgpu", lua_textgpu); pushstuple(L, "sandbox", lua_sandbox);
pushstuple(L, "fbgpu", lua_fbgpu); pushstuple(L, "textgpu", lua_textgpu);
pushstuple(L, "color", lua_util_color); pushstuple(L, "fbgpu", lua_fbgpu);
pushstuple(L, "random", lua_util_random); pushstuple(L, "color", lua_util_color);
pushstuple(L, "buffer", lua_util_buffer); pushstuple(L, "random", lua_util_random);
pushstuple(L, "buffer", lua_util_buffer);
pushstuple(L, "eepromDefault", res_eepromDefault); pushstuple(L, "eepromDefault", res_eepromDefault);
pushstuple(L, "help", res_help);
lua_setglobal(L, "moduleCode"); lua_setglobal(L, "moduleCode");
} }

View File

@ -20,7 +20,7 @@ lua_State* getL() {
return L; return L;
} }
void run_init() { void run_init(int argc, char **argv) {
L = luaL_newstate(); L = luaL_newstate();
luaL_openlibs (L); luaL_openlibs (L);
@ -36,6 +36,9 @@ void run_init() {
fprintf(stderr, "Couldn't load init: %s\n", lua_tostring(L, -1)); fprintf(stderr, "Couldn't load init: %s\n", lua_tostring(L, -1));
exit(1); exit(1);
} }
lua_call(L, 0, 0); for(int i = 0; i < argc; i++) {
lua_pushstring(L, argv[i]);
}
lua_call(L, argc, 0);
lua_close(L); lua_close(L);
} }

View File

@ -0,0 +1,37 @@
local gpudetect = {}
local function tryText()
loadModule("textgpu")
local textgpuAddr, tgfail = modules.textgpu.start()
if not textgpuAddr then
lprint("Couldn't initialize text gpu: " .. tostring(tgfail))
return false
end
return true
end
local function tryFb()
if framebuffer.isReady() then
loadModule("fbgpu")
modules.fbgpu.start()
return true
end
return false
end
function gpudetect.run()
local s = false
if hasOpt("-t", "--text") then
s = tryText()
return
end
if hasOpt("-f", "--fb") or native.isinit() then
s = tryFb()
end
if not s then
lprint("Falling back to text gpu")
s = tryText()
end
end
return gpudetect

View File

@ -15,6 +15,27 @@ function checkArg(n, have, ...)
end end
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
local argv = {...}
function hasOpt(o, n, ...)
for _, v in pairs(argv) do
if v == o then
return true
end
end
if n then
return hasOpt(n, ...)
end
return false
end
if hasOpt("-h", "--help") then
print(moduleCode.help)
os.exit(0)
end
-------------------------------------------------------------------------------
math.randomseed(native.uptime())--TODO: Make it better? math.randomseed(native.uptime())--TODO: Make it better?
lprint = function (...) lprint = function (...)
@ -26,7 +47,10 @@ lprint("LuPI L1 INIT")
modules = {} modules = {}
deadhooks = {} deadhooks = {}
local function loadModule(name) function loadModule(name)
if modules[name] then
return
end
lprint("LuPI L1 INIT > Load module > " .. name) lprint("LuPI L1 INIT > Load module > " .. name)
io.flush() io.flush()
--TODO: PRERELEASE: Module sandboxing, preferably secure-ish --TODO: PRERELEASE: Module sandboxing, preferably secure-ish
@ -63,11 +87,7 @@ function main()
loadModule("eeprom") loadModule("eeprom")
loadModule("gpio") loadModule("gpio")
if framebuffer.isReady() then loadModule("gpudetect")
loadModule("fbgpu")
else
loadModule("textgpu")
end
loadModule("filesystem") loadModule("filesystem")
loadModule("internet") loadModule("internet")
@ -89,14 +109,7 @@ function main()
end end
modules.computer.tmp = modules.filesystem.register("/tmp/lupi-" .. modules.random.uuid()) modules.computer.tmp = modules.filesystem.register("/tmp/lupi-" .. modules.random.uuid())
if framebuffer.isReady() then modules.gpudetect.run()
modules.fbgpu.start()
else
local textgpuAddr, tgfail = modules.textgpu.start()
if not textgpuAddr then
lprint("Couldn't initialize text gpu: " .. tostring(tgfail))
end
end
if native.debug then if native.debug then
modules.debug.hook() modules.debug.hook()