Skip to content

Commit

Permalink
ios: fix scaling for Metal view
Browse files Browse the repository at this point in the history
  • Loading branch information
z-dule committed Aug 14, 2024
1 parent 92a9e99 commit 9b203db
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions iosx/src/flowmgr/AVSVideoView_Metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ @implementation AVSVideoView {
int _chromaWidth;
int _chromaHeight;

dispatch_semaphore_t _gpuSemaphore;
//dispatch_semaphore_t _gpuSemaphore;

int _oldRotation;
int _oldWidth;
int _oldHeight;

NSLock *_lock;
CGRect _viewFrame;
CGSize _viewFrame;
BOOL _shouldFill;
BOOL _forceRecalc;
}
Expand Down Expand Up @@ -80,7 +80,7 @@ - (id)init
}
[self setupLayer];

_gpuSemaphore = dispatch_semaphore_create(1);
//_gpuSemaphore = dispatch_semaphore_create(1);

NSLog(@"AVSVideoView_metal: compiling source...\n");
id<MTLLibrary> metalLib = [_metalDevice newLibraryWithSource:g_ShaderSrc options:nil error:&err];
Expand Down Expand Up @@ -162,9 +162,9 @@ - (CALayer *)makeBackingLayer
- (void)getVertexData:(struct avs_vidframe *)vf
buffer:(float *)buffer
{
CGRect frame = _viewFrame; //self.frame;
float dw = (float)frame.size.width;
float dh = (float)frame.size.height;
CGSize frame = _viewFrame; //self.frame;
float dw = (float)frame.width;
float dh = (float)frame.height;

float vw = vf->w;
float vh = vf->h;
Expand Down Expand Up @@ -317,16 +317,16 @@ - (void)render:(CADisplayLink*)displayLink
}
[_lock unlock];

__block dispatch_semaphore_t blockSem = _gpuSemaphore;
//__block dispatch_semaphore_t blockSem = _gpuSemaphore;

if (!_renderPassDescriptor || _viewFrame.size.width == 0 || _viewFrame.size.height == 0) {
dispatch_semaphore_signal(blockSem);
if (!_renderPassDescriptor || _viewFrame.width == 0 || _viewFrame.height == 0) {
//dispatch_semaphore_signal(blockSem);
return;
}

id<CAMetalDrawable> currentDrawable = [_metalLayer nextDrawable];
if (!currentDrawable) {
dispatch_semaphore_signal(blockSem);
//dispatch_semaphore_signal(blockSem);
return;
}

Expand All @@ -339,7 +339,7 @@ - (void)render:(CADisplayLink*)displayLink
if (_newFrame > 0)
_newFrame--;
[_lock unlock];
dispatch_semaphore_signal(blockSem);
//dispatch_semaphore_signal(blockSem);
}];

_renderPassDescriptor.colorAttachments[0].texture = currentDrawable.texture;
Expand Down Expand Up @@ -375,14 +375,14 @@ - (BOOL)handleFrame:(struct avs_vidframe *)frame
//NSLog(@"handleFrame: frame=%dx%d\n", frame->w, frame->h);
@autoreleasepool {
// Wait until any pending GPU work is done
__block dispatch_semaphore_t blockSem = _gpuSemaphore;
dispatch_semaphore_wait(blockSem, DISPATCH_TIME_FOREVER);
//_block dispatch_semaphore_t blockSem = _gpuSemaphore;
//dispatch_semaphore_wait(blockSem, DISPATCH_TIME_FOREVER);
if ([self setupTexturesForFrame:frame]) {
[_lock lock];
_newFrame++;
[_lock unlock];
} else {
dispatch_semaphore_signal(blockSem);
//dispatch_semaphore_signal(blockSem);
}
}

Expand Down Expand Up @@ -469,10 +469,19 @@ - (BOOL) shouldFill
- (void)layoutSubviews
{
[_lock lock];
_viewFrame = self.frame;
CGFloat scale = [UIScreen mainScreen].scale;
_metalLayer.drawableSize = CGSizeMake(self.bounds.size.width * scale,
self.bounds.size.height * scale);

_viewFrame = _metalLayer.drawableSize;
_forceRecalc = YES;
[_lock unlock];


info("layoutSubviews: bounds=%dx%d frame=%dx%d drawable=%dx%d scale=%f\n",
(int)self.bounds.size.width, (int)self.bounds.size.height,
(int)self.frame.size.width, (int)self.frame.size.height,
(int)_metalLayer.drawableSize.width, (int)_metalLayer.drawableSize.height,
scale);
}

-(void)dealloc
Expand Down

0 comments on commit 9b203db

Please sign in to comment.