From 3e8392e42c783bf9d20d8128186e4c6fb54d570b Mon Sep 17 00:00:00 2001 From: Jim Van Verth Date: Fri, 8 Feb 2019 13:44:42 -0500 Subject: [PATCH] Manage Mac paint events more directly. Trying to shuttle our paint events through the Mac event system seems to be causing a number of problems, one being that we're refreshing too often. This works around that by tracking the invalidation flag per window. Bug: skia:8737 Change-Id: Ib1b8041f1d9299ff366747831a466439bfd0db29 Reviewed-on: https://skia-review.googlesource.com/c/190673 Reviewed-by: Brian Osman Commit-Queue: Jim Van Verth --- tools/sk_app/mac/GLWindowContext_mac.mm | 16 ++-------------- tools/sk_app/mac/RasterWindowContext_mac.mm | 16 ++-------------- tools/sk_app/mac/Window_mac.h | 5 ++++- tools/sk_app/mac/Window_mac.mm | 18 ++++++------------ tools/sk_app/mac/main_mac.mm | 11 +++++++++++ 5 files changed, 25 insertions(+), 41 deletions(-) diff --git a/tools/sk_app/mac/GLWindowContext_mac.mm b/tools/sk_app/mac/GLWindowContext_mac.mm index 8fc957991c..78f495f239 100644 --- a/tools/sk_app/mac/GLWindowContext_mac.mm +++ b/tools/sk_app/mac/GLWindowContext_mac.mm @@ -17,18 +17,6 @@ using sk_app::DisplayParams; using sk_app::window_context_factory::MacWindowInfo; using sk_app::GLWindowContext; -@interface GLView : NSOpenGLView -@end - -@implementation GLView - -- (void)drawRect:(NSRect)dirtyRect { - // not sure why the parent isn't getting this, but we'll pass it up - [[self superview] drawRect:dirtyRect]; -} - -@end - namespace { class GLWindowContext_mac : public GLWindowContext { @@ -44,7 +32,7 @@ public: private: NSView* fMainView; - GLView* fGLView; + NSOpenGLView* fGLView; NSOpenGLContext* fGLContext; NSOpenGLPixelFormat* fPixelFormat; @@ -111,7 +99,7 @@ sk_sp GLWindowContext_mac::onInitializeContext() { // create view NSRect rect = fMainView.bounds; - fGLView = [[GLView alloc] initWithFrame:rect]; + fGLView = [[NSOpenGLView alloc] initWithFrame:rect]; if (nil == fGLView) { [fGLContext release]; fGLContext = nil; diff --git a/tools/sk_app/mac/RasterWindowContext_mac.mm b/tools/sk_app/mac/RasterWindowContext_mac.mm index 8eddc82b36..42b135b04b 100644 --- a/tools/sk_app/mac/RasterWindowContext_mac.mm +++ b/tools/sk_app/mac/RasterWindowContext_mac.mm @@ -21,18 +21,6 @@ using sk_app::DisplayParams; using sk_app::window_context_factory::MacWindowInfo; using sk_app::GLWindowContext; -@interface RasterView : NSOpenGLView -@end - -@implementation RasterView - -- (void)drawRect:(NSRect)dirtyRect { - // not sure why the parent isn't getting this, but we'll pass it up - [[self superview] drawRect:dirtyRect]; -} - -@end - namespace { // TODO: This still uses GL to handle the update rather than using a purely raster backend, @@ -53,7 +41,7 @@ public: private: NSView* fMainView; - RasterView* fRasterView; + NSOpenGLView* fRasterView; NSOpenGLContext* fGLContext; NSOpenGLPixelFormat* fPixelFormat; sk_sp fBackbufferSurface; @@ -122,7 +110,7 @@ sk_sp RasterWindowContext_mac::onInitializeContext() { // create view NSRect rect = fMainView.bounds; - fRasterView = [[RasterView alloc] initWithFrame:rect]; + fRasterView = [[NSOpenGLView alloc] initWithFrame:rect]; if (nil == fRasterView) { [fGLContext release]; fGLContext = nil; diff --git a/tools/sk_app/mac/Window_mac.h b/tools/sk_app/mac/Window_mac.h index cef3546237..5fd1fe5d71 100644 --- a/tools/sk_app/mac/Window_mac.h +++ b/tools/sk_app/mac/Window_mac.h @@ -16,6 +16,8 @@ namespace sk_app { class Window_mac : public Window { public: + static SkTDArray gActiveWindows; + Window_mac() : INHERITED() , fWindow(nil) @@ -31,7 +33,8 @@ public: bool attach(BackendType) override; - void onInval() override; + void onInval() override {} + bool needsPaint() { return this->fIsContentInvalidated; } NSView* view() { return [fWindow contentView]; } void closeWindow(); diff --git a/tools/sk_app/mac/Window_mac.mm b/tools/sk_app/mac/Window_mac.mm index 2c3f175b5b..0693cecaf8 100644 --- a/tools/sk_app/mac/Window_mac.mm +++ b/tools/sk_app/mac/Window_mac.mm @@ -27,7 +27,7 @@ using sk_app::Window; namespace sk_app { -static int gWindowCount = 0; +SkTDArray Window_mac::gActiveWindows; Window* Window::CreateNativeWindow(void*) { Window_mac* window = new Window_mac(); @@ -36,7 +36,7 @@ Window* Window::CreateNativeWindow(void*) { return nullptr; } - ++gWindowCount; + Window_mac::gActiveWindows.push_back(window); return window; } @@ -116,14 +116,6 @@ bool Window_mac::attach(BackendType attachType) { return (SkToBool(fWindowContext)); } -void Window_mac::onInval() { - [[fWindow contentView] setNeedsDisplay:YES]; - // MacOS already queues a single drawRect event for multiple invalidations - // so we don't need to use our invalidation method (and it can mess things up - // if for some reason MacOS skips a drawRect when we need one). - this->markInvalProcessed(); -} - } // namespace sk_app /////////////////////////////////////////////////////////////////////////////// @@ -146,8 +138,10 @@ void Window_mac::onInval() { } - (BOOL)windowShouldClose:(NSWindow*)sender { - --sk_app::gWindowCount; - if (sk_app::gWindowCount < 1) { + sk_app::Window_mac* macWindow = reinterpret_cast(fWindow); + int windowIndex = sk_app::Window_mac::gActiveWindows.find(macWindow); + sk_app::Window_mac::gActiveWindows.remove(windowIndex); + if (sk_app::Window_mac::gActiveWindows.count() < 1) { [NSApp terminate:self]; } diff --git a/tools/sk_app/mac/main_mac.mm b/tools/sk_app/mac/main_mac.mm index 1cfb8a99ee..71b154186c 100644 --- a/tools/sk_app/mac/main_mac.mm +++ b/tools/sk_app/mac/main_mac.mm @@ -8,6 +8,7 @@ #import #include "../Application.h" +#include "Window_mac.h" @interface AppDelegate : NSObject @@ -39,6 +40,7 @@ /////////////////////////////////////////////////////////////////////////////////////////// using sk_app::Application; +using sk_app::Window_mac; int main(int argc, char * argv[]) { #if MAC_OS_X_VERSION_MAX_ALLOWED < 1070 @@ -79,6 +81,15 @@ int main(int argc, char * argv[]) { // Now we process the events while (![appDelegate done]) { + // Rather than depending on a Mac event to drive this, we treat our window + // invalidation flag as a separate event stream. Window::onPaint() will clear + // the invalidation flag, effectively removing it from the stream. + for (int i = 0; i < Window_mac::gActiveWindows.count(); ++i) { + if (Window_mac::gActiveWindows[i]->needsPaint()) { + Window_mac::gActiveWindows[i]->onPaint(); + } + } + NSEvent* event; do { event = [NSApp nextEventMatchingMask:NSAnyEventMask