blob: 00fa27735126fed867ac215830d5b902006e6448 [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>
10
11using namespace v8;
12
13#include "SkV8Example.h"
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000014#include "Global.h"
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000015#include "JsContext.h"
commit-bot@chromium.orgf679d1b2014-02-27 20:20:21 +000016#include "Path2D.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();
43 SkGraphics::Term();
44}
45
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000046SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsContext* context)
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000047 : INHERITED(hwnd)
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +000048 , fJsContext(context)
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000049#if SK_SUPPORT_GPU
50 , fCurContext(NULL)
51 , fCurIntf(NULL)
52 , fCurRenderTarget(NULL)
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000053 , fCurSurface(NULL)
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000054#endif
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000055{
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000056 this->setColorType(kBGRA_8888_SkColorType);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000057 this->setVisibleP(true);
58 this->setClipToBounds(false);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000059
60#if SK_SUPPORT_GPU
61 this->windowSizeChanged();
62#endif
63}
64
commit-bot@chromium.org939560b2014-02-27 17:39:40 +000065SkV8ExampleWindow::~SkV8ExampleWindow() {
66#if SK_SUPPORT_GPU
67 SkSafeUnref(fCurContext);
68 SkSafeUnref(fCurIntf);
69 SkSafeUnref(fCurRenderTarget);
70 SkSafeUnref(fCurSurface);
71#endif
72}
73
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +000074#if SK_SUPPORT_GPU
75void SkV8ExampleWindow::windowSizeChanged() {
76 if (FLAGS_gpu) {
77 SkOSWindow::AttachmentInfo attachmentInfo;
78 bool result = this->attach(
79 SkOSWindow::kNativeGL_BackEndType, 0, &attachmentInfo);
80 if (!result) {
81 printf("Failed to attach.");
82 exit(1);
83 }
84
85 fCurIntf = GrGLCreateNativeInterface();
86 fCurContext = GrContext::Create(
87 kOpenGL_GrBackend, (GrBackendContext) fCurIntf);
88 if (NULL == fCurIntf || NULL == fCurContext) {
89 printf("Failed to initialize GL.");
90 exit(1);
91 }
92
93 GrBackendRenderTargetDesc desc;
94 desc.fWidth = SkScalarRoundToInt(this->width());
95 desc.fHeight = SkScalarRoundToInt(this->height());
96 desc.fConfig = kSkia8888_GrPixelConfig;
97 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
98 desc.fSampleCnt = attachmentInfo.fSampleCount;
99 desc.fStencilBits = attachmentInfo.fStencilBits;
100 GrGLint buffer;
101 GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
102 desc.fRenderTargetHandle = buffer;
103
104 SkSafeUnref(fCurRenderTarget);
105 fCurRenderTarget = fCurContext->wrapBackendRenderTarget(desc);
commit-bot@chromium.org939560b2014-02-27 17:39:40 +0000106 SkSafeUnref(fCurSurface);
107 fCurSurface = SkSurface::NewRenderTargetDirect(fCurRenderTarget);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000108 }
109}
110#endif
111
112#if SK_SUPPORT_GPU
113SkCanvas* SkV8ExampleWindow::createCanvas() {
114 if (FLAGS_gpu) {
commit-bot@chromium.org939560b2014-02-27 17:39:40 +0000115 SkCanvas* c = fCurSurface->getCanvas();
116 // Increase the ref count since the surface keeps a reference
117 // to the canvas, but callers of createCanvas put the results
118 // in a SkAutoTUnref.
119 c->ref();
120 return c;
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000121 } else {
122 return this->INHERITED::createCanvas();
123 }
124}
125#endif
126
127void SkV8ExampleWindow::onSizeChange() {
128 this->INHERITED::onSizeChange();
129
130#if SK_SUPPORT_GPU
131 this->windowSizeChanged();
132#endif
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000133}
134
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000135void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000136
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000137 canvas->save();
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +0000138 canvas->drawColor(SK_ColorWHITE);
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000139
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000140 // Now jump into JS and call the onDraw(canvas) method defined there.
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000141 fJsContext->onDraw(canvas);
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000142
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000143 canvas->restore();
144
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000145 this->INHERITED::onDraw(canvas);
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000146
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000147#if SK_SUPPORT_GPU
148 if (FLAGS_gpu) {
149 fCurContext->flush();
150 this->present();
151 }
152#endif
153}
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000154
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000155#ifdef SK_BUILD_FOR_WIN
156void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) {
157 RECT winRect;
158 winRect.top = rect.top();
159 winRect.bottom = rect.bottom();
160 winRect.right = rect.right();
161 winRect.left = rect.left();
162 InvalidateRect((HWND)this->getHWND(), &winRect, false);
163}
164#endif
165
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000166SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000167 printf("Started\n");
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000168
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000169 SkCommandLineFlags::Parse(argc, argv);
170
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000171 // Get the default Isolate created at startup.
172 Isolate* isolate = Isolate::GetCurrent();
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000173 Global* 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
198 SkAutoTUnref<SkData> data;
199 if (FLAGS_infile.count()) {
200 data.reset(SkData::NewFromFileName(FLAGS_infile[0]));
201 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 }
commit-bot@chromium.orgf679d1b2014-02-27 20:20:21 +0000207 Path2D::AddToGlobal(global);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000208
209 if (!global->parseScript(script)) {
commit-bot@chromium.org872796b2013-12-20 15:56:52 +0000210 printf("Failed to parse file: %s.\n", FLAGS_infile[0]);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000211 exit(1);
212 }
213
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000214
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000215 JsContext* jsContext = new JsContext(global);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000216
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000217 if (!jsContext->initialize()) {
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000218 printf("Failed to initialize.\n");
219 exit(1);
220 }
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000221 SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsContext);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000222 global->setWindow(win);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000223
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000224 return win;
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000225}