• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #include <include/libplatform/libplatform.h>
11 
12 #include "SkV8Example.h"
13 #include "Global.h"
14 #include "JsContext.h"
15 #include "Path2D.h"
16 #include "Path2DBuilder.h"
17 
18 #include "gl/GrGLUtil.h"
19 #include "gl/GrGLDefines.h"
20 #include "gl/GrGLInterface.h"
21 #include "GrRenderTarget.h"
22 #include "GrContext.h"
23 #include "SkApplication.h"
24 #include "SkCommandLineFlags.h"
25 #include "SkData.h"
26 #include "SkDraw.h"
27 #include "SkGpuDevice.h"
28 #include "SkGraphics.h"
29 #include "SkScalar.h"
30 #include "SkSurface.h"
31 
32 
33 DEFINE_string2(infile, i, NULL, "Name of file to load JS from.\n");
34 DEFINE_bool(gpu, true, "Use the GPU for rendering.");
35 
application_init()36 void application_init() {
37     SkGraphics::Init();
38     SkEvent::Init();
39 }
40 
application_term()41 void application_term() {
42     SkEvent::Term();
43     SkGraphics::Term();
44 }
45 
SkV8ExampleWindow(void * hwnd,JsContext * context)46 SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsContext* context)
47     : INHERITED(hwnd)
48     , fJsContext(context)
49 #if SK_SUPPORT_GPU
50     , fCurContext(NULL)
51     , fCurIntf(NULL)
52     , fCurRenderTarget(NULL)
53     , fCurSurface(NULL)
54 #endif
55 {
56     this->setColorType(kBGRA_8888_SkColorType);
57     this->setVisibleP(true);
58     this->setClipToBounds(false);
59 
60 #if SK_SUPPORT_GPU
61     this->windowSizeChanged();
62 #endif
63 }
64 
~SkV8ExampleWindow()65 SkV8ExampleWindow::~SkV8ExampleWindow() {
66 #if SK_SUPPORT_GPU
67     SkSafeUnref(fCurContext);
68     SkSafeUnref(fCurIntf);
69     SkSafeUnref(fCurRenderTarget);
70     SkSafeUnref(fCurSurface);
71 #endif
72 }
73 
74 #if SK_SUPPORT_GPU
windowSizeChanged()75 void 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);
106         SkSafeUnref(fCurSurface);
107         fCurSurface = SkSurface::NewRenderTargetDirect(fCurRenderTarget);
108     }
109 }
110 #endif
111 
112 #if SK_SUPPORT_GPU
createSurface()113 SkSurface* SkV8ExampleWindow::createSurface() {
114     if (FLAGS_gpu) {
115         // Increase the ref count since callers of createSurface put the
116         // results in a SkAutoTUnref.
117         fCurSurface->ref();
118         return fCurSurface;
119     } else {
120         return this->INHERITED::createSurface();
121     }
122 }
123 #endif
124 
onSizeChange()125 void SkV8ExampleWindow::onSizeChange() {
126     this->INHERITED::onSizeChange();
127 
128 #if SK_SUPPORT_GPU
129     this->windowSizeChanged();
130 #endif
131 }
132 
133 Global* global = NULL;
134 
onDraw(SkCanvas * canvas)135 void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
136 
137     canvas->save();
138     canvas->drawColor(SK_ColorWHITE);
139 
140     // Now jump into JS and call the onDraw(canvas) method defined there.
141     fJsContext->onDraw(canvas);
142 
143     canvas->restore();
144 
145     this->INHERITED::onDraw(canvas);
146 
147 #if SK_SUPPORT_GPU
148     if (FLAGS_gpu) {
149         fCurContext->flush();
150         this->present();
151     }
152 #endif
153 }
154 
155 #ifdef SK_BUILD_FOR_WIN
onHandleInval(const SkIRect & rect)156 void 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 
166 
create_sk_window(void * hwnd,int argc,char ** argv)167 SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
168     printf("Started\n");
169 
170     v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
171     SkCommandLineFlags::Parse(argc, argv);
172 
173     v8::V8::InitializeICU();
174     v8::Platform* platform = v8::platform::CreateDefaultPlatform();
175     v8::V8::InitializePlatform(platform);
176     v8::V8::Initialize();
177 
178     v8::Isolate* isolate = v8::Isolate::New();
179     v8::Isolate::Scope isolate_scope(isolate);
180     v8::HandleScope handle_scope(isolate);
181     isolate->Enter();
182 
183     global = new Global(isolate);
184 
185 
186     // Set up things to look like a browser by creating
187     // a console object that invokes our print function.
188     const char* startupScript =
189             "function Console() {};                   \n"
190             "Console.prototype.log = function() {     \n"
191             "  var args = Array.prototype.slice.call(arguments).join(' '); \n"
192             "  print(args);                      \n"
193             "};                                       \n"
194             "console = new Console();                 \n";
195 
196     if (!global->parseScript(startupScript)) {
197         printf("Failed to parse startup script: %s.\n", FLAGS_infile[0]);
198         exit(1);
199     }
200 
201     const char* script =
202             "function onDraw(canvas) {              \n"
203             "    canvas.fillStyle = '#00FF00';      \n"
204             "    canvas.fillRect(20, 20, 100, 100); \n"
205             "    canvas.inval();                    \n"
206             "}                                      \n";
207 
208     SkAutoTUnref<SkData> data;
209     if (FLAGS_infile.count()) {
210         data.reset(SkData::NewFromFileName(FLAGS_infile[0]));
211         script = static_cast<const char*>(data->data());
212     }
213     if (NULL == script) {
214         printf("Could not load file: %s.\n", FLAGS_infile[0]);
215         exit(1);
216     }
217     Path2DBuilder::AddToGlobal(global);
218     Path2D::AddToGlobal(global);
219 
220     if (!global->parseScript(script)) {
221         printf("Failed to parse file: %s.\n", FLAGS_infile[0]);
222         exit(1);
223     }
224 
225 
226     JsContext* jsContext = new JsContext(global);
227 
228     if (!jsContext->initialize()) {
229         printf("Failed to initialize.\n");
230         exit(1);
231     }
232     SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsContext);
233     global->setWindow(win);
234 
235     return win;
236 }
237