blob: 7d78f76a41cb05cd2a56247948bfb8f553ef7e53 [file] [log] [blame]
commit-bot@chromium.org44a38772013-12-05 13:45:19 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 *
8 */
9#include <v8.h>
jcgregorioe22f45f2014-10-24 12:49:17 -070010#include <include/libplatform/libplatform.h>
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000011#include "SkV8Example.h"
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000012#include "Global.h"
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000013#include "JsContext.h"
commit-bot@chromium.orgf679d1b2014-02-27 20:20:21 +000014#include "Path2D.h"
jcgregorioe001da22014-10-29 05:33:27 -070015#include "Path2DBuilder.h"
Brian Salomonf43d0002017-08-31 20:04:00 +000016#include "GrBackendSurface.h"
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000017#include "gl/GrGLUtil.h"
18#include "gl/GrGLDefines.h"
19#include "gl/GrGLInterface.h"
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000020#include "GrContext.h"
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000021#include "SkApplication.h"
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +000022#include "SkCommandLineFlags.h"
23#include "SkData.h"
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000024#include "SkDraw.h"
25#include "SkGpuDevice.h"
26#include "SkGraphics.h"
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +000027#include "SkScalar.h"
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000028#include "SkSurface.h"
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000029
30
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +000031DEFINE_string2(infile, i, NULL, "Name of file to load JS from.\n");
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000032DEFINE_bool(gpu, true, "Use the GPU for rendering.");
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +000033
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000034void application_init() {
35 SkGraphics::Init();
36 SkEvent::Init();
37}
38
39void application_term() {
40 SkEvent::Term();
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000041}
42
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000043SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsContext* context)
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000044 : INHERITED(hwnd)
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000045 , fJsContext(context)
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000046#if SK_SUPPORT_GPU
47 , fCurContext(NULL)
48 , fCurIntf(NULL)
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000049 , fCurSurface(NULL)
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000050#endif
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000051{
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000052 this->setVisibleP(true);
53 this->setClipToBounds(false);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000054
55#if SK_SUPPORT_GPU
56 this->windowSizeChanged();
57#endif
58}
59
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000060SkV8ExampleWindow::~SkV8ExampleWindow() {
61#if SK_SUPPORT_GPU
62 SkSafeUnref(fCurContext);
63 SkSafeUnref(fCurIntf);
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000064 SkSafeUnref(fCurSurface);
65#endif
66}
67
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000068#if SK_SUPPORT_GPU
69void SkV8ExampleWindow::windowSizeChanged() {
70 if (FLAGS_gpu) {
71 SkOSWindow::AttachmentInfo attachmentInfo;
72 bool result = this->attach(
brianosman2d1ee792016-05-05 12:24:31 -070073 SkOSWindow::kNativeGL_BackEndType, 0, false, &attachmentInfo);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000074 if (!result) {
75 printf("Failed to attach.");
76 exit(1);
77 }
78
79 fCurIntf = GrGLCreateNativeInterface();
Greg Daniel02611d92017-07-25 10:05:01 -040080 fCurContext = GrContext::MakeGL(fCurIntf).release();
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000081 if (NULL == fCurIntf || NULL == fCurContext) {
82 printf("Failed to initialize GL.");
83 exit(1);
84 }
85
Brian Salomonf43d0002017-08-31 20:04:00 +000086 GrGLFramebufferInfo framebufferInfo;
87 GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &framebufferInfo.fFBOID);
88 GrBackendRenderTarget backendRenderTarget(SkScalarRoundToInt(this->width()),
89 SkScalarRoundToInt(this->height()),
90 attachmentInfo.fSampleCount,
91 attachmentInfo.fStencilBits,
92 kSkia8888_GrPixelConfig,
93 framebufferInfo);
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000094 SkSafeUnref(fCurSurface);
Brian Salomonf43d0002017-08-31 20:04:00 +000095 fCurSurface = SkSurface::MakeFromBackendRenderTarget(fCurContext, backendRenderTarget,
96 kBottomLeft_GrSurfaceOrigin,
robertphillips12e96622016-08-01 05:53:23 -070097 nullptr, nullptr).release();
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000098 }
99}
100#endif
101
102#if SK_SUPPORT_GPU
jcgregorioe22f45f2014-10-24 12:49:17 -0700103SkSurface* SkV8ExampleWindow::createSurface() {
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000104 if (FLAGS_gpu) {
jcgregorioe22f45f2014-10-24 12:49:17 -0700105 // Increase the ref count since callers of createSurface put the
Hal Canary2db83612016-11-04 13:02:54 -0400106 // results in a sk_sp.
jcgregorioe22f45f2014-10-24 12:49:17 -0700107 fCurSurface->ref();
108 return fCurSurface;
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000109 } else {
jcgregorioe22f45f2014-10-24 12:49:17 -0700110 return this->INHERITED::createSurface();
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000111 }
112}
113#endif
114
115void SkV8ExampleWindow::onSizeChange() {
116 this->INHERITED::onSizeChange();
117
118#if SK_SUPPORT_GPU
119 this->windowSizeChanged();
120#endif
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000121}
122
jcgregorioe001da22014-10-29 05:33:27 -0700123Global* global = NULL;
Andreas Haasae99d292017-11-09 16:52:04 +0100124std::unique_ptr<v8::Platform> platform;
jcgregorioe001da22014-10-29 05:33:27 -0700125
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000126void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000127
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000128 canvas->save();
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +0000129 canvas->drawColor(SK_ColorWHITE);
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000130
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000131 // Now jump into JS and call the onDraw(canvas) method defined there.
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000132 fJsContext->onDraw(canvas);
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000133
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000134 canvas->restore();
135
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000136 this->INHERITED::onDraw(canvas);
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000137
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000138#if SK_SUPPORT_GPU
139 if (FLAGS_gpu) {
140 fCurContext->flush();
141 this->present();
142 }
143#endif
144}
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000145
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000146#ifdef SK_BUILD_FOR_WIN
147void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) {
148 RECT winRect;
149 winRect.top = rect.top();
150 winRect.bottom = rect.bottom();
151 winRect.right = rect.right();
152 winRect.left = rect.left();
153 InvalidateRect((HWND)this->getHWND(), &winRect, false);
154}
155#endif
156
jcgregorioe22f45f2014-10-24 12:49:17 -0700157
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000158SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000159 printf("Started\n");
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000160
jcgregorioe001da22014-10-29 05:33:27 -0700161 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000162 SkCommandLineFlags::Parse(argc, argv);
163
jcgregorioe22f45f2014-10-24 12:49:17 -0700164 v8::V8::InitializeICU();
Andreas Haasae99d292017-11-09 16:52:04 +0100165 platform = std::unique_ptr<v8::Platform>(v8::platform::CreateDefaultPlatform());
166 v8::V8::InitializePlatform(platform.get());
jcgregorioe22f45f2014-10-24 12:49:17 -0700167 v8::V8::Initialize();
168
169 v8::Isolate* isolate = v8::Isolate::New();
170 v8::Isolate::Scope isolate_scope(isolate);
171 v8::HandleScope handle_scope(isolate);
172 isolate->Enter();
173
jcgregorioe001da22014-10-29 05:33:27 -0700174 global = new Global(isolate);
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000175
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000176
177 // Set up things to look like a browser by creating
178 // a console object that invokes our print function.
179 const char* startupScript =
180 "function Console() {}; \n"
181 "Console.prototype.log = function() { \n"
182 " var args = Array.prototype.slice.call(arguments).join(' '); \n"
183 " print(args); \n"
184 "}; \n"
185 "console = new Console(); \n";
186
187 if (!global->parseScript(startupScript)) {
188 printf("Failed to parse startup script: %s.\n", FLAGS_infile[0]);
189 exit(1);
190 }
191
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +0000192 const char* script =
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000193 "function onDraw(canvas) { \n"
194 " canvas.fillStyle = '#00FF00'; \n"
195 " canvas.fillRect(20, 20, 100, 100); \n"
196 " canvas.inval(); \n"
197 "} \n";
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000198
bungeman38d909e2016-08-02 14:40:46 -0700199 sk_sp<SkData> data;
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000200 if (FLAGS_infile.count()) {
bungeman38d909e2016-08-02 14:40:46 -0700201 data = SkData::MakeFromFileName(FLAGS_infile[0]);
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000202 script = static_cast<const char*>(data->data());
203 }
204 if (NULL == script) {
205 printf("Could not load file: %s.\n", FLAGS_infile[0]);
206 exit(1);
207 }
jcgregorioe001da22014-10-29 05:33:27 -0700208 Path2DBuilder::AddToGlobal(global);
commit-bot@chromium.orgf679d1b2014-02-27 20:20:21 +0000209 Path2D::AddToGlobal(global);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000210
211 if (!global->parseScript(script)) {
commit-bot@chromium.org872796b2013-12-20 15:56:52 +0000212 printf("Failed to parse file: %s.\n", FLAGS_infile[0]);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000213 exit(1);
214 }
215
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000216
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000217 JsContext* jsContext = new JsContext(global);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000218
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000219 if (!jsContext->initialize()) {
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000220 printf("Failed to initialize.\n");
221 exit(1);
222 }
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000223 SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsContext);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000224 global->setWindow(win);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000225
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000226 return win;
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000227}