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>
|
2014-10-24 19:49:17 +00:00
|
|
|
#include <include/libplatform/libplatform.h>
|
2013-12-05 13:45:19 +00:00
|
|
|
|
|
|
|
#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"
|
2014-10-29 12:33:27 +00:00
|
|
|
#include "Path2DBuilder.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();
|
|
|
|
}
|
|
|
|
|
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)
|
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
|
|
|
{
|
|
|
|
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(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(
|
2016-05-05 19:24:31 +00:00
|
|
|
SkOSWindow::kNativeGL_BackEndType, 0, false, &attachmentInfo);
|
2014-01-06 20:03:55 +00:00
|
|
|
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;
|
|
|
|
|
2014-02-27 17:39:40 +00:00
|
|
|
SkSafeUnref(fCurSurface);
|
2016-08-01 12:53:23 +00:00
|
|
|
fCurSurface = SkSurface::MakeFromBackendRenderTarget(fCurContext, desc,
|
|
|
|
nullptr, nullptr).release();
|
2014-01-06 20:03:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
2014-10-24 19:49:17 +00:00
|
|
|
SkSurface* SkV8ExampleWindow::createSurface() {
|
2014-01-06 20:03:55 +00:00
|
|
|
if (FLAGS_gpu) {
|
2014-10-24 19:49:17 +00:00
|
|
|
// Increase the ref count since callers of createSurface put the
|
2016-11-04 17:02:54 +00:00
|
|
|
// results in a sk_sp.
|
2014-10-24 19:49:17 +00:00
|
|
|
fCurSurface->ref();
|
|
|
|
return fCurSurface;
|
2014-01-06 20:03:55 +00:00
|
|
|
} else {
|
2014-10-24 19:49:17 +00:00
|
|
|
return this->INHERITED::createSurface();
|
2014-01-06 20:03:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void SkV8ExampleWindow::onSizeChange() {
|
|
|
|
this->INHERITED::onSizeChange();
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
this->windowSizeChanged();
|
|
|
|
#endif
|
2013-12-18 04:45:37 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 12:33:27 +00:00
|
|
|
Global* global = NULL;
|
|
|
|
|
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
|
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
|
2013-12-10 14:13:03 +00:00
|
|
|
SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
|
|
|
|
printf("Started\n");
|
|
|
|
|
2014-10-29 12:33:27 +00:00
|
|
|
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
|
2013-12-16 18:24:51 +00:00
|
|
|
SkCommandLineFlags::Parse(argc, argv);
|
|
|
|
|
2014-10-24 19:49:17 +00:00
|
|
|
v8::V8::InitializeICU();
|
|
|
|
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
|
|
|
|
v8::V8::InitializePlatform(platform);
|
|
|
|
v8::V8::Initialize();
|
|
|
|
|
|
|
|
v8::Isolate* isolate = v8::Isolate::New();
|
|
|
|
v8::Isolate::Scope isolate_scope(isolate);
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
isolate->Enter();
|
|
|
|
|
2014-10-29 12:33:27 +00:00
|
|
|
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
|
|
|
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> data;
|
2013-12-16 18:24:51 +00:00
|
|
|
if (FLAGS_infile.count()) {
|
2016-08-02 21:40:46 +00:00
|
|
|
data = SkData::MakeFromFileName(FLAGS_infile[0]);
|
2013-12-16 18:24:51 +00:00
|
|
|
script = static_cast<const char*>(data->data());
|
|
|
|
}
|
|
|
|
if (NULL == script) {
|
|
|
|
printf("Could not load file: %s.\n", FLAGS_infile[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
2014-10-29 12:33:27 +00:00
|
|
|
Path2DBuilder::AddToGlobal(global);
|
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
|
|
|
}
|