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