Additional cocoa events; Metal present fix

This commit is contained in:
Jack Andersen 2015-12-31 10:20:38 -10:00
parent 53ad4df63d
commit ce9d877cfa
2 changed files with 101 additions and 53 deletions

View File

@ -604,9 +604,13 @@ struct MetalCommandQueue : IGraphicsCommandQueue
void setScissor(const SWindowRect& rect) void setScissor(const SWindowRect& rect)
{ {
MTLScissorRect scissor = {NSUInteger(rect.location[0]), NSUInteger(rect.location[1]), if (m_boundTarget)
NSUInteger(rect.size[0]), NSUInteger(rect.size[1])}; {
[m_enc.get() setScissorRect:scissor]; 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<MetalTextureR*, std::pair<size_t, size_t>> m_texResizes; std::unordered_map<MetalTextureR*, std::pair<size_t, size_t>> m_texResizes;
@ -677,46 +681,10 @@ struct MetalCommandQueue : IGraphicsCommandQueue
instanceCount:instCount]; instanceCount:instCount];
} }
MetalTextureR* m_needsDisplay = nullptr;
void resolveDisplay(ITextureR* source) void resolveDisplay(ITextureR* source)
{ {
MetalContext::Window& w = m_ctx->m_windows[m_parentWindow]; m_needsDisplay = static_cast<MetalTextureR*>(source);
MetalTextureR* csource = static_cast<MetalTextureR*>(source);
[m_enc.get() endEncoding];
m_enc.reset();
@autoreleasepool
{
{
std::unique_lock<std::mutex> lk(w.m_resizeLock);
if (w.m_needsResize)
{
w.m_metalLayer.drawableSize = w.m_size;
w.m_needsResize = NO;
return;
}
}
id<CAMetalDrawable> drawable = [w.m_metalLayer nextDrawable];
if (drawable)
{
id<MTLTexture> dest = drawable.texture;
if (csource->m_tex.get().width == dest.width &&
csource->m_tex.get().height == dest.height)
{
id<MTLBlitCommandEncoder> 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];
}
}
}
} }
bool m_inProgress = false; bool m_inProgress = false;
@ -739,6 +707,9 @@ struct MetalCommandQueue : IGraphicsCommandQueue
@autoreleasepool @autoreleasepool
{ {
[m_enc.get() endEncoding];
m_enc.reset();
/* Abandon if in progress (renderer too slow) */ /* Abandon if in progress (renderer too slow) */
if (m_inProgress) if (m_inProgress)
{ {
@ -756,6 +727,47 @@ struct MetalCommandQueue : IGraphicsCommandQueue
return; return;
} }
/* Wrap up and present if needed */
if (m_needsDisplay)
{
MetalContext::Window& w = m_ctx->m_windows[m_parentWindow];
@autoreleasepool
{
{
std::unique_lock<std::mutex> lk(w.m_resizeLock);
if (w.m_needsResize)
{
w.m_metalLayer.drawableSize = w.m_size;
w.m_needsResize = NO;
m_needsDisplay = nullptr;
return;
}
}
id<CAMetalDrawable> drawable = [w.m_metalLayer nextDrawable];
if (drawable)
{
id<MTLTexture> dest = drawable.texture;
if (m_needsDisplay->m_tex.get().width == dest.width &&
m_needsDisplay->m_tex.get().height == dest.height)
{
id<MTLBlitCommandEncoder> 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_drawBuf = m_fillBuf;
m_fillBuf ^= 1; m_fillBuf ^= 1;

View File

@ -448,7 +448,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a
- (BOOL)hasMarkedText - (BOOL)hasMarkedText
{ {
NSLog(@"hasMarkedText");
if (booContext->m_callback) if (booContext->m_callback)
{ {
boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback();
@ -460,7 +459,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a
- (NSRange)markedRange - (NSRange)markedRange
{ {
NSLog(@"markedRange");
if (booContext->m_callback) if (booContext->m_callback)
{ {
boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback();
@ -475,7 +473,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a
- (NSRange)selectedRange - (NSRange)selectedRange
{ {
NSLog(@"selectedRange");
if (booContext->m_callback) if (booContext->m_callback)
{ {
boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); 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 - (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) if (booContext->m_callback)
{ {
boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback();
@ -511,7 +505,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a
- (void)unmarkText - (void)unmarkText
{ {
NSLog(@"unmarkText");
if (booContext->m_callback) if (booContext->m_callback)
{ {
boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback();
@ -527,7 +520,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a
- (NSAttributedString*)attributedSubstringForProposedRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange - (NSAttributedString*)attributedSubstringForProposedRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange
{ {
NSLog(@"attributedSubstringForProposedRange");
if (booContext->m_callback) if (booContext->m_callback)
{ {
boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback();
@ -549,7 +541,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a
- (void)insertText:(id)aString replacementRange:(NSRange)replacementRange - (void)insertText:(id)aString replacementRange:(NSRange)replacementRange
{ {
NSLog(@"insertText %@ [%lu,%lu]", aString, replacementRange.location, replacementRange.length);
if (booContext->m_callback) if (booContext->m_callback)
{ {
boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback();
@ -567,7 +558,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a
- (NSUInteger)characterIndexForPoint:(NSPoint)aPoint - (NSUInteger)characterIndexForPoint:(NSPoint)aPoint
{ {
NSLog(@"characterIndexForPoint");
if (booContext->m_callback) if (booContext->m_callback)
{ {
boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback();
@ -586,7 +576,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a
- (NSRect)firstRectForCharacterRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange - (NSRect)firstRectForCharacterRange:(NSRange)aRange actualRange:(NSRangePointer)actualRange
{ {
NSLog(@"firstRectForCharacterRange");
if (booContext->m_callback) if (booContext->m_callback)
{ {
boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback(); boo::ITextInputCallback* textCb = booContext->m_callback->getTextInputCallback();
@ -607,7 +596,6 @@ IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI a
- (void)doCommandBySelector:(SEL)aSelector - (void)doCommandBySelector:(SEL)aSelector
{ {
NSLog(@"doCommandBySelector %@", NSStringFromSelector(aSelector));
} }
static inline boo::EModifierKey getMod(NSUInteger flags) static inline boo::EModifierKey getMod(NSUInteger flags)
@ -783,6 +771,38 @@ static inline boo::EMouseButton getButton(NSEvent* event)
[self mouseMoved:theEvent]; [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 - (void)scrollWheel:(NSEvent*)theEvent
{ {
if (!booContext->m_callback) if (!booContext->m_callback)
@ -1074,6 +1094,14 @@ static boo::ESpecialKey translateKeycode(short code)
[self setOpenGLContext:sharedCtx]; [self setOpenGLContext:sharedCtx];
[sharedCtx setView:self]; [sharedCtx setView:self];
} }
NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect
options:(NSTrackingMouseEnteredAndExited |
NSTrackingMouseMoved |
NSTrackingActiveAlways |
NSTrackingInVisibleRect)
owner:self
userInfo:nil];
[self addTrackingArea:trackingArea];
return self; return self;
} }
@ -1112,6 +1140,14 @@ static boo::ESpecialKey translateKeycode(short code)
self = [self initWithFrame:NSMakeRect(0, 0, 100, 100)]; self = [self initWithFrame:NSMakeRect(0, 0, 100, 100)];
[self setWantsLayer:YES]; [self setWantsLayer:YES];
resp = [[BooCocoaResponder alloc] initWithBooContext:bctx View:self]; 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; return self;
} }