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> CreateNewRenderTarget(GrMtlGpu*, const GrSurfaceDesc&,
22                                                           SkBudgeted);
23 
24     static sk_sp<GrMtlRenderTarget> MakeWrappedRenderTarget(GrMtlGpu*, const GrSurfaceDesc&);
25 
26     ~GrMtlRenderTarget() override;
27 
28     // override of GrRenderTarget
29     ResolveType getResolveType() const override {
30         return kCantResolve_ResolveType;
31 #if 0 // TODO figure this once we support msaa
32         if (this->numColorSamples() > 1) {
33             return kCanResolve_ResolveType;
34         }
35         return kAutoResolves_ResolveType;
36 #endif
37     }
38 
39     bool canAttemptStencilAttachment() const override {
40         return true;
41     }
42 
43     GrBackendObject getRenderTargetHandle() const override;
44 
45     GrBackendRenderTarget getBackendRenderTarget() const override {
46         return GrBackendRenderTarget(); // invalid
47     }
48 
49 protected:
50     GrMtlRenderTarget(GrMtlGpu* gpu,
51                       const GrSurfaceDesc& desc,
52                       id<MTLTexture> renderTexture,
53                       id<MTLTexture> resolveTexture);
54 
55     GrMtlRenderTarget(GrMtlGpu* gpu,
56                       const GrSurfaceDesc& desc,
57                       id<MTLTexture> renderTexture);
58 
59     GrMtlGpu* getMtlGpu() const;
60 
61     void onAbandon() override;
62     void onRelease() override;
63 
64     // This accounts for the texture's memory and any MSAA renderbuffer's memory.
65     size_t onGpuMemorySize() const override {
66         int numColorSamples = this->numColorSamples();
67         // The plus 1 is to account for the resolve texture or if not using msaa the RT itself
68         if (numColorSamples > 1) {
69             ++numColorSamples;
70         }
71         return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
72                                       numColorSamples, GrMipMapped::kNo);
73     }
74 
75     id<MTLTexture> fRenderTexture;
76     id<MTLTexture> fResolveTexture;
77 
78 private:
79     GrMtlRenderTarget(GrMtlGpu* gpu,
80                       const GrSurfaceDesc& desc,
81                       SkBudgeted,
82                       id<MTLTexture> renderTexture,
83                       id<MTLTexture> resolveTexture);
84 
85     GrMtlRenderTarget(GrMtlGpu* gpu,
86                       const GrSurfaceDesc& desc,
87                       SkBudgeted,
88                       id<MTLTexture> renderTexture);
89 
90     static sk_sp<GrMtlRenderTarget> Make(GrMtlGpu*,
91                                          const GrSurfaceDesc&,
92                                          SkBudgeted,
93                                          id<MTLTexture> renderTexture,
94                                          bool isWrapped);
95 
96     bool completeStencilAttachment() override;
97 };
98 
99 
100 #endif
101 
102