Use objc_externally_retained to cut down retain/release in Metal

Until we get off of ARC, these extra retains and releases can really
add up in previous profiling of Metal, especially on iOS.

Change-Id: Ib3aaa0b863c4469bdb6ad930395a170719c0293d
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/381110
Commit-Queue: Adlai Holler <adlai@google.com>
Reviewed-by: Jim Van Verth <jvanverth@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
This commit is contained in:
Adlai Holler 2021-03-24 11:38:56 -04:00 committed by Skia Commit-Bot
parent 5ca24f94ab
commit 78381ee5e0
27 changed files with 153 additions and 43 deletions

View File

@ -25,6 +25,7 @@ skia_gpu_sources = [
"$_include/private/GrContext_Base.h",
"$_include/private/GrGLTypesPriv.h",
"$_include/private/GrImageContext.h",
"$_include/private/GrMtlTypesPriv.h",
"$_include/private/GrResourceKey.h",
"$_include/private/GrSingleOwner.h",
"$_include/private/GrTypesPriv.h",

View File

@ -29,28 +29,6 @@ typedef const void* GrMTLHandle;
#define SK_API_AVAILABLE_CA_METAL_LAYER SK_API_AVAILABLE(macos(10.11), ios(8.0))
#endif // TARGET_OS_SIMULATOR
#if defined(SK_BUILD_FOR_MAC)
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 110000
#define GR_METAL_SDK_VERSION 230
#elif __MAC_OS_X_VERSION_MAX_ALLOWED >= 101500
#define GR_METAL_SDK_VERSION 220
#elif __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
#define GR_METAL_SDK_VERSION 210
#else
#error Must use at least 10.14 SDK to build Metal backend for MacOS
#endif
#else
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000 || __TV_OS_VERSION_MAX_ALLOWED >= 140000
#define GR_METAL_SDK_VERSION 230
#elif __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 || __TV_OS_VERSION_MAX_ALLOWED >= 130000
#define GR_METAL_SDK_VERSION 220
#elif __IPHONE_OS_VERSION_MAX_ALLOWED >= 120000 || __TV_OS_VERSION_MAX_ALLOWED >= 120000
#define GR_METAL_SDK_VERSION 210
#else
#error Must use at least 12.00 SDK to build Metal backend for iOS
#endif
#endif
/**
* Types for interacting with Metal resources created externally to Skia.
* This is used by GrBackendObjects.

View File

@ -0,0 +1,54 @@
/*
* Copyright 2021 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef GrMtlTypesPriv_DEFINED
#define GrMtlTypesPriv_DEFINED
#include "include/gpu/GrTypes.h"
///////////////////////////////////////////////////////////////////////////////
#ifdef __APPLE__
#include <TargetConditionals.h>
#if defined(SK_BUILD_FOR_MAC)
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 110000
#define GR_METAL_SDK_VERSION 230
#elif __MAC_OS_X_VERSION_MAX_ALLOWED >= 101500
#define GR_METAL_SDK_VERSION 220
#elif __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
#define GR_METAL_SDK_VERSION 210
#else
#error Must use at least 10.14 SDK to build Metal backend for MacOS
#endif
#else
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 140000 || __TV_OS_VERSION_MAX_ALLOWED >= 140000
#define GR_METAL_SDK_VERSION 230
#elif __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000 || __TV_OS_VERSION_MAX_ALLOWED >= 130000
#define GR_METAL_SDK_VERSION 220
#elif __IPHONE_OS_VERSION_MAX_ALLOWED >= 120000 || __TV_OS_VERSION_MAX_ALLOWED >= 120000
#define GR_METAL_SDK_VERSION 210
#else
#error Must use at least 12.00 SDK to build Metal backend for iOS
#endif
#endif
#if __has_attribute(objc_externally_retained)
#define GR_EXTERNALLY_RETAINED __attribute__((objc_externally_retained))
#define GR_EXTERNALLY_RETAINED_BEGIN \
_Pragma("clang attribute push (__attribute__((objc_externally_retained)), apply_to=any(function,objc_method))")
#define GR_EXTERNALLY_RETAINED_END _Pragma("clang attribute pop")
#else
#define GR_EXTERNALLY_RETAINED
#define GR_EXTERNALLY_RETAINED_BEGIN
#define GR_EXTERNALLY_RETAINED_END
#endif
#endif
#endif

View File

@ -40,7 +40,7 @@ private:
GrMtlAttachment(GrMtlGpu* gpu,
SkISize dimensions,
UsageFlags supportedUsages,
const id<MTLTexture> View);
id<MTLTexture> view);
GrMtlGpu* getMtlGpu() const;

View File

@ -14,10 +14,12 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
GrMtlAttachment::GrMtlAttachment(GrMtlGpu* gpu,
SkISize dimensions,
UsageFlags supportedUsages,
const id<MTLTexture> view)
id<MTLTexture> view)
: GrAttachment(gpu, dimensions, supportedUsages, view.sampleCount, GrMipmapped::kNo,
GrProtected::kNo)
, fView(view) {
@ -64,3 +66,5 @@ GrMtlGpu* GrMtlAttachment::getMtlGpu() const {
SkASSERT(!this->wasDestroyed());
return static_cast<GrMtlGpu*>(this->getGpu());
}
GR_EXTERNALLY_RETAINED_END

View File

@ -21,6 +21,8 @@
#define VALIDATE() do {} while(false)
#endif
GR_EXTERNALLY_RETAINED_BEGIN
sk_sp<GrMtlBuffer> GrMtlBuffer::Make(GrMtlGpu* gpu, size_t size, GrGpuBufferType intendedType,
GrAccessPattern accessPattern, const void* data) {
sk_sp<GrMtlBuffer> buffer(new GrMtlBuffer(gpu, size, intendedType, accessPattern));
@ -197,3 +199,5 @@ void GrMtlBuffer::validate() const {
fMappedBuffer.length <= fMtlBuffer.length);
}
#endif
GR_EXTERNALLY_RETAINED_END

View File

@ -104,9 +104,9 @@ public:
private:
void initFeatureSet(MTLFeatureSet featureSet);
void initStencilFormat(const id<MTLDevice> device);
void initStencilFormat(id<MTLDevice> device);
void initGrCaps(const id<MTLDevice> device);
void initGrCaps(id<MTLDevice> device);
void initShaderCaps();
void applyDriverCorrectnessWorkarounds(const GrContextOptions&, const id<MTLDevice>);

View File

@ -26,6 +26,8 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
GrMtlCaps::GrMtlCaps(const GrContextOptions& contextOptions, const id<MTLDevice> device,
MTLFeatureSet featureSet)
: INHERITED(contextOptions) {
@ -263,7 +265,7 @@ bool GrMtlCaps::onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy
dst == src);
}
void GrMtlCaps::initGrCaps(const id<MTLDevice> device) {
void GrMtlCaps::initGrCaps(id<MTLDevice> device) {
// Max vertex attribs is the same on all devices
fMaxVertexAttributes = 31;
@ -1212,3 +1214,5 @@ void GrMtlCaps::onDumpJSON(SkJSONWriter* writer) const {
#else
void GrMtlCaps::onDumpJSON(SkJSONWriter* writer) const { }
#endif
GR_EXTERNALLY_RETAINED_END

View File

@ -19,6 +19,8 @@ class GrMtlGpu;
class GrMtlPipelineState;
class GrMtlOpsRenderPass;
GR_EXTERNALLY_RETAINED_BEGIN
class GrMtlCommandBuffer : public SkRefCnt {
public:
static sk_sp<GrMtlCommandBuffer> Make(id<MTLCommandQueue> queue);
@ -79,4 +81,6 @@ private:
SkSTArray<kInitialTrackedResourcesCount, sk_sp<const GrBuffer>> fTrackedGrBuffers;
};
GR_EXTERNALLY_RETAINED_END
#endif

View File

@ -15,6 +15,8 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
sk_sp<GrMtlCommandBuffer> GrMtlCommandBuffer::Make(id<MTLCommandQueue> queue) {
id<MTLCommandBuffer> mtlCommandBuffer;
mtlCommandBuffer = [queue commandBuffer];
@ -140,3 +142,4 @@ void GrMtlCommandBuffer::encodeWaitForEvent(id<MTLEvent> event, uint64_t eventVa
fHasWork = true;
}
GR_EXTERNALLY_RETAINED_END

View File

@ -13,6 +13,8 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
MTLStencilOperation skia_stencil_op_to_mtl(GrStencilOp op) {
switch (op) {
case GrStencilOp::kKeep:
@ -124,3 +126,5 @@ GrMtlDepthStencil::Key GrMtlDepthStencil::GenerateKey(const GrStencilSettings& s
return depthStencilKey;
}
GR_EXTERNALLY_RETAINED_END

View File

@ -31,6 +31,8 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
static bool get_feature_set(id<MTLDevice> device, MTLFeatureSet* featureSet) {
// Mac OSX
#ifdef SK_BUILD_FOR_MAC
@ -943,7 +945,7 @@ bool GrMtlGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
GrMtlTextureInfo info;
SkAssertResult(backendTexture.getMtlTextureInfo(&info));
id<MTLTexture> mtlTexture = GrGetMTLTexture(info.fTexture.get());
GR_EXTERNALLY_RETAINED id<MTLTexture> mtlTexture = GrGetMTLTexture(info.fTexture.get());
const MTLPixelFormat mtlFormat = mtlTexture.pixelFormat;
@ -1021,7 +1023,7 @@ bool GrMtlGpu::onUpdateBackendTexture(const GrBackendTexture& backendTexture,
MTLOrigin origin = MTLOriginMake(0, 0, 0);
GrMtlCommandBuffer* cmdBuffer = this->commandBuffer();
id<MTLBlitCommandEncoder> blitCmdEncoder = cmdBuffer->getBlitCommandEncoder();
GR_EXTERNALLY_RETAINED auto blitCmdEncoder = cmdBuffer->getBlitCommandEncoder();
GrMtlBuffer* mtlBuffer = static_cast<GrMtlBuffer*>(slice.fBuffer);
SkISize levelDimensions(backendTexture.dimensions());
@ -1629,3 +1631,5 @@ void GrMtlGpu::onDumpJSON(SkJSONWriter* writer) const {
writer->endObject();
}
#endif
GR_EXTERNALLY_RETAINED_END

View File

@ -20,6 +20,8 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
GrMtlOpsRenderPass::GrMtlOpsRenderPass(GrMtlGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin,
const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo)
@ -446,3 +448,5 @@ void GrMtlOpsRenderPass::resetBufferBindings() {
fBufferBindings[i].fBuffer = nil;
}
}
GR_EXTERNALLY_RETAINED_END

View File

@ -21,6 +21,8 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
GrMtlPipelineState::SamplerBindings::SamplerBindings(GrSamplerState state,
GrTexture* texture,
GrMtlGpu* gpu)
@ -227,3 +229,5 @@ bool GrMtlPipelineState::doesntSampleAttachment(
}
return true;
}
GR_EXTERNALLY_RETAINED_END

View File

@ -27,6 +27,8 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
GrMtlPipelineState* GrMtlPipelineStateBuilder::CreatePipelineState(
GrMtlGpu* gpu, const GrProgramDesc& desc, const GrProgramInfo& programInfo,
const GrMtlPrecompiledLibraries* precompiledLibs) {
@ -841,3 +843,5 @@ bool GrMtlPipelineStateBuilder::PrecompileShaders(GrMtlGpu* gpu, const SkData& c
precompiledLibs->fRTHeight = inputs[kFragment_GrShaderType].fRTHeight;
return true;
}
GR_EXTERNALLY_RETAINED_END

View File

@ -14,6 +14,8 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
GrMtlPipelineStateDataManager::GrMtlPipelineStateDataManager(const UniformInfoArray& uniforms,
uint32_t uniformSize)
: INHERITED(uniforms.count(), uniformSize) {
@ -125,3 +127,5 @@ void GrMtlPipelineStateDataManager::uploadAndBindUniformBuffers(
void GrMtlPipelineStateDataManager::resetDirtyBits() {
fUniformsDirty = true;
}
GR_EXTERNALLY_RETAINED_END

View File

@ -14,6 +14,8 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
// Called for wrapped non-texture render targets.
GrMtlRenderTarget::GrMtlRenderTarget(GrMtlGpu* gpu,
SkISize dimensions,
@ -147,3 +149,4 @@ bool GrMtlRenderTarget::completeStencilAttachment() {
return true;
}
GR_EXTERNALLY_RETAINED_END

View File

@ -22,6 +22,8 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
GrMtlResourceProvider::GrMtlResourceProvider(GrMtlGpu* gpu)
: fGpu(gpu) {
fPipelineStateCache.reset(new PipelineStateCache(gpu));
@ -183,3 +185,5 @@ bool GrMtlResourceProvider::PipelineStateCache::precompileShader(const SkData& k
return true;
}
GR_EXTERNALLY_RETAINED_END

View File

@ -13,6 +13,8 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
static inline MTLSamplerAddressMode wrap_mode_to_mtl_sampler_address(
GrSamplerState::WrapMode wrapMode, const GrCaps& caps) {
switch (wrapMode) {
@ -82,3 +84,5 @@ GrMtlSampler* GrMtlSampler::Create(const GrMtlGpu* gpu, GrSamplerState samplerSt
GrMtlSampler::Key GrMtlSampler::GenerateKey(GrSamplerState samplerState) {
return samplerState.asIndex();
}
GR_EXTERNALLY_RETAINED_END

View File

@ -13,6 +13,8 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
std::unique_ptr<GrMtlSemaphore> GrMtlSemaphore::Make(GrMtlGpu* gpu) {
if (@available(macOS 10.14, iOS 12.0, *)) {
id<MTLEvent> event = [gpu->device() newEvent];
@ -49,3 +51,5 @@ GrBackendSemaphore GrMtlSemaphore::backendSemaphore() const {
}
return backendSemaphore;
}
GR_EXTERNALLY_RETAINED_END

View File

@ -15,6 +15,8 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
GrMtlTexture::GrMtlTexture(GrMtlGpu* gpu,
SkBudgeted budgeted,
SkISize dimensions,
@ -120,3 +122,4 @@ GrBackendFormat GrMtlTexture::backendFormat() const {
return GrBackendFormat::MakeMtl(fTexture.pixelFormat);
}
GR_EXTERNALLY_RETAINED_END

View File

@ -13,6 +13,8 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
GrMtlTextureRenderTarget::GrMtlTextureRenderTarget(GrMtlGpu* gpu,
SkBudgeted budgeted,
SkISize dimensions,
@ -144,3 +146,5 @@ sk_sp<GrMtlTextureRenderTarget> GrMtlTextureRenderTarget::MakeWrappedTextureRend
new GrMtlTextureRenderTarget(gpu, dimensions, texture, mipmapStatus, cacheable));
}
}
GR_EXTERNALLY_RETAINED_END

View File

@ -13,8 +13,12 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
sk_sp<GrGpu> GrMtlTrampoline::MakeGpu(const GrMtlBackendContext& backendContext,
const GrContextOptions& options,
GrDirectContext* direct) {
return GrMtlGpu::Make(backendContext, options, direct);
}
GR_EXTERNALLY_RETAINED_END

View File

@ -5,14 +5,18 @@
* found in the LICENSE file.
*/
#include "src/gpu/mtl/GrMtlUniformHandler.h"
#include "include/private/GrMtlTypesPriv.h"
#include "src/gpu/GrTexture.h"
#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
#include "src/gpu/mtl/GrMtlUniformHandler.h"
#if !__has_feature(objc_arc)
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
// TODO: this class is basically copy and pasted from GrVklUniformHandler so that we can have
// some shaders working. The SkSL Metal code generator was written to work with GLSL generated for
// the Ganesh Vulkan backend, so it should all work. There might be better ways to do things in
@ -316,3 +320,5 @@ void GrMtlUniformHandler::appendUniformDecls(GrShaderFlags visibility, SkString*
out->appendf("%s\n};\n", uniformsString.c_str());
}
}
GR_EXTERNALLY_RETAINED_END

View File

@ -11,30 +11,23 @@
#import <Metal/Metal.h>
#include "include/gpu/GrBackendSurface.h"
#include "include/private/GrMtlTypesPriv.h"
#include "include/private/GrTypesPriv.h"
#include "src/sksl/ir/SkSLProgram.h"
#if defined(SK_BUILD_FOR_MAC)
#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101400
#error Must use at least 10.14 SDK to build Metal backend for MacOS
#endif
#else
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 120000 && __TV_OS_VERSION_MAX_ALLOWED < 120000
#error Must use at least 12.00 SDK to build Metal backend for iOS
#endif
#endif
class GrMtlGpu;
class GrSurface;
GR_EXTERNALLY_RETAINED_BEGIN
/**
* Returns a id<MTLTexture> to the MTLTexture pointed at by the const void*.
*/
SK_ALWAYS_INLINE id<MTLTexture> GrGetMTLTexture(const void* mtlTexture) {
#if __has_feature(objc_arc)
// ARC will retain when bridging from a CoreFoundation to an ObjC object
return (__bridge id<MTLTexture>)mtlTexture;
#else
// ARC will retain when bridging from a CoreFoundation to an ObjC object
return (id<MTLTexture>) CFRetain(mtlTexture);
#endif
}
@ -130,4 +123,6 @@ size_t GrMtlFormatBytesPerBlock(MTLPixelFormat);
int GrMtlFormatStencilBits(MTLPixelFormat);
GR_EXTERNALLY_RETAINED_END
#endif

View File

@ -24,6 +24,8 @@
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
NSError* GrCreateMtlError(NSString* description, GrMtlErrorCode errorCode) {
NSDictionary* userInfo = [NSDictionary dictionaryWithObject:description
forKey:NSLocalizedDescriptionKey];
@ -430,5 +432,4 @@ const char* GrMtlFormatToStr(GrMTLPixelFormat mtlFormat) {
#endif
GR_EXTERNALLY_RETAINED_END

View File

@ -7,10 +7,14 @@
#include "src/gpu/mtl/GrMtlVaryingHandler.h"
#include "include/private/GrMtlTypesPriv.h"
#if !__has_feature(objc_arc)
#error This file must be compiled with Arc. Use -fobjc-arc flag
#endif
GR_EXTERNALLY_RETAINED_BEGIN
static void finalize_helper(GrMtlVaryingHandler::VarArray& vars) {
int locationIndex = 0;
int componentCount = 0;
@ -40,3 +44,5 @@ void GrMtlVaryingHandler::onFinalize() {
finalize_helper(fFragInputs);
finalize_helper(fFragOutputs);
}
GR_EXTERNALLY_RETAINED_END