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/GrRenderTarget.h" 12 13 #include "include/gpu/GrBackendSurface.h" 14 #include "src/gpu/GrGpu.h" 15 #include "src/gpu/mtl/GrMtlAttachment.h" 16 #include "src/gpu/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 66 GrMtlGpu* getMtlGpu() const; 67 68 void onAbandon() override; 69 void onRelease() override; 70 71 // This returns zero since the memory should all be handled by the attachments onGpuMemorySize()72 size_t onGpuMemorySize() const override { return 0; } 73 74 sk_sp<GrMtlAttachment> fColorAttachment; 75 sk_sp<GrMtlAttachment> fResolveAttachment; 76 77 private: 78 // Extra param to disambiguate from constructor used by subclasses. 79 enum Wrapped { kWrapped }; 80 GrMtlRenderTarget(GrMtlGpu* gpu, 81 SkISize, 82 sk_sp<GrMtlAttachment> colorAttachment, 83 sk_sp<GrMtlAttachment> resolveAttachment, 84 Wrapped); 85 86 bool completeStencilAttachment(GrAttachment* stencil, bool useMSAASurface) override; 87 88 // We can have a renderpass with and without resolve attachment or stencil attachment, 89 // both of these being completely orthogonal. Thus we have a total of 4 types of render passes. 90 // We then cache a framebuffer for each type of these render passes. 91 // TODO: add support for other flags if needed 92 inline static constexpr int kNumCachedFramebuffers = 4; 93 94 sk_sp<const GrMtlFramebuffer> fCachedFramebuffers[kNumCachedFramebuffers]; 95 96 97 using INHERITED = GrRenderTarget; 98 }; 99 100 101 #endif 102 103