blob: ca63e071b77f768a569df37a5e53b8a5f912ccef [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>
Brian Salomon807371c2017-07-20 20:48:12 +000011
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000012#include "SkV8Example.h"
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000013#include "Global.h"
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000014#include "JsContext.h"
commit-bot@chromium.orgf679d1b2014-02-27 20:20:21 +000015#include "Path2D.h"
jcgregorioe001da22014-10-29 05:33:27 -070016#include "Path2DBuilder.h"
Brian Salomon807371c2017-07-20 20:48:12 +000017
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000018#include "gl/GrGLUtil.h"
19#include "gl/GrGLDefines.h"
20#include "gl/GrGLInterface.h"
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000021#include "GrContext.h"
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000022#include "SkApplication.h"
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +000023#include "SkCommandLineFlags.h"
24#include "SkData.h"
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000025#include "SkDraw.h"
26#include "SkGpuDevice.h"
27#include "SkGraphics.h"
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +000028#include "SkScalar.h"
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000029#include "SkSurface.h"
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000030
31
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +000032DEFINE_string2(infile, i, NULL, "Name of file to load JS from.\n");
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000033DEFINE_bool(gpu, true, "Use the GPU for rendering.");
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +000034
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000035void application_init() {
36 SkGraphics::Init();
37 SkEvent::Init();
38}
39
40void application_term() {
41 SkEvent::Term();
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000042}
43
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000044SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsContext* context)
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000045 : INHERITED(hwnd)
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000046 , fJsContext(context)
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000047#if SK_SUPPORT_GPU
48 , fCurContext(NULL)
49 , fCurIntf(NULL)
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000050 , fCurSurface(NULL)
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000051#endif
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000052{
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000053 this->setVisibleP(true);
54 this->setClipToBounds(false);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000055
56#if SK_SUPPORT_GPU
57 this->windowSizeChanged();
58#endif
59}
60
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000061SkV8ExampleWindow::~SkV8ExampleWindow() {
62#if SK_SUPPORT_GPU
63 SkSafeUnref(fCurContext);
64 SkSafeUnref(fCurIntf);
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000065 SkSafeUnref(fCurSurface);
66#endif
67}
68
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000069#if SK_SUPPORT_GPU
70void SkV8ExampleWindow::windowSizeChanged() {
71 if (FLAGS_gpu) {
72 SkOSWindow::AttachmentInfo attachmentInfo;
73 bool result = this->attach(
brianosman2d1ee792016-05-05 12:24:31 -070074 SkOSWindow::kNativeGL_BackEndType, 0, false, &attachmentInfo);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000075 if (!result) {
76 printf("Failed to attach.");
77 exit(1);
78 }
79
80 fCurIntf = GrGLCreateNativeInterface();
Greg Daniel02611d92017-07-25 10:05:01 -040081 fCurContext = GrContext::MakeGL(fCurIntf).release();
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000082 if (NULL == fCurIntf || NULL == fCurContext) {
83 printf("Failed to initialize GL.");
84 exit(1);
85 }
86
Brian Salomon807371c2017-07-20 20:48:12 +000087 GrBackendRenderTargetDesc desc;
88 desc.fWidth = SkScalarRoundToInt(this->width());
89 desc.fHeight = SkScalarRoundToInt(this->height());
90 desc.fConfig = kSkia8888_GrPixelConfig;
91 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
92 desc.fSampleCnt = attachmentInfo.fSampleCount;
93 desc.fStencilBits = attachmentInfo.fStencilBits;
94 GrGLint buffer;
95 GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
96 desc.fRenderTargetHandle = buffer;
97
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000098 SkSafeUnref(fCurSurface);
Brian Salomon807371c2017-07-20 20:48:12 +000099 fCurSurface = SkSurface::MakeFromBackendRenderTarget(fCurContext, desc,
robertphillips12e96622016-08-01 05:53:23 -0700100 nullptr, nullptr).release();
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000101 }
102}
103#endif
104
105#if SK_SUPPORT_GPU
jcgregorioe22f45f2014-10-24 12:49:17 -0700106SkSurface* SkV8ExampleWindow::createSurface() {
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000107 if (FLAGS_gpu) {
jcgregorioe22f45f2014-10-24 12:49:17 -0700108 // Increase the ref count since callers of createSurface put the
Hal Canary2db83612016-11-04 13:02:54 -0400109 // results in a sk_sp.
jcgregorioe22f45f2014-10-24 12:49:17 -0700110 fCurSurface->ref();
111 return fCurSurface;
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000112 } else {
jcgregorioe22f45f2014-10-24 12:49:17 -0700113 return this->INHERITED::createSurface();
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000114 }
115}
116#endif
117
118void SkV8ExampleWindow::onSizeChange() {
119 this->INHERITED::onSizeChange();
120
121#if SK_SUPPORT_GPU
122 this->windowSizeChanged();
123#endif
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000124}
125
jcgregorioe001da22014-10-29 05:33:27 -0700126Global* global = NULL;
127
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000128void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000129
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000130 canvas->save();
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +0000131 canvas->drawColor(SK_ColorWHITE);
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000132
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000133 // Now jump into JS and call the onDraw(canvas) method defined there.
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000134 fJsContext->onDraw(canvas);
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000135
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000136 canvas->restore();
137
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000138 this->INHERITED::onDraw(canvas);
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000139
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000140#if SK_SUPPORT_GPU
141 if (FLAGS_gpu) {
142 fCurContext->flush();
143 this->present();
144 }
145#endif
146}
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000147
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000148#ifdef SK_BUILD_FOR_WIN
149void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) {
150 RECT winRect;
151 winRect.top = rect.top();
152 winRect.bottom = rect.bottom();
153 winRect.right = rect.right();
154 winRect.left = rect.left();
155 InvalidateRect((HWND)this->getHWND(), &winRect, false);
156}
157#endif
158
jcgregorioe22f45f2014-10-24 12:49:17 -0700159
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000160SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000161 printf("Started\n");
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000162
jcgregorioe001da22014-10-29 05:33:27 -0700163 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000164 SkCommandLineFlags::Parse(argc, argv);
165
jcgregorioe22f45f2014-10-24 12:49:17 -0700166 v8::V8::InitializeICU();
167 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
168 v8::V8::InitializePlatform(platform);
169 v8::V8::Initialize();
170
171 v8::Isolate* isolate = v8::Isolate::New();
172 v8::Isolate::Scope isolate_scope(isolate);
173 v8::HandleScope handle_scope(isolate);
174 isolate->Enter();
175
jcgregorioe001da22014-10-29 05:33:27 -0700176 global = new Global(isolate);
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000177
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000178
179 // Set up things to look like a browser by creating
180 // a console object that invokes our print function.
181 const char* startupScript =
182 "function Console() {}; \n"
183 "Console.prototype.log = function() { \n"
184 " var args = Array.prototype.slice.call(arguments).join(' '); \n"
185 " print(args); \n"
186 "}; \n"
187 "console = new Console(); \n";
188
189 if (!global->parseScript(startupScript)) {
190 printf("Failed to parse startup script: %s.\n", FLAGS_infile[0]);
191 exit(1);
192 }
193
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +0000194 const char* script =
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000195 "function onDraw(canvas) { \n"
196 " canvas.fillStyle = '#00FF00'; \n"
197 " canvas.fillRect(20, 20, 100, 100); \n"
198 " canvas.inval(); \n"
199 "} \n";
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000200
bungeman38d909e2016-08-02 14:40:46 -0700201 sk_sp<SkData> data;
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000202 if (FLAGS_infile.count()) {
bungeman38d909e2016-08-02 14:40:46 -0700203 data = SkData::MakeFromFileName(FLAGS_infile[0]);
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000204 script = static_cast<const char*>(data->data());
205 }
206 if (NULL == script) {
207 printf("Could not load file: %s.\n", FLAGS_infile[0]);
208 exit(1);
209 }
jcgregorioe001da22014-10-29 05:33:27 -0700210 Path2DBuilder::AddToGlobal(global);
commit-bot@chromium.orgf679d1b2014-02-27 20:20:21 +0000211 Path2D::AddToGlobal(global);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000212
213 if (!global->parseScript(script)) {
commit-bot@chromium.org872796b2013-12-20 15:56:52 +0000214 printf("Failed to parse file: %s.\n", FLAGS_infile[0]);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000215 exit(1);
216 }
217
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000218
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000219 JsContext* jsContext = new JsContext(global);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000220
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000221 if (!jsContext->initialize()) {
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000222 printf("Failed to initialize.\n");
223 exit(1);
224 }
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000225 SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsContext);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000226 global->setWindow(win);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000227
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000228 return win;
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000229}