From ce9d877cfaeb63f8335acd36517b3026e6728d46 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Thu, 31 Dec 2015 10:20:38 -1000 Subject: [PATCH] Additional cocoa events; Metal present fix --- lib/graphicsdev/Metal.mm | 94 ++++++++++++++++++++++------------------ lib/mac/WindowCocoa.mm | 60 ++++++++++++++++++++----- 2 files changed, 101 insertions(+), 53 deletions(-) diff --git a/lib/graphicsdev/Metal.mm b/lib/graphicsdev/Metal.mm index 7a0efac..148b264 100644 --- a/lib/graphicsdev/Metal.mm +++ b/lib/graphicsdev/Metal.mm @@ -604,9 +604,13 @@ struct MetalCommandQueue : IGraphicsCommandQueue void setScissor(const SWindowRect& rect) { - MTLScissorRect scissor = {NSUInteger(rect.location[0]), NSUInteger(rect.location[1]), - NSUInteger(rect.size[0]), NSUInteger(rect.size[1])}; - [m_enc.get() setScissorRect:scissor]; + if (m_boundTarget) + { + MTLScissorRect scissor = {NSUInteger(rect.location[0]), + NSUInteger(m_boundTarget->m_height - rect.location[1] - rect.size[1]), + NSUInteger(rect.size[0]), NSUInteger(rect.size[1])}; + [m_enc.get() setScissorRect:scissor]; + } } std::unordered_map> m_texResizes; @@ -677,46 +681,10 @@ struct MetalCommandQueue : IGraphicsCommandQueue instanceCount:instCount]; } + MetalTextureR* m_needsDisplay = nullptr; void resolveDisplay(ITextureR* source) { - MetalContext::Window& w = m_ctx->m_windows[m_parentWindow]; - - MetalTextureR* csource = static_cast(source); - [m_enc.get() endEncoding]; - m_enc.reset(); - @autoreleasepool - { - { - std::unique_lock lk(w.m_resizeLock); - if (w.m_needsResize) - { - w.m_metalLayer.drawableSize = w.m_size; - w.m_needsResize = NO; - return; - } - } - id drawable = [w.m_metalLayer nextDrawable]; - if (drawable) - { - id dest = drawable.texture; - if (csource->m_tex.get().width == dest.width && - csource->m_tex.get().height == dest.height) - { - id blitEnc = [m_cmdBuf.get() blitCommandEncoder]; - [blitEnc copyFromTexture:csource->m_tex.get() - sourceSlice:0 - sourceLevel:0 - sourceOrigin:MTLOriginMake(0, 0, 0) - sourceSize:MTLSizeMake(dest.width, dest.height, 1) - toTexture:dest - destinationSlice:0 - destinationLevel:0 - destinationOrigin:MTLOriginMake(0, 0, 0)]; - [blitEnc endEncoding]; - [m_cmdBuf.get() presentDrawable:drawable]; - } - } - } + m_needsDisplay = static_cast(source); } bool m_inProgress = false; @@ -739,6 +707,9 @@ struct MetalCommandQueue : IGraphicsCommandQueue @autoreleasepool { + [m_enc.get() endEncoding]; + m_enc.reset(); + /* Abandon if in progress (renderer too slow) */ if (m_inProgress) { @@ -756,6 +727,47 @@ struct MetalCommandQueue : IGraphicsCommandQueue return; } + /* Wrap up and present if needed */ + if (m_needsDisplay) + { + MetalContext::Window& w = m_ctx->m_windows[m_parentWindow]; + @autoreleasepool + { + { + std::unique_lock lk(w.m_resizeLock); + if (w.m_needsResize) + { + w.m_metalLayer.drawableSize = w.m_size; + w.m_needsResize = NO; + m_needsDisplay = nullptr; + return; + } + } + id drawable = [w.m_metalLayer nextDrawable]; + if (drawable) + { + id dest = drawable.texture; + if (m_needsDisplay->m_tex.get().width == dest.width && + m_needsDisplay->m_tex.get().height == dest.height) + { + id blitEnc = [m_cmdBuf.get() blitCommandEncoder]; + [blitEnc copyFromTexture:m_needsDisplay->m_tex.get() + sourceSlice:0 + sourceLevel:0 + sourceOrigin:MTLOriginMake(0, 0, 0) + sourceSize:MTLSizeMake(dest.width, dest.height, 1) + toTexture:dest + destinationSlice:0 + destinationLevel:0 + destinationOrigin:MTLOriginMake(0, 0, 0)]; + [blitEnc endEncoding]; + [m_cmdBuf.get() presentDrawable:drawable]; + } + } + } + m_needsDisplay = nullptr; + } + m_drawBuf = m_fillBuf; m_fillBuf ^= 1; diff --git a/lib/mac/WindowCocoa.mm b/lib/mac/WindowCocoa.mm index 49b9538..1406825 100644 --- a/lib/mac/WindowCocoa.mm +++ b/lib/mac/WindowCocoa.mm @@ -448,7 +448,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a - (BOOL)hasMarkedText { - NSLog(@"hasMarkedText"); if (booContext->m_callback) { boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); @@ -460,7 +459,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a - (NSRange)markedRange { - NSLog(@"markedRange"); if (booContext->m_callback) { boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); @@ -475,7 +473,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a - (NSRange)selectedRange { - NSLog(@"selectedRange"); if (booContext->m_callback) { boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); @@ -490,9 +487,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a - (void)setMarkedText:(id)aString selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange { - NSLog(@"setMarkedText %@ [%lu,%lu] [%lu,%lu]", aString, - selectedRange.location, selectedRange.length, - replacementRange.location, replacementRange.length); if (booContext->m_callback) { boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); @@ -511,7 +505,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a - (void)unmarkText { - NSLog(@"unmarkText"); if (booContext->m_callback) { boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); @@ -527,7 +520,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a - (NSAttributedString*)attributedSubstringForProposedRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange { - NSLog(@"attributedSubstringForProposedRange"); if (booContext->m_callback) { boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); @@ -549,7 +541,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a - (void)insertText:(id)aString replacementRange:(NSRange)replacementRange { - NSLog(@"insertText %@ [%lu,%lu]", aString, replacementRange.location, replacementRange.length); if (booContext->m_callback) { boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); @@ -567,7 +558,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a - (NSUInteger)characterIndexForPoint:(NSPoint)aPoint { - NSLog(@"characterIndexForPoint"); if (booContext->m_callback) { boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); @@ -586,7 +576,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a - (NSRect)firstRectForCharacterRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange { - NSLog(@"firstRectForCharacterRange"); if (booContext->m_callback) { boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); @@ -607,7 +596,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a - (void)doCommandBySelector:(SEL)aSelector { - NSLog(@"doCommandBySelector %@", NSStringFromSelector(aSelector)); } static inline boo::EModifierKey getMod(NSUInteger flags) @@ -783,6 +771,38 @@ static inline boo::EMouseButton getButton(NSEvent* event) [self mouseMoved:theEvent]; } +- (void)mouseEntered:(NSEvent*)theEvent +{ + if (!booContext->m_callback) + return; + NSPoint liw = [parentView convertPoint:[theEvent locationInWindow] fromView:nil]; + float pixelFactor = [[parentView window] backingScaleFactor]; + NSRect frame = [parentView frame]; + boo::SWindowCoord coord = + { + {int(liw.x * pixelFactor), int(liw.y * pixelFactor)}, + {int(liw.x), int(liw.y)}, + {float(liw.x / frame.size.width), float(liw.y / frame.size.height)} + }; + booContext->m_callback->mouseEnter(coord); +} + +- (void)mouseExited:(NSEvent*)theEvent +{ + if (!booContext->m_callback) + return; + NSPoint liw = [parentView convertPoint:[theEvent locationInWindow] fromView:nil]; + float pixelFactor = [[parentView window] backingScaleFactor]; + NSRect frame = [parentView frame]; + boo::SWindowCoord coord = + { + {int(liw.x * pixelFactor), int(liw.y * pixelFactor)}, + {int(liw.x), int(liw.y)}, + {float(liw.x / frame.size.width), float(liw.y / frame.size.height)} + }; + booContext->m_callback->mouseLeave(coord); +} + - (void)scrollWheel:(NSEvent*)theEvent { if (!booContext->m_callback) @@ -1074,6 +1094,14 @@ static boo::ESpecialKey translateKeycode(short code) [self setOpenGLContext:sharedCtx]; [sharedCtx setView:self]; } + NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect + options:(NSTrackingMouseEnteredAndExited | + NSTrackingMouseMoved | + NSTrackingActiveAlways | + NSTrackingInVisibleRect) + owner:self + userInfo:nil]; + [self addTrackingArea:trackingArea]; return self; } @@ -1112,6 +1140,14 @@ static boo::ESpecialKey translateKeycode(short code) self = [self initWithFrame:NSMakeRect(0, 0, 100, 100)]; [self setWantsLayer:YES]; resp = [[BooCocoaResponder alloc] initWithBooContext:bctx View:self]; + NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect + options:(NSTrackingMouseEnteredAndExited | + NSTrackingMouseMoved | + NSTrackingActiveAlways | + NSTrackingInVisibleRect) + owner:self + userInfo:nil]; + [self addTrackingArea:trackingArea]; return self; }