文件
dayu-color/大鱼调色/Plugin/DYToolTrackingDiagnostics.inc.h
T

260 行
34 KiB
C++

{
DYDiagnosticManager *base=[[[DYDiagnosticManager alloc]init]autorelease];DYCreateParameters(base,&error);DYToolTrackingProbe *host=[[[DYToolTrackingProbe alloc]init]autorelease];host->_base=base;host->_time=kCMTimeZero;host->_state=kFxAnalysisState_NotAnalyzing;
NSMutableDictionary *doc=DYMutablePlist([[[DYToolDocument alloc]init]autorelease].document),*mask=DYMutablePlist(DYNewMask(DYMaskRectangle));DYMaskParameters p=DYMaskAtTime(mask,kCMTimeZero);p.width=.65;p.height=.65;p.feather=0;mask[@"parameters"]=[NSData dataWithBytes:&p length:sizeof(p)];doc[@"baseMasks"][@"hsl"]=[NSMutableArray arrayWithObject:mask];DYWriteTools(base,doc,kCMTimeZero);
id<DYToolHostProbe>plugin=(id)[[NSClassFromString(@"DYPlugIn")alloc]initWithAPIManager:host];[(NSObject*)plugin setValue:@YES forKey:@"_toolCaptureEnabled"];
const NSUInteger tw=320,th=192;DYDiagnosticTile *frame=[[[DYDiagnosticTile alloc]initWithDevice:gpu width:tw height:th]autorelease],*out=[[[DYDiagnosticTile alloc]initWithDevice:gpu width:tw height:th]autorelease];
auto setFrame=[&](int step){Pixels pixels(tw*th*4);for(NSUInteger y=0;y<th;y++)for(NSUInteger x=0;x<tw;x++){int xx=(int)x-step*2,yy=(int)y-step;uint32_t n=(uint32_t)(xx/3)*73856093u^(uint32_t)(yy/3)*19349663u;n^=n>>13;n*=1274126177u;float code=.15f+.7f*((n>>8)&255)/255.f,linear=code<=.04045f?code/12.92f:powf((code+.055f)/1.055f,2.4f);auto index=(y*tw+x)*4;pixels[index]=pixels[index+1]=pixels[index+2]=linear;pixels[index+3]=1;}[[frame metalTextureForDevice:gpu]replaceRegion:MTLRegionMake2D(0,0,tw,th) mipmapLevel:0 withBytes:pixels.data() bytesPerRow:tw*8];};
auto timeAt=[](int frame){return CMTimeMake(frame*1001,24000);};auto capture=[&](int step){setFrame(step);host->_time=timeAt(step);NSData *state=nil;[plugin pluginState:&state atTime:host->_time quality:(FxQuality)0 error:&error];return [plugin renderDestinationImage:out sourceImages:@[frame] pluginState:state atTime:host->_time error:&error];};
auto request=[&](int direction){if(direction!=9)host->_state=kFxAnalysisState_NotAnalyzing;[plugin toolTrackRequested:[NSNotification notificationWithName:@"DYToolTrack" object:host userInfo:@{@"node":@"base:hsl",@"mask":mask[@"id"],@"direction":@(direction)}]];};
auto drain=[&](){double end=NSProcessInfo.processInfo.systemUptime+.15;while(NSProcessInfo.processInfo.systemUptime<end)[[NSRunLoop mainRunLoop]runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:.005]];};CMTimeRange inputRange=CMTimeRangeMake(kCMTimeZero,CMTimeMake(6*1001,24000)),desired;
NSMutableArray *updates=[NSMutableArray array];
id observer=[NSNotificationCenter.defaultCenter addObserverForName:@"DYToolTrackingStatus" object:host queue:nil usingBlock:^(NSNotification *note){if(note.userInfo[@"tracking"])[updates addObject:note.userInfo];}];
host->_time=kCMTimeInvalid;request(1);
run.record(@"tracking-invalid-host-time-does-not-start-analysis",host->_forwardStarts==0&&host->_state==kFxAnalysisState_NotAnalyzing);
run.need(@"tracking-renders-valid-source-before-analysis",capture(0));run.record(@"inactive-tracking-avoids-full-frame-motion-readback",[[plugin valueForKey:@"toolCaptureDevices"]count]==0);int undoBefore=base->_undoStarts;request(1);BOOL callbacks=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:&error]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:&error];
for(int i=0;i<6&&callbacks;i++){setFrame(i);callbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:&error];}host->_state=kFxAnalysisState_AnalysisCompleted;[plugin cleanupAnalysis:nil];drain();NSDictionary *tracked=DYReadTools(base,kCMTimeZero).document[@"baseMasks"][@"hsl"][0];DYMaskParameters end=DYMaskAtTime(tracked,timeAt(5));run.record(@"host-callback-vision-tracking-six-exact-frames",callbacks&&[tracked[@"keys"]count]==6&&fabs(end.centerX-(.5+10./tw))<.004&&fabs(end.centerY-(.5+5./th))<.004,@{@"keyCount":@([tracked[@"keys"]count]),@"centerX":@(end.centerX),@"centerY":@(end.centerY),@"error":reason(error)});
NSDictionary *lastPreview=updates.count>=2?updates[updates.count-2]:nil;
DYMaskParameters shown{};
[lastPreview[@"shape"] getBytes:&shown length:sizeof(shown)];
NSData *previewPixels=lastPreview[@"rgba"];
run.record(@"mask-analysis-emits-matching-frame-geometry-and-completed-progress",
[lastPreview[@"completed"]intValue]==6&&[lastPreview[@"total"]intValue]==6&&
fabs([lastPreview[@"time"]doubleValue]-CMTimeGetSeconds(timeAt(5)))<1e-9&&
previewPixels.length==tw*th*4&&shown.centerX==end.centerX&&shown.centerY==end.centerY&&
!lastPreview[@"sourceShape"]&&![updates.lastObject[@"tracking"]boolValue]&&host->_seekCount==0);
run.record(@"tracking-results-use-one-undo-action",base->_undoStarts==undoBefore+1&&base->_undoStarts==base->_undoEnds);BOOL captureFreed=YES;for(NSDictionary *entry in [[plugin valueForKey:@"toolCaptureDevices"]allValues])captureFreed&=!entry[@"encoded"]&&!entry[@"bytes"]&&!entry[@"buffer"]&&!entry[@"previewBuffer"];run.record(@"completed-tracking-releases-4k-gpu-analysis-working-images",captureFreed);int writes=base->_undoStarts;[plugin cleanupAnalysis:nil];[plugin cleanupAnalysis:nil];drain();run.record(@"duplicate-analysis-cleanup-never-commits-twice",base->_undoStarts==writes);
// Mid-analysis author edits must reject the result rather than clobber them.
capture(0);request(1);callbacks=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];for(int i=0;i<3&&callbacks;i++){setFrame(i);callbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}NSMutableDictionary *changed=DYMutablePlist(DYReadTools(base,kCMTimeZero).document);changed[@"baseMasks"][@"hsl"][0][@"enabled"]=@NO;DYWriteTools(base,changed,kCMTimeZero);[plugin cleanupAnalysis:nil];drain();run.record(@"stale-analysis-cannot-overwrite-edited-mask",[DYReadTools(base,kCMTimeZero).document isEqual:changed]&&base->_undoStarts==writes);
DYWriteTools(base,doc,kCMTimeZero);capture(0);request(1);callbacks=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];for(int i=0;i<3&&callbacks;i++){setFrame(i);callbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}request(9);setFrame(3);NSError *cancelError=nil;BOOL stopped=![plugin analyzeFrame:frame atTime:timeAt(3) error:&cancelError];drain();tracked=DYReadTools(base,kCMTimeZero).document[@"baseMasks"][@"hsl"][0];run.record(@"cancel-keeps-only-contiguous-trusted-segment",callbacks&&stopped&&[tracked[@"keys"]count]==3&&base->_undoStarts==writes+1);run.record(@"cancel-sends-sdk-quiet-analysis-termination",[cancelError.domain isEqual:FxPlugErrorDomain]&&cancelError.code==kFxError_UserCancelled);
[updates removeAllObjects];
DYWriteTools(base,doc,kCMTimeZero);capture(2);host->_state=kFxAnalysisState_NotAnalyzing;request(0);callbacks=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];for(int i=2;i<6&&callbacks;i++){setFrame(i);callbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}[plugin cleanupAnalysis:nil];drain();BOOL backward=host->_backwardStarts==1;callbacks&=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];for(int i=2;i>=0&&callbacks;i--){setFrame(i);callbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}[plugin cleanupAnalysis:nil];drain();tracked=DYReadTools(base,kCMTimeZero).document[@"baseMasks"][@"hsl"][0];run.record(@"bidirectional-passes-share-reference-and-merge-once",callbacks&&backward&&[tracked[@"keys"]count]==6&&base->_undoStarts==writes+2);
NSUInteger previous=0;BOOL progress=YES,sawForward=NO,sawBackward=NO;
for(NSDictionary *update in updates){
if(!update[@"shape"])continue;
NSUInteger current=[update[@"completed"]unsignedIntegerValue];
progress&=current>=previous&&[update[@"total"]intValue]==6;
if([update[@"backward"]boolValue])sawBackward=YES;else sawForward=YES;
previous=current;
}
run.record(@"bidirectional-progress-counts-reference-once-and-never-regresses",progress&&sawForward&&sawBackward&&previous==6);
DYWriteTools(base,doc,kCMTimeZero);capture(2);host->_timelineOffset=CMTimeMake(4,1);host->_state=kFxAnalysisState_NotAnalyzing;request(-2);BOOL range=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&CMTimeCompare(desired.start,timeAt(1))==0&&CMTimeCompare(desired.duration,timeAt(2))==0;[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];for(int i=2;i>=1;i--){setFrame(i);[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}setFrame(0);NSError *singleStopError=nil;stopped=![plugin analyzeFrame:frame atTime:timeAt(0) error:&singleStopError];drain();tracked=DYReadTools(base,kCMTimeZero).document[@"baseMasks"][@"hsl"][0];run.record(@"backward-single-step-includes-reference-and-one-neighbor",range&&stopped&&[tracked[@"keys"]count]==2);run.record(@"single-step-sends-sdk-quiet-analysis-termination",[singleStopError.domain isEqual:FxPlugErrorDomain]&&singleStopError.code==kFxError_UserCancelled);
run.record(@"backward-single-step-seeks-timeline-with-clip-offset",host->_seekCount==1&&CMTimeCompare(host->_seekTime,CMTimeAdd(CMTimeMake(4,1),timeAt(1)))==0&&CMTimeCompare(host->_time,timeAt(1))==0);
DYWriteTools(base,doc,kCMTimeZero);capture(2);BOOL repeated=YES;for(int first=2;first<4;first++){request(2);repeated&=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];for(int i=first;i<=first+1&&repeated;i++){setFrame(i);repeated&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}[plugin cleanupAnalysis:nil];drain();repeated&=CMTimeCompare(host->_time,timeAt(first+1))==0;}run.record(@"repeated-forward-single-step-continues-source-frames-at-offset-clip",repeated&&host->_seekCount==3&&CMTimeCompare(host->_seekTime,CMTimeAdd(CMTimeMake(4,1),timeAt(4)))==0);
int priorWrites=0;for(int repeat=0;repeat<2;repeat++){priorWrites=base->_undoStarts;capture(2);request(2);callbacks=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];for(int i=2;i<=3&&callbacks;i++){setFrame(i);callbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}[plugin cleanupAnalysis:nil];drain();}run.record(@"unchanged-single-step-still-advances-without-duplicate-author-write",callbacks&&host->_seekCount==5&&CMTimeCompare(host->_time,timeAt(3))==0&&base->_undoStarts==priorWrites,@{@"seekCount":@(host->_seekCount),@"writesBefore":@(priorWrites),@"writesAfter":@(base->_undoStarts)});
// An expected loss must end the host job, not only our local tracking state.
DYWriteTools(base,doc,kCMTimeZero);capture(0);request(1);callbacks=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];for(int i=0;i<3&&callbacks;i++){setFrame(i);callbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}
Pixels flat(tw*th*4,.1f);for(size_t i=3;i<flat.size();i+=4)flat[i]=1;[[frame metalTextureForDevice:gpu]replaceRegion:MTLRegionMake2D(0,0,tw,th) mipmapLevel:0 withBytes:flat.data() bytesPerRow:tw*8];NSError *lossError=nil;stopped=![plugin analyzeFrame:frame atTime:timeAt(3) error:&lossError];drain();tracked=DYReadTools(base,kCMTimeZero).document[@"baseMasks"][@"hsl"][0];run.record(@"texture-loss-keeps-prefix-and-sends-sdk-quiet-termination",callbacks&&stopped&&[tracked[@"keys"]count]==3&&[lossError.domain isEqual:FxPlugErrorDomain]&&lossError.code==kFxError_UserCancelled);
DYWriteTools(base,doc,kCMTimeZero);capture(0);request(-1);NSError *boundaryError=nil;callbacks=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:&boundaryError]&&CMTimeCompare(desired.start,kCMTimeZero)==0&&CMTimeCompare(desired.duration,timeAt(1))==0&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:&boundaryError];setFrame(0);callbacks&=[plugin analyzeFrame:frame atTime:kCMTimeZero error:&boundaryError];host->_state=kFxAnalysisState_AnalysisCompleted;[plugin cleanupAnalysis:nil];drain();run.record(@"boundary-seed-only-range-completes-without-author-write-or-rejected-range",callbacks&&!boundaryError&&[DYReadTools(base,kCMTimeZero).document isEqual:doc]);
NSError *lateError=nil;stopped=![plugin analyzeFrame:frame atTime:timeAt(0) error:&lateError];run.record(@"late-analysis-frame-also-terminates-host-job-quietly",stopped&&[lateError.domain isEqual:FxPlugErrorDomain]&&lateError.code==kFxError_UserCancelled);
// A host cache hit can move the playhead without re-entering renderDestinationImage.
// Starting at a middle frame must use analyzer input even on a fresh instance.
DYWriteTools(base,doc,kCMTimeZero);id<DYToolHostProbe>cold=(id)[[NSClassFromString(@"DYPlugIn")alloc]initWithAPIManager:host];host->_time=timeAt(2);host->_state=kFxAnalysisState_NotAnalyzing;int starts=host->_forwardStarts;
[cold toolTrackRequested:[NSNotification notificationWithName:@"DYToolTrack" object:host userInfo:@{@"node":@"base:hsl",@"mask":mask[@"id"],@"direction":@1}]];
callbacks=host->_forwardStarts==starts+1&&[cold desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&CMTimeCompare(desired.start,timeAt(2))==0&&[cold setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];
for(int i=2;i<6&&callbacks;i++){setFrame(i);callbacks&=[cold analyzeFrame:frame atTime:timeAt(i) error:nil];}[cold cleanupAnalysis:nil];drain();tracked=DYReadTools(base,kCMTimeZero).document[@"baseMasks"][@"hsl"][0];run.record(@"middle-frame-start-with-no-preview-render-cache",callbacks&&[tracked[@"keys"]count]==4&&CMTimeCompare(CMTimeMake([tracked[@"keys"][0][@"value"]longLongValue],[tracked[@"keys"][0][@"scale"]intValue]),timeAt(2))==0);
[cold release];
DYWriteTools(base,doc,kCMTimeZero);capture(0);host->_time=timeAt(2);request(1);callbacks=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];
for(int i=2;i<6&&callbacks;i++){setFrame(i);callbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}[plugin cleanupAnalysis:nil];drain();tracked=DYReadTools(base,kCMTimeZero).document[@"baseMasks"][@"hsl"][0];run.record(@"middle-frame-start-ignores-stale-preview-cache",callbacks&&[tracked[@"keys"]count]==4);
DYWriteTools(base,doc,kCMTimeZero);capture(0);request(1);callbacks=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];
// Deliberately different preview pixels must not veto an authoritative analysis seed.
for(int i=0;i<3&&callbacks;i++){setFrame(i+2);callbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}[plugin cleanupAnalysis:nil];drain();tracked=DYReadTools(base,kCMTimeZero).document[@"baseMasks"][@"hsl"][0];run.record(@"analysis-seed-does-not-require-preview-pixel-equality",callbacks&&[tracked[@"keys"]count]==3);
{
const NSUInteger w=3840,h=2160;DYDiagnosticTile *fine=[[[DYDiagnosticTile alloc]initWithDevice:gpu width:w height:h]autorelease];Pixels pixels(w*h*4);
for(NSUInteger y=0;y<h;y++)for(NSUInteger x=0;x<w;x++){float encoded=x%3==1?.8f:.2f,linear=powf((encoded+.055f)/1.055f,2.4f);auto i=(y*w+x)*4;pixels[i]=pixels[i+1]=pixels[i+2]=linear;pixels[i+3]=1;}
[[fine metalTextureForDevice:gpu]replaceRegion:MTLRegionMake2D(0,0,w,h) mipmapLevel:0 withBytes:pixels.data() bytesPerRow:w*8];auto gray=[plugin toolGrayFrame:fine error:nil];double maximum=0;for(NSUInteger y=0;y<(NSUInteger)gray.height;y++)for(NSUInteger x=0;x<(NSUInteger)gray.width;x++){double code=x%3==1?.8:.2;double expected=std::round(std::pow(std::pow((code+.055)/1.055,2.4),1/2.2)*255)/255.;maximum=std::max(maximum,fabs(double(gray.pixels[y*gray.width+x])-expected));}
run.record(@"tracking-4k-frame-preserves-full-detail-for-vision-crops",gray.valid()&&gray.width==3840&&gray.height==2160&&maximum<.002,@{@"width":@(gray.width),@"height":@(gray.height),@"maximumError":@(maximum)});
auto compact=[plugin toolGrayFrame:fine preview:nullptr error:nil];BOOL identical=compact.valid()&&compact.pixels.empty()&&compact.bytes.size()==w*h;
for(size_t i=0;i<compact.bytes.size()&&identical;++i)identical=compact.at(i)==gray.pixels[i];
run.record(@"tracking-metal-gray8-compact-readback-matches-float-reference",identical,@{@"readbackBytes":@(compact.bytes.size()),@"previousReadbackBytes":@(gray.pixels.size()*sizeof(float))});
id<DYToolHostProbe> previewOnly=[(id)[[NSClassFromString(@"DYPlugIn") alloc] initWithAPIManager:host] autorelease];
NSDictionary *color=nil,*referenceColor=nil;
BOOL colorOK=[previewOnly captureTrackingTile:fine gray:nullptr preview:&color error:nil];
auto full=[plugin toolGrayFrame:fine preview:&referenceColor error:nil];
BOOL noGrayBuffer=YES;for(NSDictionary *entry in [[previewOnly valueForKey:@"toolCaptureDevices"] allValues])noGrayBuffer&=entry[@"buffer"]==nil;
run.record(@"mosaic-tracking-4k-color-input-matches-without-gray-buffer",colorOK&&full.valid()&&color[@"rgba"]&&[color isEqual:referenceColor]&&noGrayBuffer);
}
{
const NSUInteger w=323,h=181;
DYDiagnosticTile *tile=[[[DYDiagnosticTile alloc]initWithDevice:gpu width:w height:h]autorelease];
Pixels color(w*h*4);
for(NSUInteger y=0;y<h;++y)for(NSUInteger x=0;x<w;++x){
const auto i=(y*w+x)*4;
color[i]=(x%23)/22.f;color[i+1]=(y%29)/28.f;color[i+2]=((x+y)%37)/36.f;color[i+3]=(x%7)/6.f;
}
[[tile metalTextureForDevice:gpu]replaceRegion:MTLRegionMake2D(0,0,w,h) mipmapLevel:0 withBytes:color.data() bytesPerRow:w*8];
id<DYToolHostProbe> legacy=[(id)[[NSClassFromString(@"DYPlugIn") alloc] initWithAPIManager:host] autorelease];
id<DYToolHostProbe> direct=[(id)[[NSClassFromString(@"DYPlugIn") alloc] initWithAPIManager:host] autorelease];
BOOL exact=tile!=nil;NSUInteger compared=0;
for(FxImageOrigin origin : {kFxImageOrigin_TOP_LEFT,kFxImageOrigin_BOTTOM_LEFT}){
[tile setDiagnosticImageOrigin:origin];
for(BOOL flip : {NO,YES}){@autoreleasepool{
NSDictionary *rgba=nil,*packet=nil;
[legacy captureTrackingTile:tile gray:nullptr preview:&rgba error:nil];
[direct captureTrackingTile:tile gray:nullptr preview:&packet visionFlip:@(flip) error:nil];
CVPixelBufferRef image=(CVPixelBufferRef)packet[@"visionImage"];
NSData *reference=rgba[@"rgba"];
if(!image||!CVPixelBufferGetIOSurface(image)||packet[@"rgba"]||reference.length!=w*h*4||
CVPixelBufferGetPixelFormatType(image)!=kCVPixelFormatType_32BGRA||
CVPixelBufferLockBaseAddress(image,kCVPixelBufferLock_ReadOnly)!=kCVReturnSuccess){exact=NO;continue;}
const auto *actual=static_cast<const uint8_t*>(CVPixelBufferGetBaseAddress(image));
const auto *expected=static_cast<const uint8_t*>(reference.bytes);
const size_t stride=CVPixelBufferGetBytesPerRow(image);
for(NSUInteger y=0;y<h;++y)for(NSUInteger x=0;x<w;++x){
const auto *a=actual+y*stride+x*4,*e=expected+((flip?h-1-y:y)*w+x)*4;
exact&=a[0]==e[2]&&a[1]==e[1]&&a[2]==e[0]&&a[3]==255;
}
CVPixelBufferUnlockBaseAddress(image,kCVPixelBufferLock_ReadOnly);
++compared;
}}
}
for(NSDictionary *entry in [[direct valueForKey:@"toolCaptureDevices"] allValues])exact&=!entry[@"previewBuffer"]&&!entry[@"buffer"];
run.record(@"tracking-vision-iosurface-matches-cpu-bgra-byte-for-byte",exact&&compared==4,@{@"comparisons":@(compared),@"width":@(w),@"height":@(h)});
}
// UI aliases of a shared object are one tracking target. Switching from the
// global editor to either linked module must not cancel a valid analysis.
{
NSMutableDictionary *sharedDoc=DYMutablePlist([[[DYToolDocument alloc]init]autorelease].document),*sharedMask=(id)DYAddSharedMask(sharedDoc,DYMaskRectangle);NSString *sharedID=sharedMask[@"id"];DYMaskParameters shape=DYMaskAtTime(sharedMask,kCMTimeZero);shape.width=shape.height=.65;shape.feather=0;sharedMask[@"parameters"]=[NSData dataWithBytes:&shape length:sizeof(shape)];DYSetSharedMaskAssigned(sharedDoc,sharedID,@"base:hsl",YES);DYSetSharedMaskAssigned(sharedDoc,sharedID,@"base:spider",YES);
DYWriteTools(base,sharedDoc,kCMTimeZero);capture(0);host->_state=kFxAnalysisState_NotAnalyzing;
[plugin toolTrackRequested:[NSNotification notificationWithName:@"DYToolTrack" object:host userInfo:@{@"node":@"base:hsl",@"mask":sharedID,@"direction":@1}]];int beforeShared=base->_undoStarts;
BOOL sharedCallbacks=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];
for(int i=0;i<3&&sharedCallbacks;i++){setFrame(i);sharedCallbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}
for(NSString *alias in @[@"shared",@"base:spider",@"base:hsl"])[NSNotificationCenter.defaultCenter postNotificationName:@"DYToolCanvasSelection" object:host userInfo:@{@"node":alias,@"mask":sharedID,@"document":DYReadTools(base,kCMTimeZero).document,@"draw":@NO}];
for(int i=3;i<6&&sharedCallbacks;i++){setFrame(i);sharedCallbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}host->_state=kFxAnalysisState_AnalysisCompleted;[plugin cleanupAnalysis:nil];drain();NSDictionary *saved=DYReadTools(base,kCMTimeZero).document;
run.record(@"shared-tracking-same-mask-switches-global-hsl-spider-without-interruption",sharedCallbacks&&[DYSharedMaskWithID(saved,sharedID)[@"keys"]count]==6&&DYMasksForTool(saved,@"base:hsl").firstObject==DYMasksForTool(saved,@"base:spider").firstObject&&base->_undoStarts==beforeShared+1&&base->_undoStarts==base->_undoEnds);
// Removing the original UUID binding cannot delete or invalidate the shared
// object while another module still references it.
NSMutableDictionary *deletable=DYMutablePlist(sharedDoc);NSDictionary *extra=DYInsertTool(deletable,@"hsl",@"base:hsl");NSString *extraID=extra[@"id"];DYSetSharedMaskAssigned(deletable,sharedID,@"base:hsl",NO);DYSetSharedMaskAssigned(deletable,sharedID,extraID,YES);DYWriteTools(base,deletable,kCMTimeZero);capture(0);host->_state=kFxAnalysisState_NotAnalyzing;
[plugin toolTrackRequested:[NSNotification notificationWithName:@"DYToolTrack" object:host userInfo:@{@"node":extraID,@"mask":sharedID,@"direction":@1}]];beforeShared=base->_undoStarts;sharedCallbacks=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];
for(int i=0;i<3&&sharedCallbacks;i++){setFrame(i);sharedCallbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}
NSMutableDictionary *removed=DYMutablePlist(DYReadTools(base,kCMTimeZero).document);BOOL removedAlias=DYRemoveTool(removed,extraID);DYWriteTools(base,removed,kCMTimeZero);[NSNotificationCenter.defaultCenter postNotificationName:@"DYToolCanvasSelection" object:host userInfo:@{@"node":@"base:spider",@"mask":sharedID,@"document":removed,@"draw":@NO}];
for(int i=3;i<6&&sharedCallbacks;i++){setFrame(i);sharedCallbacks&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}host->_state=kFxAnalysisState_AnalysisCompleted;[plugin cleanupAnalysis:nil];drain();saved=DYReadTools(base,kCMTimeZero).document;
run.record(@"shared-tracking-survives-removing-original-module-and-commits-one-object",removedAlias&&sharedCallbacks&&!DYToolWithID(saved,extraID)&&[DYSharedMaskWithID(saved,sharedID)[@"keys"]count]==6&&[DYMasksForTool(saved,@"base:spider").firstObject[@"id"]isEqual:sharedID]&&DYToolIDsForSharedMask(saved,sharedID).count==1&&base->_undoStarts==beforeShared+1&&base->_undoStarts==base->_undoEnds);
id panel=[[[NSClassFromString(@"DYToolMaskPanel")alloc]initWithManager:host nodeID:@"base:spider"]autorelease];[panel refreshWithDocument:saved time:kCMTimeZero];[panel performSelector:@selector(canvas:)withObject:nil];BOOL aliasActive=[[panel valueForKey:@"_canvasActive"]boolValue];
[NSNotificationCenter.defaultCenter postNotificationName:@"DYToolTrackingStatus" object:host userInfo:@{@"node":@"shared",@"mask":sharedID,@"message":@"共享跟踪状态"}];BOOL statusVisible=[[[panel valueForKey:@"_status"]stringValue]isEqual:@"共享跟踪状态"];
run.record(@"shared-mask-inspector-alias-activates-canvas-and-receives-canonical-status",aliasActive&&statusVisible&&[[(NSObject*)plugin valueForKey:@"_selectedToolNode"]isEqual:@"shared"]);
}
{
NSMutableDictionary *stampDoc=DYMutablePlist([[[DYToolDocument alloc]init]autorelease].document);
NSMutableDictionary *stamp=(id)DYAddSharedMask(stampDoc,DYMaskEllipse);
NSString *stampID=stamp[@"id"];
DYMaskParameters target=DYMaskAtTime(stamp,kCMTimeZero);
target.centerX=.75f;target.centerY=.5f;target.width=.32f;target.height=.54f;
stamp[@"parameters"]=[NSData dataWithBytes:&target length:sizeof(target)];
stamp[@"stampSource"]=@[@.25,@.5,@.32,@.54];
auto noise=[](int x,int y){
uint32_t n=uint32_t(x)*73856093u^uint32_t(y)*19349663u;
n^=n>>13;n*=1274126177u;
return .15f+.7f*((n>>8)&255)/255.f;
};
auto pairedFrame=[&](int step,bool flatSource){
Pixels pixels(tw*th*4);
for(NSUInteger y=0;y<th;y++)for(NSUInteger x=0;x<tw;x++){
bool source=x<tw/2;
float cx=tw*(source?.25f:.75f),cy=th*.5f,scale=1+step*(source?-.025f:.035f);
float u=(cx+(float(x)-cx-step*(source?-1.5f:2.f))/scale)/4;
float v=(cy+(float(y)-cy-step*(source?1.f:-.75f))/scale)/4;
int ix=int(floorf(u)),iy=int(floorf(v));float fx=u-ix,fy=v-iy;
float top=noise(ix,iy)*(1-fx)+noise(ix+1,iy)*fx;
float bottom=noise(ix,iy+1)*(1-fx)+noise(ix+1,iy+1)*fx;
float code=source&&flatSource?.4f:top*(1-fy)+bottom*fy;
float linear=powf((code+.055f)/1.055f,2.4f);
auto index=(y*tw+x)*4;pixels[index]=pixels[index+1]=pixels[index+2]=linear;pixels[index+3]=1;
}
[[frame metalTextureForDevice:gpu]replaceRegion:MTLRegionMake2D(0,0,tw,th) mipmapLevel:0 withBytes:pixels.data() bytesPerRow:tw*8];
};
auto requestStamp=[&](int start){
host->_time=timeAt(start);host->_state=kFxAnalysisState_NotAnalyzing;
[plugin toolTrackRequested:[NSNotification notificationWithName:@"DYToolTrack" object:host userInfo:@{@"node":@"shared",@"mask":stampID,@"direction":@1}]];
return [plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&
[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];
};
auto finish=[&](){host->_state=kFxAnalysisState_AnalysisCompleted;[plugin cleanupAnalysis:nil];drain();};
DYWriteTools(base,stampDoc,kCMTimeZero);
int beforeStamp=base->_undoStarts;
BOOL paired=requestStamp(0);
NSError *pairError=nil;
for(int i=0;i<6&&paired;i++){pairedFrame(i,false);paired&=[plugin analyzeFrame:frame atTime:timeAt(i) error:&pairError];}
finish();
NSDictionary *saved=DYSharedMaskWithID(DYReadTools(base,kCMTimeZero).document,stampID);
DYStampMotion motion=DYStampMotionAtTime(saved,timeAt(5));
DYMaskParameters covered=DYMaskAtTime(saved,timeAt(5));
NSArray *sampled=DYStampSourceAtTime(saved,timeAt(5));
lastPreview=updates.count>=2?updates[updates.count-2]:nil;
DYMaskParameters shownSource{},shownTarget{};
[lastPreview[@"sourceShape"] getBytes:&shownSource length:sizeof(shownSource)];
[lastPreview[@"shape"] getBytes:&shownTarget length:sizeof(shownTarget)];
run.record(@"clone-analysis-preview-displays-both-regions-on-the-same-frame",
[lastPreview[@"completed"]intValue]==6&&fabs([lastPreview[@"time"]doubleValue]-CMTimeGetSeconds(timeAt(5)))<1e-9&&
fabs(shownSource.centerX-[sampled[0]doubleValue])<1e-6&&shownTarget.centerX==covered.centerX&&
fabs(shownTarget.width-covered.width)<1e-6&&[lastPreview[@"rgba"]length]==tw*th*4);
run.record(@"host-clone-stamp-follows-two-independent-centers-and-scales",
paired&&[saved[@"keys"] count]==6&&motion.active&&
fabs(covered.centerX-(.75+10./tw))<.006&&fabs(covered.centerY-(.5-3.75/th))<.008&&
fabs([sampled[0] doubleValue]-(.25-7.5/tw))<.006&&fabs([sampled[1] doubleValue]-(.5+5./th))<.008&&
fabs(motion.targetScaleX-1.175)<.045&&fabs(motion.targetScaleY-1.175)<.045&&
fabs(motion.sourceScaleX-.875)<.04&&fabs(motion.sourceScaleY-.875)<.04&&base->_undoStarts==beforeStamp+1,
@{@"keys":@([saved[@"keys"] count]),@"source":sampled?:@[],@"target":@[@(covered.centerX),@(covered.centerY)],
@"sourceScale":@[@(motion.sourceScaleX),@(motion.sourceScaleY)],
@"targetScale":@[@(motion.targetScaleX),@(motion.targetScaleY)],@"error":reason(pairError)});
DYStampMotion restart=DYStampMotionAtTime(saved,timeAt(2));
paired=requestStamp(2);
for(int i=2;i<6&&paired;i++){pairedFrame(i,false);paired&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}
finish();
saved=DYSharedMaskWithID(DYReadTools(base,kCMTimeZero).document,stampID);
DYStampMotion resumed=DYStampMotionAtTime(saved,timeAt(2));
run.record(@"host-clone-stamp-restarts-from-current-paired-scale",
paired&&[saved[@"keys"] count]==6&&resumed.active&&restart.sourceScaleX==resumed.sourceScaleX&&restart.targetScaleX==resumed.targetScaleX);
DYWriteTools(base,stampDoc,kCMTimeZero);
paired=requestStamp(0);
for(int i=0;i<3&&paired;i++){pairedFrame(i,false);paired&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}
pairedFrame(3,true);NSError *sourceLoss=nil;
BOOL ended=![plugin analyzeFrame:frame atTime:timeAt(3) error:&sourceLoss];
finish();
saved=DYSharedMaskWithID(DYReadTools(base,kCMTimeZero).document,stampID);
run.record(@"host-clone-stamp-source-loss-stops-both-tracks-at-last-paired-frame",
paired&&ended&&[saved[@"keys"] count]==3&&[sourceLoss.domain isEqual:FxPlugErrorDomain]&&
sourceLoss.code==kFxError_UserCancelled&&[sourceLoss.localizedDescription containsString:@"取样区域"]);
}
{
NSMutableDictionary *objectDoc=DYMutablePlist([[[DYToolDocument alloc]init]autorelease].document),*objectMask=(id)DYAddSharedMask(objectDoc,DYMaskRectangle);
NSString *ID=objectMask[@"id"];DYMaskParameters shape=DYMaskAtTime(objectMask,kCMTimeZero);shape.width=shape.height=.45;shape.feather=0;objectMask[@"parameters"]=[NSData dataWithBytes:&shape length:sizeof(shape)];
objectDoc[@"mosaic"]=[NSMutableDictionary dictionaryWithDictionary:@{@"enabled":@YES,@"source":@"mask",@"maskIDs":@[ID],@"size":@16}];
run.record(@"mosaic-selected-mask-uses-object-tracking",DYMosaicTracksMask(objectDoc,ID)&&!DYMosaicTracksMask(objectDoc,@"missing"));
shape.kind=DYMaskBezier;objectMask[@"parameters"]=[NSData dataWithBytes:&shape length:sizeof(shape)];
run.record(@"bezier-mosaic-uses-vision-object-tracking",DYMosaicTracksMask(objectDoc,ID));
shape.kind=DYMaskRectangle;objectMask[@"parameters"]=[NSData dataWithBytes:&shape length:sizeof(shape)];
objectDoc[@"mosaic"][@"enabled"]=@NO;run.record(@"disabled-mosaic-uses-precise-vision-registration",!DYMosaicTracksMask(objectDoc,ID));objectDoc[@"mosaic"][@"enabled"]=@YES;
DYWriteTools(base,objectDoc,kCMTimeZero);capture(0);host->_state=kFxAnalysisState_NotAnalyzing;
[plugin toolTrackRequested:[NSNotification notificationWithName:@"DYToolTrack" object:host userInfo:@{@"node":@"shared",@"mask":ID,@"direction":@1}]];
BOOL ok=[plugin desiredAnalysisTimeRange:&desired forInputWithTimeRange:inputRange error:nil]&&[plugin setupAnalysisForTimeRange:desired frameDuration:timeAt(1) error:nil];
for(int i=0;i<6&&ok;i++){setFrame(i);ok&=[plugin analyzeFrame:frame atTime:timeAt(i) error:nil];}
run.record(@"mosaic-object-host-callbacks-produce-six-frames",ok&&[[(NSObject*)plugin valueForKey:@"trackingSamples"]count]==6);
// Simulate cancellation while two provisional predictions are awaiting a
// confirmed endpoint. Those keys must never reach the host document.
[(NSObject*)plugin setValue:@2 forKey:@"trackingObjectPending"];[plugin cleanupAnalysis:nil];drain();
NSDictionary *savedMask=DYSharedMaskWithID(DYReadTools(base,kCMTimeZero).document,ID);
run.record(@"object-tracking-discards-unconfirmed-tail-before-commit",[savedMask[@"keys"]count]==4&&[savedMask[@"status"]containsString:@"未确认预测"]);
}
[NSNotificationCenter.defaultCenter removeObserver:observer];
[plugin release];
}
{
dy::ObjectMotion motion;dy::ObjectBox box{.3,.4,.1,.15,.95};motion.reset(box,25);bool ok=true;
for(int i=1;i<=10;i++){box.cx=.3f+.002f*i;ok&=motion.advance(box,true).valid;}
auto predicted=motion.advance({},false);run.record(@"object-short-loss-predicts-prior-motion",ok&&predicted.valid&&predicted.predicted&&predicted.box.cx>box.cx&&predicted.box.confidence<.3);
motion.advance({},false);box.cx=.326;auto first=motion.advance(box,true);box.cx=.328;auto second=motion.advance(box,true);
run.record(@"object-reacquisition-requires-two-consistent-observations",first.predicted&&second.recovered&&second.gap==3&&!second.predicted);
auto teleport=box;teleport.cx=.9;run.record(@"object-motion-rejects-distant-distractor",motion.advance(teleport,true).predicted);
dy::ObjectStep last;for(int i=0;i<25;i++)last=motion.advance({},false);run.record(@"object-long-loss-stops-bounded-prediction",!last.valid&&!motion.active());
motion.reset({.5,.5,.1,.1,.9},std::numeric_limits<double>::quiet_NaN());
run.record(@"object-invalid-fps-does-not-enable-prediction",!motion.active());
DYMaskParameters a=dyDefaultMaskParameters(),b=a;a.centerX=.2;b.centerX=.8;a.width=.1;b.width=.4;
NSMutableArray *keys=[NSMutableArray arrayWithObjects:DYMaskKey(CMTimeMake(3,25),a,NO,1),DYMaskKey(CMTimeMake(2,25),a,NO,.1),DYMaskKey(CMTimeMake(1,25),a,NO,.1),nil];
BOOL repaired=DYRepairObjectTrackingGap(keys,b,2);DYMaskParameters x,y;[keys[1][@"parameters"]getBytes:&x length:sizeof(x)];[keys[2][@"parameters"]getBytes:&y length:sizeof(y)];
run.record(@"object-gap-repair-uses-both-endpoints-in-reverse-order",repaired&&fabs(x.centerX-.4)<1e-6&&fabs(y.centerX-.6)<1e-6&&[keys[1][@"value"]intValue]==2&&[keys[1][@"confidence"]floatValue]<.5);
NSArray *before=[[keys copy]autorelease];run.record(@"object-gap-repair-rejects-missing-confirmed-anchor",!DYRepairObjectTrackingGap(keys,b,3)&&[keys isEqual:before]);
keys[1]=DYMaskKey(CMTimeMake(2,25),a,YES,1);before=[[keys copy]autorelease];run.record(@"object-gap-repair-never-overwrites-manual-key",!DYRepairObjectTrackingGap(keys,b,2)&&[keys isEqual:before]);
}