diff options
author | joshualitt <joshualitt@chromium.org> | 2015-05-27 09:19:03 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-05-27 09:19:03 -0700 |
commit | da7b843fbd263e3421eef27c833206024dae8528 (patch) | |
tree | 2d6f9062e1638d8c18c7eaf40bff716472347886 | |
parent | a681433d42323d6d48e1aa3cb2ef97fb9d958d93 (diff) |
CL to add setFullscreen and setVsync to SkWindow
BUG=skia:
Review URL: https://codereview.chromium.org/1151333004
-rw-r--r-- | gyp/most.gyp | 4 | ||||
-rw-r--r-- | gyp/visualbench.gyp | 60 | ||||
-rw-r--r-- | include/views/SkOSWindow_Unix.h | 3 | ||||
-rw-r--r-- | include/views/SkWindow.h | 4 | ||||
-rw-r--r-- | src/views/unix/SkOSWindow_Unix.cpp | 61 | ||||
-rw-r--r-- | tools/VisualBench.cpp | 147 | ||||
-rw-r--r-- | tools/VisualBench.h | 59 |
7 files changed, 338 insertions, 0 deletions
diff --git a/gyp/most.gyp b/gyp/most.gyp index 27ce0f1689..d674802238 100644 --- a/gyp/most.gyp +++ b/gyp/most.gyp @@ -27,11 +27,13 @@ 'pathops_skpclip.gyp:*', # 'pdfviewer.gyp:pdfviewer', 'dm.gyp:dm', + 'visualbench.gyp:visualbench', ], 'conditions': [ [ 'skia_gpu == 0 or skia_os == "android"', { 'dependencies!': [ 'example.gyp:HelloWorld', + 'visualbench.gyp:visualbench', ], }], ['skia_os == "android"', { @@ -41,6 +43,7 @@ 'dependencies!': [ 'example.gyp:HelloWorld', 'SampleApp.gyp:SampleApp', + 'visualbench.gyp:visualbench', ], 'dependencies': ['iOSShell.gyp:iOSShell' ], }], @@ -52,6 +55,7 @@ 'dependencies!': [ 'example.gyp:HelloWorld', 'SampleApp.gyp:SampleApp', + 'visualbench.gyp:visualbench', ] } ] diff --git a/gyp/visualbench.gyp b/gyp/visualbench.gyp new file mode 100644 index 0000000000..ee0321a937 --- /dev/null +++ b/gyp/visualbench.gyp @@ -0,0 +1,60 @@ +# Copyright 2015 Google Inc. +# +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. +# GYP file to build visual bench tool +{ + 'targets': [ + { + 'target_name': 'visualbench', + 'type': 'executable', + 'mac_bundle' : 1, + 'include_dirs' : [ + '../include/gpu', + '../src/images', + ], + 'sources': [ + '../tools/VisualBench.h', + '../tools/VisualBench.cpp', + '../src/images/SkForceLinking.cpp', + ], + 'dependencies': [ + 'flags.gyp:flags_common', + 'images.gyp:images', + 'skia_lib.gyp:skia_lib', + 'tools.gyp:timer', + 'views.gyp:views', + ], + 'conditions' : [ + [ 'skia_os == "win"', { + 'sources' : [ + '../src/views/win/SkOSWindow_Win.cpp', + '../src/views/win/skia_win.cpp', + ], + }], + [ 'skia_os == "mac"', { + 'sources': [ + '../example/mac/HelloWorldNSView.mm', + '../example/mac/HelloWorldDelegate.mm', + + '../src/views/mac/SkEventNotifier.mm', + '../src/views/mac/skia_mac.mm', + '../src/views/mac/SkNSView.mm', + '../src/views/mac/SkOptionsTableView.mm', + '../src/views/mac/SkOSWindow_Mac.mm', + '../src/views/mac/SkTextFieldCell.m', + ], + 'include_dirs' : [ + '../src/views/mac/' + ], + 'xcode_settings' : { + 'INFOPLIST_FILE' : '../example/mac/HelloWorld-Info.plist', + }, + 'mac_bundle_resources' : [ + '../example/mac/HelloWorld.xib' + ], + }], + ], + }, + ], +} diff --git a/include/views/SkOSWindow_Unix.h b/include/views/SkOSWindow_Unix.h index 2811ff855c..78670cde13 100644 --- a/include/views/SkOSWindow_Unix.h +++ b/include/views/SkOSWindow_Unix.h @@ -46,6 +46,9 @@ public: //static bool PostEvent(SkEvent* evt, SkEventSinkID, SkMSec delay); + void setFullscreen(bool) override; + void setVsync(bool) override; + protected: // Overridden from from SkWindow: void onSetTitle(const char title[]) override; diff --git a/include/views/SkWindow.h b/include/views/SkWindow.h index 748e6da967..70cbcc19e1 100644 --- a/include/views/SkWindow.h +++ b/include/views/SkWindow.h @@ -79,6 +79,10 @@ public: virtual void onPDFSaved(const char title[], const char desc[], const char path[]) {} + + virtual void setFullscreen(bool) {} + virtual void setVsync(bool) {} + protected: virtual bool onEvent(const SkEvent&); virtual bool onDispatchClick(int x, int y, Click::State, void* owner, unsigned modi); diff --git a/src/views/unix/SkOSWindow_Unix.cpp b/src/views/unix/SkOSWindow_Unix.cpp index e22377a264..ea2d60b0f3 100644 --- a/src/views/unix/SkOSWindow_Unix.cpp +++ b/src/views/unix/SkOSWindow_Unix.cpp @@ -326,6 +326,29 @@ void SkOSWindow::mapWindowAndWait() { } +//////////////////////////////////////////////// + +// Some helper code to load the correct version of glXSwapInterval +#define GLX_GET_PROC_ADDR(name) glXGetProcAddress(reinterpret_cast<const GLubyte*>((name))) +#define EXT_WRANGLE(name, type, ...) \ + if (GLX_GET_PROC_ADDR(#name)) { \ + static type k##name; \ + if (!k##name) { \ + k##name = (type) GLX_GET_PROC_ADDR(#name); \ + } \ + k##name(__VA_ARGS__); \ + SkDebugf("using %s\n", #name); \ + return; \ + } + +static void glXSwapInterval(Display* dsp, GLXDrawable drawable, int interval) { + EXT_WRANGLE(glXSwapIntervalEXT, PFNGLXSWAPINTERVALEXTPROC, dsp, drawable, interval); + EXT_WRANGLE(glXSwapIntervalMESA, PFNGLXSWAPINTERVALMESAPROC, interval); + EXT_WRANGLE(glXSwapIntervalSGI, PFNGLXSWAPINTERVALSGIPROC, interval); +} + +///////////////////////////////////////////////////////////////////////// + bool SkOSWindow::attach(SkBackEndTypes, int msaaSampleCount, AttachmentInfo* info) { this->initWindow(msaaSampleCount, info); @@ -427,6 +450,44 @@ void SkOSWindow::doPaint() { width, height); } +enum { + _NET_WM_STATE_REMOVE =0, + _NET_WM_STATE_ADD = 1, + _NET_WM_STATE_TOGGLE =2 +}; + +void SkOSWindow::setFullscreen(bool setFullscreen) { + Display* dsp = fUnixWindow.fDisplay; + if (NULL == dsp) { + return; + } + Window win = fUnixWindow.fWin; + + // Full screen + Atom wm_state = XInternAtom(dsp, "_NET_WM_STATE", False); + Atom fullscreen = XInternAtom(dsp, "_NET_WM_STATE_FULLSCREEN", False); + + XEvent evt; + sk_bzero(&evt, sizeof(evt)); + evt.type = ClientMessage; + evt.xclient.window = win; + evt.xclient.message_type = wm_state; + evt.xclient.format = 32; + evt.xclient.data.l[0] = setFullscreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE; + evt.xclient.data.l[1] = fullscreen; + evt.xclient.data.l[2] = 0; + + XSendEvent(dsp, DefaultRootWindow(dsp), False, + SubstructureRedirectMask | SubstructureNotifyMask, &evt); +} + +void SkOSWindow::setVsync(bool vsync) { + if (fUnixWindow.fDisplay && fUnixWindow.fGLContext && fUnixWindow.fWin) { + int swapInterval = vsync ? 1 : 0; + glXSwapInterval(fUnixWindow.fDisplay, fUnixWindow.fWin, swapInterval); + } +} + /////////////////////////////////////////////////////////////////////////////// void SkEvent::SignalNonEmptyQueue() { diff --git a/tools/VisualBench.cpp b/tools/VisualBench.cpp new file mode 100644 index 0000000000..3f8d8036aa --- /dev/null +++ b/tools/VisualBench.cpp @@ -0,0 +1,147 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + */ + +#include "VisualBench.h" + +#include "SkApplication.h" +#include "SkCanvas.h" +#include "SkCommandLineFlags.h" +#include "SkCommonFlags.h" +#include "SkForceLinking.h" +#include "SkGraphics.h" +#include "SkGr.h" +#include "SkImageDecoder.h" +#include "SkOSFile.h" +#include "SkStream.h" +#include "Timer.h" +#include "gl/GrGLInterface.h" + +__SK_FORCE_IMAGE_DECODER_LINKING; + +VisualBench::VisualBench(void* hwnd, int argc, char** argv) + : INHERITED(hwnd) + , fCurrentLoops(1) + , fCurrentPicture(0) + , fCurrentFrame(0) { + SkCommandLineFlags::Parse(argc, argv); + + SkTArray<SkString> skps; + for (int i = 0; i < FLAGS_skps.count(); i++) { + if (SkStrEndsWith(FLAGS_skps[i], ".skp")) { + skps.push_back() = FLAGS_skps[i]; + } else { + SkOSFile::Iter it(FLAGS_skps[i], ".skp"); + SkString path; + while (it.next(&path)) { + skps.push_back() = SkOSPath::Join(FLAGS_skps[0], path.c_str()); + } + } + } + + this->setTitle(); + this->setupBackend(); + + // Load picture for playback + for (int i = 0; i < skps.count(); i++) { + SkFILEStream stream(skps[i].c_str()); + if (stream.isValid()) { + fPictures.push_back(SkPicture::CreateFromStream(&stream)); + } else { + SkDebugf("couldn't load picture at \"path\"\n", skps[i].c_str()); + } + } +} + +VisualBench::~VisualBench() { + for (int i = 0; i < fPictures.count(); i++) { + fPictures[i]->~SkPicture(); + } + INHERITED::detach(); +} + +void VisualBench::setTitle() { + SkString title("VisualBench"); + INHERITED::setTitle(title.c_str()); +} + +SkSurface* VisualBench::createSurface() { + SkSurfaceProps props(INHERITED::getSurfaceProps()); + return SkSurface::NewRenderTargetDirect(fRenderTarget, &props); +} + +bool VisualBench::setupBackend() { + this->setColorType(kRGBA_8888_SkColorType); + this->setVisibleP(true); + this->setClipToBounds(false); + + if (!this->attach(kNativeGL_BackEndType, 0 /*msaa*/, &fAttachmentInfo)) { + SkDebugf("Not possible to create backend.\n"); + INHERITED::detach(); + return false; + } + + this->setFullscreen(true); + this->setVsync(false); + + fInterface.reset(GrGLCreateNativeInterface()); + SkASSERT(fInterface); + + // setup contexts + fContext.reset(GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fInterface.get())); + SkASSERT(fContext); + + // setup rendertargets + this->setupRenderTarget(); + return true; +} + +void VisualBench::setupRenderTarget() { + fRenderTarget.reset(this->renderTarget(fAttachmentInfo, fInterface, fContext)); +} + +void VisualBench::draw(SkCanvas* canvas) { + fCurrentFrame++; + WallTimer timer; + timer.start(); + for (int i = 0; i < fCurrentLoops; i++) { + canvas->drawPicture(fPictures[fCurrentPicture]); + } + // in case we have queued drawing calls + fContext->flush(); + INHERITED::present(); + timer.end(); + + SkDebugf("%s\n", HumanizeMs(timer.fWall).c_str()); + + // Invalidate the window to force a redraw. Poor man's animation mechanism. + this->inval(NULL); +} + +void VisualBench::onSizeChange() { + this->setupRenderTarget(); +} + +bool VisualBench::onHandleChar(SkUnichar unichar) { + return true; +} + +// Externally declared entry points +void application_init() { + SkGraphics::Init(); + SkEvent::Init(); +} + +void application_term() { + SkEvent::Term(); + SkGraphics::Term(); +} + +SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { + return new VisualBench(hwnd, argc, argv); +} + diff --git a/tools/VisualBench.h b/tools/VisualBench.h new file mode 100644 index 0000000000..d9e0b50866 --- /dev/null +++ b/tools/VisualBench.h @@ -0,0 +1,59 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + */ + +#ifndef VisualBench_DEFINED +#define VisualBench_DEFINED + +#include "SkWindow.h" + +#include "SkPicture.h" +#include "SkSurface.h" +#include "gl/SkGLContext.h" + +class GrContext; +struct GrGLInterface; +class GrRenderTarget; +class SkCanvas; + +/* + * A Visual benchmarking tool for gpu benchmarking + */ +class VisualBench : public SkOSWindow { +public: + VisualBench(void* hwnd, int argc, char** argv); + ~VisualBench() override; + +protected: + SkSurface* createSurface() override; + + void draw(SkCanvas* canvas) override; + + void onSizeChange() override; + +private: + void setTitle(); + bool setupBackend(); + void setupRenderTarget(); + bool onHandleChar(SkUnichar unichar) override; + + int fCurrentLoops; + int fCurrentPicture; + int fCurrentFrame; + SkTArray<SkPicture*> fPictures; + + // support framework + SkAutoTUnref<SkSurface> fSurface; + SkAutoTUnref<GrContext> fContext; + SkAutoTUnref<GrRenderTarget> fRenderTarget; + AttachmentInfo fAttachmentInfo; + SkAutoTUnref<const GrGLInterface> fInterface; + + typedef SkOSWindow INHERITED; +}; + +#endif |