• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 skgpu_graphite_MtlBlitCommandEncoder_DEFINED
9 #define skgpu_graphite_MtlBlitCommandEncoder_DEFINED
10 
11 #include "include/core/SkRect.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/ports/SkCFObject.h"
14 #include "src/gpu/graphite/Resource.h"
15 
16 #import <Metal/Metal.h>
17 
18 namespace skgpu::graphite {
19 
20 /**
21  * Wraps a MTLMtlBlitCommandEncoder object
22  */
23 class MtlBlitCommandEncoder : public Resource {
24 public:
Make(const SharedContext * sharedContext,id<MTLCommandBuffer> commandBuffer)25     static sk_sp<MtlBlitCommandEncoder> Make(const SharedContext* sharedContext,
26                                              id<MTLCommandBuffer> commandBuffer) {
27         // Adding a retain here to keep our own ref separate from the autorelease pool
28         sk_cfp<id<MTLBlitCommandEncoder>> encoder =
29                 sk_ret_cfp<id<MTLBlitCommandEncoder>>([commandBuffer blitCommandEncoder]);
30         return sk_sp<MtlBlitCommandEncoder>(new MtlBlitCommandEncoder(sharedContext,
31                                                                       std::move(encoder)));
32     }
33 
pushDebugGroup(NSString * string)34     void pushDebugGroup(NSString* string) {
35         [(*fCommandEncoder) pushDebugGroup:string];
36     }
popDebugGroup()37     void popDebugGroup() {
38         [(*fCommandEncoder) popDebugGroup];
39     }
40 #ifdef SK_BUILD_FOR_MAC
synchronizeResource(id<MTLBuffer> buffer)41     void synchronizeResource(id<MTLBuffer> buffer) {
42         [(*fCommandEncoder) synchronizeResource: buffer];
43     }
44 #endif
45 
fillBuffer(id<MTLBuffer> buffer,size_t bufferOffset,size_t bytes,uint8_t value)46     void fillBuffer(id<MTLBuffer> buffer, size_t bufferOffset, size_t bytes, uint8_t value) {
47         [(*fCommandEncoder) fillBuffer:buffer
48                                  range:NSMakeRange(bufferOffset, bytes)
49                                  value:value];
50     }
51 
copyFromTexture(id<MTLTexture> texture,SkIRect srcRect,id<MTLBuffer> buffer,size_t bufferOffset,size_t bufferRowBytes)52     void copyFromTexture(id<MTLTexture> texture,
53                          SkIRect srcRect,
54                          id<MTLBuffer> buffer,
55                          size_t bufferOffset,
56                          size_t bufferRowBytes) {
57         [(*fCommandEncoder) copyFromTexture: texture
58                                 sourceSlice: 0
59                                 sourceLevel: 0
60                                sourceOrigin: MTLOriginMake(srcRect.left(), srcRect.top(), 0)
61                                  sourceSize: MTLSizeMake(srcRect.width(), srcRect.height(), 1)
62                                    toBuffer: buffer
63                           destinationOffset: bufferOffset
64                      destinationBytesPerRow: bufferRowBytes
65                    destinationBytesPerImage: bufferRowBytes * srcRect.height()];
66     }
67 
copyFromBuffer(id<MTLBuffer> buffer,size_t bufferOffset,size_t bufferRowBytes,id<MTLTexture> texture,SkIRect dstRect,unsigned int dstLevel)68     void copyFromBuffer(id<MTLBuffer> buffer,
69                         size_t bufferOffset,
70                         size_t bufferRowBytes,
71                         id<MTLTexture> texture,
72                         SkIRect dstRect,
73                         unsigned int dstLevel) {
74         [(*fCommandEncoder) copyFromBuffer: buffer
75                               sourceOffset: bufferOffset
76                          sourceBytesPerRow: bufferRowBytes
77                        sourceBytesPerImage: bufferRowBytes * dstRect.height()
78                                 sourceSize: MTLSizeMake(dstRect.width(), dstRect.height(), 1)
79                                  toTexture: texture
80                           destinationSlice: 0
81                           destinationLevel: dstLevel
82                          destinationOrigin: MTLOriginMake(dstRect.left(), dstRect.top(), 0)];
83     }
84 
copyTextureToTexture(id<MTLTexture> srcTexture,SkIRect srcRect,id<MTLTexture> dstTexture,SkIPoint dstPoint)85     void copyTextureToTexture(id<MTLTexture> srcTexture,
86                               SkIRect srcRect,
87                               id<MTLTexture> dstTexture,
88                               SkIPoint dstPoint) {
89         [(*fCommandEncoder) copyFromTexture: srcTexture
90                                 sourceSlice: 0
91                                 sourceLevel: 0
92                                sourceOrigin: MTLOriginMake(srcRect.x(), srcRect.y(), 0)
93                                  sourceSize: MTLSizeMake(srcRect.width(), srcRect.height(), 1)
94                                   toTexture: dstTexture
95                            destinationSlice: 0
96                            destinationLevel: 0
97                           destinationOrigin: MTLOriginMake(dstPoint.fX, dstPoint.fY, 0)];
98     }
99 
copyBufferToBuffer(id<MTLBuffer> srcBuffer,size_t srcOffset,id<MTLBuffer> dstBuffer,size_t dstOffset,size_t size)100     void copyBufferToBuffer(id<MTLBuffer> srcBuffer,
101                             size_t srcOffset,
102                             id<MTLBuffer> dstBuffer,
103                             size_t dstOffset,
104                             size_t size) {
105         [(*fCommandEncoder) copyFromBuffer: srcBuffer
106                               sourceOffset: srcOffset
107                                   toBuffer: dstBuffer
108                          destinationOffset: dstOffset
109                                       size: size];
110     }
111 
endEncoding()112     void endEncoding() {
113         [(*fCommandEncoder) endEncoding];
114     }
115 
116 private:
MtlBlitCommandEncoder(const SharedContext * sharedContext,sk_cfp<id<MTLBlitCommandEncoder>> encoder)117     MtlBlitCommandEncoder(const SharedContext* sharedContext,
118                           sk_cfp<id<MTLBlitCommandEncoder>> encoder)
119             : Resource(sharedContext, Ownership::kOwned, skgpu::Budgeted::kYes)
120             , fCommandEncoder(std::move(encoder)) {}
121 
freeGpuData()122     void freeGpuData() override {
123         fCommandEncoder.reset();
124     }
125 
126     sk_cfp<id<MTLBlitCommandEncoder>> fCommandEncoder;
127 };
128 
129 } // namespace skgpu::graphite
130 
131 #endif // skgpu_graphite_MtlBlitCommandEncoder_DEFINED
132