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