1 /* 2 * Copyright 2017 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef GrMtlRenderTarget_DEFINED 9 #define GrMtlRenderTarget_DEFINED 10 11 #include "src/gpu/ganesh/GrRenderTarget.h" 12 13 #include "include/gpu/GrBackendSurface.h" 14 #include "src/gpu/ganesh/GrGpu.h" 15 #include "src/gpu/ganesh/mtl/GrMtlAttachment.h" 16 #include "src/gpu/ganesh/mtl/GrMtlFramebuffer.h" 17 18 #import <Metal/Metal.h> 19 20 class GrMtlGpu; 21 22 class GrMtlRenderTarget: public GrRenderTarget { 23 public: 24 // If sampleCnt is greater than 1 and the texture is single sampled, then a MSAA texture 25 // is created that will resolve to the wrapped single sample texture. 26 static sk_sp<GrMtlRenderTarget> MakeWrappedRenderTarget(GrMtlGpu*, 27 SkISize, 28 int sampleCnt, 29 id<MTLTexture>); 30 31 ~GrMtlRenderTarget() override; 32 canAttemptStencilAttachment(bool useMSAASurface)33 bool canAttemptStencilAttachment(bool useMSAASurface) const override { 34 SkASSERT(useMSAASurface == (this->numSamples() > 1)); 35 return true; 36 } 37 colorAttachment()38 GrMtlAttachment* colorAttachment() const { return fColorAttachment.get(); } colorMTLTexture()39 id<MTLTexture> colorMTLTexture() const { return fColorAttachment->mtlTexture(); } resolveAttachment()40 GrMtlAttachment* resolveAttachment() const { return fResolveAttachment.get(); } resolveMTLTexture()41 id<MTLTexture> resolveMTLTexture() const { return fResolveAttachment->mtlTexture(); } 42 43 // Returns the GrMtlAttachment of the non-msaa attachment. If the color attachment has 1 sample, 44 // then the color attachment will be returned. Otherwise, the resolve attachment is returned. nonMSAAAttachment()45 GrMtlAttachment* nonMSAAAttachment() const { 46 if (fColorAttachment->numSamples() == 1) { 47 return fColorAttachment.get(); 48 } else { 49 return fResolveAttachment.get(); 50 } 51 } 52 53 GrBackendRenderTarget getBackendRenderTarget() const override; 54 55 GrBackendFormat backendFormat() const override; 56 57 const GrMtlFramebuffer* getFramebuffer(bool withResolve, 58 bool withStencil); 59 60 protected: 61 GrMtlRenderTarget(GrMtlGpu* gpu, 62 SkISize, 63 sk_sp<GrMtlAttachment> colorAttachment, 64 sk_sp<GrMtlAttachment> resolveAttachment, 65 std::string_view label); 66 67 GrMtlGpu* getMtlGpu() const; 68 69 void onAbandon() override; 70 void onRelease() override; 71 72 // This returns zero since the memory should all be handled by the attachments onGpuMemorySize()73 size_t onGpuMemorySize() const override { return 0; } 74 75 void onSetLabel() override; 76 77 sk_sp<GrMtlAttachment> fColorAttachment; 78 sk_sp<GrMtlAttachment> fResolveAttachment; 79 80 private: 81 // Extra param to disambiguate from constructor used by subclasses. 82 enum Wrapped { kWrapped }; 83 GrMtlRenderTarget(GrMtlGpu* gpu, 84 SkISize, 85 sk_sp<GrMtlAttachment> colorAttachment, 86 sk_sp<GrMtlAttachment> resolveAttachment, 87 Wrapped, 88 std::string_view label); 89 90 bool completeStencilAttachment(GrAttachment* stencil, bool useMSAASurface) override; 91 92 // We can have a renderpass with and without resolve attachment or stencil attachment, 93 // both of these being completely orthogonal. Thus we have a total of 4 types of render passes. 94 // We then cache a framebuffer for each type of these render passes. 95 // TODO: add support for other flags if needed 96 inline static constexpr int kNumCachedFramebuffers = 4; 97 98 sk_sp<const GrMtlFramebuffer> fCachedFramebuffers[kNumCachedFramebuffers]; 99 100 101 using INHERITED = GrRenderTarget; 102 }; 103 104 105 #endif 106 107