blob: 5cad12577fe011825e2cc08b9a41ef7d6fbef759 [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;
124
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000125void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000126
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000127 canvas->save();
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +0000128 canvas->drawColor(SK_ColorWHITE);
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000129
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000130 // Now jump into JS and call the onDraw(canvas) method defined there.
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000131 fJsContext->onDraw(canvas);
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000132
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000133 canvas->restore();
134
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000135 this->INHERITED::onDraw(canvas);
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000136
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000137#if SK_SUPPORT_GPU
138 if (FLAGS_gpu) {
139 fCurContext->flush();
140 this->present();
141 }
142#endif
143}
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000144
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000145#ifdef SK_BUILD_FOR_WIN
146void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) {
147 RECT winRect;
148 winRect.top = rect.top();
149 winRect.bottom = rect.bottom();
150 winRect.right = rect.right();
151 winRect.left = rect.left();
152 InvalidateRect((HWND)this->getHWND(), &winRect, false);
153}
154#endif
155
jcgregorioe22f45f2014-10-24 12:49:17 -0700156
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000157SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000158 printf("Started\n");
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000159
jcgregorioe001da22014-10-29 05:33:27 -0700160 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000161 SkCommandLineFlags::Parse(argc, argv);
162
jcgregorioe22f45f2014-10-24 12:49:17 -0700163 v8::V8::InitializeICU();
164 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
165 v8::V8::InitializePlatform(platform);
166 v8::V8::Initialize();
167
168 v8::Isolate* isolate = v8::Isolate::New();
169 v8::Isolate::Scope isolate_scope(isolate);
170 v8::HandleScope handle_scope(isolate);
171 isolate->Enter();
172
jcgregorioe001da22014-10-29 05:33:27 -0700173 global = new Global(isolate);
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000174
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000175
176 // Set up things to look like a browser by creating
177 // a console object that invokes our print function.
178 const char* startupScript =
179 "function Console() {}; \n"
180 "Console.prototype.log = function() { \n"
181 " var args = Array.prototype.slice.call(arguments).join(' '); \n"
182 " print(args); \n"
183 "}; \n"
184 "console = new Console(); \n";
185
186 if (!global->parseScript(startupScript)) {
187 printf("Failed to parse startup script: %s.\n", FLAGS_infile[0]);
188 exit(1);
189 }
190
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +0000191 const char* script =
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000192 "function onDraw(canvas) { \n"
193 " canvas.fillStyle = '#00FF00'; \n"
194 " canvas.fillRect(20, 20, 100, 100); \n"
195 " canvas.inval(); \n"
196 "} \n";
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000197
bungeman38d909e2016-08-02 14:40:46 -0700198 sk_sp<SkData> data;
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000199 if (FLAGS_infile.count()) {
bungeman38d909e2016-08-02 14:40:46 -0700200 data = SkData::MakeFromFileName(FLAGS_infile[0]);
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000201 script = static_cast<const char*>(data->data());
202 }
203 if (NULL == script) {
204 printf("Could not load file: %s.\n", FLAGS_infile[0]);
205 exit(1);
206 }
jcgregorioe001da22014-10-29 05:33:27 -0700207 Path2DBuilder::AddToGlobal(global);
commit-bot@chromium.orgf679d1b2014-02-27 20:20:21 +0000208 Path2D::AddToGlobal(global);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000209
210 if (!global->parseScript(script)) {
commit-bot@chromium.org872796b2013-12-20 15:56:52 +0000211 printf("Failed to parse file: %s.\n", FLAGS_infile[0]);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000212 exit(1);
213 }
214
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000215
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000216 JsContext* jsContext = new JsContext(global);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000217
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000218 if (!jsContext->initialize()) {
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000219 printf("Failed to initialize.\n");
220 exit(1);
221 }
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000222 SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsContext);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000223 global->setWindow(win);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000224
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000225 return win;
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000226}