blob: 69d47b6b0412f93b412b1e9c14ff8e1e77fb98b6 [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"
joshualitt6ebd4232015-12-04 09:02:34 -080021#include "VisualDebugModule.h"
joshualitt189aef72015-09-08 07:08:11 -070022#include "VisualLightweightBenchModule.h"
jvanverthf5d1b2d2015-09-15 07:40:56 -070023#include "VisualInteractiveModule.h"
joshualittda7b8432015-05-27 09:19:03 -070024#include "gl/GrGLInterface.h"
25
cdaltonbaf8fcb2015-11-24 09:20:24 -080026#include <stdlib.h>
27
bsalomon45171e22015-06-16 13:52:18 -070028DEFINE_bool2(fullscreen, f, true, "Run fullscreen.");
joshualitt6ebd4232015-12-04 09:02:34 -080029DEFINE_string(mode, "classic", "one of: classic interactive debugger");
cdaltonc70483f2015-10-26 13:14:36 -070030DEFINE_bool2(dif, d, false, "Use device-independent fonts.");
joshualitt7fe8ee42015-06-01 10:03:54 -070031
joshualittda7b8432015-05-27 09:19:03 -070032VisualBench::VisualBench(void* hwnd, int argc, char** argv)
joshualittfb33ae12015-09-18 06:47:39 -070033 : INHERITED(hwnd) {
joshualitt6ebd4232015-12-04 09:02:34 -080034 SkDebugf("Command line arguments: ");
35 for (int i = 1; i < argc; ++i) {
36 SkDebugf("%s ", argv[i]);
joshualitt43f610f2015-11-10 08:27:22 -080037 }
joshualitt6ebd4232015-12-04 09:02:34 -080038 SkDebugf("\n");
joshualitt43f610f2015-11-10 08:27:22 -080039
joshualitt898e02e2015-11-23 13:57:46 -080040 SkCommandLineFlags::Parse(argc, argv);
41
cdaltonbaf8fcb2015-11-24 09:20:24 -080042 if (FLAGS_nvpr && !FLAGS_msaa) {
43 SkDebugf("Got nvpr without msaa. Exiting.\n");
44 exit(-1);
45 }
46
cdaltonc70483f2015-10-26 13:14:36 -070047 // these have to happen after commandline parsing
48 if (FLAGS_dif) {
49 const SkSurfaceProps& props(INHERITED::getSurfaceProps());
50 uint32_t flags = SkSurfaceProps::kUseDeviceIndependentFonts_Flag | props.flags();
51 INHERITED::setSurfaceProps(SkSurfaceProps(flags, props.pixelGeometry()));
52 }
joshualittfb33ae12015-09-18 06:47:39 -070053 fModule.reset(new VisualLightweightBenchModule(this));
joshualitt6ebd4232015-12-04 09:02:34 -080054
55 if (FLAGS_mode.count()) {
56 SkASSERT(FLAGS_mode.count() == 1);
57 SkString mode(FLAGS_mode[0]);
58 if (mode == SkString("interactive")) {
59 fModule.reset(new VisualInteractiveModule(this));
60 } else if (mode == SkString("debugger")) {
61 fModule.reset(new VisualDebugModule(this));
62 }
jvanverthf5d1b2d2015-09-15 07:40:56 -070063 }
64
joshualitt7fe8ee42015-06-01 10:03:54 -070065 this->setTitle();
66 this->setupBackend();
joshualittda7b8432015-05-27 09:19:03 -070067}
68
69VisualBench::~VisualBench() {
joshualittfb02cda2015-10-21 08:04:24 -070070 this->tearDownContext();
joshualittda7b8432015-05-27 09:19:03 -070071}
72
73void VisualBench::setTitle() {
74 SkString title("VisualBench");
75 INHERITED::setTitle(title.c_str());
76}
77
78SkSurface* VisualBench::createSurface() {
joshualittd4fa90f2015-07-15 06:18:57 -070079 if (!fSurface) {
80 SkSurfaceProps props(INHERITED::getSurfaceProps());
81 fSurface.reset(SkSurface::NewRenderTargetDirect(fRenderTarget, &props));
82 }
83
84 // The caller will wrap the SkSurface in an SkAutoTUnref
85 return SkRef(fSurface.get());
joshualittda7b8432015-05-27 09:19:03 -070086}
87
88bool VisualBench::setupBackend() {
joshualittda7b8432015-05-27 09:19:03 -070089 this->setVisibleP(true);
90 this->setClipToBounds(false);
91
bsalomon45171e22015-06-16 13:52:18 -070092 if (FLAGS_fullscreen) {
93 if (!this->makeFullscreen()) {
94 SkDebugf("Could not go fullscreen!");
95 }
bsalomon85ab5512015-06-16 12:47:25 -070096 }
joshualittda7b8432015-05-27 09:19:03 -070097
joshualitt7fe8ee42015-06-01 10:03:54 -070098 this->resetContext();
99 return true;
100}
joshualittda7b8432015-05-27 09:19:03 -0700101
joshualitt7fe8ee42015-06-01 10:03:54 -0700102void VisualBench::resetContext() {
joshualittfb02cda2015-10-21 08:04:24 -0700103 this->tearDownContext();
104 this->setupContext();
105}
106
107void VisualBench::setupContext() {
cdaltonbaf8fcb2015-11-24 09:20:24 -0800108 int screenSamples = FLAGS_offscreen ? 0 : FLAGS_msaa;
109 if (!this->attach(kNativeGL_BackEndType, screenSamples, &fAttachmentInfo)) {
joshualittfb02cda2015-10-21 08:04:24 -0700110 SkDebugf("Not possible to create backend.\n");
mtklein18300a32016-03-16 13:53:35 -0700111 INHERITED::release();
joshualittfb02cda2015-10-21 08:04:24 -0700112 SkFAIL("Could not create backend\n");
113 }
114
115 this->setVsync(false);
116
halcanary96fcdcc2015-08-27 07:41:13 -0700117 fSurface.reset(nullptr);
joshualitt31b21f62015-07-16 13:40:51 -0700118
joshualittda7b8432015-05-27 09:19:03 -0700119 fInterface.reset(GrGLCreateNativeInterface());
joshualitt5a74afe2015-10-06 08:05:10 -0700120
joshualittc9dd93c2015-10-15 09:49:31 -0700121 // TODO use the GLContext creation factories and also set this all up in configs
cdaltonbaf8fcb2015-11-24 09:20:24 -0800122 if (!FLAGS_nvpr) {
joshualittc9dd93c2015-10-15 09:49:31 -0700123 fInterface.reset(GrGLInterfaceRemoveNVPR(fInterface));
124 }
joshualittda7b8432015-05-27 09:19:03 -0700125 SkASSERT(fInterface);
126
127 // setup contexts
128 fContext.reset(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface.get()));
129 SkASSERT(fContext);
130
131 // setup rendertargets
132 this->setupRenderTarget();
joshualittda7b8432015-05-27 09:19:03 -0700133}
134
joshualittfb02cda2015-10-21 08:04:24 -0700135void VisualBench::tearDownContext() {
136 if (fContext) {
137 // We abandon the context in case SkWindow has kept a ref to the surface
138 fContext->abandonContext();
139 fContext.reset();
140 fSurface.reset();
141 fInterface.reset();
mtklein18300a32016-03-16 13:53:35 -0700142 this->release();
joshualittfb02cda2015-10-21 08:04:24 -0700143 }
144}
145
joshualittda7b8432015-05-27 09:19:03 -0700146void VisualBench::setupRenderTarget() {
joshualitt030dc842015-06-12 12:51:44 -0700147 if (fContext) {
148 fRenderTarget.reset(this->renderTarget(fAttachmentInfo, fInterface, fContext));
149 }
joshualittda7b8432015-05-27 09:19:03 -0700150}
151
bsalomon45171e22015-06-16 13:52:18 -0700152void VisualBench::draw(SkCanvas* canvas) {
joshualitt189aef72015-09-08 07:08:11 -0700153 fModule->draw(canvas);
joshualittda7b8432015-05-27 09:19:03 -0700154
155 // Invalidate the window to force a redraw. Poor man's animation mechanism.
halcanary96fcdcc2015-08-27 07:41:13 -0700156 this->inval(nullptr);
joshualittda7b8432015-05-27 09:19:03 -0700157}
158
jvanverthf5d1b2d2015-09-15 07:40:56 -0700159void VisualBench::clear(SkCanvas* canvas, SkColor color, int frames) {
160 canvas->clear(color);
161 for (int i = 0; i < frames - 1; ++i) {
162 canvas->flush();
163 this->present();
164 canvas->clear(color);
165 }
166}
167
joshualittda7b8432015-05-27 09:19:03 -0700168void VisualBench::onSizeChange() {
169 this->setupRenderTarget();
170}
171
172bool VisualBench::onHandleChar(SkUnichar unichar) {
jvanverthf5d1b2d2015-09-15 07:40:56 -0700173 static const auto kEscKey = 27;
174 if (kEscKey == unichar) {
175 this->closeWindow();
176 return true;
177 }
178
179 return fModule->onHandleChar(unichar);
joshualittda7b8432015-05-27 09:19:03 -0700180}
181
182// Externally declared entry points
183void application_init() {
184 SkGraphics::Init();
185 SkEvent::Init();
186}
187
188void application_term() {
189 SkEvent::Term();
joshualittda7b8432015-05-27 09:19:03 -0700190}
191
192SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
193 return new VisualBench(hwnd, argc, argv);
194}
195