blob: 5a86c94aecab4f01137dcc1fdaa1ccb66c98b068 [file] [log] [blame]
joshualittda7b8432015-05-27 09:19:03 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 *
7 */
8
9#include "VisualBench.h"
10
bsalomonf276ac52015-10-09 13:36:42 -070011#include "GrContext.h"
joshualitt7fe8ee42015-06-01 10:03:54 -070012#include "ProcStats.h"
joshualittda7b8432015-05-27 09:19:03 -070013#include "SkApplication.h"
14#include "SkCanvas.h"
15#include "SkCommandLineFlags.h"
joshualittda7b8432015-05-27 09:19:03 -070016#include "SkGraphics.h"
17#include "SkGr.h"
joshualittda7b8432015-05-27 09:19:03 -070018#include "SkOSFile.h"
19#include "SkStream.h"
joshualitt7fe8ee42015-06-01 10:03:54 -070020#include "Stats.h"
joshualitt189aef72015-09-08 07:08:11 -070021#include "VisualLightweightBenchModule.h"
jvanverthf5d1b2d2015-09-15 07:40:56 -070022#include "VisualInteractiveModule.h"
joshualittda7b8432015-05-27 09:19:03 -070023#include "gl/GrGLInterface.h"
24
bsalomon45171e22015-06-16 13:52:18 -070025DEFINE_bool2(fullscreen, f, true, "Run fullscreen.");
jvanverthf5d1b2d2015-09-15 07:40:56 -070026DEFINE_bool2(interactive, n, false, "Run in interactive mode.");
cdaltonc70483f2015-10-26 13:14:36 -070027DEFINE_bool2(dif, d, false, "Use device-independent fonts.");
joshualitt7fe8ee42015-06-01 10:03:54 -070028
joshualittda7b8432015-05-27 09:19:03 -070029VisualBench::VisualBench(void* hwnd, int argc, char** argv)
joshualittfb33ae12015-09-18 06:47:39 -070030 : INHERITED(hwnd) {
joshualittda7b8432015-05-27 09:19:03 -070031 SkCommandLineFlags::Parse(argc, argv);
32
joshualitt43f610f2015-11-10 08:27:22 -080033 SkDebugf("Command line arguments:");
34 for (int i = 0; i < argc; ++i) {
35 SkDebugf("%s\n", argv[i]);
36 }
37
cdaltonc70483f2015-10-26 13:14:36 -070038 // these have to happen after commandline parsing
39 if (FLAGS_dif) {
40 const SkSurfaceProps& props(INHERITED::getSurfaceProps());
41 uint32_t flags = SkSurfaceProps::kUseDeviceIndependentFonts_Flag | props.flags();
42 INHERITED::setSurfaceProps(SkSurfaceProps(flags, props.pixelGeometry()));
43 }
joshualittfb33ae12015-09-18 06:47:39 -070044 fModule.reset(new VisualLightweightBenchModule(this));
jvanverthf5d1b2d2015-09-15 07:40:56 -070045 if (FLAGS_interactive) {
46 fModule.reset(new VisualInteractiveModule(this));
47 }
48
joshualitt7fe8ee42015-06-01 10:03:54 -070049 this->setTitle();
50 this->setupBackend();
joshualittda7b8432015-05-27 09:19:03 -070051}
52
53VisualBench::~VisualBench() {
joshualittfb02cda2015-10-21 08:04:24 -070054 this->tearDownContext();
joshualittda7b8432015-05-27 09:19:03 -070055}
56
57void VisualBench::setTitle() {
58 SkString title("VisualBench");
59 INHERITED::setTitle(title.c_str());
60}
61
62SkSurface* VisualBench::createSurface() {
joshualittd4fa90f2015-07-15 06:18:57 -070063 if (!fSurface) {
64 SkSurfaceProps props(INHERITED::getSurfaceProps());
65 fSurface.reset(SkSurface::NewRenderTargetDirect(fRenderTarget, &props));
66 }
67
68 // The caller will wrap the SkSurface in an SkAutoTUnref
69 return SkRef(fSurface.get());
joshualittda7b8432015-05-27 09:19:03 -070070}
71
72bool VisualBench::setupBackend() {
73 this->setColorType(kRGBA_8888_SkColorType);
74 this->setVisibleP(true);
75 this->setClipToBounds(false);
76
bsalomon45171e22015-06-16 13:52:18 -070077 if (FLAGS_fullscreen) {
78 if (!this->makeFullscreen()) {
79 SkDebugf("Could not go fullscreen!");
80 }
bsalomon85ab5512015-06-16 12:47:25 -070081 }
joshualittda7b8432015-05-27 09:19:03 -070082
joshualitt7fe8ee42015-06-01 10:03:54 -070083 this->resetContext();
84 return true;
85}
joshualittda7b8432015-05-27 09:19:03 -070086
joshualitt7fe8ee42015-06-01 10:03:54 -070087void VisualBench::resetContext() {
joshualittfb02cda2015-10-21 08:04:24 -070088 this->tearDownContext();
89 this->setupContext();
90}
91
92void VisualBench::setupContext() {
93 if (!this->attach(kNativeGL_BackEndType, FLAGS_msaa, &fAttachmentInfo)) {
94 SkDebugf("Not possible to create backend.\n");
95 INHERITED::detach();
96 SkFAIL("Could not create backend\n");
97 }
98
99 this->setVsync(false);
100
halcanary96fcdcc2015-08-27 07:41:13 -0700101 fSurface.reset(nullptr);
joshualitt31b21f62015-07-16 13:40:51 -0700102
joshualittda7b8432015-05-27 09:19:03 -0700103 fInterface.reset(GrGLCreateNativeInterface());
joshualitt5a74afe2015-10-06 08:05:10 -0700104
joshualittc9dd93c2015-10-15 09:49:31 -0700105 // TODO use the GLContext creation factories and also set this all up in configs
106 if (!FLAGS_nvpr) {
107 fInterface.reset(GrGLInterfaceRemoveNVPR(fInterface));
108 }
joshualittda7b8432015-05-27 09:19:03 -0700109 SkASSERT(fInterface);
110
111 // setup contexts
112 fContext.reset(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface.get()));
113 SkASSERT(fContext);
114
115 // setup rendertargets
116 this->setupRenderTarget();
joshualittda7b8432015-05-27 09:19:03 -0700117}
118
joshualittfb02cda2015-10-21 08:04:24 -0700119void VisualBench::tearDownContext() {
120 if (fContext) {
121 // We abandon the context in case SkWindow has kept a ref to the surface
122 fContext->abandonContext();
123 fContext.reset();
124 fSurface.reset();
125 fInterface.reset();
126 this->detach();
127 }
128}
129
joshualittda7b8432015-05-27 09:19:03 -0700130void VisualBench::setupRenderTarget() {
joshualitt030dc842015-06-12 12:51:44 -0700131 if (fContext) {
132 fRenderTarget.reset(this->renderTarget(fAttachmentInfo, fInterface, fContext));
133 }
joshualittda7b8432015-05-27 09:19:03 -0700134}
135
bsalomon45171e22015-06-16 13:52:18 -0700136void VisualBench::draw(SkCanvas* canvas) {
joshualitt189aef72015-09-08 07:08:11 -0700137 fModule->draw(canvas);
joshualittda7b8432015-05-27 09:19:03 -0700138
139 // Invalidate the window to force a redraw. Poor man's animation mechanism.
halcanary96fcdcc2015-08-27 07:41:13 -0700140 this->inval(nullptr);
joshualittda7b8432015-05-27 09:19:03 -0700141}
142
jvanverthf5d1b2d2015-09-15 07:40:56 -0700143void VisualBench::clear(SkCanvas* canvas, SkColor color, int frames) {
144 canvas->clear(color);
145 for (int i = 0; i < frames - 1; ++i) {
146 canvas->flush();
147 this->present();
148 canvas->clear(color);
149 }
150}
151
joshualittda7b8432015-05-27 09:19:03 -0700152void VisualBench::onSizeChange() {
153 this->setupRenderTarget();
154}
155
156bool VisualBench::onHandleChar(SkUnichar unichar) {
jvanverthf5d1b2d2015-09-15 07:40:56 -0700157 static const auto kEscKey = 27;
158 if (kEscKey == unichar) {
159 this->closeWindow();
160 return true;
161 }
162
163 return fModule->onHandleChar(unichar);
joshualittda7b8432015-05-27 09:19:03 -0700164}
165
166// Externally declared entry points
167void application_init() {
168 SkGraphics::Init();
169 SkEvent::Init();
170}
171
172void application_term() {
173 SkEvent::Term();
joshualittda7b8432015-05-27 09:19:03 -0700174}
175
176SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
177 return new VisualBench(hwnd, argc, argv);
178}
179