Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Side by Side Diff: experimental/SkV8Example/SkV8Example.cpp

Issue 122453003: Add GPU, controlled via command line flag. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rebase Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « experimental/SkV8Example/SkV8Example.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 Google Inc.
3 * 3 *
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 * 7 *
8 */ 8 */
9 #include <v8.h> 9 #include <v8.h>
10 10
11 using namespace v8; 11 using namespace v8;
12 12
13 #include "SkV8Example.h" 13 #include "SkV8Example.h"
14 #include "Global.h" 14 #include "Global.h"
15 #include "JsContext.h" 15 #include "JsContext.h"
16 #include "Path.h" 16 #include "Path.h"
17 17
18 #include "gl/GrGLUtil.h" 18 #include "gl/GrGLUtil.h"
19 #include "gl/GrGLDefines.h" 19 #include "gl/GrGLDefines.h"
20 #include "gl/GrGLInterface.h" 20 #include "gl/GrGLInterface.h"
21 #include "GrRenderTarget.h"
22 #include "GrContext.h"
21 #include "SkApplication.h" 23 #include "SkApplication.h"
22 #include "SkCommandLineFlags.h" 24 #include "SkCommandLineFlags.h"
23 #include "SkData.h" 25 #include "SkData.h"
24 #include "SkDraw.h" 26 #include "SkDraw.h"
25 #include "SkGpuDevice.h" 27 #include "SkGpuDevice.h"
26 #include "SkGraphics.h" 28 #include "SkGraphics.h"
27 #include "SkScalar.h" 29 #include "SkScalar.h"
28 30
29 31
30 DEFINE_string2(infile, i, NULL, "Name of file to load JS from.\n"); 32 DEFINE_string2(infile, i, NULL, "Name of file to load JS from.\n");
33 DEFINE_bool(gpu, true, "Use the GPU for rendering.");
31 34
32 void application_init() { 35 void application_init() {
33 SkGraphics::Init(); 36 SkGraphics::Init();
34 SkEvent::Init(); 37 SkEvent::Init();
35 } 38 }
36 39
37 void application_term() { 40 void application_term() {
38 SkEvent::Term(); 41 SkEvent::Term();
39 SkGraphics::Term(); 42 SkGraphics::Term();
40 } 43 }
41 44
42
43 SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsContext* context) 45 SkV8ExampleWindow::SkV8ExampleWindow(void* hwnd, JsContext* context)
44 : INHERITED(hwnd) 46 : INHERITED(hwnd)
45 , fJsContext(context) 47 , fJsContext(context)
48 #if SK_SUPPORT_GPU
49 , fCurContext(NULL)
50 , fCurIntf(NULL)
51 , fCurRenderTarget(NULL)
52 #endif
46 { 53 {
47 this->setConfig(SkBitmap::kARGB_8888_Config); 54 this->setConfig(SkBitmap::kARGB_8888_Config);
48 this->setVisibleP(true); 55 this->setVisibleP(true);
49 this->setClipToBounds(false); 56 this->setClipToBounds(false);
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
50 } 117 }
51 118
52 void SkV8ExampleWindow::onDraw(SkCanvas* canvas) { 119 void SkV8ExampleWindow::onDraw(SkCanvas* canvas) {
53 120
54 canvas->save(); 121 canvas->save();
55 canvas->drawColor(SK_ColorWHITE); 122 canvas->drawColor(SK_ColorWHITE);
56 123
57 // Now jump into JS and call the onDraw(canvas) method defined there. 124 // Now jump into JS and call the onDraw(canvas) method defined there.
58 fJsContext->onDraw(canvas); 125 fJsContext->onDraw(canvas);
59 126
60 canvas->restore(); 127 canvas->restore();
61 128
62 INHERITED::onDraw(canvas); 129 this->INHERITED::onDraw(canvas);
130
131 #if SK_SUPPORT_GPU
132 if (FLAGS_gpu) {
133 fCurContext->flush();
134 this->present();
135 }
136 #endif
63 } 137 }
64 138
65
66 #ifdef SK_BUILD_FOR_WIN 139 #ifdef SK_BUILD_FOR_WIN
67 void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) { 140 void SkV8ExampleWindow::onHandleInval(const SkIRect& rect) {
68 RECT winRect; 141 RECT winRect;
69 winRect.top = rect.top(); 142 winRect.top = rect.top();
70 winRect.bottom = rect.bottom(); 143 winRect.bottom = rect.bottom();
71 winRect.right = rect.right(); 144 winRect.right = rect.right();
72 winRect.left = rect.left(); 145 winRect.left = rect.left();
73 InvalidateRect((HWND)this->getHWND(), &winRect, false); 146 InvalidateRect((HWND)this->getHWND(), &winRect, false);
74 } 147 }
75 #endif 148 #endif
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 } 180 }
108 181
109 JsContext* jsContext = new JsContext(global); 182 JsContext* jsContext = new JsContext(global);
110 183
111 if (!jsContext->initialize()) { 184 if (!jsContext->initialize()) {
112 printf("Failed to initialize.\n"); 185 printf("Failed to initialize.\n");
113 exit(1); 186 exit(1);
114 } 187 }
115 SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsContext); 188 SkV8ExampleWindow* win = new SkV8ExampleWindow(hwnd, jsContext);
116 global->setWindow(win); 189 global->setWindow(win);
190
117 return win; 191 return win;
118 } 192 }
OLDNEW
« no previous file with comments | « experimental/SkV8Example/SkV8Example.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698