1 /*
2  * Copyright 2018 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 GrMtlGpuCommandBuffer_DEFINED
9 #define GrMtlGpuCommandBuffer_DEFINED
10 
11 #include "GrGpuCommandBuffer.h"
12 #include "GrMtlGpu.h"
13 #include "GrMesh.h"
14 
15 #import <metal/metal.h>
16 
17 typedef uint32_t GrColor;
18 class GrMtlPipelineState;
19 class GrMtlRenderTarget;
20 
21 class GrMtlGpuTextureCommandBuffer : public GrGpuTextureCommandBuffer {
22 public:
GrMtlGpuTextureCommandBuffer(GrMtlGpu * gpu,GrTexture * texture,GrSurfaceOrigin origin)23     GrMtlGpuTextureCommandBuffer(GrMtlGpu* gpu, GrTexture* texture, GrSurfaceOrigin origin)
24             : INHERITED(texture, origin)
25             , fGpu(gpu) {
26     }
27 
~GrMtlGpuTextureCommandBuffer()28     ~GrMtlGpuTextureCommandBuffer() override {}
29 
copy(GrSurface * src,GrSurfaceOrigin srcOrigin,const SkIRect & srcRect,const SkIPoint & dstPoint)30     void copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect,
31               const SkIPoint& dstPoint) override {
32         fGpu->copySurface(fTexture, fOrigin, src, srcOrigin, srcRect, dstPoint);
33     }
34 
insertEventMarker(const char * msg)35     void insertEventMarker(const char* msg) override {}
36 
37 private:
38     GrMtlGpu* fGpu;
39 
40     typedef GrGpuTextureCommandBuffer INHERITED;
41 };
42 
43 class GrMtlGpuRTCommandBuffer : public GrGpuRTCommandBuffer, private GrMesh::SendToGpuImpl {
44 public:
45     GrMtlGpuRTCommandBuffer(GrMtlGpu* gpu, GrRenderTarget* rt, GrSurfaceOrigin origin,
46                             const SkRect& bounds,
47                             const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
48                             const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo);
49 
50     ~GrMtlGpuRTCommandBuffer() override;
51 
begin()52     void begin() override {}
end()53     void end() override {}
54 
discard()55     void discard() override {}
56 
insertEventMarker(const char * msg)57     void insertEventMarker(const char* msg) override {}
58 
inlineUpload(GrOpFlushState * state,GrDeferredTextureUploadFn & upload)59     void inlineUpload(GrOpFlushState* state, GrDeferredTextureUploadFn& upload) override {}
60 
61     void copy(GrSurface* src, GrSurfaceOrigin srcOrigin, const SkIRect& srcRect,
62               const SkIPoint& dstPoint) override;
63 
64     void submit();
65 
66 private:
67     void internalBegin();
68     void internalEnd();
69 
gpu()70     GrGpu* gpu() override { return fGpu; }
71 
72     GrMtlPipelineState* prepareDrawState(
73             const GrPrimitiveProcessor& primProc,
74             const GrPipeline& pipeline,
75             const GrPipeline::FixedDynamicState* fixedDynamicState,
76             const GrMesh meshes[],
77             int meshCount);
78 
79     void onDraw(const GrPrimitiveProcessor& primProc,
80                 const GrPipeline& pipeline,
81                 const GrPipeline::FixedDynamicState* fixedDynamicState,
82                 const GrPipeline::DynamicStateArrays* dynamicStateArrays,
83                 const GrMesh mesh[],
84                 int meshCount,
85                 const SkRect& bounds) override;
86 
87     void onClear(const GrFixedClip& clip, const SkPMColor4f& color) override;
88 
89     void onClearStencilClip(const GrFixedClip& clip, bool insideStencilMask) override;
90 
91     MTLRenderPassDescriptor* createRenderPassDesc() const;
92 
93     void bindGeometry(const GrBuffer* vertexBuffer, const GrBuffer* instanceBuffer);
94 
95     // GrMesh::SendToGpuImpl methods. These issue the actual Metal draw commands.
96     // Marked final as a hint to the compiler to not use virtual dispatch.
sendMeshToGpu(GrPrimitiveType primType,const GrBuffer * vertexBuffer,int vertexCount,int baseVertex)97     void sendMeshToGpu(GrPrimitiveType primType, const GrBuffer* vertexBuffer, int vertexCount,
98                        int baseVertex) final {
99         this->sendInstancedMeshToGpu(primType, vertexBuffer, vertexCount, baseVertex, nullptr, 1,
100                                      0);
101     }
102 
sendIndexedMeshToGpu(GrPrimitiveType primType,const GrBuffer * indexBuffer,int indexCount,int baseIndex,uint16_t,uint16_t,const GrBuffer * vertexBuffer,int baseVertex,GrPrimitiveRestart restart)103     void sendIndexedMeshToGpu(GrPrimitiveType primType, const GrBuffer* indexBuffer, int indexCount,
104                               int baseIndex, uint16_t /*minIndexValue*/, uint16_t /*maxIndexValue*/,
105                               const GrBuffer* vertexBuffer, int baseVertex,
106                               GrPrimitiveRestart restart) final {
107         SkASSERT(restart == GrPrimitiveRestart::kNo);
108         this->sendIndexedInstancedMeshToGpu(primType, indexBuffer, indexCount, baseIndex,
109                                             vertexBuffer, baseVertex, nullptr, 1, 0,
110                                             GrPrimitiveRestart::kNo);
111     }
112 
113     void sendInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* vertexBuffer, int vertexCount,
114                                 int baseVertex, const GrBuffer* instanceBuffer, int instanceCount,
115                                 int baseInstance) final;
116 
117     void sendIndexedInstancedMeshToGpu(GrPrimitiveType, const GrBuffer* indexBuffer, int indexCount,
118                                        int baseIndex, const GrBuffer* vertexBuffer, int baseVertex,
119                                        const GrBuffer* instanceBuffer, int instanceCount,
120                                        int baseInstance, GrPrimitiveRestart) final;
121 
122     GrMtlGpu*                                     fGpu;
123     // GrRenderTargetProxy bounds
124 #ifdef SK_DEBUG
125     SkRect                                        fBounds;
126 #endif
127     GrGpuRTCommandBuffer::LoadAndStoreInfo        fColorLoadAndStoreInfo;
128     GrGpuRTCommandBuffer::StencilLoadAndStoreInfo fStencilLoadAndStoreInfo;
129 
130     id<MTLRenderCommandEncoder> fActiveRenderCmdEncoder;
131     MTLRenderPassDescriptor* fRenderPassDesc;
132 
133     struct CommandBufferInfo {
134         SkRect fBounds;
135     };
136 
137     CommandBufferInfo fCommandBufferInfo;
138 
139     typedef GrGpuRTCommandBuffer INHERITED;
140 };
141 
142 #endif
143 
144