diff --git a/experimental/webtry/README b/experimental/webtry/README new file mode 100644 index 000000000..a36ac8a3f --- /dev/null +++ b/experimental/webtry/README @@ -0,0 +1,16 @@ +WebTry +====== + +Allows trying out Skia code in the browser. Run a local webserver +and from the pages it serves try out Skia code and see the results +immediately. + +Running +======= + +$ cd experimental/webtry +$ python server.py + +Then visit http://localhost:8765 in your browser. + + diff --git a/experimental/webtry/index.html b/experimental/webtry/index.html new file mode 100644 index 000000000..b3903e675 --- /dev/null +++ b/experimental/webtry/index.html @@ -0,0 +1,97 @@ + + + + Skia WebTry + + + + +
#include "SkCanvas.h"
+#include "SkGraphics.h"
+#include "SkImageEncoder.h"
+#include "SkImageInfo.h"
+#include "SkForceLinking.h"
+
+int main() {
+  SkForceLinking(false);
+  SkGraphics::Init();
+
+  SkImageInfo info = SkImageInfo::MakeN32(300, 300, kPremul_SkAlphaType);
+  SkBitmap bitmap;
+  bitmap.setConfig(info);
+  bitmap.allocPixels();
+  SkCanvas c(bitmap);
+  c.drawColor(SK_ColorWHITE);
+
+  
+
+  if (!SkImageEncoder::EncodeFile("foo.png", bitmap, SkImageEncoder::kPNG_Type, 100)) {
+    printf("Failed to encode\n");
+  }
+}
+
+ +

Image appears here:

+ + +
+ + + + + diff --git a/experimental/webtry/server.py b/experimental/webtry/server.py new file mode 100644 index 000000000..8367b3e46 --- /dev/null +++ b/experimental/webtry/server.py @@ -0,0 +1,79 @@ +import BaseHTTPServer +import base64 +import json +import logging +import string +import subprocess + +HOST_NAME = 'localhost' +PORT_NUMBER = 8765 + +def runCode(usercode): + f = open('template.cpp', 'rb') + template = string.Template(f.read()) + f.close() + + code = template.substitute(usercode=usercode) + + f = open('result.cpp', 'wb') + f.write(code) + f.close() + + msg = "" + img = "" + try: + logging.info("compiling") + msg = subprocess.check_output('ninja -C ../../out/Debug webtry'.split()) + try: + logging.info("running") + msg = subprocess.check_output('../../out/Debug/webtry'.split()) + f = open('foo.png', 'rb') + img = base64.b64encode(f.read()) + f.close() + except subprocess.CalledProcessError as e: + logging.info(e) + msg = e.output + except subprocess.CalledProcessError as e: + logging.info(e) + msg = e.output + + retval = { + 'message': msg + } + if img: + retval['img'] = img + return retval + +class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): + def do_POST(self): + logging.info("POST") + body = "" + l = self.rfile.readline() + while l.strip() != "EOF": + body += l + l = self.rfile.readline() + self.send_response(200) + self.send_header("Content-type", "application/json") + self.end_headers() + resp = runCode(body) + self.wfile.write(json.dumps(resp)) + self.end_headers() + + def do_GET(self): + """Respond to a GET request.""" + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + f = open('index.html', 'rb') + self.wfile.write(f.read()) + f.close() + +if __name__ == '__main__': + server_class = BaseHTTPServer.HTTPServer + httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler) + logging.info("Server Start: %s:%s" % (HOST_NAME, PORT_NUMBER)) + try: + httpd.serve_forever() + except KeyboardInterrupt: + pass + httpd.server_close() diff --git a/experimental/webtry/template.cpp b/experimental/webtry/template.cpp new file mode 100644 index 000000000..20b9086cd --- /dev/null +++ b/experimental/webtry/template.cpp @@ -0,0 +1,23 @@ +#include "SkCanvas.h" +#include "SkGraphics.h" +#include "SkImageEncoder.h" +#include "SkImageInfo.h" +#include "SkForceLinking.h" + +int main() { + SkForceLinking(false); + SkGraphics::Init(); + + SkImageInfo info = SkImageInfo::MakeN32(300, 300, kPremul_SkAlphaType); + SkBitmap bitmap; + bitmap.setConfig(info); + bitmap.allocPixels(); + SkCanvas c(bitmap); + c.drawColor(SK_ColorWHITE); + + $usercode + + if (!SkImageEncoder::EncodeFile("foo.png", bitmap, SkImageEncoder::kPNG_Type, 100)) { + printf("Failed to encode\n"); + } +} diff --git a/gyp/everything.gyp b/gyp/everything.gyp index bf67d1fc6..c1e7937bc 100644 --- a/gyp/everything.gyp +++ b/gyp/everything.gyp @@ -23,6 +23,7 @@ 'debugger.gyp:debugger', 'pdfviewer.gyp:pdfviewer', #'v8.gyp:SkV8Example', + #'webtry.gyp:webtry', ], }], ], diff --git a/gyp/webtry.gyp b/gyp/webtry.gyp new file mode 100644 index 000000000..ba0b9263a --- /dev/null +++ b/gyp/webtry.gyp @@ -0,0 +1,22 @@ +# GYP file to build a the webtry sample. +{ + 'targets': [ + { + 'target_name': 'webtry', + 'type': 'executable', + 'mac_bundle' : 1, + 'include_dirs' : [ + '../src/core', + '../src/images', + ], + 'sources': [ + '../experimental/webtry/result.cpp', + ], + 'dependencies': [ + 'flags.gyp:flags', + 'skia_lib.gyp:skia_lib', + 'images.gyp:images', + ], + }, + ], +}