Skip to content

Commit d58b841

Browse files
stuartcarnieocornut
authored andcommitted
Backends: OSX, Metal: Tweaks. Use preferred method of obtaining a timestamp. (#4821)
+ Rename ImGuiFocusObserver. Docking branch will use it for more than focus.
1 parent 3e5dde9 commit d58b841

File tree

3 files changed

+22
-38
lines changed

3 files changed

+22
-38
lines changed

backends/imgui_impl_metal.mm

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,15 @@
2727

2828
#include "imgui.h"
2929
#include "imgui_impl_metal.h"
30-
30+
#import <time.h>
3131
#import <Metal/Metal.h>
32-
// #import <QuartzCore/CAMetalLayer.h> // Not supported in XCode 9.2. Maybe a macro to detect the SDK version can be used (something like #if MACOS_SDK >= 10.13 ...)
33-
#import <simd/simd.h>
3432

3533
#pragma mark - Support classes
3634

3735
// A wrapper around a MTLBuffer object that knows the last time it was reused
3836
@interface MetalBuffer : NSObject
3937
@property (nonatomic, strong) id<MTLBuffer> buffer;
40-
@property (nonatomic, assign) NSTimeInterval lastReuseTime;
38+
@property (nonatomic, assign) double lastReuseTime;
4139
- (instancetype)initWithBuffer:(id<MTLBuffer>)buffer;
4240
@end
4341

@@ -61,7 +59,7 @@ @interface MetalContext : NSObject
6159
@property (nonatomic, strong) NSMutableDictionary *renderPipelineStateCache; // pipeline cache; keyed on framebuffer descriptors
6260
@property (nonatomic, strong, nullable) id<MTLTexture> fontTexture;
6361
@property (nonatomic, strong) NSMutableArray<MetalBuffer *> *bufferCache;
64-
@property (nonatomic, assign) NSTimeInterval lastBufferCachePurge;
62+
@property (nonatomic, assign) double lastBufferCachePurge;
6563
- (void)makeDeviceObjectsWithDevice:(id<MTLDevice>)device;
6664
- (void)makeFontTextureWithDevice:(id<MTLDevice>)device;
6765
- (MetalBuffer *)dequeueReusableBufferOfLength:(NSUInteger)length device:(id<MTLDevice>)device;
@@ -90,6 +88,8 @@ - (void)renderDrawData:(ImDrawData *)drawData
9088
static ImGui_ImplMetal_Data* ImGui_ImplMetal_GetBackendData() { return ImGui::GetCurrentContext() ? (ImGui_ImplMetal_Data*)ImGui::GetIO().BackendRendererUserData : NULL; }
9189
static void ImGui_ImplMetal_DestroyBackendData() { IM_DELETE(ImGui_ImplMetal_GetBackendData()); }
9290

91+
static inline CFTimeInterval GetMachAbsoluteTimeInSeconds() { return static_cast<CFTimeInterval>(static_cast<double>(clock_gettime_nsec_np(CLOCK_UPTIME_RAW)) / 1e9); }
92+
9393
#ifdef IMGUI_IMPL_METAL_CPP
9494

9595
#pragma mark - Dear ImGui Metal C++ Backend API
@@ -207,7 +207,7 @@ - (instancetype)initWithBuffer:(id<MTLBuffer>)buffer
207207
if ((self = [super init]))
208208
{
209209
_buffer = buffer;
210-
_lastReuseTime = [NSDate date].timeIntervalSince1970;
210+
_lastReuseTime = GetMachAbsoluteTimeInSeconds();
211211
}
212212
return self;
213213
}
@@ -269,7 +269,7 @@ - (instancetype)init {
269269
{
270270
_renderPipelineStateCache = [NSMutableDictionary dictionary];
271271
_bufferCache = [NSMutableArray array];
272-
_lastBufferCachePurge = [NSDate date].timeIntervalSince1970;
272+
_lastBufferCachePurge = GetMachAbsoluteTimeInSeconds();
273273
}
274274
return self;
275275
}
@@ -309,7 +309,7 @@ - (void)makeFontTextureWithDevice:(id<MTLDevice>)device
309309

310310
- (MetalBuffer *)dequeueReusableBufferOfLength:(NSUInteger)length device:(id<MTLDevice>)device
311311
{
312-
NSTimeInterval now = [NSDate date].timeIntervalSince1970;
312+
uint64_t now = GetMachAbsoluteTimeInSeconds();
313313

314314
// Purge old buffers that haven't been useful for a while
315315
if (now - self.lastBufferCachePurge > 1.0)

backends/imgui_impl_osx.mm

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
#import "imgui.h"
1818
#import "imgui_impl_osx.h"
1919
#import <Cocoa/Cocoa.h>
20-
#import <mach/mach_time.h>
2120
#import <Carbon/Carbon.h>
2221
#import <GameController/GameController.h>
22+
#import <time.h>
2323

2424
// CHANGELOG
2525
// (minor and older changes stripped away, please see git history for details)
@@ -54,17 +54,16 @@
5454
#define APPLE_HAS_CONTROLLER (__IPHONE_OS_VERSION_MIN_REQUIRED >= 140000 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 110000 || __TV_OS_VERSION_MIN_REQUIRED >= 140000)
5555
#define APPLE_HAS_THUMBSTICKS (__IPHONE_OS_VERSION_MIN_REQUIRED >= 120100 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 101401 || __TV_OS_VERSION_MIN_REQUIRED >= 120100)
5656

57-
@class ImFocusObserver;
57+
@class ImGuiObserver;
5858
@class KeyEventResponder;
5959

6060
// Data
6161
struct ImGui_ImplOSX_Data
6262
{
63-
double HostClockPeriod;
64-
double Time;
63+
CFTimeInterval Time;
6564
NSCursor* MouseCursors[ImGuiMouseCursor_COUNT];
6665
bool MouseCursorHidden;
67-
ImFocusObserver* FocusObserver;
66+
ImGuiObserver* Observer;
6867
KeyEventResponder* KeyEventResponder;
6968
NSTextInputContext* InputContext;
7069

@@ -75,6 +74,8 @@
7574
static ImGui_ImplOSX_Data* ImGui_ImplOSX_GetBackendData() { return (ImGui_ImplOSX_Data*)ImGui::GetIO().BackendPlatformUserData; }
7675
static void ImGui_ImplOSX_DestroyBackendData() { IM_DELETE(ImGui_ImplOSX_GetBackendData()); }
7776

77+
static inline CFTimeInterval GetMachAbsoluteTimeInSeconds() { return static_cast<CFTimeInterval>(static_cast<double>(clock_gettime_nsec_np(CLOCK_UPTIME_RAW)) / 1e9); }
78+
7879
// Undocumented methods for creating cursors.
7980
@interface NSCursor()
8081
+ (id)_windowResizeNorthWestSouthEastCursor;
@@ -83,20 +84,6 @@ + (id)_windowResizeNorthSouthCursor;
8384
+ (id)_windowResizeEastWestCursor;
8485
@end
8586

86-
static void InitHostClockPeriod()
87-
{
88-
ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
89-
struct mach_timebase_info info;
90-
mach_timebase_info(&info);
91-
bd->HostClockPeriod = 1e-9 * ((double)info.denom / (double)info.numer); // Period is the reciprocal of frequency.
92-
}
93-
94-
static double GetMachAbsoluteTimeInSeconds()
95-
{
96-
ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
97-
return (double)mach_absolute_time() * bd->HostClockPeriod;
98-
}
99-
10087
/**
10188
KeyEventResponder implements the NSTextInputClient protocol as is required by the macOS text input manager.
10289
@@ -225,14 +212,14 @@ - (void)unmarkText
225212

226213
@end
227214

228-
@interface ImFocusObserver : NSObject
215+
@interface ImGuiObserver : NSObject
229216

230217
- (void)onApplicationBecomeActive:(NSNotification*)aNotification;
231218
- (void)onApplicationBecomeInactive:(NSNotification*)aNotification;
232219

233220
@end
234221

235-
@implementation ImFocusObserver
222+
@implementation ImGuiObserver
236223

237224
- (void)onApplicationBecomeActive:(NSNotification*)aNotification
238225
{
@@ -385,6 +372,8 @@ bool ImGui_ImplOSX_Init(NSView* view)
385372
//io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy)
386373
io.BackendPlatformName = "imgui_impl_osx";
387374

375+
bd->Observer = [ImGuiObserver new];
376+
388377
// Load cursors. Some of them are undocumented.
389378
bd->MouseCursorHidden = false;
390379
bd->MouseCursors[ImGuiMouseCursor_Arrow] = [NSCursor arrowCursor];
@@ -426,12 +415,11 @@ bool ImGui_ImplOSX_Init(NSView* view)
426415
return s_clipboard.Data;
427416
};
428417

429-
bd->FocusObserver = [[ImFocusObserver alloc] init];
430-
[[NSNotificationCenter defaultCenter] addObserver:bd->FocusObserver
418+
[[NSNotificationCenter defaultCenter] addObserver:bd->Observer
431419
selector:@selector(onApplicationBecomeActive:)
432420
name:NSApplicationDidBecomeActiveNotification
433421
object:nil];
434-
[[NSNotificationCenter defaultCenter] addObserver:bd->FocusObserver
422+
[[NSNotificationCenter defaultCenter] addObserver:bd->Observer
435423
selector:@selector(onApplicationBecomeInactive:)
436424
name:NSApplicationDidResignActiveNotification
437425
object:nil];
@@ -473,7 +461,7 @@ bool ImGui_ImplOSX_Init(NSView* view)
473461
void ImGui_ImplOSX_Shutdown()
474462
{
475463
ImGui_ImplOSX_Data* bd = ImGui_ImplOSX_GetBackendData();
476-
bd->FocusObserver = NULL;
464+
bd->Observer = NULL;
477465
ImGui_ImplOSX_DestroyBackendData();
478466
}
479467

@@ -592,10 +580,8 @@ void ImGui_ImplOSX_NewFrame(NSView* view)
592580

593581
// Setup time step
594582
if (bd->Time == 0.0)
595-
{
596-
InitHostClockPeriod();
597583
bd->Time = GetMachAbsoluteTimeInSeconds();
598-
}
584+
599585
double current_time = GetMachAbsoluteTimeInSeconds();
600586
io.DeltaTime = (float)(current_time - bd->Time);
601587
bd->Time = current_time;

examples/example_apple_metal/main.mm

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ -(void)drawInMTKView:(MTKView*)view
125125
#endif
126126
io.DisplayFramebufferScale = ImVec2(framebufferScale, framebufferScale);
127127

128-
io.DeltaTime = 1 / float(view.preferredFramesPerSecond ?: 60);
129-
130128
id<MTLCommandBuffer> commandBuffer = [self.commandQueue commandBuffer];
131129

132130
MTLRenderPassDescriptor* renderPassDescriptor = view.currentRenderPassDescriptor;

0 commit comments

Comments
 (0)