blob: bb448bad15174d5aa8bffdf3377c760219ad707e [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)
51 , fCurRenderTarget(NULL)
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000052 , fCurSurface(NULL)
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000053#endif
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000054{
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000055 this->setVisibleP(true);
56 this->setClipToBounds(false);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000057
58#if SK_SUPPORT_GPU
59 this->windowSizeChanged();
60#endif
61}
62
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000063SkV8ExampleWindow::~SkV8ExampleWindow() {
64#if SK_SUPPORT_GPU
65 SkSafeUnref(fCurContext);
66 SkSafeUnref(fCurIntf);
67 SkSafeUnref(fCurRenderTarget);
68 SkSafeUnref(fCurSurface);
69#endif
70}
71
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000072#if SK_SUPPORT_GPU
73void SkV8ExampleWindow::windowSizeChanged() {
74 if (FLAGS_gpu) {
75 SkOSWindow::AttachmentInfo attachmentInfo;
76 bool result = this->attach(
brianosman2d1ee792016-05-05 12:24:31 -070077 SkOSWindow::kNativeGL_BackEndType, 0, false, &attachmentInfo);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000078 if (!result) {
79 printf("Failed to attach.");
80 exit(1);
81 }
82
83 fCurIntf = GrGLCreateNativeInterface();
84 fCurContext = GrContext::Create(
85 kOpenGL_GrBackend, (GrBackendContext) fCurIntf);
86 if (NULL == fCurIntf || NULL == fCurContext) {
87 printf("Failed to initialize GL.");
88 exit(1);
89 }
90
91 GrBackendRenderTargetDesc desc;
92 desc.fWidth = SkScalarRoundToInt(this->width());
93 desc.fHeight = SkScalarRoundToInt(this->height());
94 desc.fConfig = kSkia8888_GrPixelConfig;
95 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
96 desc.fSampleCnt = attachmentInfo.fSampleCount;
97 desc.fStencilBits = attachmentInfo.fStencilBits;
98 GrGLint buffer;
99 GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
100 desc.fRenderTargetHandle = buffer;
101
102 SkSafeUnref(fCurRenderTarget);
103 fCurRenderTarget = fCurContext->wrapBackendRenderTarget(desc);
commit-bot@chromium.org939560b2014-02-27 17:39:40 +0000104 SkSafeUnref(fCurSurface);
105 fCurSurface = SkSurface::NewRenderTargetDirect(fCurRenderTarget);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000106 }
107}
108#endif
109
110#if SK_SUPPORT_GPU
jcgregorioe22f45f2014-10-24 12:49:17 -0700111SkSurface* SkV8ExampleWindow::createSurface() {
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000112 if (FLAGS_gpu) {
jcgregorioe22f45f2014-10-24 12:49:17 -0700113 // Increase the ref count since callers of createSurface put the
114 // results in a SkAutoTUnref.
115 fCurSurface->ref();
116 return fCurSurface;
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000117 } else {
jcgregorioe22f45f2014-10-24 12:49:17 -0700118 return this->INHERITED::createSurface();
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000119 }
120}
121#endif
122
123void SkV8ExampleWindow::onSizeChange() {
124 this->INHERITED::onSizeChange();
125
126#if SK_SUPPORT_GPU
127 this->windowSizeChanged();
128#endif
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000129}
130
jcgregorioe001da22014-10-29 05:33:27 -0700131Global* global = NULL;
132
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000133void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000134
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000135 canvas->save();
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +0000136 canvas->drawColor(SK_ColorWHITE);
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000137
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000138 // Now jump into JS and call the onDraw(canvas) method defined there.
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000139 fJsContext->onDraw(canvas);
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000140
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000141 canvas->restore();
142
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000143 this->INHERITED::onDraw(canvas);
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000144
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000145#if SK_SUPPORT_GPU
146 if (FLAGS_gpu) {
147 fCurContext->flush();
148 this->present();
149 }
150#endif
151}
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000152
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000153#ifdef SK_BUILD_FOR_WIN
154void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) {
155 RECT winRect;
156 winRect.top = rect.top();
157 winRect.bottom = rect.bottom();
158 winRect.right = rect.right();
159 winRect.left = rect.left();
160 InvalidateRect((HWND)this->getHWND(), &winRect, false);
161}
162#endif
163
jcgregorioe22f45f2014-10-24 12:49:17 -0700164
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000165SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000166 printf("Started\n");
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000167
jcgregorioe001da22014-10-29 05:33:27 -0700168 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000169 SkCommandLineFlags::Parse(argc, argv);
170
jcgregorioe22f45f2014-10-24 12:49:17 -0700171 v8::V8::InitializeICU();
172 v8::Platform* platform = v8::platform::CreateDefaultPlatform();
173 v8::V8::InitializePlatform(platform);
174 v8::V8::Initialize();
175
176 v8::Isolate* isolate = v8::Isolate::New();
177 v8::Isolate::Scope isolate_scope(isolate);
178 v8::HandleScope handle_scope(isolate);
179 isolate->Enter();
180
jcgregorioe001da22014-10-29 05:33:27 -0700181 global = new Global(isolate);
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000182
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000183
184 // Set up things to look like a browser by creating
185 // a console object that invokes our print function.
186 const char* startupScript =
187 "function Console() {}; \n"
188 "Console.prototype.log = function() { \n"
189 " var args = Array.prototype.slice.call(arguments).join(' '); \n"
190 " print(args); \n"
191 "}; \n"
192 "console = new Console(); \n";
193
194 if (!global->parseScript(startupScript)) {
195 printf("Failed to parse startup script: %s.\n", FLAGS_infile[0]);
196 exit(1);
197 }
198
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +0000199 const char* script =
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000200 "function onDraw(canvas) { \n"
201 " canvas.fillStyle = '#00FF00'; \n"
202 " canvas.fillRect(20, 20, 100, 100); \n"
203 " canvas.inval(); \n"
204 "} \n";
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000205
206 SkAutoTUnref<SkData> data;
207 if (FLAGS_infile.count()) {
208 data.reset(SkData::NewFromFileName(FLAGS_infile[0]));
209 script = static_cast<const char*>(data->data());
210 }
211 if (NULL == script) {
212 printf("Could not load file: %s.\n", FLAGS_infile[0]);
213 exit(1);
214 }
jcgregorioe001da22014-10-29 05:33:27 -0700215 Path2DBuilder::AddToGlobal(global);
commit-bot@chromium.orgf679d1b2014-02-27 20:20:21 +0000216 Path2D::AddToGlobal(global);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000217
218 if (!global->parseScript(script)) {
commit-bot@chromium.org872796b2013-12-20 15:56:52 +0000219 printf("Failed to parse file: %s.\n", FLAGS_infile[0]);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000220 exit(1);
221 }
222
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000223
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000224 JsContext* jsContext = new JsContext(global);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000225
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000226 if (!jsContext->initialize()) {
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000227 printf("Failed to initialize.\n");
228 exit(1);
229 }
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000230 SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsContext);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000231 global->setWindow(win);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000232
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000233 return win;
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000234}