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