|
| 1 | +/* Copyright 2026 Diligent Graphics LLC |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 10 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. |
| 12 | + * |
| 13 | + * In no event and under no legal theory, whether in tort (including negligence), |
| 14 | + * contract, or otherwise, unless required by applicable law (such as deliberate |
| 15 | + * and grossly negligent acts) or agreed to in writing, shall any Contributor be |
| 16 | + * liable for any damages, including any direct, indirect, special, incidental, |
| 17 | + * or consequential damages of any character arising as a result of this License or |
| 18 | + * out of the use or inability to use the software (including but not limited to damages |
| 19 | + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and |
| 20 | + * all other commercial damages or losses), even if such Contributor has been advised |
| 21 | + * of the possibility of such damages. |
| 22 | + */ |
| 23 | + |
| 24 | +#import <QuartzCore/CAMetalLayer.h> |
| 25 | +#import "WebGPUView.h" |
| 26 | + |
| 27 | +@implementation WebGPUView |
| 28 | +{ |
| 29 | +} |
| 30 | + |
| 31 | +- (id)initWithFrame:(CGRect)frame |
| 32 | +{ |
| 33 | + self = [super initWithFrame:frame]; |
| 34 | + if (self) |
| 35 | + { |
| 36 | + self.renderMode = Diligent::MacOSAppBase::RenderMode::WebGPU; |
| 37 | + } |
| 38 | + return self; |
| 39 | +} |
| 40 | + |
| 41 | +- (id)initWithCoder:(NSCoder*)coder |
| 42 | +{ |
| 43 | + self = [super initWithCoder:coder]; |
| 44 | + if (self) |
| 45 | + { |
| 46 | + self.renderMode = Diligent::MacOSAppBase::RenderMode::WebGPU; |
| 47 | + } |
| 48 | + return self; |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +- (void) awakeFromNib |
| 53 | +{ |
| 54 | + [super awakeFromNib]; |
| 55 | + |
| 56 | + // Back the view with a layer created by the makeBackingLayer method. |
| 57 | + self.wantsLayer = YES; |
| 58 | + |
| 59 | + [self initApp:self]; |
| 60 | + |
| 61 | +#pragma clang diagnostic push |
| 62 | +#pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 63 | + |
| 64 | + CVDisplayLinkRef displayLink; |
| 65 | + CVDisplayLinkCreateWithActiveCGDisplays(&displayLink); |
| 66 | + [self setDisplayLink:displayLink]; |
| 67 | + CVDisplayLinkSetOutputCallback(displayLink, &DisplayLinkCallback, (__bridge void*)self); |
| 68 | + CVDisplayLinkStart(displayLink); |
| 69 | + |
| 70 | +#pragma clang diagnostic pop |
| 71 | + |
| 72 | + [self setPostsBoundsChangedNotifications:YES]; |
| 73 | + [self setPostsFrameChangedNotifications:YES]; |
| 74 | + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boundsDidChange:) name:NSViewBoundsDidChangeNotification object:self]; |
| 75 | + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boundsDidChange:) name:NSViewFrameDidChangeNotification object:self]; |
| 76 | +} |
| 77 | + |
| 78 | +// Indicates that the view wants to draw using the backing |
| 79 | +// layer instead of using drawRect:. |
| 80 | +-(BOOL) wantsUpdateLayer |
| 81 | +{ |
| 82 | + return YES; |
| 83 | +} |
| 84 | + |
| 85 | +// Returns a Metal-compatible layer. |
| 86 | ++(Class) layerClass |
| 87 | +{ |
| 88 | + return [CAMetalLayer class]; |
| 89 | +} |
| 90 | + |
| 91 | +// If the wantsLayer property is set to YES, this method will |
| 92 | +// be invoked to return a layer instance. |
| 93 | +-(CALayer*) makeBackingLayer |
| 94 | +{ |
| 95 | + CALayer* layer = [self.class.layerClass layer]; |
| 96 | + CGSize viewScale = [self convertSizeToBacking: CGSizeMake(1.0, 1.0)]; |
| 97 | + layer.contentsScale = MIN(viewScale.width, viewScale.height); |
| 98 | + return layer; |
| 99 | +} |
| 100 | + |
| 101 | +-(void)render |
| 102 | +{ |
| 103 | + auto* theApp = [self lockApp]; |
| 104 | + if (theApp) |
| 105 | + { |
| 106 | + theApp->Update(); |
| 107 | + theApp->Render(); |
| 108 | + theApp->Present(); |
| 109 | + } |
| 110 | + [self unlockApp]; |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +- (CVReturn) getFrameForTime:(const CVTimeStamp*)outputTime |
| 115 | +{ |
| 116 | + // There is no autorelease pool when this method is called |
| 117 | + // because it will be called from a background thread. |
| 118 | + // It's important to create one or app can leak objects. |
| 119 | + @autoreleasepool { |
| 120 | + [self render]; |
| 121 | + } |
| 122 | + return kCVReturnSuccess; |
| 123 | +} |
| 124 | + |
| 125 | +// Rendering loop callback function for use with a CVDisplayLink. |
| 126 | +static CVReturn DisplayLinkCallback(CVDisplayLinkRef displayLink, |
| 127 | + const CVTimeStamp* now, |
| 128 | + const CVTimeStamp* outputTime, |
| 129 | + CVOptionFlags flagsIn, |
| 130 | + CVOptionFlags* flagsOut, |
| 131 | + void* target) |
| 132 | +{ |
| 133 | + WebGPUView* view = (__bridge WebGPUView*)target; |
| 134 | + CVReturn result = [view getFrameForTime:outputTime]; |
| 135 | + return result; |
| 136 | +} |
| 137 | + |
| 138 | +-(void)boundsDidChange:(NSNotification *)notification |
| 139 | +{ |
| 140 | + NSRect viewRectPoints = [self bounds]; |
| 141 | + NSRect viewRectPixels = [self convertRectToBacking:viewRectPoints]; |
| 142 | + auto* theApp = [self lockApp]; |
| 143 | + if (theApp) |
| 144 | + { |
| 145 | + theApp->WindowResize(viewRectPixels.size.width, viewRectPixels.size.height); |
| 146 | + theApp->Update(); |
| 147 | + theApp->Render(); |
| 148 | + theApp->Present(); |
| 149 | + } |
| 150 | + [self unlockApp]; |
| 151 | +} |
| 152 | + |
| 153 | +@end |
0 commit comments