2013-12-05 13:45:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <v8.h>
|
|
|
|
|
|
|
|
using namespace v8;
|
|
|
|
|
|
|
|
#include "SkV8Example.h"
|
2013-12-18 04:45:37 +00:00
|
|
|
#include "Global.h"
|
2014-01-06 18:17:24 +00:00
|
|
|
#include "JsContext.h"
|
2014-02-27 20:20:21 +00:00
|
|
|
#include "Path2D.h"
|
2013-12-05 13:45:19 +00:00
|
|
|
|
|
|
|
#include "gl/GrGLUtil.h"
|
|
|
|
#include "gl/GrGLDefines.h"
|
|
|
|
#include "gl/GrGLInterface.h"
|
2014-01-06 20:03:55 +00:00
|
|
|
#include "GrRenderTarget.h"
|
|
|
|
#include "GrContext.h"
|
2013-12-05 13:45:19 +00:00
|
|
|
#include "SkApplication.h"
|
2013-12-16 18:24:51 +00:00
|
|
|
#include "SkCommandLineFlags.h"
|
|
|
|
#include "SkData.h"
|
2013-12-05 13:45:19 +00:00
|
|
|
#include "SkDraw.h"
|
|
|
|
#include "SkGpuDevice.h"
|
|
|
|
#include "SkGraphics.h"
|
2013-12-11 17:34:58 +00:00
|
|
|
#include "SkScalar.h"
|
2014-02-27 17:39:40 +00:00
|
|
|
#include "SkSurface.h"
|
2013-12-05 13:45:19 +00:00
|
|
|
|
|
|
|
|
2013-12-16 18:24:51 +00:00
|
|
|
DEFINE_string2(infile, i, NULL, "Name of file to load JS from.\n");
|
2014-01-06 20:03:55 +00:00
|
|
|
DEFINE_bool(gpu, true, "Use the GPU for rendering.");
|
2013-12-16 18:24:51 +00:00
|
|
|
|
2013-12-05 13:45:19 +00:00
|
|
|
void application_init() {
|
|
|
|
SkGraphics::Init();
|
|
|
|
SkEvent::Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void application_term() {
|
|
|
|
SkEvent::Term();
|
|
|
|
SkGraphics::Term();
|
|
|
|
}
|
|
|
|
|
2014-01-06 18:17:24 +00:00
|
|
|
SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsContext* context)
|
2013-12-18 04:45:37 +00:00
|
|
|
: INHERITED(hwnd)
|
2014-01-06 18:17:24 +00:00
|
|
|
, fJsContext(context)
|
2014-01-06 20:03:55 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
, fCurContext(NULL)
|
|
|
|
, fCurIntf(NULL)
|
|
|
|
, fCurRenderTarget(NULL)
|
2014-02-27 17:39:40 +00:00
|
|
|
, fCurSurface(NULL)
|
2014-01-06 20:03:55 +00:00
|
|
|
#endif
|
2013-12-18 04:45:37 +00:00
|
|
|
{
|
2014-02-27 17:39:40 +00:00
|
|
|
this->setColorType(kBGRA_8888_SkColorType);
|
2013-12-18 04:45:37 +00:00
|
|
|
this->setVisibleP(true);
|
|
|
|
this->setClipToBounds(false);
|
2014-01-06 20:03:55 +00:00
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
this->windowSizeChanged();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-27 17:39:40 +00:00
|
|
|
SkV8ExampleWindow::~SkV8ExampleWindow() {
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
SkSafeUnref(fCurContext);
|
|
|
|
SkSafeUnref(fCurIntf);
|
|
|
|
SkSafeUnref(fCurRenderTarget);
|
|
|
|
SkSafeUnref(fCurSurface);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-01-06 20:03:55 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
void SkV8ExampleWindow::windowSizeChanged() {
|
|
|
|
if (FLAGS_gpu) {
|
|
|
|
SkOSWindow::AttachmentInfo attachmentInfo;
|
|
|
|
bool result = this->attach(
|
|
|
|
SkOSWindow::kNativeGL_BackEndType, 0, &attachmentInfo);
|
|
|
|
if (!result) {
|
|
|
|
printf("Failed to attach.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fCurIntf = GrGLCreateNativeInterface();
|
|
|
|
fCurContext = GrContext::Create(
|
|
|
|
kOpenGL_GrBackend, (GrBackendContext) fCurIntf);
|
|
|
|
if (NULL == fCurIntf || NULL == fCurContext) {
|
|
|
|
printf("Failed to initialize GL.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
GrBackendRenderTargetDesc desc;
|
|
|
|
desc.fWidth = SkScalarRoundToInt(this->width());
|
|
|
|
desc.fHeight = SkScalarRoundToInt(this->height());
|
|
|
|
desc.fConfig = kSkia8888_GrPixelConfig;
|
|
|
|
desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
|
|
|
|
desc.fSampleCnt = attachmentInfo.fSampleCount;
|
|
|
|
desc.fStencilBits = attachmentInfo.fStencilBits;
|
|
|
|
GrGLint buffer;
|
|
|
|
GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
|
|
|
|
desc.fRenderTargetHandle = buffer;
|
|
|
|
|
|
|
|
SkSafeUnref(fCurRenderTarget);
|
|
|
|
fCurRenderTarget = fCurContext->wrapBackendRenderTarget(desc);
|
2014-02-27 17:39:40 +00:00
|
|
|
SkSafeUnref(fCurSurface);
|
|
|
|
fCurSurface = SkSurface::NewRenderTargetDirect(fCurRenderTarget);
|
2014-01-06 20:03:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
SkCanvas* SkV8ExampleWindow::createCanvas() {
|
|
|
|
if (FLAGS_gpu) {
|
2014-02-27 17:39:40 +00:00
|
|
|
SkCanvas* c = fCurSurface->getCanvas();
|
|
|
|
// Increase the ref count since the surface keeps a reference
|
|
|
|
// to the canvas, but callers of createCanvas put the results
|
|
|
|
// in a SkAutoTUnref.
|
|
|
|
c->ref();
|
|
|
|
return c;
|
2014-01-06 20:03:55 +00:00
|
|
|
} else {
|
|
|
|
return this->INHERITED::createCanvas();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void SkV8ExampleWindow::onSizeChange() {
|
|
|
|
this->INHERITED::onSizeChange();
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
this->windowSizeChanged();
|
|
|
|
#endif
|
2013-12-18 04:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->drawColor(SK_ColorWHITE);
|
|
|
|
|
|
|
|
// Now jump into JS and call the onDraw(canvas) method defined there.
|
2014-01-06 18:17:24 +00:00
|
|
|
fJsContext->onDraw(canvas);
|
2013-12-18 04:45:37 +00:00
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
|
2014-01-06 20:03:55 +00:00
|
|
|
this->INHERITED::onDraw(canvas);
|
2013-12-18 04:45:37 +00:00
|
|
|
|
2014-01-06 20:03:55 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
if (FLAGS_gpu) {
|
|
|
|
fCurContext->flush();
|
|
|
|
this->present();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2013-12-18 04:45:37 +00:00
|
|
|
|
|
|
|
#ifdef SK_BUILD_FOR_WIN
|
|
|
|
void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) {
|
|
|
|
RECT winRect;
|
|
|
|
winRect.top = rect.top();
|
|
|
|
winRect.bottom = rect.bottom();
|
|
|
|
winRect.right = rect.right();
|
|
|
|
winRect.left = rect.left();
|
|
|
|
InvalidateRect((HWND)this->getHWND(), &winRect, false);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-12-10 14:13:03 +00:00
|
|
|
SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
|
|
|
|
printf("Started\n");
|
|
|
|
|
2013-12-16 18:24:51 +00:00
|
|
|
SkCommandLineFlags::Parse(argc, argv);
|
|
|
|
|
2013-12-10 14:13:03 +00:00
|
|
|
// Get the default Isolate created at startup.
|
|
|
|
Isolate* isolate = Isolate::GetCurrent();
|
2013-12-18 04:45:37 +00:00
|
|
|
Global* global = new Global(isolate);
|
2013-12-16 18:24:51 +00:00
|
|
|
|
2014-01-10 21:33:01 +00:00
|
|
|
|
|
|
|
// Set up things to look like a browser by creating
|
|
|
|
// a console object that invokes our print function.
|
|
|
|
const char* startupScript =
|
|
|
|
"function Console() {}; \n"
|
|
|
|
"Console.prototype.log = function() { \n"
|
|
|
|
" var args = Array.prototype.slice.call(arguments).join(' '); \n"
|
|
|
|
" print(args); \n"
|
|
|
|
"}; \n"
|
|
|
|
"console = new Console(); \n";
|
|
|
|
|
|
|
|
if (!global->parseScript(startupScript)) {
|
|
|
|
printf("Failed to parse startup script: %s.\n", FLAGS_infile[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-12-11 17:34:58 +00:00
|
|
|
const char* script =
|
2014-01-10 21:33:01 +00:00
|
|
|
"function onDraw(canvas) { \n"
|
|
|
|
" canvas.fillStyle = '#00FF00'; \n"
|
|
|
|
" canvas.fillRect(20, 20, 100, 100); \n"
|
|
|
|
" canvas.inval(); \n"
|
|
|
|
"} \n";
|
2013-12-16 18:24:51 +00:00
|
|
|
|
|
|
|
SkAutoTUnref<SkData> data;
|
|
|
|
if (FLAGS_infile.count()) {
|
|
|
|
data.reset(SkData::NewFromFileName(FLAGS_infile[0]));
|
|
|
|
script = static_cast<const char*>(data->data());
|
|
|
|
}
|
|
|
|
if (NULL == script) {
|
|
|
|
printf("Could not load file: %s.\n", FLAGS_infile[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
2014-02-27 20:20:21 +00:00
|
|
|
Path2D::AddToGlobal(global);
|
2013-12-18 04:45:37 +00:00
|
|
|
|
|
|
|
if (!global->parseScript(script)) {
|
2013-12-20 15:56:52 +00:00
|
|
|
printf("Failed to parse file: %s.\n", FLAGS_infile[0]);
|
2013-12-18 04:45:37 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2014-01-10 21:33:01 +00:00
|
|
|
|
2014-01-06 18:17:24 +00:00
|
|
|
JsContext* jsContext = new JsContext(global);
|
2013-12-18 04:45:37 +00:00
|
|
|
|
2014-01-06 18:17:24 +00:00
|
|
|
if (!jsContext->initialize()) {
|
2013-12-10 14:13:03 +00:00
|
|
|
printf("Failed to initialize.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2014-01-06 18:17:24 +00:00
|
|
|
SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsContext);
|
2013-12-18 04:45:37 +00:00
|
|
|
global->setWindow(win);
|
2014-01-06 20:03:55 +00:00
|
|
|
|
2013-12-18 04:45:37 +00:00
|
|
|
return win;
|
2013-12-05 13:45:19 +00:00
|
|
|
}
|