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> |
| 10 | |
| 11 | using namespace v8; |
| 12 | |
| 13 | #include "SkV8Example.h" |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 14 | #include "Global.h" |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 15 | #include "JsContext.h" |
| 16 | #include "Path.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 | 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(); |
| 42 | SkGraphics::Term(); |
| 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) |
| 52 | #endif |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 53 | { |
| 54 | this->setConfig(SkBitmap::kARGB_8888_Config); |
| 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 | |
| 63 | #if SK_SUPPORT_GPU |
| 64 | void 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 |
| 100 | SkCanvas* 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 | |
| 111 | void SkV8ExampleWindow::onSizeChange() { |
| 112 | this->INHERITED::onSizeChange(); |
| 113 | |
| 114 | #if SK_SUPPORT_GPU |
| 115 | this->windowSizeChanged(); |
| 116 | #endif |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 117 | } |
| 118 | |
commit-bot@chromium.org | 80bd0c9 | 2013-12-06 15:24:52 +0000 | [diff] [blame] | 119 | void SkV8ExampleWindow::onDraw(SkCanvas* canvas) { |
commit-bot@chromium.org | 80bd0c9 | 2013-12-06 15:24:52 +0000 | [diff] [blame] | 120 | |
commit-bot@chromium.org | 9bde13c | 2013-12-10 14:13:03 +0000 | [diff] [blame] | 121 | canvas->save(); |
commit-bot@chromium.org | 3a6143d | 2013-12-11 17:34:58 +0000 | [diff] [blame] | 122 | canvas->drawColor(SK_ColorWHITE); |
commit-bot@chromium.org | 80bd0c9 | 2013-12-06 15:24:52 +0000 | [diff] [blame] | 123 | |
commit-bot@chromium.org | 9bde13c | 2013-12-10 14:13:03 +0000 | [diff] [blame] | 124 | // 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] | 125 | fJsContext->onDraw(canvas); |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 126 | |
commit-bot@chromium.org | 9bde13c | 2013-12-10 14:13:03 +0000 | [diff] [blame] | 127 | canvas->restore(); |
| 128 | |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 129 | this->INHERITED::onDraw(canvas); |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 130 | |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 131 | #if SK_SUPPORT_GPU |
| 132 | if (FLAGS_gpu) { |
| 133 | fCurContext->flush(); |
| 134 | this->present(); |
| 135 | } |
| 136 | #endif |
| 137 | } |
commit-bot@chromium.org | 80bd0c9 | 2013-12-06 15:24:52 +0000 | [diff] [blame] | 138 | |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 139 | #ifdef SK_BUILD_FOR_WIN |
| 140 | void 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.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 150 | SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { |
commit-bot@chromium.org | 9bde13c | 2013-12-10 14:13:03 +0000 | [diff] [blame] | 151 | printf("Started\n"); |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 152 | |
commit-bot@chromium.org | 48d94b8c | 2013-12-16 18:24:51 +0000 | [diff] [blame] | 153 | SkCommandLineFlags::Parse(argc, argv); |
| 154 | |
commit-bot@chromium.org | 9bde13c | 2013-12-10 14:13:03 +0000 | [diff] [blame] | 155 | // Get the default Isolate created at startup. |
| 156 | Isolate* isolate = Isolate::GetCurrent(); |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 157 | Global* global = new Global(isolate); |
commit-bot@chromium.org | 48d94b8c | 2013-12-16 18:24:51 +0000 | [diff] [blame] | 158 | |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame^] | 159 | |
| 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.org | 3a6143d | 2013-12-11 17:34:58 +0000 | [diff] [blame] | 175 | const char* script = |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame^] | 176 | "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.org | 48d94b8c | 2013-12-16 18:24:51 +0000 | [diff] [blame] | 181 | |
| 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.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 191 | Path::AddToGlobal(global); |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 192 | |
| 193 | if (!global->parseScript(script)) { |
commit-bot@chromium.org | 872796b | 2013-12-20 15:56:52 +0000 | [diff] [blame] | 194 | printf("Failed to parse file: %s.\n", FLAGS_infile[0]); |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 195 | exit(1); |
| 196 | } |
| 197 | |
commit-bot@chromium.org | cd11018 | 2014-01-10 21:33:01 +0000 | [diff] [blame^] | 198 | |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 199 | JsContext* jsContext = new JsContext(global); |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 200 | |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 201 | if (!jsContext->initialize()) { |
commit-bot@chromium.org | 9bde13c | 2013-12-10 14:13:03 +0000 | [diff] [blame] | 202 | printf("Failed to initialize.\n"); |
| 203 | exit(1); |
| 204 | } |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 205 | SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsContext); |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 206 | global->setWindow(win); |
commit-bot@chromium.org | 9ee0e32 | 2014-01-06 20:03:55 +0000 | [diff] [blame] | 207 | |
commit-bot@chromium.org | 0fc0dea | 2013-12-18 04:45:37 +0000 | [diff] [blame] | 208 | return win; |
commit-bot@chromium.org | 44a3877 | 2013-12-05 13:45:19 +0000 | [diff] [blame] | 209 | } |