Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

devdraw: cocoa metal screen adds a delayed update #270

Merged
merged 2 commits into from
Jun 19, 2019
Merged

Conversation

jxy
Copy link
Contributor

@jxy jxy commented Jun 19, 2019

The immediate display of the screen sometimes miss the update from
the CPU side memory. No obvious synchronization mechanism is available.
In order to make sure the screen updates properly, we set needsDisplay
again after a minimal delay to ensure a second screen update.

Fix #268 for the time being and I will see if I can fix it properly.

The immediate display of the screen sometimes miss the update from
the CPU side memory.  No obvious synchronization mechanism is available.
In order to make sure the screen updates properly, we set needsDisplay
again after a minimal delay to ensure a second screen update.
@jacobvosmaer
Copy link
Contributor

I don't know why but with this patch applied I can still get missed screen updates.

@jxy
Copy link
Contributor Author

jxy commented Jun 19, 2019

Can you try adding a qlock as in #268 (comment) ?

@jacobvosmaer
Copy link
Contributor

Added this, does not help.

diff --git a/src/cmd/devdraw/cocoa-screen-metal.m b/src/cmd/devdraw/cocoa-screen-metal.m
index 1588992f..6ef95af7 100644
--- a/src/cmd/devdraw/cocoa-screen-metal.m
+++ b/src/cmd/devdraw/cocoa-screen-metal.m
@@ -67,6 +67,7 @@ static DrawLayer *layer;
 static id<MTLDevice> device;
 static id<MTLCommandQueue> commandQueue;
 static id<MTLTexture> texture;
+static QLock texturel;
 
 static Memimage *img = NULL;
 
@@ -795,6 +796,7 @@ struct Cursors {
 
 	LOG(@"display got drawable");
 
+	qlock(&texturel);
 	blit = [cbuf blitCommandEncoder];
 	[blit copyFromTexture:texture
 		sourceSlice:0
@@ -811,6 +813,7 @@ struct Cursors {
 	drawable = nil;
 }
 	[cbuf addCompletedHandler:^(id<MTLCommandBuffer> cmdBuff){
+		qunlock(&texturel);
 		if(cmdBuff.error){
 			NSLog(@"command buffer finished with error: %@",
 				cmdBuff.error.localizedDescription);
@@ -953,7 +956,9 @@ initimg(void)
 	textureDesc.allowGPUOptimizedContents = YES;
 	textureDesc.usage = MTLTextureUsageShaderRead;
 	textureDesc.cpuCacheMode = MTLCPUCacheModeWriteCombined;
+	qlock(&texturel);
 	texture = [device newTextureWithDescriptor:textureDesc];
+	qunlock(&texturel);
 
 	scale = [win backingScaleFactor];
 	[layer setDrawableSize:size];
@@ -981,11 +986,13 @@ _flushmemscreen(Rectangle r)
 	}
 
 	@autoreleasepool{
+		qlock(&texturel);
 		[texture
 			replaceRegion:MTLRegionMake2D(r.min.x, r.min.y, Dx(r), Dy(r))
 			mipmapLevel:0
 			withBytes:byteaddr(img, Pt(r.min.x, r.min.y))
 			bytesPerRow:img->width*sizeof(u32int)];
+		qunlock(&texturel);
 		[AppDelegate
 			performSelectorOnMainThread:@selector(callsetNeedsDisplayInRect:)
 			withObject:[NSValue valueWithRect:NSMakeRect(r.min.x, r.min.y, Dx(r), Dy(r))]

@jxy
Copy link
Contributor Author

jxy commented Jun 19, 2019

How about increasing the dispatch time?

@jacobvosmaer
Copy link
Contributor

16ms dispatch + qlock: OK.

16ms dispatch without qlock: OK

@rsc rsc merged commit e995a0c into 9fans:master Jun 19, 2019
@rsc
Copy link
Contributor

rsc commented Jun 19, 2019

I changed the code to 16ms and merged this commit. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

devdraw: acme missed screen updates
3 participants