diff --git a/src/gpu/mtl/GrMtlCaps.mm b/src/gpu/mtl/GrMtlCaps.mm index fbaedef13a..b0d92c5d69 100644 --- a/src/gpu/mtl/GrMtlCaps.mm +++ b/src/gpu/mtl/GrMtlCaps.mm @@ -252,13 +252,16 @@ void GrMtlCaps::initGrCaps(const id device) { bool supportsMTLEvent = false; #ifdef GR_METAL_SDK_SUPPORTS_EVENTS - NSOperatingSystemVersion osVersion = [[NSProcessInfo processInfo] operatingSystemVersion]; + // TODO: this may be redundant + if (@available(macOS 10.14, iOS 12.0, *)) { + NSOperatingSystemVersion osVersion = [[NSProcessInfo processInfo] operatingSystemVersion]; #ifdef SK_BUILD_FOR_MAC - supportsMTLEvent = (osVersion.majorVersion > 10 || - (osVersion.majorVersion == 10 && osVersion.minorVersion >= 14)); + supportsMTLEvent = (osVersion.majorVersion > 10 || + (osVersion.majorVersion == 10 && osVersion.minorVersion >= 14)); #else - supportsMTLEvent = (osVersion.majorVersion >= 12); + supportsMTLEvent = (osVersion.majorVersion >= 12); #endif + } #endif fFenceSyncSupport = supportsMTLEvent; fSemaphoreSupport = supportsMTLEvent; diff --git a/src/gpu/mtl/GrMtlCommandBuffer.h b/src/gpu/mtl/GrMtlCommandBuffer.h index c144d11284..c0116105b0 100644 --- a/src/gpu/mtl/GrMtlCommandBuffer.h +++ b/src/gpu/mtl/GrMtlCommandBuffer.h @@ -34,8 +34,8 @@ public: } #ifdef GR_METAL_SDK_SUPPORTS_EVENTS - void encodeSignalEvent(id event, uint64_t value); - void encodeWaitForEvent(id event, uint64_t value); + void encodeSignalEvent(id, uint64_t value) API_AVAILABLE(macos(10.14), ios(12.0)); + void encodeWaitForEvent(id, uint64_t value) API_AVAILABLE(macos(10.14), ios(12.0)); #endif private: diff --git a/src/gpu/mtl/GrMtlCommandBuffer.mm b/src/gpu/mtl/GrMtlCommandBuffer.mm index 0651b5a1f3..f904d4abf2 100644 --- a/src/gpu/mtl/GrMtlCommandBuffer.mm +++ b/src/gpu/mtl/GrMtlCommandBuffer.mm @@ -123,13 +123,17 @@ void GrMtlCommandBuffer::endAllEncoding() { void GrMtlCommandBuffer::encodeSignalEvent(id event, uint64_t eventValue) { SkASSERT(fCmdBuffer); this->endAllEncoding(); // ensure we don't have any active command encoders - [fCmdBuffer encodeSignalEvent:event value:eventValue]; + if (@available(macOS 10.14, iOS 12.0, *)) { + [fCmdBuffer encodeSignalEvent:event value:eventValue]; + } } void GrMtlCommandBuffer::encodeWaitForEvent(id event, uint64_t eventValue) { SkASSERT(fCmdBuffer); this->endAllEncoding(); // ensure we don't have any active command encoders // TODO: not sure if needed but probably - [fCmdBuffer encodeWaitForEvent:event value:eventValue]; + if (@available(macOS 10.14, iOS 12.0, *)) { + [fCmdBuffer encodeWaitForEvent:event value:eventValue]; + } } #endif diff --git a/src/gpu/mtl/GrMtlGpu.h b/src/gpu/mtl/GrMtlGpu.h index a944349283..98c149bb95 100644 --- a/src/gpu/mtl/GrMtlGpu.h +++ b/src/gpu/mtl/GrMtlGpu.h @@ -243,8 +243,8 @@ private: #ifdef GR_METAL_SDK_SUPPORTS_EVENTS // For FenceSync - id fSharedEvent; - MTLSharedEventListener* fSharedEventListener; + id fSharedEvent API_AVAILABLE(macos(10.14), ios(12.0)); + MTLSharedEventListener* fSharedEventListener API_AVAILABLE(macos(10.14), ios(12.0)); uint64_t fLatestEvent; #endif diff --git a/src/gpu/mtl/GrMtlGpu.mm b/src/gpu/mtl/GrMtlGpu.mm index 37fd1a848d..e02456fa76 100644 --- a/src/gpu/mtl/GrMtlGpu.mm +++ b/src/gpu/mtl/GrMtlGpu.mm @@ -107,11 +107,13 @@ GrMtlGpu::GrMtlGpu(GrContext* context, const GrContextOptions& options, fMtlCaps.reset(new GrMtlCaps(options, fDevice, featureSet)); fCaps = fMtlCaps; #ifdef GR_METAL_SDK_SUPPORTS_EVENTS - if (fMtlCaps->fenceSyncSupport()) { - fSharedEvent = [fDevice newSharedEvent]; - dispatch_queue_t dispatchQueue = dispatch_queue_create("MTLFenceSync", NULL); - fSharedEventListener = [[MTLSharedEventListener alloc] initWithDispatchQueue:dispatchQueue]; - fLatestEvent = 0; + if (@available(macOS 10.14, iOS 12.0, *)) { + if (fMtlCaps->fenceSyncSupport()) { + fSharedEvent = [fDevice newSharedEvent]; + dispatch_queue_t dispatchQ = dispatch_queue_create("MTLFenceSync", NULL); + fSharedEventListener = [[MTLSharedEventListener alloc] initWithDispatchQueue:dispatchQ]; + fLatestEvent = 0; + } } #endif } @@ -1048,12 +1050,14 @@ bool GrMtlGpu::onReadPixels(GrSurface* surface, int left, int top, int width, in GrFence SK_WARN_UNUSED_RESULT GrMtlGpu::insertFence() { #ifdef GR_METAL_SDK_SUPPORTS_EVENTS - if (this->caps()->fenceSyncSupport()) { - GrMtlCommandBuffer* cmdBuffer = this->commandBuffer(); - ++fLatestEvent; - cmdBuffer->encodeSignalEvent(fSharedEvent, fLatestEvent); + if (@available(macOS 10.14, iOS 12.0, *)) { + if (this->caps()->fenceSyncSupport()) { + GrMtlCommandBuffer* cmdBuffer = this->commandBuffer(); + ++fLatestEvent; + cmdBuffer->encodeSignalEvent(fSharedEvent, fLatestEvent); - return fLatestEvent; + return fLatestEvent; + } } #endif return 0; @@ -1061,20 +1065,22 @@ GrFence SK_WARN_UNUSED_RESULT GrMtlGpu::insertFence() { bool GrMtlGpu::waitFence(GrFence value, uint64_t timeout) { #ifdef GR_METAL_SDK_SUPPORTS_EVENTS - if (this->caps()->fenceSyncSupport()) { - dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); + if (@available(macOS 10.14, iOS 12.0, *)) { + if (this->caps()->fenceSyncSupport()) { + dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); - // Add listener for this particular value or greater - __block dispatch_semaphore_t block_sema = semaphore; - [fSharedEvent notifyListener: fSharedEventListener - atValue: value - block: ^(id sharedEvent, uint64_t value) { - dispatch_semaphore_signal(block_sema); - }]; + // Add listener for this particular value or greater + __block dispatch_semaphore_t block_sema = semaphore; + [fSharedEvent notifyListener: fSharedEventListener + atValue: value + block: ^(id sharedEvent, uint64_t value) { + dispatch_semaphore_signal(block_sema); + }]; - long result = dispatch_semaphore_wait(semaphore, timeout); + long result = dispatch_semaphore_wait(semaphore, timeout); - return !result; + return !result; + } } #endif return true; @@ -1107,17 +1113,21 @@ sk_sp GrMtlGpu::wrapBackendSemaphore(const GrBackendSemaphore& sema void GrMtlGpu::insertSemaphore(sk_sp semaphore) { #ifdef GR_METAL_SDK_SUPPORTS_EVENTS - GrMtlSemaphore* mtlSem = static_cast(semaphore.get()); + if (@available(macOS 10.14, iOS 12.0, *)) { + GrMtlSemaphore* mtlSem = static_cast(semaphore.get()); - this->commandBuffer()->encodeSignalEvent(mtlSem->event(), mtlSem->value()); + this->commandBuffer()->encodeSignalEvent(mtlSem->event(), mtlSem->value()); + } #endif } void GrMtlGpu::waitSemaphore(sk_sp semaphore) { #ifdef GR_METAL_SDK_SUPPORTS_EVENTS - GrMtlSemaphore* mtlSem = static_cast(semaphore.get()); + if (@available(macOS 10.14, iOS 12.0, *)) { + GrMtlSemaphore* mtlSem = static_cast(semaphore.get()); - this->commandBuffer()->encodeWaitForEvent(mtlSem->event(), mtlSem->value()); + this->commandBuffer()->encodeWaitForEvent(mtlSem->event(), mtlSem->value()); + } #endif } diff --git a/src/gpu/mtl/GrMtlSemaphore.h b/src/gpu/mtl/GrMtlSemaphore.h index e9eb911371..b8cd397dfe 100644 --- a/src/gpu/mtl/GrMtlSemaphore.h +++ b/src/gpu/mtl/GrMtlSemaphore.h @@ -27,18 +27,19 @@ public: uint64_t value, GrWrapOwnership ownership); - id event() const { return fEvent; } + id event() const API_AVAILABLE(macos(10.14), ios(12.0)) { return fEvent; } uint64_t value() const { return fValue; } GrBackendSemaphore backendSemaphore() const override; private: - GrMtlSemaphore(GrMtlGpu* gpu, id event, uint64_t value, bool isOwned); + GrMtlSemaphore(GrMtlGpu* gpu, id event, + uint64_t value, bool isOwned) API_AVAILABLE(macos(10.14), ios(12.0)); void onRelease() override; void onAbandon() override; - id fEvent; + id fEvent API_AVAILABLE(macos(10.14), ios(12.0)); uint64_t fValue; typedef GrSemaphore INHERITED; diff --git a/src/gpu/mtl/GrMtlSemaphore.mm b/src/gpu/mtl/GrMtlSemaphore.mm index 12f7eedd12..767150f23d 100644 --- a/src/gpu/mtl/GrMtlSemaphore.mm +++ b/src/gpu/mtl/GrMtlSemaphore.mm @@ -14,9 +14,13 @@ #ifdef GR_METAL_SDK_SUPPORTS_EVENTS sk_sp GrMtlSemaphore::Make(GrMtlGpu* gpu, bool isOwned) { - id event = [gpu->device() newEvent]; - uint64_t value = 1; // seems like a reasonable starting point - return sk_sp(new GrMtlSemaphore(gpu, event, value, isOwned)); + if (@available(macOS 10.14, iOS 12.0, *)) { + id event = [gpu->device() newEvent]; + uint64_t value = 1; // seems like a reasonable starting point + return sk_sp(new GrMtlSemaphore(gpu, event, value, isOwned)); + } else { + return nullptr; + } } sk_sp GrMtlSemaphore::MakeWrapped(GrMtlGpu* gpu, @@ -25,10 +29,14 @@ sk_sp GrMtlSemaphore::MakeWrapped(GrMtlGpu* gpu, GrWrapOwnership ownership) { // The GrMtlSemaphore will have strong ownership at this point. // The GrMTLHandle will subsequently only have weak ownership. - id mtlEvent = (__bridge_transfer id)event; - auto sema = sk_sp(new GrMtlSemaphore(gpu, mtlEvent, value, - kBorrow_GrWrapOwnership != ownership)); - return sema; + if (@available(macOS 10.14, iOS 12.0, *)) { + id mtlEvent = (__bridge_transfer id)event; + auto sema = sk_sp(new GrMtlSemaphore(gpu, mtlEvent, value, + kBorrow_GrWrapOwnership != ownership)); + return sema; + } else { + return nullptr; + } } GrMtlSemaphore::GrMtlSemaphore(GrMtlGpu* gpu, id event, uint64_t value, bool isOwned) @@ -38,12 +46,16 @@ GrMtlSemaphore::GrMtlSemaphore(GrMtlGpu* gpu, id event, uint64_t value } void GrMtlSemaphore::onRelease() { - fEvent = nil; + if (@available(macOS 10.14, iOS 12.0, *)) { + fEvent = nil; + } INHERITED::onRelease(); } void GrMtlSemaphore::onAbandon() { - fEvent = nil; + if (@available(macOS 10.14, iOS 12.0, *)) { + fEvent = nil; + } INHERITED::onAbandon(); } @@ -51,8 +63,10 @@ GrBackendSemaphore GrMtlSemaphore::backendSemaphore() const { GrBackendSemaphore backendSemaphore; // The GrMtlSemaphore and the GrBackendSemaphore will have strong ownership at this point. // Whoever uses the GrBackendSemaphore will subsquently steal this ref (see MakeWrapped, above). - GrMTLHandle handle = (__bridge_retained GrMTLHandle)(fEvent); - backendSemaphore.initMetal(handle, fValue); + if (@available(macOS 10.14, iOS 12.0, *)) { + GrMTLHandle handle = (__bridge_retained GrMTLHandle)(fEvent); + backendSemaphore.initMetal(handle, fValue); + } return backendSemaphore; } #endif