commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 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> |
jcgregorio | e22f45f | 2014-10-24 12:49:17 -0700 | [diff] [blame] | 10 | #include <include/libplatform/libplatform.h> |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 11 | #include "SkV8Example.h" |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 12 | #include "Global.h" |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 13 | #include "JsContext.h" |
commit-bot@chromium.org | f679d1b | 2014-02-27 20:20:21 +0000 | [diff] [blame] | 14 | #include "Path2D.h" |
jcgregorio | e001da2 | 2014-10-29 05:33:27 -0700 | [diff] [blame] | 15 | #include "Path2DBuilder.h" |
Brian Salomon | f43d000 | 2017-08-31 20:04:00 +0000 | [diff] [blame] | 16 | #include "GrBackendSurface.h" |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 17 | #include "gl/GrGLUtil.h" |
| 18 | #include "gl/GrGLDefines.h" |
| 19 | #include "gl/GrGLInterface.h" |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 20 | #include "GrContext.h" |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 21 | #include "SkApplication.h" |
commit-bot@chromium.org | 48d94b8c | 2013-12-16 18:24:51 +0000 | [diff] [blame] | 22 | #include "SkCommandLineFlags.h" |
| 23 | #include "SkData.h" |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 24 | #include "SkDraw.h" |
| 25 | #include "SkGpuDevice.h" |
| 26 | #include "SkGraphics.h" |
commit-bot@chromium.org | 3a6143d | 2013-12-11 17:34:58 +0000 | [diff] [blame] | 27 | #include "SkScalar.h" |
commit-bot@chromium.org | 939560b | 2014-02-27 17:39:40 +0000 | [diff] [blame] | 28 | #include "SkSurface.h" |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 29 | |
| 30 | |
commit-bot@chromium.org | 48d94b8c | 2013-12-16 18:24:51 +0000 | [diff] [blame] | 31 | DEFINE_string2(infile, i, NULL, "Name of file to load JS from.\n"); |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 32 | DEFINE_bool(gpu, true, "Use the GPU for rendering."); |
commit-bot@chromium.org | 48d94b8c | 2013-12-16 18:24:51 +0000 | [diff] [blame] | 33 | |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 34 | void application_init() { |
| 35 | SkGraphics::Init(); |
| 36 | SkEvent::Init(); |
| 37 | } |
| 38 | |
| 39 | void application_term() { |
| 40 | SkEvent::Term(); |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 41 | } |
| 42 | |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 43 | SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsContext* context) |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 44 | : INHERITED(hwnd) |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 45 | , fJsContext(context) |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 46 | #if SK_SUPPORT_GPU |
| 47 | , fCurContext(NULL) |
| 48 | , fCurIntf(NULL) |
commit-bot@chromium.org | 939560b | 2014-02-27 17:39:40 +0000 | [diff] [blame] | 49 | , fCurSurface(NULL) |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 50 | #endif |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 51 | { |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 52 | this->setVisibleP(true); |
| 53 | this->setClipToBounds(false); |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 54 | |
| 55 | #if SK_SUPPORT_GPU |
| 56 | this->windowSizeChanged(); |
| 57 | #endif |
| 58 | } |
| 59 | |
commit-bot@chromium.org | 939560b | 2014-02-27 17:39:40 +0000 | [diff] [blame] | 60 | SkV8ExampleWindow::~SkV8ExampleWindow() { |
| 61 | #if SK_SUPPORT_GPU |
| 62 | SkSafeUnref(fCurContext); |
| 63 | SkSafeUnref(fCurIntf); |
commit-bot@chromium.org | 939560b | 2014-02-27 17:39:40 +0000 | [diff] [blame] | 64 | SkSafeUnref(fCurSurface); |
| 65 | #endif |
| 66 | } |
| 67 | |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 68 | #if SK_SUPPORT_GPU |
| 69 | void SkV8ExampleWindow::windowSizeChanged() { |
| 70 | if (FLAGS_gpu) { |
| 71 | SkOSWindow::AttachmentInfo attachmentInfo; |
| 72 | bool result = this->attach( |
brianosman | 2d1ee79 | 2016-05-05 12:24:31 -0700 | [diff] [blame] | 73 | SkOSWindow::kNativeGL_BackEndType, 0, false, &attachmentInfo); |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 74 | if (!result) { |
| 75 | printf("Failed to attach."); |
| 76 | exit(1); |
| 77 | } |
| 78 | |
| 79 | fCurIntf = GrGLCreateNativeInterface(); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 80 | fCurContext = GrContext::MakeGL(fCurIntf).release(); |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 81 | if (NULL == fCurIntf || NULL == fCurContext) { |
| 82 | printf("Failed to initialize GL."); |
| 83 | exit(1); |
| 84 | } |
| 85 | |
Brian Salomon | f43d000 | 2017-08-31 20:04:00 +0000 | [diff] [blame] | 86 | GrGLFramebufferInfo framebufferInfo; |
| 87 | GR_GL_GetIntegerv(fCurIntf, GR_GL_FRAMEBUFFER_BINDING, &framebufferInfo.fFBOID); |
| 88 | GrBackendRenderTarget backendRenderTarget(SkScalarRoundToInt(this->width()), |
| 89 | SkScalarRoundToInt(this->height()), |
| 90 | attachmentInfo.fSampleCount, |
| 91 | attachmentInfo.fStencilBits, |
| 92 | kSkia8888_GrPixelConfig, |
| 93 | framebufferInfo); |
commit-bot@chromium.org | 939560b | 2014-02-27 17:39:40 +0000 | [diff] [blame] | 94 | SkSafeUnref(fCurSurface); |
Brian Salomon | f43d000 | 2017-08-31 20:04:00 +0000 | [diff] [blame] | 95 | fCurSurface = SkSurface::MakeFromBackendRenderTarget(fCurContext, backendRenderTarget, |
| 96 | kBottomLeft_GrSurfaceOrigin, |
robertphillips | 12e9662 | 2016-08-01 05:53:23 -0700 | [diff] [blame] | 97 | nullptr, nullptr).release(); |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | #endif |
| 101 | |
| 102 | #if SK_SUPPORT_GPU |
jcgregorio | e22f45f | 2014-10-24 12:49:17 -0700 | [diff] [blame] | 103 | SkSurface* SkV8ExampleWindow::createSurface() { |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 104 | if (FLAGS_gpu) { |
jcgregorio | e22f45f | 2014-10-24 12:49:17 -0700 | [diff] [blame] | 105 | // Increase the ref count since callers of createSurface put the |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 106 | // results in a sk_sp. |
jcgregorio | e22f45f | 2014-10-24 12:49:17 -0700 | [diff] [blame] | 107 | fCurSurface->ref(); |
| 108 | return fCurSurface; |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 109 | } else { |
jcgregorio | e22f45f | 2014-10-24 12:49:17 -0700 | [diff] [blame] | 110 | return this->INHERITED::createSurface(); |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | #endif |
| 114 | |
| 115 | void SkV8ExampleWindow::onSizeChange() { |
| 116 | this->INHERITED::onSizeChange(); |
| 117 | |
| 118 | #if SK_SUPPORT_GPU |
| 119 | this->windowSizeChanged(); |
| 120 | #endif |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 121 | } |
| 122 | |
jcgregorio | e001da2 | 2014-10-29 05:33:27 -0700 | [diff] [blame] | 123 | Global* global = NULL; |
Andreas Haas | ae99d29 | 2017-11-09 16:52:04 +0100 | [diff] [blame] | 124 | std::unique_ptr<v8::Platform> platform; |
jcgregorio | e001da2 | 2014-10-29 05:33:27 -0700 | [diff] [blame] | 125 | |
commit-bot@chromium.org | 80bd0c9 | 2013-12-06 15:24:52 +0000 | [diff] [blame] | 126 | void SkV8ExampleWindow::onDraw(SkCanvas* canvas) { |
commit-bot@chromium.org | 80bd0c9 | 2013-12-06 15:24:52 +0000 | [diff] [blame] | 127 | |
commit-bot@chromium.org | 9bde13c | 2013-12-10 14:13:03 +0000 | [diff] [blame] | 128 | canvas->save(); |
commit-bot@chromium.org | 3a6143d | 2013-12-11 17:34:58 +0000 | [diff] [blame] | 129 | canvas->drawColor(SK_ColorWHITE); |
commit-bot@chromium.org | 80bd0c9 | 2013-12-06 15:24:52 +0000 | [diff] [blame] | 130 | |
commit-bot@chromium.org | 9bde13c | 2013-12-10 14:13:03 +0000 | [diff] [blame] | 131 | // Now jump into JS and call the onDraw(canvas) method defined there. |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 132 | fJsContext->onDraw(canvas); |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 133 | |
commit-bot@chromium.org | 9bde13c | 2013-12-10 14:13:03 +0000 | [diff] [blame] | 134 | canvas->restore(); |
| 135 | |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 136 | this->INHERITED::onDraw(canvas); |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 137 | |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 138 | #if SK_SUPPORT_GPU |
| 139 | if (FLAGS_gpu) { |
| 140 | fCurContext->flush(); |
| 141 | this->present(); |
| 142 | } |
| 143 | #endif |
| 144 | } |
commit-bot@chromium.org | 80bd0c9 | 2013-12-06 15:24:52 +0000 | [diff] [blame] | 145 | |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 146 | #ifdef SK_BUILD_FOR_WIN |
| 147 | void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) { |
| 148 | RECT winRect; |
| 149 | winRect.top = rect.top(); |
| 150 | winRect.bottom = rect.bottom(); |
| 151 | winRect.right = rect.right(); |
| 152 | winRect.left = rect.left(); |
| 153 | InvalidateRect((HWND)this->getHWND(), &winRect, false); |
| 154 | } |
| 155 | #endif |
| 156 | |
jcgregorio | e22f45f | 2014-10-24 12:49:17 -0700 | [diff] [blame] | 157 | |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 158 | SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { |
commit-bot@chromium.org | 9bde13c | 2013-12-10 14:13:03 +0000 | [diff] [blame] | 159 | printf("Started\n"); |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 160 | |
jcgregorio | e001da2 | 2014-10-29 05:33:27 -0700 | [diff] [blame] | 161 | v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
commit-bot@chromium.org | 48d94b8c | 2013-12-16 18:24:51 +0000 | [diff] [blame] | 162 | SkCommandLineFlags::Parse(argc, argv); |
| 163 | |
jcgregorio | e22f45f | 2014-10-24 12:49:17 -0700 | [diff] [blame] | 164 | v8::V8::InitializeICU(); |
Andreas Haas | ae99d29 | 2017-11-09 16:52:04 +0100 | [diff] [blame] | 165 | platform = std::unique_ptr<v8::Platform>(v8::platform::CreateDefaultPlatform()); |
| 166 | v8::V8::InitializePlatform(platform.get()); |
jcgregorio | e22f45f | 2014-10-24 12:49:17 -0700 | [diff] [blame] | 167 | v8::V8::Initialize(); |
| 168 | |
| 169 | v8::Isolate* isolate = v8::Isolate::New(); |
| 170 | v8::Isolate::Scope isolate_scope(isolate); |
| 171 | v8::HandleScope handle_scope(isolate); |
| 172 | isolate->Enter(); |
| 173 | |
jcgregorio | e001da2 | 2014-10-29 05:33:27 -0700 | [diff] [blame] | 174 | global = new Global(isolate); |
commit-bot@chromium.org | 48d94b8c | 2013-12-16 18:24:51 +0000 | [diff] [blame] | 175 | |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 176 | |
| 177 | // Set up things to look like a browser by creating |
| 178 | // a console object that invokes our print function. |
| 179 | const char* startupScript = |
| 180 | "function Console() {}; \n" |
| 181 | "Console.prototype.log = function() { \n" |
| 182 | " var args = Array.prototype.slice.call(arguments).join(' '); \n" |
| 183 | " print(args); \n" |
| 184 | "}; \n" |
| 185 | "console = new Console(); \n"; |
| 186 | |
| 187 | if (!global->parseScript(startupScript)) { |
| 188 | printf("Failed to parse startup script: %s.\n", FLAGS_infile[0]); |
| 189 | exit(1); |
| 190 | } |
| 191 | |
commit-bot@chromium.org | 3a6143d | 2013-12-11 17:34:58 +0000 | [diff] [blame] | 192 | const char* script = |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 193 | "function onDraw(canvas) { \n" |
| 194 | " canvas.fillStyle = '#00FF00'; \n" |
| 195 | " canvas.fillRect(20, 20, 100, 100); \n" |
| 196 | " canvas.inval(); \n" |
| 197 | "} \n"; |
commit-bot@chromium.org | 48d94b8c | 2013-12-16 18:24:51 +0000 | [diff] [blame] | 198 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 199 | sk_sp<SkData> data; |
commit-bot@chromium.org | 48d94b8c | 2013-12-16 18:24:51 +0000 | [diff] [blame] | 200 | if (FLAGS_infile.count()) { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 201 | data = SkData::MakeFromFileName(FLAGS_infile[0]); |
commit-bot@chromium.org | 48d94b8c | 2013-12-16 18:24:51 +0000 | [diff] [blame] | 202 | script = static_cast<const char*>(data->data()); |
| 203 | } |
| 204 | if (NULL == script) { |
| 205 | printf("Could not load file: %s.\n", FLAGS_infile[0]); |
| 206 | exit(1); |
| 207 | } |
jcgregorio | e001da2 | 2014-10-29 05:33:27 -0700 | [diff] [blame] | 208 | Path2DBuilder::AddToGlobal(global); |
commit-bot@chromium.org | f679d1b | 2014-02-27 20:20:21 +0000 | [diff] [blame] | 209 | Path2D::AddToGlobal(global); |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 210 | |
| 211 | if (!global->parseScript(script)) { |
commit-bot@chromium.org | 872796b | 2013-12-20 15:56:52 +0000 | [diff] [blame] | 212 | printf("Failed to parse file: %s.\n", FLAGS_infile[0]); |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 213 | exit(1); |
| 214 | } |
| 215 | |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame] | 216 | |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 217 | JsContext* jsContext = new JsContext(global); |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 218 | |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 219 | if (!jsContext->initialize()) { |
commit-bot@chromium.org | 9bde13c | 2013-12-10 14:13:03 +0000 | [diff] [blame] | 220 | printf("Failed to initialize.\n"); |
| 221 | exit(1); |
| 222 | } |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 223 | SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsContext); |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 224 | global->setWindow(win); |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 225 | |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 226 | return win; |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 227 | } |