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

66 行
14 KiB
C++

// Shared author identity and disposable render bindings must stay separate.
{
NSMutableDictionary *doc=DYMutablePlist([[[DYToolDocument alloc]init]autorelease].document);
NSMutableDictionary *shared=(id)DYAddSharedMask(doc,DYMaskRectangle);NSString *maskID=shared[@"id"];
run.need(@"shared-mask-can-be-authored-before-any-module-assignment",shared&&DYValidToolDocument(doc)&&DYSharedMasks(doc).count==1&&DYMasksForTool(doc,@"shared").firstObject==shared&&DYMasksForTool(doc,@"base:hsl").count==0);
NSDictionary *layer=DYInsertTool(doc,@"curve",@"base:curve");NSString *layerID=layer[@"id"];
BOOL assigned=DYSetSharedMaskAssigned(doc,maskID,@"base:hsl",YES)&&DYSetSharedMaskAssigned(doc,maskID,layerID,YES);
run.record(@"shared-mask-two-module-bindings-retain-one-author-object",assigned&&DYValidToolDocument(doc)&&DYSharedMasks(doc).count==1&&DYMasksForTool(doc,@"base:hsl").firstObject==DYMasksForTool(doc,layerID).firstObject&&DYToolIDsForSharedMask(doc,maskID).count==2&&[doc[@"baseMasks"][@"hsl"][0][@"sharedID"]isEqual:maskID]&&[doc[@"schema"]isEqual:@3]);
DYMaskParameters p=DYMaskAtTime(shared,kCMTimeZero);p.centerX=.31;shared[@"parameters"]=[NSData dataWithBytes:&p length:sizeof(p)];DYMaskParameters q=DYMaskAtTime(DYMasksForTool(doc,layerID).firstObject,kCMTimeZero);
run.record(@"editing-one-shared-mask-updates-all-linked-modules",fabs(q.centerX-.31)<1e-6&&DYSharedMasks(doc).count==1);
DYMaskParameters moved=p;moved.centerX=.71;DYMergeTracking(shared,@[DYMaskKey(kCMTimeZero,p,YES,1),DYMaskKey(CMTimeMake(2,24),moved,NO,.9)]);
q=DYMaskAtTime(DYMasksForTool(doc,layerID).firstObject,CMTimeMake(1,24));
run.record(@"shared-tracking-is-single-timeline-for-every-module",fabs(q.centerX-.51)<1e-6&&[DYMasksForTool(doc,@"base:hsl").firstObject[@"keys"]count]==2);
DYToolDocument *author=[[[DYToolDocument alloc]init]autorelease];author.document=doc;NSData *encoded=[NSKeyedArchiver archivedDataWithRootObject:author requiringSecureCoding:YES error:&error];DYToolDocument *decoded=[NSKeyedUnarchiver unarchivedObjectOfClass:DYToolDocument.class fromData:encoded error:&error];
run.record(@"shared-mask-archive-roundtrip-keeps-unique-object-and-links",[decoded isEqual:author]&&DYSharedMasks(decoded.document).count==1&&DYToolIDsForSharedMask(decoded.document,maskID).count==2);
NSDictionary *snapshot=DYToolRenderSnapshot(doc,CMTimeMake(1,24)),*again=DYToolRenderSnapshot(doc,CMTimeMake(1,24));NSDictionary *base=DYMasksForTool(snapshot,@"base:hsl").firstObject,*extra=DYMasksForTool(snapshot,layerID).firstObject;
run.record(@"shared-render-snapshot-freezes-all-bindings-with-stable-private-identities",DYValidToolDocument(snapshot)&&[snapshot isEqual:again]&&![base[@"id"]isEqual:extra[@"id"]]&&[base[@"sourceSharedID"]isEqual:maskID]&&[extra[@"sourceSharedID"]isEqual:maskID]&&[base[@"keys"]count]==0&&[extra[@"keys"]count]==0&&fabs(DYMaskAtTime(base,kCMTimeZero).centerX-.51)<1e-6&&[shared[@"keys"]count]==2);
NSMutableDictionary *bad=DYMutablePlist(doc);bad[@"baseMasks"][@"hsl"]=[NSMutableArray arrayWithObjects:@{@"sharedID":maskID},@{@"sharedID":maskID},nil];run.record(@"shared-mask-rejects-duplicate-binding-in-one-module",!DYValidToolDocument(bad));
bad=DYMutablePlist(doc);bad[@"baseMasks"][@"hsl"]=[NSMutableArray arrayWithObject:@{@"sharedID":NSUUID.UUID.UUIDString}];run.record(@"shared-mask-rejects-dangling-reference",!DYValidToolDocument(bad));
bad=DYMutablePlist(doc);bad[@"baseMasks"][@"hsl"]=[NSMutableArray arrayWithObject:DYMutablePlist(shared)];run.record(@"shared-mask-rejects-copied-author-object-with-same-identity",!DYValidToolDocument(bad));
bad=DYMutablePlist(doc);bad[@"schema"]=@2;run.record(@"legacy-document-cannot-silently-contain-shared-references",!DYValidToolDocument(bad));
BOOL detached=DYSetSharedMaskAssigned(doc,maskID,@"base:hsl",NO);run.record(@"unlinking-one-module-keeps-shared-mask-and-other-module",detached&&DYMasksForTool(doc,@"base:hsl").count==0&&DYMasksForTool(doc,layerID).count==1&&DYSharedMasks(doc).count==1&&DYValidToolDocument(doc));
BOOL removed=DYRemoveSharedMask(doc,maskID);run.record(@"deleting-shared-mask-removes-all-references-without-deleting-modules",removed&&DYSharedMasks(doc).count==0&&DYMasksForTool(doc,layerID).count==0&&DYToolWithID(doc,layerID)&&DYValidToolDocument(doc));
NSMutableDictionary *legacy=DYMutablePlist([[[DYToolDocument alloc]init]autorelease].document);NSDictionary *old=DYNewMask(DYMaskEllipse);legacy[@"baseMasks"][@"curve"]=[NSMutableArray arrayWithObject:old];NSDictionary *promoted=DYShareToolMask(legacy,@"base:curve",old[@"id"]);
run.record(@"sharing-legacy-mask-moves-one-object-without-changing-geometry-or-identity",promoted&&[promoted isEqual:old]&&DYSharedMasks(legacy).count==1&&DYMasksForTool(legacy,@"base:curve").firstObject==promoted&&DYValidToolDocument(legacy));
BOOL removedBase=DYSetBaseToolRemoved(legacy,@"curve",YES);run.record(@"removing-base-module-detaches-reference-and-preserves-shared-object",removedBase&&DYValidToolDocument(legacy)&&DYSharedMasks(legacy).count==1&&DYToolIDsForSharedMask(legacy,old[@"id"]).count==0);
NSMutableDictionary *limits=DYMutablePlist([[[DYToolDocument alloc]init]autorelease].document);BOOL sixteen=YES;for(int i=0;i<DYSharedMaskLimit;i++){NSDictionary *item=DYAddSharedMask(limits,DYMaskEllipse);if(i<DYToolMaskLimit)sixteen&=DYSetSharedMaskAssigned(limits,item[@"id"],@"base:hsl",YES);}NSDictionary *seventeenth=DYSharedMasks(limits)[DYToolMaskLimit];
run.record(@"shared-library-and-per-module-reference-limits-remain-independent",sixteen&&DYSharedMasks(limits).count==DYSharedMaskLimit&&DYMasksForTool(limits,@"base:hsl").count==DYToolMaskLimit&&!DYAddSharedMask(limits,DYMaskRectangle)&&!DYSetSharedMaskAssigned(limits,seventeenth[@"id"],@"base:hsl",YES)&&DYValidToolDocument(limits));
// More than 65,536 referenced keys, but only 7,000 uniquely authored keys.
NSMutableDictionary *many=DYMutablePlist([[[DYToolDocument alloc]init]autorelease].document);NSMutableDictionary *timeline=(id)DYAddSharedMask(many,DYMaskRectangle);NSMutableArray *keys=[NSMutableArray arrayWithCapacity:7000];for(int i=0;i<7000;i++)[keys addObject:DYMaskKey(CMTimeMake(i,24),p,NO,.9)];timeline[@"keys"]=keys;BOOL allBindings=YES;for(NSString *type in [DYToolTypes()subarrayWithRange:NSMakeRange(0,10)])allBindings&=DYSetSharedMaskAssigned(many,timeline[@"id"],[@"base:" stringByAppendingString:type],YES);
run.record(@"shared-key-budget-counts-author-object-once-regardless-of-bindings",allBindings&&DYValidToolDocument(many)&&DYValidToolDocument(DYToolRenderSnapshot(many,CMTimeMake(50,24)))&&DYValidToolDocument(DYResolvedToolDocument(many)));
BOOL compactFits=YES,compactRows=YES;
for(int kind=0;kind<6;kind++){
NSMutableDictionary *state=DYMutablePlist([[[DYToolDocument alloc]init]autorelease].document);NSDictionary *mask=DYAddSharedMask(state,kind);NSView<DYWorkspaceMaskEditing> *editor=DYCreateWorkspaceMaskEditor(manager,@"shared");[editor refreshWithDocument:state time:kCMTimeZero];[editor selectMaskID:mask[@"id"]];
for(NSNumber *width in @[@377,@600,@860,@1200]){[editor setFrameSize:NSMakeSize(width.doubleValue,500)];[editor setFrameSize:NSMakeSize(width.doubleValue,editor.contentHeight)];[editor layoutSubtreeIfNeeded];for(NSView *view in editor.subviews)if(!view.hidden)compactFits&=NSContainsRect(editor.bounds,view.frame)&&view.frame.size.width>0&&view.frame.size.height>0;for(NSDictionary *row in [editor valueForKey:@"_rows"]){NSView *label=row[@"label"],*field=row[@"field"],*slider=row[@"slider"];if(label.hidden)continue;compactRows&=!NSIntersectsRect(label.frame,field.frame)&&!NSIntersectsRect(label.frame,slider.frame)&&!NSIntersectsRect(field.frame,slider.frame)&&field.frame.size.width>=70&&slider.frame.size.width>=140;}}
}
run.record(@"workspace-six-mask-editors-fit-377-600-860-1200-point-widths",compactFits&&compactRows);
}
// Exercise the actual IOSurface/Metal path against independently owned masks.
// This reference never uses the shared-reference expander to construct masks.
{
[[source metalTextureForDevice:gpu]replaceRegion:MTLRegionMake2D(0,0,width,height) mipmapLevel:0 withBytes:input.data() bytesPerRow:width*8];
DYUniforms active=defaults;active.skinProtection.x=0;active.hslSectionEnabled=1;active.spiderSectionEnabled=1;active.spiderMix=1;
active.hslHueA=active.hslHueB=(DYFloat4){24,24,24,24};active.hslSatA=active.hslSatB=(DYFloat4){25,25,25,25};
auto mesh=dy::neutralMesh(6,6);std::bitset<dy::SpiderMeshCapacity>selected;for(int i=0;i<dy::meshCount(mesh);i++)if(!dy::meshBoundary(mesh,i))selected[i]=true;mesh=dy::moveMesh(mesh,selected,-.045f,.035f);NSData *meshData=[NSData dataWithBytes:&mesh length:sizeof(mesh)];
NSMutableDictionary *sharedDocument=DYMutablePlist([[[DYToolDocument alloc]init]autorelease].document),*sharedMask=(id)DYAddSharedMask(sharedDocument,DYMaskRectangle);NSString *sharedID=sharedMask[@"id"];
DYMaskParameters geometry=DYMaskAtTime(sharedMask,kCMTimeZero);geometry.centerX=.28;geometry.centerY=.5;geometry.width=.35;geometry.height=.7;geometry.feather=0;sharedMask[@"parameters"]=[NSData dataWithBytes:&geometry length:sizeof(geometry)];
run.need(@"gpu-shared-hsl-spider-use-one-rectangle-author",DYSetSharedMaskAssigned(sharedDocument,sharedID,@"base:hsl",YES)&&DYSetSharedMaskAssigned(sharedDocument,sharedID,@"base:spider",YES)&&DYValidToolDocument(sharedDocument));
auto independentDocument=[&](){NSMutableDictionary *reference=DYMutablePlist([[[DYToolDocument alloc]init]autorelease].document);for(NSString *type in @[@"hsl",@"spider"]){NSMutableDictionary *copy=DYMutablePlist(sharedMask);copy[@"id"]=NSUUID.UUID.UUIDString;reference[@"baseMasks"][type]=[NSMutableArray arrayWithObject:copy];}return reference;};
auto renderAt=[&](NSDictionary *document,CMTime time,DYUniforms uniforms,NSString *name){NSError *failure=nil;NSDictionary *snapshot=DYToolRenderSnapshot(document,time);BOOL valid=DYValidToolDocument(snapshot);BOOL rendered=valid&&[renderer renderSource:source destination:destination uniforms:uniforms paths:@{} spider:meshData tools:snapshot error:&failure];run.need(name,rendered,@{@"error":reason(failure),@"validSnapshot":@(valid)});return read(destination,gpu);};
auto alphaPreserved=[&](const Pixels& pixels){if(pixels.size()!=input.size())return false;for(NSUInteger i=3;i<input.size();i+=4)if(pixels[i]!=input[i])return false;return true;};
Pixels first=renderAt(sharedDocument,kCMTimeZero,active,@"gpu-shared-hsl-spider-initial-render"),reference=renderAt(independentDocument(),kCMTimeZero,active,@"gpu-independent-hsl-spider-initial-reference");Difference initialError=difference(first,reference);
run.record(@"gpu-shared-hsl-spider-equals-two-independent-masks-per-pixel",initialError.nonfinite==0&&initialError.maximum<.0001&&alphaPreserved(first)&&alphaPreserved(reference),measure(initialError));
DYUniforms hslOnly=active,spiderOnly=active;hslOnly.spiderSectionEnabled=0;spiderOnly.hslSectionEnabled=0;Pixels hslFrame=renderAt(sharedDocument,kCMTimeZero,hslOnly,@"gpu-shared-hsl-only-control"),spiderFrame=renderAt(sharedDocument,kCMTimeZero,spiderOnly,@"gpu-shared-spider-only-control");
run.record(@"gpu-shared-region-both-hsl-and-spider-affect-real-pixels",difference(first,hslFrame).different>10&&difference(first,spiderFrame).different>10);
geometry.centerX=.72;sharedMask[@"parameters"]=[NSData dataWithBytes:&geometry length:sizeof(geometry)];Pixels edited=renderAt(sharedDocument,kCMTimeZero,active,@"gpu-shared-edited-rectangle-render"),editedReference=renderAt(independentDocument(),kCMTimeZero,active,@"gpu-independent-edited-rectangle-reference");Difference editedError=difference(edited,editedReference),editedChange=difference(first,edited);
run.record(@"gpu-editing-unique-shared-geometry-updates-both-operations",editedError.nonfinite==0&&editedError.maximum<.0001&&editedChange.different>20&&alphaPreserved(edited),@{@"maximumReferenceError":@(editedError.maximum),@"changedComponents":@(editedChange.different),@"uniqueAuthorObjects":@(DYSharedMasks(sharedDocument).count)});
DYMaskParameters tracked=geometry;tracked.centerX=.28;BOOL merged=DYMergeTracking(sharedMask,@[DYMaskKey(kCMTimeZero,geometry,YES,1),DYMaskKey(CMTimeMake(2,24),tracked,NO,.93)]);CMTime middle=CMTimeMake(1,24);Pixels animated=renderAt(sharedDocument,middle,active,@"gpu-shared-tracking-middle-render"),animatedReference=renderAt(independentDocument(),middle,active,@"gpu-independent-tracking-middle-reference");Difference trackingError=difference(animated,animatedReference),trackingChange=difference(edited,animated);
run.record(@"gpu-one-shared-trajectory-drives-hsl-and-spider-per-frame",merged&&trackingError.nonfinite==0&&trackingError.maximum<.0001&&trackingChange.different>20&&alphaPreserved(animated)&&DYSharedMasks(sharedDocument).count==1&&[DYMasksForTool(sharedDocument,@"base:hsl").firstObject[@"keys"]count]==2&&DYMasksForTool(sharedDocument,@"base:hsl").firstObject==DYMasksForTool(sharedDocument,@"base:spider").firstObject,@{@"maximumReferenceError":@(trackingError.maximum),@"changedComponents":@(trackingChange.different),@"evaluatedCenterX":@(DYMaskAtTime(sharedMask,middle).centerX)});
// Use the real preset serializer, then the same JSON/base64-plist path that
// loadPreset validates, with a separate manager so other fixtures stay intact.
DYDiagnosticManager *presetHost=[[[DYDiagnosticManager alloc]init]autorelease];DYCreateParameters(presetHost,&error);DYWriteTools(presetHost,sharedDocument,kCMTimeZero);DYAssetState *presetAssets=[[[DYAssetState alloc]init]autorelease];presetAssets.spiderMesh=meshData;presetAssets.spiderLinked=YES;DYWriteAssets(presetHost,presetAssets,kCMTimeZero);DYInspector *presetInspector=[[[DYInspector alloc]initWithAPIManager:presetHost renderer:renderer]autorelease];NSDictionary *preset=[presetInspector performSelector:@selector(presetSnapshot)];NSData *json=[NSJSONSerialization dataWithJSONObject:preset options:0 error:&error];NSDictionary *loaded=json?[NSJSONSerialization JSONObjectWithData:json options:0 error:&error]:nil;NSData *toolBytes=[loaded[@"tools"]isKindOfClass:NSString.class]?[[[NSData alloc]initWithBase64EncodedString:loaded[@"tools"] options:0]autorelease]:nil;NSDictionary *restored=toolBytes?[NSPropertyListSerialization propertyListWithData:toolBytes options:NSPropertyListImmutable format:NULL error:&error]:nil;
run.record(@"preset-v7-json-roundtrip-preserves-unique-shared-object-and-trajectory",[loaded[@"version"]isEqual:@7]&&[loaded[@"format"]isEqual:@"com.dayu.color.preset"]&&DYValidToolDocument(restored)&&[restored isEqual:sharedDocument]&&DYSharedMasks(restored).count==1&&DYToolIDsForSharedMask(restored,sharedID).count==2&&[DYSpiderFromJSON(loaded[@"spider"])isEqual:meshData]);
}