• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GrMtlGpu_DEFINED
9 #define GrMtlGpu_DEFINED
10 
11 #include "include/gpu/ganesh/mtl/GrMtlBackendContext.h"
12 #include "include/private/base/SkDeque.h"
13 #include "src/gpu/ganesh/mtl/GrMtlTypesPriv.h"
14 
15 #include "src/gpu/ganesh/GrGpu.h"
16 #include "src/gpu/ganesh/GrRenderTarget.h"
17 #include "src/gpu/ganesh/GrRingBuffer.h"
18 #include "src/gpu/ganesh/GrSemaphore.h"
19 #include "src/gpu/ganesh/GrStagingBufferManager.h"
20 #include "src/gpu/ganesh/GrTexture.h"
21 
22 #include "src/gpu/ganesh/mtl/GrMtlAttachment.h"
23 #include "src/gpu/ganesh/mtl/GrMtlCaps.h"
24 #include "src/gpu/ganesh/mtl/GrMtlCommandBuffer.h"
25 #include "src/gpu/ganesh/mtl/GrMtlResourceProvider.h"
26 #include "src/gpu/ganesh/mtl/GrMtlUtil.h"
27 
28 #import <Metal/Metal.h>
29 
30 class GrMtlOpsRenderPass;
31 class GrMtlTexture;
32 class GrSemaphore;
33 class GrMtlCommandBuffer;
34 
35 class GrMtlGpu : public GrGpu {
36 public:
37     static std::unique_ptr<GrGpu> Make(const GrMtlBackendContext&,
38                                        const GrContextOptions&,
39                                        GrDirectContext*);
40     ~GrMtlGpu() override;
41 
42     void disconnect(DisconnectType) override;
43 
44     GrThreadSafePipelineBuilder* pipelineBuilder() override;
45     sk_sp<GrThreadSafePipelineBuilder> refPipelineBuilder() override;
46 
mtlCaps()47     const GrMtlCaps& mtlCaps() const { return *fMtlCaps; }
48 
device()49     id<MTLDevice> device() const { return fDevice; }
50 
resourceProvider()51     GrMtlResourceProvider& resourceProvider() { return fResourceProvider; }
52 
stagingBufferManager()53     GrStagingBufferManager* stagingBufferManager() override { return &fStagingBufferManager; }
54 
55     GrMtlCommandBuffer* commandBuffer();
56 
57     enum SyncQueue {
58         kForce_SyncQueue,
59         kSkip_SyncQueue
60     };
61 
62     void deleteBackendTexture(const GrBackendTexture&) override;
63 
64     bool compile(const GrProgramDesc&, const GrProgramInfo&) override;
65 
66     bool precompileShader(const SkData& key, const SkData& data) override;
67 
68 #if defined(GR_TEST_UTILS)
69     bool isTestingOnlyBackendTexture(const GrBackendTexture&) const override;
70 
71     GrBackendRenderTarget createTestingOnlyBackendRenderTarget(SkISize dimensions,
72                                                                GrColorType,
73                                                                int sampleCnt,
74                                                                GrProtected) override;
75     void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) override;
76 
resetShaderCacheForTesting()77     void resetShaderCacheForTesting() const override {
78         fResourceProvider.resetShaderCacheForTesting();
79     }
80 #endif
81 
82     void copySurfaceAsResolve(GrSurface* dst, GrSurface* src);
83 
84     void copySurfaceAsBlit(GrSurface* dst, GrSurface* src,
85                            GrMtlAttachment* dstAttachment, GrMtlAttachment* srcAttachment,
86                            const SkIRect& srcRect, const SkIPoint& dstPoint);
87 
88     bool onCopySurface(GrSurface* dst, const SkIRect& dstRect,
89                        GrSurface* src, const SkIRect& srcRect,
90                        GrSamplerState::Filter) override;
91 
92     void submit(GrOpsRenderPass* renderPass) override;
93 
94     [[nodiscard]] std::unique_ptr<GrSemaphore> makeSemaphore(bool isOwned) override;
95     std::unique_ptr<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore&,
96                                                       GrSemaphoreWrapType,
97                                                       GrWrapOwnership) override;
98     void insertSemaphore(GrSemaphore* semaphore) override;
99     void waitSemaphore(GrSemaphore* semaphore) override;
checkFinishProcs()100     void checkFinishProcs() override { this->checkForFinishedCommandBuffers(); }
101     void finishOutstandingGpuWork() override;
102     std::unique_ptr<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override;
103 
104     GrMtlRenderCommandEncoder* loadMSAAFromResolve(GrAttachment* dst,
105                                                    GrMtlAttachment* src,
106                                                    const SkIRect& srcRect,
107                                                    MTLRenderPassStencilAttachmentDescriptor*);
108 
109     // When the Metal backend actually uses indirect command buffers, this function will actually do
110     // what it says. For now, every command is encoded directly into the primary command buffer, so
111     // this function is pretty useless, except for indicating that a render target has been drawn
112     // to.
submitIndirectCommandBuffer(GrSurface * surface,GrSurfaceOrigin origin,const SkIRect * bounds)113     void submitIndirectCommandBuffer(GrSurface* surface, GrSurfaceOrigin origin,
114                                      const SkIRect* bounds) {
115         this->didWriteToSurface(surface, origin, bounds);
116     }
117 
uniformsRingBuffer()118     GrRingBuffer* uniformsRingBuffer() override { return &fUniformsRingBuffer; }
119 
120 private:
121     GrMtlGpu(GrDirectContext*, const GrContextOptions&, id<MTLDevice>, id<MTLCommandQueue>);
122 
123     void destroyResources();
124 
xferBarrier(GrRenderTarget *,GrXferBarrierType)125     void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
126 
127     void takeOwnershipOfBuffer(sk_sp<GrGpuBuffer>) override;
128 
129     GrBackendTexture onCreateBackendTexture(SkISize dimensions,
130                                             const GrBackendFormat&,
131                                             GrRenderable,
132                                             skgpu::Mipmapped,
133                                             GrProtected,
134                                             std::string_view label) override;
135 
136     bool onClearBackendTexture(const GrBackendTexture&,
137                                sk_sp<skgpu::RefCntedCallback> finishedCallback,
138                                std::array<float, 4> color) override;
139 
140     GrBackendTexture onCreateCompressedBackendTexture(SkISize dimensions,
141                                                       const GrBackendFormat&,
142                                                       skgpu::Mipmapped,
143                                                       GrProtected) override;
144 
145     bool onUpdateCompressedBackendTexture(const GrBackendTexture&,
146                                           sk_sp<skgpu::RefCntedCallback> finishedCallback,
147                                           const void* data,
148                                           size_t size) override;
149 
150     sk_sp<GrTexture> onCreateTexture(SkISize,
151                                      const GrBackendFormat&,
152                                      GrRenderable,
153                                      int renderTargetSampleCnt,
154                                      skgpu::Budgeted,
155                                      GrProtected,
156                                      int mipLevelCount,
157                                      uint32_t levelClearMask,
158                                      std::string_view label) override;
159     sk_sp<GrTexture> onCreateCompressedTexture(SkISize dimensions,
160                                                const GrBackendFormat&,
161                                                skgpu::Budgeted,
162                                                skgpu::Mipmapped,
163                                                GrProtected,
164                                                const void* data,
165                                                size_t dataSize) override;
166 
167     sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&,
168                                           GrWrapOwnership,
169                                           GrWrapCacheable,
170                                           GrIOType) override;
171 
172     sk_sp<GrTexture> onWrapCompressedBackendTexture(const GrBackendTexture&,
173                                                     GrWrapOwnership,
174                                                     GrWrapCacheable) override;
175 
176     sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&,
177                                                     int sampleCnt,
178                                                     GrWrapOwnership,
179                                                     GrWrapCacheable) override;
180 
181     sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) override;
182 
183     sk_sp<GrGpuBuffer> onCreateBuffer(size_t, GrGpuBufferType, GrAccessPattern) override;
184 
185     bool onReadPixels(GrSurface* surface,
186                       SkIRect,
187                       GrColorType surfaceColorType,
188                       GrColorType bufferColorType,
189                       void*,
190                       size_t rowBytes) override;
191 
192     bool onTransferFromBufferToBuffer(sk_sp<GrGpuBuffer> src,
193                                       size_t srcOffset,
194                                       sk_sp<GrGpuBuffer> dst,
195                                       size_t dstOffset,
196                                       size_t size) override;
197 
198     bool onWritePixels(GrSurface*,
199                        SkIRect,
200                        GrColorType surfaceColorType,
201                        GrColorType bufferColorType,
202                        const GrMipLevel[],
203                        int mipLevelCount,
204                        bool prepForTexSampling) override;
205 
206     bool onTransferPixelsTo(GrTexture*,
207                             SkIRect,
208                             GrColorType textureColorType,
209                             GrColorType bufferColorType,
210                             sk_sp<GrGpuBuffer>,
211                             size_t offset,
212                             size_t rowBytes) override;
213 
214     bool onTransferPixelsFrom(GrSurface*,
215                               SkIRect,
216                               GrColorType surfaceColorType,
217                               GrColorType bufferColorType,
218                               sk_sp<GrGpuBuffer>,
219                               size_t offset) override;
220 
221     bool onRegenerateMipMapLevels(GrTexture*) override;
222 
223     void onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect) override;
224 
225     void resolve(GrMtlAttachment* resolveAttachment, GrMtlAttachment* msaaAttachment);
226 
227     void addFinishedProc(GrGpuFinishedProc finishedProc,
228                          GrGpuFinishedContext finishedContext) override;
229     void addFinishedCallback(sk_sp<skgpu::RefCntedCallback> finishedCallback);
230 
231     GrOpsRenderPass* onGetOpsRenderPass(
232             GrRenderTarget*,
233             bool useMSAASurface,
234             GrAttachment*,
235             GrSurfaceOrigin,
236             const SkIRect&,
237             const GrOpsRenderPass::LoadAndStoreInfo&,
238             const GrOpsRenderPass::StencilLoadAndStoreInfo&,
239             const skia_private::TArray<GrSurfaceProxy*, true>& sampledProxies,
240             GrXferBarrierFlags renderPassXferBarriers) override;
241 
242     bool onSubmitToGpu(GrSyncCpu sync) override;
243 
244     // Commits the current command buffer to the queue and then creates a new command buffer. If
245     // sync is set to kForce_SyncQueue, the function will wait for all work in the committed
246     // command buffer to finish before returning.
247     bool submitCommandBuffer(SyncQueue sync);
248 
249     void checkForFinishedCommandBuffers();
250 
251     // Function that uploads data onto textures with private storage mode (GPU access only).
252     bool uploadToTexture(GrMtlTexture* tex,
253                          SkIRect rect,
254                          GrColorType dataColorType,
255                          const GrMipLevel texels[],
256                          int mipLevels);
257 
258     // Function that fills texture levels with transparent black based on levelMask.
259     bool clearTexture(GrMtlTexture*, size_t bbp, uint32_t levelMask);
260 
261     bool readOrTransferPixels(GrSurface* surface,
262                               SkIRect rect,
263                               GrColorType dstColorType,
264                               id<MTLBuffer> transferBuffer,
265                               size_t offset,
266                               size_t imageBytes,
267                               size_t rowBytes);
268 
269     sk_sp<GrAttachment> makeStencilAttachment(const GrBackendFormat& /*colorFormat*/,
270                                               SkISize dimensions, int numStencilSamples) override;
271 
getPreferredStencilFormat(const GrBackendFormat &)272     GrBackendFormat getPreferredStencilFormat(const GrBackendFormat&) override {
273         return GrBackendFormats::MakeMtl(this->mtlCaps().preferredStencilFormat());
274     }
275 
276     sk_sp<GrAttachment> makeMSAAAttachment(SkISize dimensions,
277                                            const GrBackendFormat& format,
278                                            int numSamples,
279                                            GrProtected isProtected,
280                                            GrMemoryless isMemoryless) override;
281 
282     bool createMtlTextureForBackendSurface(MTLPixelFormat,
283                                            SkISize dimensions,
284                                            int sampleCnt,
285                                            GrTexturable,
286                                            GrRenderable,
287                                            skgpu::Mipmapped,
288                                            GrMtlTextureInfo*);
289 
290 #if defined(GR_TEST_UTILS)
291     void testingOnly_startCapture() override;
292     void testingOnly_stopCapture() override;
293 #endif
294 
295 #ifdef SK_ENABLE_DUMP_GPU
296     void onDumpJSON(SkJSONWriter*) const override;
297 #endif
298 
299     sk_sp<GrMtlCaps> fMtlCaps;
300 
301     id<MTLDevice> fDevice;
302     id<MTLCommandQueue> fQueue;
303 
304     sk_sp<GrMtlCommandBuffer> fCurrentCmdBuffer;
305 
306     using OutstandingCommandBuffer = sk_sp<GrMtlCommandBuffer>;
307     SkDeque fOutstandingCommandBuffers;
308 
309     GrMtlResourceProvider fResourceProvider;
310     GrStagingBufferManager fStagingBufferManager;
311     GrRingBuffer fUniformsRingBuffer;
312 
313     bool fDisconnected;
314 
315     using INHERITED = GrGpu;
316 };
317 
318 #endif
319