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 <brianosman@google.com>
Commit-Queue: Jim Van Verth <jvanverth@google.com>
This commit is contained in:
Jim Van Verth 2019-02-08 13:44:42 -05:00 committed by Skia Commit-Bot
parent 58a2e3d7ff
commit 3e8392e42c
5 changed files with 25 additions and 41 deletions

View File

@ -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<const GrGLInterface> 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;

View File

@ -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<SkSurface> fBackbufferSurface;
@ -122,7 +110,7 @@ sk_sp<const GrGLInterface> 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;

View File

@ -16,6 +16,8 @@ namespace sk_app {
class Window_mac : public Window {
public:
static SkTDArray<Window_mac*> 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();

View File

@ -27,7 +27,7 @@ using sk_app::Window;
namespace sk_app {
static int gWindowCount = 0;
SkTDArray<Window_mac*> 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<sk_app::Window_mac*>(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];
}

View File

@ -8,6 +8,7 @@
#import <Cocoa/Cocoa.h>
#include "../Application.h"
#include "Window_mac.h"
@interface AppDelegate : NSObject<NSApplicationDelegate, NSWindowDelegate>
@ -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