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 | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 21 | #include "VisualLightweightBenchModule.h" |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 22 | #include "VisualInteractiveModule.h" |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 23 | #include "gl/GrGLInterface.h" |
| 24 | |
bsalomon | 45171e2 | 2015-06-16 13:52:18 -0700 | [diff] [blame] | 25 | DEFINE_bool2(fullscreen, f, true, "Run fullscreen."); |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 26 | DEFINE_bool2(interactive, n, false, "Run in interactive mode."); |
cdalton | c70483f | 2015-10-26 13:14:36 -0700 | [diff] [blame] | 27 | DEFINE_bool2(dif, d, false, "Use device-independent fonts."); |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 28 | |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 29 | VisualBench::VisualBench(void* hwnd, int argc, char** argv) |
joshualitt | fb33ae1 | 2015-09-18 06:47:39 -0700 | [diff] [blame] | 30 | : INHERITED(hwnd) { |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 31 | SkCommandLineFlags::Parse(argc, argv); |
| 32 | |
joshualitt | 43f610f | 2015-11-10 08:27:22 -0800 | [diff] [blame] | 33 | SkDebugf("Command line arguments:"); |
| 34 | for (int i = 0; i < argc; ++i) { |
| 35 | SkDebugf("%s\n", argv[i]); |
| 36 | } |
| 37 | |
cdalton | c70483f | 2015-10-26 13:14:36 -0700 | [diff] [blame] | 38 | // 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 | } |
joshualitt | fb33ae1 | 2015-09-18 06:47:39 -0700 | [diff] [blame] | 44 | fModule.reset(new VisualLightweightBenchModule(this)); |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 45 | if (FLAGS_interactive) { |
| 46 | fModule.reset(new VisualInteractiveModule(this)); |
| 47 | } |
| 48 | |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 49 | this->setTitle(); |
| 50 | this->setupBackend(); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | VisualBench::~VisualBench() { |
joshualitt | fb02cda | 2015-10-21 08:04:24 -0700 | [diff] [blame] | 54 | this->tearDownContext(); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void VisualBench::setTitle() { |
| 58 | SkString title("VisualBench"); |
| 59 | INHERITED::setTitle(title.c_str()); |
| 60 | } |
| 61 | |
| 62 | SkSurface* VisualBench::createSurface() { |
joshualitt | d4fa90f | 2015-07-15 06:18:57 -0700 | [diff] [blame] | 63 | 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()); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | bool VisualBench::setupBackend() { |
| 73 | this->setColorType(kRGBA_8888_SkColorType); |
| 74 | this->setVisibleP(true); |
| 75 | this->setClipToBounds(false); |
| 76 | |
bsalomon | 45171e2 | 2015-06-16 13:52:18 -0700 | [diff] [blame] | 77 | if (FLAGS_fullscreen) { |
| 78 | if (!this->makeFullscreen()) { |
| 79 | SkDebugf("Could not go fullscreen!"); |
| 80 | } |
bsalomon | 85ab551 | 2015-06-16 12:47:25 -0700 | [diff] [blame] | 81 | } |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 82 | |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 83 | this->resetContext(); |
| 84 | return true; |
| 85 | } |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 86 | |
joshualitt | 7fe8ee4 | 2015-06-01 10:03:54 -0700 | [diff] [blame] | 87 | void VisualBench::resetContext() { |
joshualitt | fb02cda | 2015-10-21 08:04:24 -0700 | [diff] [blame] | 88 | this->tearDownContext(); |
| 89 | this->setupContext(); |
| 90 | } |
| 91 | |
| 92 | void 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 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 101 | fSurface.reset(nullptr); |
joshualitt | 31b21f6 | 2015-07-16 13:40:51 -0700 | [diff] [blame] | 102 | |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 103 | fInterface.reset(GrGLCreateNativeInterface()); |
joshualitt | 5a74afe | 2015-10-06 08:05:10 -0700 | [diff] [blame] | 104 | |
joshualitt | c9dd93c | 2015-10-15 09:49:31 -0700 | [diff] [blame] | 105 | // TODO use the GLContext creation factories and also set this all up in configs |
| 106 | if (!FLAGS_nvpr) { |
| 107 | fInterface.reset(GrGLInterfaceRemoveNVPR(fInterface)); |
| 108 | } |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 109 | 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(); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 117 | } |
| 118 | |
joshualitt | fb02cda | 2015-10-21 08:04:24 -0700 | [diff] [blame] | 119 | void 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 | |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 130 | void VisualBench::setupRenderTarget() { |
joshualitt | 030dc84 | 2015-06-12 12:51:44 -0700 | [diff] [blame] | 131 | if (fContext) { |
| 132 | fRenderTarget.reset(this->renderTarget(fAttachmentInfo, fInterface, fContext)); |
| 133 | } |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 134 | } |
| 135 | |
bsalomon | 45171e2 | 2015-06-16 13:52:18 -0700 | [diff] [blame] | 136 | void VisualBench::draw(SkCanvas* canvas) { |
joshualitt | 189aef7 | 2015-09-08 07:08:11 -0700 | [diff] [blame] | 137 | fModule->draw(canvas); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 138 | |
| 139 | // Invalidate the window to force a redraw. Poor man's animation mechanism. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 140 | this->inval(nullptr); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 141 | } |
| 142 | |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 143 | void 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 | |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 152 | void VisualBench::onSizeChange() { |
| 153 | this->setupRenderTarget(); |
| 154 | } |
| 155 | |
| 156 | bool VisualBench::onHandleChar(SkUnichar unichar) { |
jvanverth | f5d1b2d | 2015-09-15 07:40:56 -0700 | [diff] [blame] | 157 | static const auto kEscKey = 27; |
| 158 | if (kEscKey == unichar) { |
| 159 | this->closeWindow(); |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | return fModule->onHandleChar(unichar); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Externally declared entry points |
| 167 | void application_init() { |
| 168 | SkGraphics::Init(); |
| 169 | SkEvent::Init(); |
| 170 | } |
| 171 | |
| 172 | void application_term() { |
| 173 | SkEvent::Term(); |
joshualitt | da7b843 | 2015-05-27 09:19:03 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { |
| 177 | return new VisualBench(hwnd, argc, argv); |
| 178 | } |
| 179 | |