Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 7730d7c

Browse files
jvanverthSkia Commit-Bot
authored andcommitted
Revert "Change Metal to not take ownership of objects"
This reverts commit 186a295. Reason for revert: Metal bots failing Original change's description: > Change Metal to not take ownership of objects > > Prior to this change, Skia/Metal interfaces take ownership of the Metal > objects passed in (that is, the caller should count passing the object > to Skia as "freeing" the object). > > Change this behavior so that Skia/Metal retains its own separate > ownership of the Metal objects. > > Make GrBackendTexture and GrBackendRenderTarget maintain their own > references to the underlying MTLTexture by using the CFRetain/CFRelease > interfaces. Do this by adding a private GrMtlBackendSurfaceInfo. > > Move GrMtlBackendSurfaceInfo (formerly GrMtlTextureInfo) out of the > union in GrBackendTexture and GrBackendRenderTarget because unions > cannot have nontrivial constructors and destructors (how fVkInfo isn't > causing a compile error is unclear). > > Change-Id: Iae3719c0715825d86503d03c766e47f0f6015bdf > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/215685 > Commit-Queue: Jim Van Verth <[email protected]> > Reviewed-by: Jim Van Verth <[email protected]> [email protected],[email protected],[email protected],[email protected] # Not skipping CQ checks because original CL landed > 1 day ago. Change-Id: Ie569fe1938857706b5413876a9480ef1eb3314ea Reviewed-on: https://skia-review.googlesource.com/c/skia/+/216221 Reviewed-by: Jim Van Verth <[email protected]> Commit-Queue: Jim Van Verth <[email protected]>
1 parent dbddfff commit 7730d7c

File tree

12 files changed

+64
-137
lines changed

12 files changed

+64
-137
lines changed

gn/gpu.gni

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,6 @@ skia_vk_sources = [
701701

702702
skia_metal_sources = [
703703
"$_include/gpu/mtl/GrMtlTypes.h",
704-
"$_include/private/GrMtlTypesPriv.h",
705704
"$_src/gpu/mtl/GrMtlBuffer.h",
706705
"$_src/gpu/mtl/GrMtlBuffer.mm",
707706
"$_src/gpu/mtl/GrMtlCaps.h",
@@ -739,7 +738,6 @@ skia_metal_sources = [
739738
"$_src/gpu/mtl/GrMtlTextureRenderTarget.mm",
740739
"$_src/gpu/mtl/GrMtlTrampoline.h",
741740
"$_src/gpu/mtl/GrMtlTrampoline.mm",
742-
"$_src/gpu/mtl/GrMtlTypesPriv.cpp",
743741
"$_src/gpu/mtl/GrMtlUniformHandler.h",
744742
"$_src/gpu/mtl/GrMtlUniformHandler.mm",
745743
"$_src/gpu/mtl/GrMtlUtil.h",

include/gpu/GrBackendSurface.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class GrVkImageLayout;
1818

1919
#ifdef SK_METAL
2020
#include "include/gpu/mtl/GrMtlTypes.h"
21-
#include "include/private/GrMtlTypesPriv.h"
2221
#endif
2322

2423
#if !SK_SUPPORT_GPU
@@ -146,7 +145,6 @@ class SK_API GrBackendTexture {
146145
const GrVkImageInfo& vkInfo);
147146

148147
#ifdef SK_METAL
149-
// This will take a strong reference to the MTLTexture in mtlInfo.
150148
GrBackendTexture(int width,
151149
int height,
152150
GrMipMapped,
@@ -255,11 +253,11 @@ class SK_API GrBackendTexture {
255253
union {
256254
GrGLTextureInfo fGLInfo;
257255
GrVkBackendSurfaceInfo fVkInfo;
258-
GrMockTextureInfo fMockInfo;
259-
};
260256
#ifdef SK_METAL
261-
GrMtlBackendSurfaceInfo fMtlInfo;
257+
GrMtlTextureInfo fMtlInfo;
262258
#endif
259+
GrMockTextureInfo fMockInfo;
260+
};
263261
};
264262

265263
class SK_API GrBackendRenderTarget {
@@ -283,7 +281,6 @@ class SK_API GrBackendRenderTarget {
283281
GrBackendRenderTarget(int width, int height, int sampleCnt, const GrVkImageInfo& vkInfo);
284282

285283
#ifdef SK_METAL
286-
// This will take a strong reference to the MTLTexture in mtlInfo.
287284
GrBackendRenderTarget(int width,
288285
int height,
289286
int sampleCnt,
@@ -373,16 +370,17 @@ class SK_API GrBackendRenderTarget {
373370
int fSampleCnt;
374371
int fStencilBits;
375372
GrPixelConfig fConfig;
373+
376374
GrBackendApi fBackend;
377375

378376
union {
379377
GrGLFramebufferInfo fGLInfo;
380378
GrVkBackendSurfaceInfo fVkInfo;
381-
GrMockRenderTargetInfo fMockInfo;
382-
};
383379
#ifdef SK_METAL
384-
GrMtlBackendSurfaceInfo fMtlInfo;
380+
GrMtlTextureInfo fMtlInfo;
385381
#endif
382+
GrMockRenderTargetInfo fMockInfo;
383+
};
386384
};
387385

388386
#endif

include/gpu/GrContext.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ class SK_API GrContext : public GrRecordingContext {
6161
#ifdef SK_METAL
6262
/**
6363
* Makes a GrContext which uses Metal as the backend. The device parameter is an MTLDevice
64-
* and queue is an MTLCommandQueue which should be used by the backend. These objects will
65-
* be retained by Ganesh, and released when the GrContext is destroyed.
64+
* and queue is an MTLCommandQueue which should be used by the backend. These objects must
65+
* have a ref on them which can be transferred to Ganesh which will release the ref when the
66+
* GrContext is destroyed.
6667
*/
6768
static sk_sp<GrContext> MakeMetal(void* device, void* queue, const GrContextOptions& options);
6869
static sk_sp<GrContext> MakeMetal(void* device, void* queue);

include/gpu/mtl/GrMtlTypes.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ typedef unsigned int GrMTLPixelFormat;
1717

1818
///////////////////////////////////////////////////////////////////////////////
1919
/**
20-
* Types for interacting with Metal resources created externally to Skia. Holds
21-
* the MTLTexture as a weak const void*. This is used by GrBackendObjects. Note
22-
* that GrBackendObjects created with a GrMtlTextureInfo will take a strong
23-
* reference to the MTLTexture.
20+
* Types for interacting with Metal resources created externally to Skia. Holds the MTLTexture as a
21+
* const void*. This is used by GrBackendObjects.
2422
*/
2523
struct GrMtlTextureInfo {
2624
public:

include/private/GrMtlTypesPriv.h

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/gpu/GrBackendSurface.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,6 @@ void GrBackendTexture::cleanup() {
250250
fVkInfo.cleanup();
251251
}
252252
#endif
253-
#ifdef SK_METAL
254-
if (this->isValid() && GrBackendApi::kMetal == fBackend) {
255-
fMtlInfo.cleanup();
256-
}
257-
#endif
258253
}
259254

260255
GrBackendTexture::GrBackendTexture(const GrBackendTexture& that) : fIsValid(false) {
@@ -284,7 +279,7 @@ GrBackendTexture& GrBackendTexture::operator=(const GrBackendTexture& that) {
284279
break;
285280
#ifdef SK_METAL
286281
case GrBackendApi::kMetal:
287-
fMtlInfo.assign(that.fMtlInfo, this->isValid());
282+
fMtlInfo = that.fMtlInfo;
288283
break;
289284
#endif
290285
case GrBackendApi::kMock:
@@ -334,7 +329,7 @@ sk_sp<GrVkImageLayout> GrBackendTexture::getGrVkImageLayout() const {
334329
#ifdef SK_METAL
335330
bool GrBackendTexture::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
336331
if (this->isValid() && GrBackendApi::kMetal == fBackend) {
337-
*outInfo = fMtlInfo.snapTextureInfo();
332+
*outInfo = fMtlInfo;
338333
return true;
339334
}
340335
return false;
@@ -381,8 +376,7 @@ bool GrBackendTexture::isSameTexture(const GrBackendTexture& that) {
381376
#endif
382377
#ifdef SK_METAL
383378
case GrBackendApi::kMetal:
384-
return this->fMtlInfo.snapTextureInfo().fTexture ==
385-
that.fMtlInfo.snapTextureInfo().fTexture;
379+
return this->fMtlInfo.fTexture == that.fMtlInfo.fTexture;
386380
#endif
387381
case GrBackendApi::kMock:
388382
return fMockInfo.fID == that.fMockInfo.fID;
@@ -554,11 +548,6 @@ void GrBackendRenderTarget::cleanup() {
554548
fVkInfo.cleanup();
555549
}
556550
#endif
557-
#ifdef SK_METAL
558-
if (this->isValid() && GrBackendApi::kMetal == fBackend) {
559-
fMtlInfo.cleanup();
560-
}
561-
#endif
562551
}
563552

564553
GrBackendRenderTarget::GrBackendRenderTarget(const GrBackendRenderTarget& that) : fIsValid(false) {
@@ -589,7 +578,7 @@ GrBackendRenderTarget& GrBackendRenderTarget::operator=(const GrBackendRenderTar
589578
break;
590579
#ifdef SK_METAL
591580
case GrBackendApi::kMetal:
592-
fMtlInfo.assign(that.fMtlInfo, this->isValid());
581+
fMtlInfo = that.fMtlInfo;
593582
break;
594583
#endif
595584
case GrBackendApi::kMock:
@@ -632,7 +621,7 @@ sk_sp<GrVkImageLayout> GrBackendRenderTarget::getGrVkImageLayout() const {
632621
#ifdef SK_METAL
633622
bool GrBackendRenderTarget::getMtlTextureInfo(GrMtlTextureInfo* outInfo) const {
634623
if (this->isValid() && GrBackendApi::kMetal == fBackend) {
635-
*outInfo = fMtlInfo.snapTextureInfo();
624+
*outInfo = fMtlInfo;
636625
return true;
637626
}
638627
return false;

src/gpu/mtl/GrMtlGpu.mm

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -495,20 +495,21 @@ static bool check_max_blit_width(int widthInPixels) {
495495
return std::move(tex);
496496
}
497497

498-
static id<MTLTexture> get_texture_from_backend(const GrBackendTexture& backendTex) {
498+
static id<MTLTexture> get_texture_from_backend(const GrBackendTexture& backendTex,
499+
GrWrapOwnership ownership) {
499500
GrMtlTextureInfo textureInfo;
500501
if (!backendTex.getMtlTextureInfo(&textureInfo)) {
501502
return nil;
502503
}
503-
return GrGetMTLTexture(textureInfo.fTexture);
504+
return GrGetMTLTexture(textureInfo.fTexture, ownership);
504505
}
505506

506507
static id<MTLTexture> get_texture_from_backend(const GrBackendRenderTarget& backendRT) {
507508
GrMtlTextureInfo textureInfo;
508509
if (!backendRT.getMtlTextureInfo(&textureInfo)) {
509510
return nil;
510511
}
511-
return GrGetMTLTexture(textureInfo.fTexture);
512+
return GrGetMTLTexture(textureInfo.fTexture, GrWrapOwnership::kBorrow_GrWrapOwnership);
512513
}
513514

514515
static inline void init_surface_desc(GrSurfaceDesc* surfaceDesc, id<MTLTexture> mtlTexture,
@@ -526,7 +527,7 @@ static inline void init_surface_desc(GrSurfaceDesc* surfaceDesc, id<MTLTexture>
526527
sk_sp<GrTexture> GrMtlGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
527528
GrWrapOwnership ownership,
528529
GrWrapCacheable cacheable, GrIOType ioType) {
529-
id<MTLTexture> mtlTexture = get_texture_from_backend(backendTex);
530+
id<MTLTexture> mtlTexture = get_texture_from_backend(backendTex, ownership);
530531
if (!mtlTexture) {
531532
return nullptr;
532533
}
@@ -541,7 +542,7 @@ static inline void init_surface_desc(GrSurfaceDesc* surfaceDesc, id<MTLTexture>
541542
int sampleCnt,
542543
GrWrapOwnership ownership,
543544
GrWrapCacheable cacheable) {
544-
id<MTLTexture> mtlTexture = get_texture_from_backend(backendTex);
545+
id<MTLTexture> mtlTexture = get_texture_from_backend(backendTex, ownership);
545546
if (!mtlTexture) {
546547
return nullptr;
547548
}
@@ -575,7 +576,8 @@ static inline void init_surface_desc(GrSurfaceDesc* surfaceDesc, id<MTLTexture>
575576

576577
sk_sp<GrRenderTarget> GrMtlGpu::onWrapBackendTextureAsRenderTarget(
577578
const GrBackendTexture& backendTex, int sampleCnt) {
578-
id<MTLTexture> mtlTexture = get_texture_from_backend(backendTex);
579+
id<MTLTexture> mtlTexture = get_texture_from_backend(backendTex,
580+
GrWrapOwnership::kBorrow_GrWrapOwnership);
579581
if (!mtlTexture) {
580582
return nullptr;
581583
}
@@ -695,7 +697,7 @@ static inline void init_surface_desc(GrSurfaceDesc* surfaceDesc, id<MTLTexture>
695697
[cmdBuffer waitUntilCompleted];
696698
transferBuffer = nil;
697699

698-
info->fTexture = GrGetPtrFromId(testTexture);
700+
info->fTexture = GrReleaseId(testTexture);
699701

700702
return true;
701703
}
@@ -800,6 +802,12 @@ static bool mtl_format_to_pixel_config(MTLPixelFormat format, GrPixelConfig* con
800802

801803
void GrMtlGpu::deleteBackendTexture(const GrBackendTexture& tex) {
802804
SkASSERT(GrBackendApi::kMetal == tex.fBackend);
805+
806+
GrMtlTextureInfo info;
807+
if (tex.getMtlTextureInfo(&info)) {
808+
// Adopts the metal texture so that ARC will clean it up.
809+
GrGetMTLTexture(info.fTexture, GrWrapOwnership::kAdopt_GrWrapOwnership);
810+
}
803811
}
804812

805813
#if GR_TEST_UTILS
@@ -810,7 +818,8 @@ static bool mtl_format_to_pixel_config(MTLPixelFormat format, GrPixelConfig* con
810818
if (!tex.getMtlTextureInfo(&info)) {
811819
return false;
812820
}
813-
id<MTLTexture> mtlTexture = GrGetMTLTexture(info.fTexture);
821+
id<MTLTexture> mtlTexture = GrGetMTLTexture(info.fTexture,
822+
GrWrapOwnership::kBorrow_GrWrapOwnership);
814823
if (!mtlTexture) {
815824
return false;
816825
}
@@ -846,6 +855,8 @@ static bool mtl_format_to_pixel_config(MTLPixelFormat format, GrPixelConfig* con
846855
GrMtlTextureInfo info;
847856
if (rt.getMtlTextureInfo(&info)) {
848857
this->testingOnly_flushGpuAndSync();
858+
// Adopts the metal texture so that ARC will clean it up.
859+
GrGetMTLTexture(info.fTexture, GrWrapOwnership::kAdopt_GrWrapOwnership);
849860
}
850861
}
851862

src/gpu/mtl/GrMtlTrampoline.mm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
const GrContextOptions& options,
1818
void* device,
1919
void* queue) {
20-
return GrMtlGpu::Make(
21-
context, options, (__bridge id<MTLDevice>)device, (__bridge id<MTLCommandQueue>)queue);
20+
return GrMtlGpu::Make(context,
21+
options,
22+
(__bridge_transfer id<MTLDevice>)device,
23+
(__bridge_transfer id<MTLCommandQueue>)queue);
2224
}
2325

src/gpu/mtl/GrMtlTypesPriv.cpp

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/gpu/mtl/GrMtlUtil.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,27 @@ class GrSurface;
3030
#endif
3131
#endif
3232

33-
#if !__has_feature(objc_arc)
34-
#error This file must be compiled with Arc. Use -fobjc-arc flag
35-
#endif
36-
3733
/**
3834
* Returns the Metal texture format for the given GrPixelConfig
3935
*/
4036
bool GrPixelConfigToMTLFormat(GrPixelConfig config, MTLPixelFormat* format);
4137

4238
/**
43-
* Returns a id<MTLTexture> to the MTLTexture pointed at by the const void* (uses __bridge).
39+
* Returns a id<MTLTexture> to the MTLTexture pointed at by the const void*. Will use
40+
* __bridge_transfer if we are adopting ownership.
4441
*/
45-
id<MTLTexture> GrGetMTLTexture(const void* mtlTexture);
42+
id<MTLTexture> GrGetMTLTexture(const void* mtlTexture, GrWrapOwnership);
4643

4744
/**
48-
* Returns a weak const void* to whatever the id object is pointing to (uses __bridge).
45+
* Returns a const void* to whatever the id object is pointing to. Always uses __bridge.
4946
*/
5047
const void* GrGetPtrFromId(id idObject);
5148

49+
/**
50+
* Returns a const void* to whatever the id object is pointing to. Always uses __bridge_retained.
51+
*/
52+
const void* GrReleaseId(id idObject);
53+
5254
/**
5355
* Returns a MTLTextureDescriptor which describes the MTLTexture. Useful when creating a duplicate
5456
* MTLTexture without the same storage allocation.

0 commit comments

Comments
 (0)