joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
bsalomon | f276ac5 | 2015-10-09 13:36:42 -0700 | [diff] [blame] | 11 | #include "GrContext.h" |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 12 | #include "ProcStats.h" |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 13 | #include "SkApplication.h" |
| 14 | #include "SkCanvas.h" |
| 15 | #include "SkCommandLineFlags.h" |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 16 | #include "SkGraphics.h" |
| 17 | #include "SkGr.h" |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 18 | #include "SkOSFile.h" |
| 19 | #include "SkStream.h" |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 20 | #include "Stats.h" |
joshualitt | 6ebd423 | 2015-12-04 09:02:34 -0800 | [diff] [blame] | 21 | #include "VisualDebugModule.h" |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 22 | #include "VisualLightweightBenchModule.h" |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 23 | #include "VisualInteractiveModule.h" |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 24 | #include "gl/GrGLInterface.h" |
| 25 | |
cdalton | baf8fcb | 2015-11-24 09:20:24 -0800 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | |
bsalomon | 45171e2 | 2015-06-16 13:52:18 -0700 | [diff] [blame] | 28 | DEFINE_bool2(fullscreen, f, true, "Run fullscreen."); |
joshualitt | 6ebd423 | 2015-12-04 09:02:34 -0800 | [diff] [blame] | 29 | DEFINE_string(mode, "classic", "one of: classic interactive debugger"); |
cdalton | c70483f | 2015-10-26 13:14:36 -0700 | [diff] [blame] | 30 | DEFINE_bool2(dif, d, false, "Use device-independent fonts."); |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 31 | |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 32 | VisualBench::VisualBench(void* hwnd, int argc, char** argv) |
joshualitt | fb33ae1 | 2015-09-18 06:47:39 -0700 | [diff] [blame] | 33 | : INHERITED(hwnd) { |
joshualitt | 6ebd423 | 2015-12-04 09:02:34 -0800 | [diff] [blame] | 34 | SkDebugf("Command line arguments: "); |
| 35 | for (int i = 1; i < argc; ++i) { |
| 36 | SkDebugf("%s ", argv[i]); |
joshualitt | 43f610f | 2015-11-10 08:27:22 -0800 | [diff] [blame] | 37 | } |
joshualitt | 6ebd423 | 2015-12-04 09:02:34 -0800 | [diff] [blame] | 38 | SkDebugf("\n"); |
joshualitt | 43f610f | 2015-11-10 08:27:22 -0800 | [diff] [blame] | 39 | |
joshualitt | 898e02e | 2015-11-23 13:57:46 -0800 | [diff] [blame] | 40 | SkCommandLineFlags::Parse(argc, argv); |
| 41 | |
cdalton | baf8fcb | 2015-11-24 09:20:24 -0800 | [diff] [blame] | 42 | if (FLAGS_nvpr && !FLAGS_msaa) { |
| 43 | SkDebugf("Got nvpr without msaa. Exiting.\n"); |
| 44 | exit(-1); |
| 45 | } |
| 46 | |
cdalton | c70483f | 2015-10-26 13:14:36 -0700 | [diff] [blame] | 47 | // 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 | } |
joshualitt | fb33ae1 | 2015-09-18 06:47:39 -0700 | [diff] [blame] | 53 | fModule.reset(new VisualLightweightBenchModule(this)); |
joshualitt | 6ebd423 | 2015-12-04 09:02:34 -0800 | [diff] [blame] | 54 | |
| 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 | } |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 63 | } |
| 64 | |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 65 | this->setTitle(); |
| 66 | this->setupBackend(); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | VisualBench::~VisualBench() { |
joshualitt | fb02cda | 2015-10-21 08:04:24 -0700 | [diff] [blame] | 70 | this->tearDownContext(); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void VisualBench::setTitle() { |
| 74 | SkString title("VisualBench"); |
| 75 | INHERITED::setTitle(title.c_str()); |
| 76 | } |
| 77 | |
| 78 | SkSurface* VisualBench::createSurface() { |
joshualitt | d4fa90f | 2015-07-15 06:18:57 -0700 | [diff] [blame] | 79 | 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()); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | bool VisualBench::setupBackend() { |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 89 | this->setVisibleP(true); |
| 90 | this->setClipToBounds(false); |
| 91 | |
bsalomon | 45171e2 | 2015-06-16 13:52:18 -0700 | [diff] [blame] | 92 | if (FLAGS_fullscreen) { |
| 93 | if (!this->makeFullscreen()) { |
| 94 | SkDebugf("Could not go fullscreen!"); |
| 95 | } |
bsalomon | 85ab551 | 2015-06-16 12:47:25 -0700 | [diff] [blame] | 96 | } |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 97 | |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 98 | this->resetContext(); |
| 99 | return true; |
| 100 | } |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 101 | |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 102 | void VisualBench::resetContext() { |
joshualitt | fb02cda | 2015-10-21 08:04:24 -0700 | [diff] [blame] | 103 | this->tearDownContext(); |
| 104 | this->setupContext(); |
| 105 | } |
| 106 | |
| 107 | void VisualBench::setupContext() { |
cdalton | baf8fcb | 2015-11-24 09:20:24 -0800 | [diff] [blame] | 108 | int screenSamples = FLAGS_offscreen ? 0 : FLAGS_msaa; |
| 109 | if (!this->attach(kNativeGL_BackEndType, screenSamples, &fAttachmentInfo)) { |
joshualitt | fb02cda | 2015-10-21 08:04:24 -0700 | [diff] [blame] | 110 | SkDebugf("Not possible to create backend.\n"); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame^] | 111 | INHERITED::release(); |
joshualitt | fb02cda | 2015-10-21 08:04:24 -0700 | [diff] [blame] | 112 | SkFAIL("Could not create backend\n"); |
| 113 | } |
| 114 | |
| 115 | this->setVsync(false); |
| 116 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 117 | fSurface.reset(nullptr); |
joshualitt | 31b21f6 | 2015-07-16 13:40:51 -0700 | [diff] [blame] | 118 | |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 119 | fInterface.reset(GrGLCreateNativeInterface()); |
joshualitt | 5a74afe | 2015-10-06 08:05:10 -0700 | [diff] [blame] | 120 | |
joshualitt | c9dd93c | 2015-10-15 09:49:31 -0700 | [diff] [blame] | 121 | // TODO use the GLContext creation factories and also set this all up in configs |
cdalton | baf8fcb | 2015-11-24 09:20:24 -0800 | [diff] [blame] | 122 | if (!FLAGS_nvpr) { |
joshualitt | c9dd93c | 2015-10-15 09:49:31 -0700 | [diff] [blame] | 123 | fInterface.reset(GrGLInterfaceRemoveNVPR(fInterface)); |
| 124 | } |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 125 | 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(); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 133 | } |
| 134 | |
joshualitt | fb02cda | 2015-10-21 08:04:24 -0700 | [diff] [blame] | 135 | void 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(); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame^] | 142 | this->release(); |
joshualitt | fb02cda | 2015-10-21 08:04:24 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 146 | void VisualBench::setupRenderTarget() { |
joshualitt | 030dc84 | 2015-06-12 12:51:44 -0700 | [diff] [blame] | 147 | if (fContext) { |
| 148 | fRenderTarget.reset(this->renderTarget(fAttachmentInfo, fInterface, fContext)); |
| 149 | } |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 150 | } |
| 151 | |
bsalomon | 45171e2 | 2015-06-16 13:52:18 -0700 | [diff] [blame] | 152 | void VisualBench::draw(SkCanvas* canvas) { |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 153 | fModule->draw(canvas); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 154 | |
| 155 | // Invalidate the window to force a redraw. Poor man's animation mechanism. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 156 | this->inval(nullptr); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 157 | } |
| 158 | |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 159 | void 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 | |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 168 | void VisualBench::onSizeChange() { |
| 169 | this->setupRenderTarget(); |
| 170 | } |
| 171 | |
| 172 | bool VisualBench::onHandleChar(SkUnichar unichar) { |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 173 | static const auto kEscKey = 27; |
| 174 | if (kEscKey == unichar) { |
| 175 | this->closeWindow(); |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | return fModule->onHandleChar(unichar); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // Externally declared entry points |
| 183 | void application_init() { |
| 184 | SkGraphics::Init(); |
| 185 | SkEvent::Init(); |
| 186 | } |
| 187 | |
| 188 | void application_term() { |
| 189 | SkEvent::Term(); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { |
| 193 | return new VisualBench(hwnd, argc, argv); |
| 194 | } |
| 195 | |