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 16 #import <Metal/Metal.h> 17 18 class GrMtlGpu; 19 20 class GrMtlRenderTarget: public GrRenderTarget { 21 public: 22 static sk_sp<GrMtlRenderTarget> MakeWrappedRenderTarget(GrMtlGpu*, 23 SkISize, 24 int sampleCnt, 25 id<MTLTexture>); 26 27 ~GrMtlRenderTarget() override; 28 canAttemptStencilAttachment()29 bool canAttemptStencilAttachment() const override { 30 return true; 31 } 32 mtlColorTexture()33 id<MTLTexture> mtlColorTexture() const { return fColorTexture; } mtlResolveTexture()34 id<MTLTexture> mtlResolveTexture() const { return fResolveTexture; } 35 36 GrBackendRenderTarget getBackendRenderTarget() const override; 37 38 GrBackendFormat backendFormat() const override; 39 40 protected: 41 GrMtlRenderTarget(GrMtlGpu* gpu, 42 SkISize, 43 int sampleCnt, 44 id<MTLTexture> colorTexture, 45 id<MTLTexture> resolveTexture); 46 47 GrMtlRenderTarget(GrMtlGpu* gpu, SkISize, id<MTLTexture> colorTexture); 48 49 GrMtlGpu* getMtlGpu() const; 50 51 void onAbandon() override; 52 void onRelease() override; 53 54 // This accounts for the texture's memory and any MSAA renderbuffer's memory. onGpuMemorySize()55 size_t onGpuMemorySize() const override { 56 int numColorSamples = this->numSamples(); 57 // TODO: When used as render targets certain formats may actually have a larger size than 58 // the base format size. Check to make sure we are reporting the correct value here. 59 // The plus 1 is to account for the resolve texture or if not using msaa the RT itself 60 if (numColorSamples > 1) { 61 ++numColorSamples; 62 } 63 const GrCaps& caps = *this->getGpu()->caps(); 64 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(), 65 numColorSamples, GrMipMapped::kNo); 66 } 67 68 id<MTLTexture> fColorTexture; 69 id<MTLTexture> fResolveTexture; 70 71 private: 72 // Extra param to disambiguate from constructor used by subclasses. 73 enum Wrapped { kWrapped }; 74 GrMtlRenderTarget(GrMtlGpu* gpu, 75 SkISize, 76 int sampleCnt, 77 id<MTLTexture> colorTexture, 78 id<MTLTexture> resolveTexture, 79 Wrapped); 80 GrMtlRenderTarget(GrMtlGpu* gpu, SkISize, id<MTLTexture> colorTexture, Wrapped); 81 82 bool completeStencilAttachment() override; 83 84 typedef GrRenderTarget INHERITED; 85 }; 86 87 88 #endif 89 90