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