blob: eb0e7133769eec94198d58352b8b36509a0968d1 [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"
16#include "Path.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.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();
42 SkGraphics::Term();
43}
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)
52#endif
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +000053{
54 this->setConfig(SkBitmap::kARGB_8888_Config);
55 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
63#if SK_SUPPORT_GPU
64void SkV8ExampleWindow::windowSizeChanged() {
65 if (FLAGS_gpu) {
66 SkOSWindow::AttachmentInfo attachmentInfo;
67 bool result = this->attach(
68 SkOSWindow::kNativeGL_BackEndType, 0, &attachmentInfo);
69 if (!result) {
70 printf("Failed to attach.");
71 exit(1);
72 }
73
74 fCurIntf = GrGLCreateNativeInterface();
75 fCurContext = GrContext::Create(
76 kOpenGL_GrBackend, (GrBackendContext) fCurIntf);
77 if (NULL == fCurIntf || NULL == fCurContext) {
78 printf("Failed to initialize GL.");
79 exit(1);
80 }
81
82 GrBackendRenderTargetDesc desc;
83 desc.fWidth = SkScalarRoundToInt(this->width());
84 desc.fHeight = SkScalarRoundToInt(this->height());
85 desc.fConfig = kSkia8888_GrPixelConfig;
86 desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
87 desc.fSampleCnt = attachmentInfo.fSampleCount;
88 desc.fStencilBits = attachmentInfo.fStencilBits;
89 GrGLint buffer;
90 GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &buffer);
91 desc.fRenderTargetHandle = buffer;
92
93 SkSafeUnref(fCurRenderTarget);
94 fCurRenderTarget = fCurContext->wrapBackendRenderTarget(desc);
95 }
96}
97#endif
98
99#if SK_SUPPORT_GPU
100SkCanvas* SkV8ExampleWindow::createCanvas() {
101 if (FLAGS_gpu) {
102 SkAutoTUnref<SkBaseDevice> device(
103 new SkGpuDevice(fCurContext, fCurRenderTarget));
104 return new SkCanvas(device);
105 } else {
106 return this->INHERITED::createCanvas();
107 }
108}
109#endif
110
111void SkV8ExampleWindow::onSizeChange() {
112 this->INHERITED::onSizeChange();
113
114#if SK_SUPPORT_GPU
115 this->windowSizeChanged();
116#endif
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000117}
118
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000119void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000120
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000121 canvas->save();
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +0000122 canvas->drawColor(SK_ColorWHITE);
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000123
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000124 // Now jump into JS and call the onDraw(canvas) method defined there.
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000125 fJsContext->onDraw(canvas);
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000126
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000127 canvas->restore();
128
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000129 this->INHERITED::onDraw(canvas);
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000130
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000131#if SK_SUPPORT_GPU
132 if (FLAGS_gpu) {
133 fCurContext->flush();
134 this->present();
135 }
136#endif
137}
commit-bot@chromium.org80bd0c92013-12-06 15:24:52 +0000138
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000139#ifdef SK_BUILD_FOR_WIN
140void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) {
141 RECT winRect;
142 winRect.top = rect.top();
143 winRect.bottom = rect.bottom();
144 winRect.right = rect.right();
145 winRect.left = rect.left();
146 InvalidateRect((HWND)this->getHWND(), &winRect, false);
147}
148#endif
149
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000150SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000151 printf("Started\n");
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000152
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000153 SkCommandLineFlags::Parse(argc, argv);
154
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000155 // Get the default Isolate created at startup.
156 Isolate* isolate = Isolate::GetCurrent();
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000157 Global* global = new Global(isolate);
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000158
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000159
160 // Set up things to look like a browser by creating
161 // a console object that invokes our print function.
162 const char* startupScript =
163 "function Console() {}; \n"
164 "Console.prototype.log = function() { \n"
165 " var args = Array.prototype.slice.call(arguments).join(' '); \n"
166 " print(args); \n"
167 "}; \n"
168 "console = new Console(); \n";
169
170 if (!global->parseScript(startupScript)) {
171 printf("Failed to parse startup script: %s.\n", FLAGS_infile[0]);
172 exit(1);
173 }
174
commit-bot@chromium.org3a6143d2013-12-11 17:34:58 +0000175 const char* script =
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000176 "function onDraw(canvas) { \n"
177 " canvas.fillStyle = '#00FF00'; \n"
178 " canvas.fillRect(20, 20, 100, 100); \n"
179 " canvas.inval(); \n"
180 "} \n";
commit-bot@chromium.org48d94b8c2013-12-16 18:24:51 +0000181
182 SkAutoTUnref<SkData> data;
183 if (FLAGS_infile.count()) {
184 data.reset(SkData::NewFromFileName(FLAGS_infile[0]));
185 script = static_cast<const char*>(data->data());
186 }
187 if (NULL == script) {
188 printf("Could not load file: %s.\n", FLAGS_infile[0]);
189 exit(1);
190 }
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000191 Path::AddToGlobal(global);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000192
193 if (!global->parseScript(script)) {
commit-bot@chromium.org872796b2013-12-20 15:56:52 +0000194 printf("Failed to parse file: %s.\n", FLAGS_infile[0]);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000195 exit(1);
196 }
197
commit-bot@chromium.orgcd110182014-01-10 21:33:01 +0000198
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000199 JsContext* jsContext = new JsContext(global);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000200
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000201 if (!jsContext->initialize()) {
commit-bot@chromium.org9bde13c2013-12-10 14:13:03 +0000202 printf("Failed to initialize.\n");
203 exit(1);
204 }
commit-bot@chromium.orgc8d73282014-01-06 18:17:24 +0000205 SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsContext);
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000206 global->setWindow(win);
commit-bot@chromium.org9ee0e322014-01-06 20:03:55 +0000207
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +0000208 return win;
commit-bot@chromium.org44a38772013-12-05 13:45:19 +0000209}