blob: 5bfd64d6e26ae75f0bebd8a50ee605e8264c7068 [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
12#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"
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000017
18#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 "GrRenderTarget.h"
22#include "GrContext.h"
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000023#include "SkApplication.h"
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +000024#include "SkCommandLineFlags.h"
25#include "SkData.h"
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000026#include "SkDraw.h"
27#include "SkGpuDevice.h"
28#include "SkGraphics.h"
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +000029#include "SkScalar.h"
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000030#include "SkSurface.h"
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000031
32
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +000033DEFINE_string2(infile, i, NULL, "Name of file to load JS from.\n");
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000034DEFINE_bool(gpu, true, "Use the GPU for rendering.");
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +000035
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000036void application_init() {
37 SkGraphics::Init();
38 SkEvent::Init();
39}
40
41void application_term() {
42 SkEvent::Term();
commit-bot@chromium.org44a38772013-12-05 13:45:19 +000043}
44
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000045SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsContext* context)
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000046 : INHERITED(hwnd)
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000047 , fJsContext(context)
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000048#if SK_SUPPORT_GPU
49 , fCurContext(NULL)
50 , fCurIntf(NULL)
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000051 , fCurSurface(NULL)
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000052#endif
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000053{
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000054 this->setVisibleP(true);
55 this->setClipToBounds(false);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000056
57#if SK_SUPPORT_GPU
58 this->windowSizeChanged();
59#endif
60}
61
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000062SkV8ExampleWindow::~SkV8ExampleWindow() {
63#if SK_SUPPORT_GPU
64 SkSafeUnref(fCurContext);
65 SkSafeUnref(fCurIntf);
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000066 SkSafeUnref(fCurSurface);
67#endif
68}
69
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000070#if SK_SUPPORT_GPU
71void SkV8ExampleWindow::windowSizeChanged() {
72 if (FLAGS_gpu) {
73 SkOSWindow::AttachmentInfo attachmentInfo;
74 bool result = this->attach(
brianosman2d1ee792016-05-05 12:24:31 -070075 SkOSWindow::kNativeGL_BackEndType, 0, false, &attachmentInfo);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000076 if (!result) {
77 printf("Failed to attach.");
78 exit(1);
79 }
80
81 fCurIntf = GrGLCreateNativeInterface();
82 fCurContext = GrContext::Create(
83 kOpenGL_GrBackend, (GrBackendContext) fCurIntf);
84 if (NULL == fCurIntf || NULL == fCurContext) {
85 printf("Failed to initialize GL.");
86 exit(1);
87 }
88
89 GrBackendRenderTargetDesc desc;
90 desc.fWidth = SkScalarRoundToInt(this->width());
91 desc.fHeight = SkScalarRoundToInt(this->height());
92 desc.fConfig = kSkia8888_GrPixelConfig;
93 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
94 desc.fSampleCnt = attachmentInfo.fSampleCount;
95 desc.fStencilBits = attachmentInfo.fStencilBits;
96 GrGLint buffer;
97 GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
98 desc.fRenderTargetHandle = buffer;
99
commit-bot@chromium.org939560b2014-02-27 17:39:40 +0000100 SkSafeUnref(fCurSurface);
robertphillips12e96622016-08-01 05:53:23 -0700101 fCurSurface = SkSurface::MakeFromBackendRenderTarget(fCurContext, desc,
102 nullptr, nullptr).release();
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000103 }
104}
105#endif
106
107#if SK_SUPPORT_GPU
jcgregorioe22f45f2014-10-24 12:49:17 -0700108SkSurface* SkV8ExampleWindow::createSurface() {
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000109 if (FLAGS_gpu) {
jcgregorioe22f45f2014-10-24 12:49:17 -0700110 // Increase the ref count since callers of createSurface put the
111 // results in a SkAutoTUnref.
112 fCurSurface->ref();
113 return fCurSurface;
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000114 } else {
jcgregorioe22f45f2014-10-24 12:49:17 -0700115 return this->INHERITED::createSurface();
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000116 }
117}
118#endif
119
120void SkV8ExampleWindow::onSizeChange() {
121 this->INHERITED::onSizeChange();
122
123#if SK_SUPPORT_GPU
124 this->windowSizeChanged();
125#endif
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000126}
127
jcgregorioe001da22014-10-29 05:33:27 -0700128Global* global = NULL;
129
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000130void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000131
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000132 canvas->save();
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +0000133 canvas->drawColor(SK_ColorWHITE);
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000134
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000135 // Now jump into JS and call the onDraw(canvas) method defined there.
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000136 fJsContext->onDraw(canvas);
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000137
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000138 canvas->restore();
139
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000140 this->INHERITED::onDraw(canvas);
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000141
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000142#if SK_SUPPORT_GPU
143 if (FLAGS_gpu) {
144 fCurContext->flush();
145 this->present();
146 }
147#endif
148}
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000149
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000150#ifdef SK_BUILD_FOR_WIN
151void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) {
152 RECT winRect;
153 winRect.top = rect.top();
154 winRect.bottom = rect.bottom();
155 winRect.right = rect.right();
156 winRect.left = rect.left();
157 InvalidateRect((HWND)this->getHWND(), &winRect, false);
158}
159#endif
160
jcgregorioe22f45f2014-10-24 12:49:17 -0700161
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000162SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000163 printf("Started\n");
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000164
jcgregorioe001da22014-10-29 05:33:27 -0700165 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000166 SkCommandLineFlags::Parse(argc, argv);
167
jcgregorioe22f45f2014-10-24 12:49:17 -0700168 v8::V8::InitializeICU();
169 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
170 v8::V8::InitializePlatform(platform);
171 v8::V8::Initialize();
172
173 v8::Isolate* isolate = v8::Isolate::New();
174 v8::Isolate::Scope isolate_scope(isolate);
175 v8::HandleScope handle_scope(isolate);
176 isolate->Enter();
177
jcgregorioe001da22014-10-29 05:33:27 -0700178 global = new Global(isolate);
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000179
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000180
181 // Set up things to look like a browser by creating
182 // a console object that invokes our print function.
183 const char* startupScript =
184 "function Console() {}; \n"
185 "Console.prototype.log = function() { \n"
186 " var args = Array.prototype.slice.call(arguments).join(' '); \n"
187 " print(args); \n"
188 "}; \n"
189 "console = new Console(); \n";
190
191 if (!global->parseScript(startupScript)) {
192 printf("Failed to parse startup script: %s.\n", FLAGS_infile[0]);
193 exit(1);
194 }
195
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +0000196 const char* script =
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000197 "function onDraw(canvas) { \n"
198 " canvas.fillStyle = '#00FF00'; \n"
199 " canvas.fillRect(20, 20, 100, 100); \n"
200 " canvas.inval(); \n"
201 "} \n";
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000202
bungeman38d909e2016-08-02 14:40:46 -0700203 sk_sp<SkData> data;
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000204 if (FLAGS_infile.count()) {
bungeman38d909e2016-08-02 14:40:46 -0700205 data = SkData::MakeFromFileName(FLAGS_infile[0]);
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000206 script = static_cast<const char*>(data->data());
207 }
208 if (NULL == script) {
209 printf("Could not load file: %s.\n", FLAGS_infile[0]);
210 exit(1);
211 }
jcgregorioe001da22014-10-29 05:33:27 -0700212 Path2DBuilder::AddToGlobal(global);
commit-bot@chromium.orgf679d1b2014-02-27 20:20:21 +0000213 Path2D::AddToGlobal(global);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000214
215 if (!global->parseScript(script)) {
commit-bot@chromium.org872796b2013-12-20 15:56:52 +0000216 printf("Failed to parse file: %s.\n", FLAGS_infile[0]);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000217 exit(1);
218 }
219
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000220
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000221 JsContext* jsContext = new JsContext(global);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000222
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000223 if (!jsContext->initialize()) {
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000224 printf("Failed to initialize.\n");
225 exit(1);
226 }
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000227 SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsContext);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000228 global->setWindow(win);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000229
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000230 return win;
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000231}