Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/mtl/GrMtlGpu.h" |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/core/SkConvertPixels.h" |
| 11 | #include "src/gpu/GrRenderTargetPriv.h" |
| 12 | #include "src/gpu/GrTexturePriv.h" |
| 13 | #include "src/gpu/mtl/GrMtlBuffer.h" |
| 14 | #include "src/gpu/mtl/GrMtlCommandBuffer.h" |
| 15 | #include "src/gpu/mtl/GrMtlGpuCommandBuffer.h" |
| 16 | #include "src/gpu/mtl/GrMtlTexture.h" |
| 17 | #include "src/gpu/mtl/GrMtlTextureRenderTarget.h" |
| 18 | #include "src/gpu/mtl/GrMtlUtil.h" |
| 19 | #include "src/sksl/SkSLCompiler.h" |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 20 | |
| 21 | #import <simd/simd.h> |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 22 | |
Greg Daniel | 6b7e0e2 | 2017-07-12 16:21:09 -0400 | [diff] [blame] | 23 | #if !__has_feature(objc_arc) |
| 24 | #error This file must be compiled with Arc. Use -fobjc-arc flag |
| 25 | #endif |
| 26 | |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 27 | static bool get_feature_set(id<MTLDevice> device, MTLFeatureSet* featureSet) { |
| 28 | // Mac OSX |
| 29 | #ifdef SK_BUILD_FOR_MAC |
| 30 | if ([device supportsFeatureSet:MTLFeatureSet_OSX_GPUFamily1_v2]) { |
| 31 | *featureSet = MTLFeatureSet_OSX_GPUFamily1_v2; |
| 32 | return true; |
| 33 | } |
| 34 | if ([device supportsFeatureSet:MTLFeatureSet_OSX_GPUFamily1_v1]) { |
| 35 | *featureSet = MTLFeatureSet_OSX_GPUFamily1_v1; |
| 36 | return true; |
| 37 | } |
| 38 | #endif |
| 39 | |
| 40 | // iOS Family group 3 |
| 41 | #ifdef SK_BUILD_FOR_IOS |
| 42 | if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v2]) { |
| 43 | *featureSet = MTLFeatureSet_iOS_GPUFamily3_v2; |
| 44 | return true; |
| 45 | } |
| 46 | if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v1]) { |
| 47 | *featureSet = MTLFeatureSet_iOS_GPUFamily3_v1; |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | // iOS Family group 2 |
| 52 | if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily2_v3]) { |
| 53 | *featureSet = MTLFeatureSet_iOS_GPUFamily2_v3; |
| 54 | return true; |
| 55 | } |
| 56 | if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily2_v2]) { |
| 57 | *featureSet = MTLFeatureSet_iOS_GPUFamily2_v2; |
| 58 | return true; |
| 59 | } |
| 60 | if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily2_v1]) { |
| 61 | *featureSet = MTLFeatureSet_iOS_GPUFamily2_v1; |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | // iOS Family group 1 |
| 66 | if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v3]) { |
| 67 | *featureSet = MTLFeatureSet_iOS_GPUFamily1_v3; |
| 68 | return true; |
| 69 | } |
| 70 | if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v2]) { |
| 71 | *featureSet = MTLFeatureSet_iOS_GPUFamily1_v2; |
| 72 | return true; |
| 73 | } |
| 74 | if ([device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily1_v1]) { |
| 75 | *featureSet = MTLFeatureSet_iOS_GPUFamily1_v1; |
| 76 | return true; |
| 77 | } |
| 78 | #endif |
| 79 | // No supported feature sets were found |
| 80 | return false; |
| 81 | } |
| 82 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 83 | sk_sp<GrGpu> GrMtlGpu::Make(GrContext* context, const GrContextOptions& options, |
| 84 | id<MTLDevice> device, id<MTLCommandQueue> queue) { |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 85 | if (!device || !queue) { |
| 86 | return nullptr; |
| 87 | } |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 88 | MTLFeatureSet featureSet; |
| 89 | if (!get_feature_set(device, &featureSet)) { |
| 90 | return nullptr; |
| 91 | } |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 92 | return sk_sp<GrGpu>(new GrMtlGpu(context, options, device, queue, featureSet)); |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 95 | GrMtlGpu::GrMtlGpu(GrContext* context, const GrContextOptions& options, |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 96 | id<MTLDevice> device, id<MTLCommandQueue> queue, MTLFeatureSet featureSet) |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 97 | : INHERITED(context) |
| 98 | , fDevice(device) |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 99 | , fQueue(queue) |
Jim Van Verth | 75c3ae4 | 2019-04-22 17:07:53 -0400 | [diff] [blame] | 100 | , fCmdBuffer(nullptr) |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 101 | , fCompiler(new SkSL::Compiler()) |
| 102 | , fCopyManager(this) |
Jim Van Verth | c03cfaf | 2019-04-18 13:11:01 -0400 | [diff] [blame] | 103 | , fResourceProvider(this) |
Jim Van Verth | 75c3ae4 | 2019-04-22 17:07:53 -0400 | [diff] [blame] | 104 | , fDisconnected(false) { |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 105 | fMtlCaps.reset(new GrMtlCaps(options, fDevice, featureSet)); |
| 106 | fCaps = fMtlCaps; |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 107 | } |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 108 | |
Jim Van Verth | 75c3ae4 | 2019-04-22 17:07:53 -0400 | [diff] [blame] | 109 | GrMtlGpu::~GrMtlGpu() { |
| 110 | if (!fDisconnected) { |
| 111 | this->destroyResources(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void GrMtlGpu::disconnect(DisconnectType type) { |
| 116 | INHERITED::disconnect(type); |
| 117 | |
| 118 | if (DisconnectType::kCleanup == type) { |
| 119 | this->destroyResources(); |
| 120 | } else { |
| 121 | delete fCmdBuffer; |
| 122 | fCmdBuffer = nullptr; |
| 123 | |
| 124 | fQueue = nil; |
| 125 | fDevice = nil; |
| 126 | |
| 127 | fDisconnected = true; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void GrMtlGpu::destroyResources() { |
| 132 | // Will implicitly delete the command buffer |
| 133 | this->submitCommandBuffer(SyncQueue::kForce_SyncQueue); |
| 134 | |
| 135 | fQueue = nil; |
| 136 | fDevice = nil; |
| 137 | } |
| 138 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 139 | GrGpuRTCommandBuffer* GrMtlGpu::getCommandBuffer( |
Ethan Nicholas | 56d19a5 | 2018-10-15 11:26:20 -0400 | [diff] [blame] | 140 | GrRenderTarget* renderTarget, GrSurfaceOrigin origin, const SkRect& bounds, |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 141 | const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo, |
| 142 | const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) { |
Jim Van Verth | f16e074 | 2019-04-02 14:33:03 -0400 | [diff] [blame] | 143 | SK_BEGIN_AUTORELEASE_BLOCK |
Ethan Nicholas | 56d19a5 | 2018-10-15 11:26:20 -0400 | [diff] [blame] | 144 | return new GrMtlGpuRTCommandBuffer(this, renderTarget, origin, bounds, colorInfo, stencilInfo); |
Jim Van Verth | f16e074 | 2019-04-02 14:33:03 -0400 | [diff] [blame] | 145 | SK_END_AUTORELEASE_BLOCK |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 148 | GrGpuTextureCommandBuffer* GrMtlGpu::getCommandBuffer(GrTexture* texture, |
| 149 | GrSurfaceOrigin origin) { |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 150 | return new GrMtlGpuTextureCommandBuffer(this, texture, origin); |
| 151 | } |
| 152 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 153 | void GrMtlGpu::submit(GrGpuCommandBuffer* buffer) { |
Jim Van Verth | 9896a0d | 2019-04-10 15:11:12 -0400 | [diff] [blame] | 154 | GrMtlGpuRTCommandBuffer* mtlRTCmdBuffer = |
| 155 | reinterpret_cast<GrMtlGpuRTCommandBuffer*>(buffer->asRTCommandBuffer()); |
| 156 | if (mtlRTCmdBuffer) { |
| 157 | mtlRTCmdBuffer->submit(); |
| 158 | } |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 159 | delete buffer; |
| 160 | } |
| 161 | |
Jim Van Verth | 75c3ae4 | 2019-04-22 17:07:53 -0400 | [diff] [blame] | 162 | GrMtlCommandBuffer* GrMtlGpu::commandBuffer() { |
Jim Van Verth | 568eb8d | 2019-04-04 14:30:42 -0400 | [diff] [blame] | 163 | if (!fCmdBuffer) { |
Jim Van Verth | 75c3ae4 | 2019-04-22 17:07:53 -0400 | [diff] [blame] | 164 | fCmdBuffer = GrMtlCommandBuffer::Create(fQueue); |
Jim Van Verth | 568eb8d | 2019-04-04 14:30:42 -0400 | [diff] [blame] | 165 | } |
| 166 | return fCmdBuffer; |
| 167 | } |
| 168 | |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 169 | void GrMtlGpu::submitCommandBuffer(SyncQueue sync) { |
Jim Van Verth | 75c3ae4 | 2019-04-22 17:07:53 -0400 | [diff] [blame] | 170 | if (fCmdBuffer) { |
| 171 | fCmdBuffer->commit(SyncQueue::kForce_SyncQueue == sync); |
| 172 | delete fCmdBuffer; |
| 173 | fCmdBuffer = nullptr; |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 174 | } |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 177 | sk_sp<GrGpuBuffer> GrMtlGpu::onCreateBuffer(size_t size, GrGpuBufferType type, |
| 178 | GrAccessPattern accessPattern, const void* data) { |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 179 | return GrMtlBuffer::Make(this, size, type, accessPattern, data); |
Timothy Liang | 49528b6 | 2018-08-02 14:18:37 -0400 | [diff] [blame] | 180 | } |
| 181 | |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 182 | static bool check_max_blit_width(int widthInPixels) { |
| 183 | if (widthInPixels > 32767) { |
| 184 | SkASSERT(false); // surfaces should not be this wide anyway |
| 185 | return false; |
| 186 | } |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | bool GrMtlGpu::uploadToTexture(GrMtlTexture* tex, int left, int top, int width, int height, |
| 191 | GrColorType dataColorType, const GrMipLevel texels[], |
| 192 | int mipLevelCount) { |
| 193 | SkASSERT(this->caps()->isConfigTexturable(tex->config())); |
Jim Van Verth | 444b309 | 2019-03-11 11:31:23 -0400 | [diff] [blame] | 194 | // The assumption is either that we have no mipmaps, or that our rect is the entire texture |
| 195 | SkASSERT(1 == mipLevelCount || |
| 196 | (0 == left && 0 == top && width == tex->width() && height == tex->height())); |
| 197 | |
| 198 | // We assume that if the texture has mip levels, we either upload to all the levels or just the |
| 199 | // first. |
| 200 | SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1)); |
| 201 | |
| 202 | // If we're uploading compressed data then we should be using uploadCompressedTexData |
| 203 | SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType, |
| 204 | GrSRGBEncoded::kNo))); |
| 205 | |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 206 | if (!check_max_blit_width(width)) { |
| 207 | return false; |
| 208 | } |
| 209 | if (width == 0 || height == 0) { |
| 210 | return false; |
| 211 | } |
| 212 | if (GrPixelConfigToColorType(tex->config()) != dataColorType) { |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | id<MTLTexture> mtlTexture = tex->mtlTexture(); |
| 217 | SkASSERT(mtlTexture); |
| 218 | // Either upload only the first miplevel or all miplevels |
| 219 | SkASSERT(1 == mipLevelCount || mipLevelCount == (int)mtlTexture.mipmapLevelCount); |
| 220 | |
Jim Van Verth | 444b309 | 2019-03-11 11:31:23 -0400 | [diff] [blame] | 221 | // TODO: implement some way of reusing transfer buffers? |
| 222 | size_t bpp = GrColorTypeBytesPerPixel(dataColorType); |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 223 | |
Jim Van Verth | 444b309 | 2019-03-11 11:31:23 -0400 | [diff] [blame] | 224 | SkTArray<size_t> individualMipOffsets(mipLevelCount); |
| 225 | individualMipOffsets.push_back(0); |
| 226 | size_t combinedBufferSize = width * bpp * height; |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 227 | int currentWidth = width; |
| 228 | int currentHeight = height; |
Jim Van Verth | 444b309 | 2019-03-11 11:31:23 -0400 | [diff] [blame] | 229 | if (!texels[0].fPixels) { |
| 230 | combinedBufferSize = 0; |
| 231 | } |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 232 | |
Jim Van Verth | 444b309 | 2019-03-11 11:31:23 -0400 | [diff] [blame] | 233 | // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image |
| 234 | // config. This works with the assumption that the bytes in pixel config is always a power of 2. |
| 235 | SkASSERT((bpp & (bpp - 1)) == 0); |
| 236 | const size_t alignmentMask = 0x3 | (bpp - 1); |
| 237 | for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) { |
| 238 | currentWidth = SkTMax(1, currentWidth/2); |
| 239 | currentHeight = SkTMax(1, currentHeight/2); |
| 240 | |
| 241 | if (texels[currentMipLevel].fPixels) { |
| 242 | const size_t trimmedSize = currentWidth * bpp * currentHeight; |
| 243 | const size_t alignmentDiff = combinedBufferSize & alignmentMask; |
| 244 | if (alignmentDiff != 0) { |
| 245 | combinedBufferSize += alignmentMask - alignmentDiff + 1; |
| 246 | } |
| 247 | individualMipOffsets.push_back(combinedBufferSize); |
| 248 | combinedBufferSize += trimmedSize; |
| 249 | } else { |
| 250 | individualMipOffsets.push_back(0); |
| 251 | } |
| 252 | } |
| 253 | if (0 == combinedBufferSize) { |
| 254 | // We don't actually have any data to upload so just return success |
| 255 | return true; |
| 256 | } |
| 257 | |
Jim Van Verth | dd15d69 | 2019-04-22 15:29:53 -0400 | [diff] [blame] | 258 | sk_sp<GrMtlBuffer> transferBuffer = GrMtlBuffer::Make(this, combinedBufferSize, |
| 259 | GrGpuBufferType::kXferCpuToGpu, |
| 260 | kStream_GrAccessPattern); |
| 261 | if (!transferBuffer) { |
Jim Van Verth | 444b309 | 2019-03-11 11:31:23 -0400 | [diff] [blame] | 262 | return false; |
| 263 | } |
| 264 | |
Jim Van Verth | dd15d69 | 2019-04-22 15:29:53 -0400 | [diff] [blame] | 265 | char* buffer = (char*) transferBuffer->map(); |
| 266 | size_t bufferOffset = transferBuffer->offset(); |
Jim Van Verth | 444b309 | 2019-03-11 11:31:23 -0400 | [diff] [blame] | 267 | |
| 268 | currentWidth = width; |
| 269 | currentHeight = height; |
| 270 | int layerHeight = tex->height(); |
| 271 | MTLOrigin origin = MTLOriginMake(left, top, 0); |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 272 | |
Jim Van Verth | 75c3ae4 | 2019-04-22 17:07:53 -0400 | [diff] [blame] | 273 | id<MTLBlitCommandEncoder> blitCmdEncoder = this->commandBuffer()->getBlitCommandEncoder(); |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 274 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
Jim Van Verth | 444b309 | 2019-03-11 11:31:23 -0400 | [diff] [blame] | 275 | if (texels[currentMipLevel].fPixels) { |
| 276 | SkASSERT(1 == mipLevelCount || currentHeight == layerHeight); |
| 277 | const size_t trimRowBytes = currentWidth * bpp; |
| 278 | const size_t rowBytes = texels[currentMipLevel].fRowBytes |
| 279 | ? texels[currentMipLevel].fRowBytes |
| 280 | : trimRowBytes; |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 281 | |
Jim Van Verth | 444b309 | 2019-03-11 11:31:23 -0400 | [diff] [blame] | 282 | // copy data into the buffer, skipping the trailing bytes |
| 283 | char* dst = buffer + individualMipOffsets[currentMipLevel]; |
| 284 | const char* src = (const char*)texels[currentMipLevel].fPixels; |
| 285 | SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight); |
| 286 | |
Jim Van Verth | dd15d69 | 2019-04-22 15:29:53 -0400 | [diff] [blame] | 287 | [blitCmdEncoder copyFromBuffer: transferBuffer->mtlBuffer() |
| 288 | sourceOffset: bufferOffset + individualMipOffsets[currentMipLevel] |
Jim Van Verth | 444b309 | 2019-03-11 11:31:23 -0400 | [diff] [blame] | 289 | sourceBytesPerRow: trimRowBytes |
| 290 | sourceBytesPerImage: trimRowBytes*currentHeight |
Jim Van Verth | 686046b | 2019-03-18 15:39:22 -0400 | [diff] [blame] | 291 | sourceSize: MTLSizeMake(currentWidth, currentHeight, 1) |
Jim Van Verth | 444b309 | 2019-03-11 11:31:23 -0400 | [diff] [blame] | 292 | toTexture: mtlTexture |
| 293 | destinationSlice: 0 |
| 294 | destinationLevel: currentMipLevel |
| 295 | destinationOrigin: origin]; |
| 296 | } |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 297 | currentWidth = SkTMax(1, currentWidth/2); |
| 298 | currentHeight = SkTMax(1, currentHeight/2); |
Jim Van Verth | 444b309 | 2019-03-11 11:31:23 -0400 | [diff] [blame] | 299 | layerHeight = currentHeight; |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 300 | } |
Jim Van Verth | dd15d69 | 2019-04-22 15:29:53 -0400 | [diff] [blame] | 301 | transferBuffer->unmap(); |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 302 | |
| 303 | if (mipLevelCount < (int) tex->mtlTexture().mipmapLevelCount) { |
| 304 | tex->texturePriv().markMipMapsDirty(); |
| 305 | } |
Jim Van Verth | 444b309 | 2019-03-11 11:31:23 -0400 | [diff] [blame] | 306 | |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 307 | return true; |
| 308 | } |
| 309 | |
Jim Van Verth | 686046b | 2019-03-18 15:39:22 -0400 | [diff] [blame] | 310 | bool GrMtlGpu::clearTexture(GrMtlTexture* tex, GrColorType dataColorType) { |
| 311 | SkASSERT(this->caps()->isConfigTexturable(tex->config())); |
| 312 | |
| 313 | // If we're uploading compressed data then we should be using uploadCompressedTexData |
| 314 | SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType, |
| 315 | GrSRGBEncoded::kNo))); |
| 316 | |
| 317 | id<MTLTexture> mtlTexture = tex->mtlTexture(); |
| 318 | SkASSERT(mtlTexture); |
| 319 | // Either upload only the first miplevel or all miplevels |
| 320 | int mipLevelCount = (int)mtlTexture.mipmapLevelCount; |
| 321 | |
| 322 | // TODO: implement some way of reusing transfer buffers? |
| 323 | size_t bpp = GrColorTypeBytesPerPixel(dataColorType); |
| 324 | |
| 325 | SkTArray<size_t> individualMipOffsets(mipLevelCount); |
| 326 | individualMipOffsets.push_back(0); |
| 327 | int width = tex->width(); |
| 328 | int height = tex->height(); |
| 329 | size_t combinedBufferSize = width * bpp * height; |
| 330 | int currentWidth = width; |
| 331 | int currentHeight = height; |
| 332 | |
| 333 | // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image |
| 334 | // config. This works with the assumption that the bytes in pixel config is always a power of 2. |
| 335 | // TODO: can we just copy from a single buffer the size of the top level w/o a perf penalty? |
| 336 | SkASSERT((bpp & (bpp - 1)) == 0); |
| 337 | const size_t alignmentMask = 0x3 | (bpp - 1); |
| 338 | for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) { |
| 339 | currentWidth = SkTMax(1, currentWidth/2); |
| 340 | currentHeight = SkTMax(1, currentHeight/2); |
| 341 | |
| 342 | const size_t trimmedSize = currentWidth * bpp * currentHeight; |
| 343 | const size_t alignmentDiff = combinedBufferSize & alignmentMask; |
| 344 | if (alignmentDiff != 0) { |
| 345 | combinedBufferSize += alignmentMask - alignmentDiff + 1; |
| 346 | } |
| 347 | individualMipOffsets.push_back(combinedBufferSize); |
| 348 | combinedBufferSize += trimmedSize; |
| 349 | } |
| 350 | if (0 == combinedBufferSize) { |
| 351 | // We don't actually have any data to upload so just return success |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | // TODO: Create GrMtlTransferBuffer |
| 356 | id<MTLBuffer> transferBuffer = [fDevice newBufferWithLength: combinedBufferSize |
| 357 | options: MTLResourceStorageModePrivate]; |
| 358 | if (nil == transferBuffer) { |
| 359 | return false; |
| 360 | } |
| 361 | |
Jim Van Verth | 75c3ae4 | 2019-04-22 17:07:53 -0400 | [diff] [blame] | 362 | id<MTLBlitCommandEncoder> blitCmdEncoder = this->commandBuffer()->getBlitCommandEncoder(); |
Jim Van Verth | 686046b | 2019-03-18 15:39:22 -0400 | [diff] [blame] | 363 | // clear the buffer to transparent black |
| 364 | NSRange clearRange; |
| 365 | clearRange.location = 0; |
| 366 | clearRange.length = combinedBufferSize; |
| 367 | [blitCmdEncoder fillBuffer: transferBuffer |
| 368 | range: clearRange |
| 369 | value: 0]; |
| 370 | |
| 371 | // now copy buffer to texture |
| 372 | currentWidth = width; |
| 373 | currentHeight = height; |
| 374 | MTLOrigin origin = MTLOriginMake(0, 0, 0); |
| 375 | for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) { |
| 376 | const size_t rowBytes = currentWidth * bpp; |
| 377 | |
| 378 | [blitCmdEncoder copyFromBuffer: transferBuffer |
| 379 | sourceOffset: individualMipOffsets[currentMipLevel] |
| 380 | sourceBytesPerRow: rowBytes |
| 381 | sourceBytesPerImage: rowBytes*currentHeight |
| 382 | sourceSize: MTLSizeMake(currentWidth, currentHeight, 1) |
| 383 | toTexture: mtlTexture |
| 384 | destinationSlice: 0 |
| 385 | destinationLevel: currentMipLevel |
| 386 | destinationOrigin: origin]; |
| 387 | currentWidth = SkTMax(1, currentWidth/2); |
| 388 | currentHeight = SkTMax(1, currentHeight/2); |
| 389 | } |
Jim Van Verth | 686046b | 2019-03-18 15:39:22 -0400 | [diff] [blame] | 390 | |
| 391 | if (mipLevelCount < (int) tex->mtlTexture().mipmapLevelCount) { |
| 392 | tex->texturePriv().markMipMapsDirty(); |
| 393 | } |
| 394 | |
| 395 | return true; |
| 396 | } |
| 397 | |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 398 | GrStencilAttachment* GrMtlGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt, |
| 399 | int width, |
| 400 | int height) { |
| 401 | SkASSERT(width >= rt->width()); |
| 402 | SkASSERT(height >= rt->height()); |
| 403 | |
| 404 | int samples = rt->numStencilSamples(); |
| 405 | |
| 406 | const GrMtlCaps::StencilFormat& sFmt = this->mtlCaps().preferredStencilFormat(); |
| 407 | |
Jim Van Verth | f16e074 | 2019-04-02 14:33:03 -0400 | [diff] [blame] | 408 | SK_BEGIN_AUTORELEASE_BLOCK |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 409 | GrMtlStencilAttachment* stencil(GrMtlStencilAttachment::Create(this, |
| 410 | width, |
| 411 | height, |
| 412 | samples, |
| 413 | sFmt)); |
| 414 | fStats.incStencilAttachmentCreates(); |
| 415 | return stencil; |
Jim Van Verth | f16e074 | 2019-04-02 14:33:03 -0400 | [diff] [blame] | 416 | SK_END_AUTORELEASE_BLOCK |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 417 | } |
| 418 | |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 419 | sk_sp<GrTexture> GrMtlGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 420 | const GrMipLevel texels[], int mipLevelCount) { |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 421 | int mipLevels = !mipLevelCount ? 1 : mipLevelCount; |
| 422 | |
| 423 | if (!fMtlCaps->isConfigTexturable(desc.fConfig)) { |
| 424 | return nullptr; |
| 425 | } |
Timothy Liang | 58f153d | 2018-07-02 17:36:20 -0400 | [diff] [blame] | 426 | MTLPixelFormat format; |
| 427 | if (!GrPixelConfigToMTLFormat(desc.fConfig, &format)) { |
| 428 | return nullptr; |
| 429 | } |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 430 | |
Jim Van Verth | 1676cb9 | 2019-01-15 13:24:45 -0500 | [diff] [blame] | 431 | if (GrPixelConfigIsCompressed(desc.fConfig)) { |
| 432 | return nullptr; // TODO: add compressed texture support |
| 433 | } |
| 434 | |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 435 | bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 436 | |
Jim Van Verth | 61610be | 2019-03-07 15:33:49 -0500 | [diff] [blame] | 437 | sk_sp<GrMtlTexture> tex; |
| 438 | SK_BEGIN_AUTORELEASE_BLOCK |
Timothy Liang | 58f153d | 2018-07-02 17:36:20 -0400 | [diff] [blame] | 439 | // This TexDesc refers to the texture that will be read by the client. Thus even if msaa is |
Jim Van Verth | 61610be | 2019-03-07 15:33:49 -0500 | [diff] [blame] | 440 | // requested, this TexDesc describes the resolved texture. Therefore we always have samples |
| 441 | // set to 1. |
Timothy Liang | 58f153d | 2018-07-02 17:36:20 -0400 | [diff] [blame] | 442 | MTLTextureDescriptor* texDesc = [[MTLTextureDescriptor alloc] init]; |
| 443 | texDesc.textureType = MTLTextureType2D; |
| 444 | texDesc.pixelFormat = format; |
| 445 | texDesc.width = desc.fWidth; |
| 446 | texDesc.height = desc.fHeight; |
| 447 | texDesc.depth = 1; |
| 448 | texDesc.mipmapLevelCount = mipLevels; |
| 449 | texDesc.sampleCount = 1; |
| 450 | texDesc.arrayLength = 1; |
| 451 | texDesc.cpuCacheMode = MTLCPUCacheModeWriteCombined; |
| 452 | // Make all textures have private gpu only access. We can use transfer buffers or textures |
| 453 | // to copy to them. |
| 454 | texDesc.storageMode = MTLStorageModePrivate; |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 455 | texDesc.usage = MTLTextureUsageShaderRead; |
| 456 | texDesc.usage |= renderTarget ? MTLTextureUsageRenderTarget : 0; |
Timothy Liang | 58f153d | 2018-07-02 17:36:20 -0400 | [diff] [blame] | 457 | |
| 458 | GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated; |
| 459 | if (mipLevels > 1) { |
Jim Van Verth | 9896a0d | 2019-04-10 15:11:12 -0400 | [diff] [blame] | 460 | mipMapsStatus = GrMipMapsStatus::kValid; |
| 461 | for (int i = 0; i < mipLevels; ++i) { |
| 462 | if (!texels[i].fPixels) { |
| 463 | mipMapsStatus = GrMipMapsStatus::kDirty; |
| 464 | break; |
Timothy Liang | 58f153d | 2018-07-02 17:36:20 -0400 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | } |
Jim Van Verth | 9896a0d | 2019-04-10 15:11:12 -0400 | [diff] [blame] | 468 | |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 469 | if (renderTarget) { |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 470 | tex = GrMtlTextureRenderTarget::CreateNewTextureRenderTarget(this, budgeted, |
Timothy Liang | 58f153d | 2018-07-02 17:36:20 -0400 | [diff] [blame] | 471 | desc, texDesc, mipMapsStatus); |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 472 | } else { |
Timothy Liang | 58f153d | 2018-07-02 17:36:20 -0400 | [diff] [blame] | 473 | tex = GrMtlTexture::CreateNewTexture(this, budgeted, desc, texDesc, mipMapsStatus); |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | if (!tex) { |
| 477 | return nullptr; |
| 478 | } |
| 479 | |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 480 | auto colorType = GrPixelConfigToColorType(desc.fConfig); |
| 481 | if (mipLevelCount && texels[0].fPixels) { |
| 482 | if (!this->uploadToTexture(tex.get(), 0, 0, desc.fWidth, desc.fHeight, colorType, texels, |
| 483 | mipLevelCount)) { |
| 484 | tex->unref(); |
| 485 | return nullptr; |
| 486 | } |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | if (desc.fFlags & kPerformInitialClear_GrSurfaceFlag) { |
Jim Van Verth | 686046b | 2019-03-18 15:39:22 -0400 | [diff] [blame] | 490 | this->clearTexture(tex.get(), colorType); |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 491 | } |
Jim Van Verth | 686046b | 2019-03-18 15:39:22 -0400 | [diff] [blame] | 492 | SK_END_AUTORELEASE_BLOCK |
| 493 | |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 494 | return std::move(tex); |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 495 | } |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 496 | |
| 497 | static id<MTLTexture> get_texture_from_backend(const GrBackendTexture& backendTex, |
| 498 | GrWrapOwnership ownership) { |
| 499 | GrMtlTextureInfo textureInfo; |
| 500 | if (!backendTex.getMtlTextureInfo(&textureInfo)) { |
| 501 | return nil; |
| 502 | } |
| 503 | return GrGetMTLTexture(textureInfo.fTexture, ownership); |
| 504 | } |
| 505 | |
| 506 | static id<MTLTexture> get_texture_from_backend(const GrBackendRenderTarget& backendRT) { |
| 507 | GrMtlTextureInfo textureInfo; |
| 508 | if (!backendRT.getMtlTextureInfo(&textureInfo)) { |
| 509 | return nil; |
| 510 | } |
| 511 | return GrGetMTLTexture(textureInfo.fTexture, GrWrapOwnership::kBorrow_GrWrapOwnership); |
| 512 | } |
| 513 | |
| 514 | static inline void init_surface_desc(GrSurfaceDesc* surfaceDesc, id<MTLTexture> mtlTexture, |
| 515 | bool isRenderTarget, GrPixelConfig config) { |
| 516 | if (isRenderTarget) { |
| 517 | SkASSERT(MTLTextureUsageRenderTarget & mtlTexture.usage); |
| 518 | } |
| 519 | surfaceDesc->fFlags = isRenderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags; |
| 520 | surfaceDesc->fWidth = mtlTexture.width; |
| 521 | surfaceDesc->fHeight = mtlTexture.height; |
| 522 | surfaceDesc->fConfig = config; |
| 523 | surfaceDesc->fSampleCnt = 1; |
| 524 | } |
| 525 | |
| 526 | sk_sp<GrTexture> GrMtlGpu::onWrapBackendTexture(const GrBackendTexture& backendTex, |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 527 | GrWrapOwnership ownership, |
| 528 | GrWrapCacheable cacheable, GrIOType ioType) { |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 529 | id<MTLTexture> mtlTexture = get_texture_from_backend(backendTex, ownership); |
| 530 | if (!mtlTexture) { |
| 531 | return nullptr; |
| 532 | } |
| 533 | |
| 534 | GrSurfaceDesc surfDesc; |
| 535 | init_surface_desc(&surfDesc, mtlTexture, false, backendTex.config()); |
| 536 | |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 537 | return GrMtlTexture::MakeWrappedTexture(this, surfDesc, mtlTexture, cacheable, ioType); |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | sk_sp<GrTexture> GrMtlGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex, |
| 541 | int sampleCnt, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 542 | GrWrapOwnership ownership, |
| 543 | GrWrapCacheable cacheable) { |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 544 | id<MTLTexture> mtlTexture = get_texture_from_backend(backendTex, ownership); |
| 545 | if (!mtlTexture) { |
| 546 | return nullptr; |
| 547 | } |
| 548 | |
| 549 | GrSurfaceDesc surfDesc; |
| 550 | init_surface_desc(&surfDesc, mtlTexture, true, backendTex.config()); |
| 551 | surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, surfDesc.fConfig); |
| 552 | if (!surfDesc.fSampleCnt) { |
| 553 | return nullptr; |
| 554 | } |
| 555 | |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 556 | return GrMtlTextureRenderTarget::MakeWrappedTextureRenderTarget(this, surfDesc, mtlTexture, |
| 557 | cacheable); |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | sk_sp<GrRenderTarget> GrMtlGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) { |
| 561 | // TODO: Revisit this when the Metal backend is completed. It may support MSAA render targets. |
| 562 | if (backendRT.sampleCnt() > 1) { |
| 563 | return nullptr; |
| 564 | } |
| 565 | id<MTLTexture> mtlTexture = get_texture_from_backend(backendRT); |
| 566 | if (!mtlTexture) { |
| 567 | return nullptr; |
| 568 | } |
| 569 | |
| 570 | GrSurfaceDesc surfDesc; |
| 571 | init_surface_desc(&surfDesc, mtlTexture, true, backendRT.config()); |
| 572 | |
| 573 | return GrMtlRenderTarget::MakeWrappedRenderTarget(this, surfDesc, mtlTexture); |
| 574 | } |
| 575 | |
| 576 | sk_sp<GrRenderTarget> GrMtlGpu::onWrapBackendTextureAsRenderTarget( |
| 577 | const GrBackendTexture& backendTex, int sampleCnt) { |
| 578 | id<MTLTexture> mtlTexture = get_texture_from_backend(backendTex, |
| 579 | GrWrapOwnership::kBorrow_GrWrapOwnership); |
| 580 | if (!mtlTexture) { |
| 581 | return nullptr; |
| 582 | } |
| 583 | |
| 584 | GrSurfaceDesc surfDesc; |
| 585 | init_surface_desc(&surfDesc, mtlTexture, true, backendTex.config()); |
| 586 | surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, surfDesc.fConfig); |
| 587 | if (!surfDesc.fSampleCnt) { |
| 588 | return nullptr; |
| 589 | } |
| 590 | |
| 591 | return GrMtlRenderTarget::MakeWrappedRenderTarget(this, surfDesc, mtlTexture); |
| 592 | } |
| 593 | |
Jim Van Verth | 686046b | 2019-03-18 15:39:22 -0400 | [diff] [blame] | 594 | bool GrMtlGpu::onRegenerateMipMapLevels(GrTexture* texture) { |
Jim Van Verth | 9896a0d | 2019-04-10 15:11:12 -0400 | [diff] [blame] | 595 | GrMtlTexture* grMtlTexture = static_cast<GrMtlTexture*>(texture); |
| 596 | id<MTLTexture> mtlTexture = grMtlTexture->mtlTexture(); |
| 597 | |
| 598 | // Automatic mipmap generation is only supported by color-renderable formats |
| 599 | if (!fMtlCaps->isConfigRenderable(texture->config()) && |
| 600 | // We have pixel configs marked as textureable-only that use RGBA8 as the internal format |
| 601 | MTLPixelFormatRGBA8Unorm != mtlTexture.pixelFormat) { |
| 602 | return false; |
| 603 | } |
| 604 | |
Jim Van Verth | 75c3ae4 | 2019-04-22 17:07:53 -0400 | [diff] [blame] | 605 | id<MTLBlitCommandEncoder> blitCmdEncoder = this->commandBuffer()->getBlitCommandEncoder(); |
Jim Van Verth | 9896a0d | 2019-04-10 15:11:12 -0400 | [diff] [blame] | 606 | [blitCmdEncoder generateMipmapsForTexture: mtlTexture]; |
Jim Van Verth | 9896a0d | 2019-04-10 15:11:12 -0400 | [diff] [blame] | 607 | |
| 608 | return true; |
Jim Van Verth | 686046b | 2019-03-18 15:39:22 -0400 | [diff] [blame] | 609 | } |
| 610 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 611 | bool GrMtlGpu::createTestingOnlyMtlTextureInfo(GrPixelConfig config, MTLPixelFormat format, |
| 612 | int w, int h, bool texturable, |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 613 | bool renderable, GrMipMapped mipMapped, |
| 614 | const void* srcData, size_t srcRowBytes, |
| 615 | GrMtlTextureInfo* info) { |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 616 | SkASSERT(texturable || renderable); |
| 617 | if (!texturable) { |
| 618 | SkASSERT(GrMipMapped::kNo == mipMapped); |
| 619 | SkASSERT(!srcData); |
| 620 | } |
| 621 | |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 622 | if (texturable && !fMtlCaps->isConfigTexturable(config)) { |
| 623 | return false; |
| 624 | } |
| 625 | if (renderable && !fMtlCaps->isConfigRenderable(config)) { |
| 626 | return false; |
| 627 | } |
| 628 | // Currently we don't support uploading pixel data when mipped. |
| 629 | if (srcData && GrMipMapped::kYes == mipMapped) { |
| 630 | return false; |
| 631 | } |
| 632 | if(!check_max_blit_width(w)) { |
| 633 | return false; |
| 634 | } |
| 635 | |
Jim Van Verth | 61610be | 2019-03-07 15:33:49 -0500 | [diff] [blame] | 636 | SK_BEGIN_AUTORELEASE_BLOCK |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 637 | bool mipmapped = mipMapped == GrMipMapped::kYes ? true : false; |
| 638 | MTLTextureDescriptor* desc = |
| 639 | [MTLTextureDescriptor texture2DDescriptorWithPixelFormat: format |
| 640 | width: w |
| 641 | height: h |
| 642 | mipmapped: mipmapped]; |
| 643 | desc.cpuCacheMode = MTLCPUCacheModeWriteCombined; |
| 644 | desc.storageMode = MTLStorageModePrivate; |
| 645 | desc.usage = texturable ? MTLTextureUsageShaderRead : 0; |
| 646 | desc.usage |= renderable ? MTLTextureUsageRenderTarget : 0; |
| 647 | id<MTLTexture> testTexture = [fDevice newTextureWithDescriptor: desc]; |
| 648 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 649 | size_t bpp = GrBytesPerPixel(config); |
Jim Van Verth | 8cc4248 | 2019-04-05 14:16:37 -0400 | [diff] [blame] | 650 | if (!srcRowBytes) { |
Jim Van Verth | 568eb8d | 2019-04-04 14:30:42 -0400 | [diff] [blame] | 651 | srcRowBytes = w * bpp; |
Jim Van Verth | 8cc4248 | 2019-04-05 14:16:37 -0400 | [diff] [blame] | 652 | #ifdef SK_BUILD_FOR_MAC |
| 653 | // On MacOS, the fillBuffer command needs a range with a multiple of 4 bytes |
| 654 | srcRowBytes = ((srcRowBytes + 3) & (~3)); |
| 655 | #endif |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 656 | } |
Jim Van Verth | 8cc4248 | 2019-04-05 14:16:37 -0400 | [diff] [blame] | 657 | size_t bufferSize = srcRowBytes * h; |
| 658 | NSUInteger options = 0; // TODO: consider other options here |
Timothy Liang | 4e67984 | 2018-08-08 14:21:12 -0400 | [diff] [blame] | 659 | #ifdef SK_BUILD_FOR_MAC |
Jim Van Verth | 568eb8d | 2019-04-04 14:30:42 -0400 | [diff] [blame] | 660 | options |= MTLResourceStorageModeManaged; |
Timothy Liang | 4e67984 | 2018-08-08 14:21:12 -0400 | [diff] [blame] | 661 | #else |
Jim Van Verth | 568eb8d | 2019-04-04 14:30:42 -0400 | [diff] [blame] | 662 | options |= MTLResourceStorageModeShared; |
Timothy Liang | 4e67984 | 2018-08-08 14:21:12 -0400 | [diff] [blame] | 663 | #endif |
Jim Van Verth | 568eb8d | 2019-04-04 14:30:42 -0400 | [diff] [blame] | 664 | |
| 665 | // TODO: Create GrMtlTransferBuffer |
Jim Van Verth | 8cc4248 | 2019-04-05 14:16:37 -0400 | [diff] [blame] | 666 | id<MTLBuffer> transferBuffer; |
| 667 | if (srcData) { |
| 668 | transferBuffer = [fDevice newBufferWithBytes: srcData |
| 669 | length: bufferSize |
| 670 | options: options]; |
| 671 | } else { |
| 672 | transferBuffer = [fDevice newBufferWithLength: bufferSize |
| 673 | options: options]; |
| 674 | } |
Jim Van Verth | 568eb8d | 2019-04-04 14:30:42 -0400 | [diff] [blame] | 675 | if (nil == transferBuffer) { |
| 676 | return false; |
| 677 | } |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 678 | |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 679 | id<MTLCommandBuffer> cmdBuffer = [fQueue commandBuffer]; |
| 680 | id<MTLBlitCommandEncoder> blitCmdEncoder = [cmdBuffer blitCommandEncoder]; |
Jim Van Verth | 8cc4248 | 2019-04-05 14:16:37 -0400 | [diff] [blame] | 681 | if (!srcData) { |
| 682 | [blitCmdEncoder fillBuffer: transferBuffer |
| 683 | range: NSMakeRange(0, bufferSize) |
| 684 | value: 0]; |
| 685 | } |
Jim Van Verth | 568eb8d | 2019-04-04 14:30:42 -0400 | [diff] [blame] | 686 | [blitCmdEncoder copyFromBuffer: transferBuffer |
| 687 | sourceOffset: 0 |
Jim Van Verth | 8cc4248 | 2019-04-05 14:16:37 -0400 | [diff] [blame] | 688 | sourceBytesPerRow: srcRowBytes |
| 689 | sourceBytesPerImage: bufferSize |
Jim Van Verth | 568eb8d | 2019-04-04 14:30:42 -0400 | [diff] [blame] | 690 | sourceSize: MTLSizeMake(w, h, 1) |
| 691 | toTexture: testTexture |
| 692 | destinationSlice: 0 |
| 693 | destinationLevel: 0 |
Jim Van Verth | 8cc4248 | 2019-04-05 14:16:37 -0400 | [diff] [blame] | 694 | destinationOrigin: MTLOriginMake(0, 0, 0)]; |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 695 | [blitCmdEncoder endEncoding]; |
| 696 | [cmdBuffer commit]; |
| 697 | [cmdBuffer waitUntilCompleted]; |
| 698 | |
| 699 | info->fTexture = GrReleaseId(testTexture); |
Jim Van Verth | 61610be | 2019-03-07 15:33:49 -0500 | [diff] [blame] | 700 | SK_END_AUTORELEASE_BLOCK |
| 701 | |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 702 | return true; |
| 703 | } |
| 704 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 705 | static bool mtl_format_to_pixel_config(MTLPixelFormat format, GrPixelConfig* config) { |
| 706 | GrPixelConfig dontCare; |
| 707 | if (!config) { |
| 708 | config = &dontCare; |
| 709 | } |
| 710 | |
| 711 | switch (format) { |
| 712 | case MTLPixelFormatInvalid: |
| 713 | *config = kUnknown_GrPixelConfig; |
| 714 | return false; |
| 715 | case MTLPixelFormatRGBA8Unorm: |
| 716 | *config = kRGBA_8888_GrPixelConfig; |
| 717 | return true; |
| 718 | case MTLPixelFormatRG8Unorm: |
| 719 | *config = kRG_88_GrPixelConfig; |
| 720 | return true; |
| 721 | case MTLPixelFormatBGRA8Unorm: |
| 722 | *config = kBGRA_8888_GrPixelConfig; |
| 723 | return true; |
| 724 | case MTLPixelFormatRGBA8Unorm_sRGB: |
| 725 | *config = kSRGBA_8888_GrPixelConfig; |
| 726 | return true; |
| 727 | case MTLPixelFormatBGRA8Unorm_sRGB: |
| 728 | *config = kSBGRA_8888_GrPixelConfig; |
| 729 | return true; |
| 730 | case MTLPixelFormatRGB10A2Unorm: |
| 731 | *config = kRGBA_1010102_GrPixelConfig; |
| 732 | return true; |
| 733 | #ifdef SK_BUILD_FOR_IOS |
| 734 | case MTLPixelFormatB5G6R5Unorm: |
| 735 | *config = kRGB_565_GrPixelConfig; |
| 736 | return true; |
| 737 | case MTLPixelFormatABGR4Unorm: |
| 738 | *config = kRGBA_4444_GrPixelConfig; |
| 739 | return true; |
| 740 | #endif |
| 741 | case MTLPixelFormatR8Unorm: |
| 742 | *config = kAlpha_8_GrPixelConfig; |
| 743 | return true; |
| 744 | case MTLPixelFormatRGBA32Float: |
| 745 | *config = kRGBA_float_GrPixelConfig; |
| 746 | return true; |
| 747 | case MTLPixelFormatRG32Float: |
| 748 | *config = kRG_float_GrPixelConfig; |
| 749 | return true; |
| 750 | case MTLPixelFormatRGBA16Float: |
| 751 | *config = kRGBA_half_GrPixelConfig; |
| 752 | return true; |
| 753 | case MTLPixelFormatR16Float: |
| 754 | *config = kAlpha_half_GrPixelConfig; |
| 755 | return true; |
| 756 | #ifdef SK_BUILD_FOR_IOS |
| 757 | case MTLPixelFormatETC2_RGB8: |
| 758 | *config = kRGB_ETC1_GrPixelConfig; |
| 759 | return true; |
| 760 | #endif |
| 761 | default: |
| 762 | return false; |
| 763 | } |
| 764 | |
| 765 | SK_ABORT("Unexpected config"); |
| 766 | return false; |
| 767 | } |
| 768 | |
| 769 | GrBackendTexture GrMtlGpu::createTestingOnlyBackendTexture(int w, int h, |
| 770 | const GrBackendFormat& format, |
| 771 | GrMipMapped mipMapped, |
| 772 | GrRenderable renderable, |
| 773 | const void* pixels, size_t rowBytes) { |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 774 | if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) { |
| 775 | return GrBackendTexture(); |
| 776 | } |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 777 | |
| 778 | const GrMTLPixelFormat* mtlFormat = format.getMtlFormat(); |
| 779 | if (!mtlFormat) { |
| 780 | return GrBackendTexture(); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 781 | } |
| 782 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 783 | GrPixelConfig config; |
| 784 | |
| 785 | if (!mtl_format_to_pixel_config(static_cast<MTLPixelFormat>(*mtlFormat), &config)) { |
| 786 | return GrBackendTexture(); |
| 787 | } |
| 788 | |
| 789 | GrMtlTextureInfo info; |
| 790 | if (!this->createTestingOnlyMtlTextureInfo(config, static_cast<MTLPixelFormat>(*mtlFormat), |
| 791 | w, h, true, |
| 792 | GrRenderable::kYes == renderable, mipMapped, |
| 793 | pixels, rowBytes, &info)) { |
| 794 | return {}; |
| 795 | } |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 796 | |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 797 | GrBackendTexture backendTex(w, h, mipMapped, info); |
| 798 | backendTex.fConfig = config; |
| 799 | return backendTex; |
| 800 | } |
| 801 | |
Robert Phillips | f0ced62 | 2019-05-16 09:06:25 -0400 | [diff] [blame^] | 802 | void GrMtlGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) { |
| 803 | SkASSERT(GrBackendApi::kMetal == tex.fBackend); |
| 804 | |
| 805 | GrMtlTextureInfo info; |
| 806 | if (tex.getMtlTextureInfo(&info)) { |
| 807 | // Adopts the metal texture so that ARC will clean it up. |
| 808 | GrGetMTLTexture(info.fTexture, GrWrapOwnership::kAdopt_GrWrapOwnership); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | #if GR_TEST_UTILS |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 813 | bool GrMtlGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const { |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 814 | SkASSERT(GrBackendApi::kMetal == tex.backend()); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 815 | |
| 816 | GrMtlTextureInfo info; |
| 817 | if (!tex.getMtlTextureInfo(&info)) { |
| 818 | return false; |
| 819 | } |
| 820 | id<MTLTexture> mtlTexture = GrGetMTLTexture(info.fTexture, |
| 821 | GrWrapOwnership::kBorrow_GrWrapOwnership); |
| 822 | if (!mtlTexture) { |
| 823 | return false; |
| 824 | } |
| 825 | return mtlTexture.usage & MTLTextureUsageShaderRead; |
| 826 | } |
| 827 | |
Brian Osman | 2d010b6 | 2018-08-09 10:55:09 -0400 | [diff] [blame] | 828 | GrBackendRenderTarget GrMtlGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) { |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 829 | if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) { |
| 830 | return GrBackendRenderTarget(); |
| 831 | } |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 832 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 833 | GrPixelConfig config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo); |
| 834 | |
| 835 | MTLPixelFormat format; |
| 836 | if (!GrPixelConfigToMTLFormat(config, &format)) { |
| 837 | return GrBackendRenderTarget(); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 838 | } |
| 839 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 840 | GrMtlTextureInfo info; |
| 841 | if (!this->createTestingOnlyMtlTextureInfo(config, format, w, h, false, true, |
| 842 | GrMipMapped::kNo, nullptr, 0, &info)) { |
| 843 | return {}; |
| 844 | } |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 845 | |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 846 | GrBackendRenderTarget backendRT(w, h, 1, info); |
| 847 | backendRT.fConfig = config; |
| 848 | return backendRT; |
| 849 | } |
| 850 | |
| 851 | void GrMtlGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) { |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 852 | SkASSERT(GrBackendApi::kMetal == rt.fBackend); |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 853 | |
| 854 | GrMtlTextureInfo info; |
| 855 | if (rt.getMtlTextureInfo(&info)) { |
| 856 | this->testingOnly_flushGpuAndSync(); |
| 857 | // Adopts the metal texture so that ARC will clean it up. |
| 858 | GrGetMTLTexture(info.fTexture, GrWrapOwnership::kAdopt_GrWrapOwnership); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | void GrMtlGpu::testingOnly_flushGpuAndSync() { |
| 863 | this->submitCommandBuffer(kForce_SyncQueue); |
| 864 | } |
| 865 | #endif // GR_TEST_UTILS |
| 866 | |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 867 | static int get_surface_sample_cnt(GrSurface* surf) { |
| 868 | if (const GrRenderTarget* rt = surf->asRenderTarget()) { |
| 869 | return rt->numColorSamples(); |
| 870 | } |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | bool GrMtlGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 875 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
| 876 | const SkIRect& srcRect, const SkIPoint& dstPoint) { |
| 877 | #ifdef SK_DEBUG |
| 878 | int dstSampleCnt = get_surface_sample_cnt(dst); |
| 879 | int srcSampleCnt = get_surface_sample_cnt(src); |
| 880 | SkASSERT(this->mtlCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstOrigin, |
| 881 | src->config(), srcSampleCnt, srcOrigin, |
| 882 | srcRect, dstPoint, dst == src)); |
| 883 | #endif |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 884 | id<MTLTexture> dstTex = GrGetMTLTextureFromSurface(dst, false); |
| 885 | id<MTLTexture> srcTex = GrGetMTLTextureFromSurface(src, false); |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 886 | |
| 887 | // Flip rect if necessary |
| 888 | SkIRect srcMtlRect; |
| 889 | srcMtlRect.fLeft = srcRect.fLeft; |
| 890 | srcMtlRect.fRight = srcRect.fRight; |
| 891 | SkIRect dstRect; |
| 892 | dstRect.fLeft = dstPoint.fX; |
| 893 | dstRect.fRight = dstPoint.fX + srcRect.width(); |
| 894 | |
| 895 | if (kBottomLeft_GrSurfaceOrigin == srcOrigin) { |
| 896 | srcMtlRect.fTop = srcTex.height - srcRect.fBottom; |
| 897 | srcMtlRect.fBottom = srcTex.height - srcRect.fTop; |
| 898 | } else { |
| 899 | srcMtlRect.fTop = srcRect.fTop; |
| 900 | srcMtlRect.fBottom = srcRect.fBottom; |
| 901 | } |
| 902 | |
| 903 | if (kBottomLeft_GrSurfaceOrigin == dstOrigin) { |
| 904 | dstRect.fTop = dstTex.height - dstPoint.fY - srcMtlRect.height(); |
| 905 | } else { |
| 906 | dstRect.fTop = dstPoint.fY; |
| 907 | } |
| 908 | dstRect.fBottom = dstRect.fTop + srcMtlRect.height(); |
| 909 | |
Jim Van Verth | 75c3ae4 | 2019-04-22 17:07:53 -0400 | [diff] [blame] | 910 | id<MTLBlitCommandEncoder> blitCmdEncoder = this->commandBuffer()->getBlitCommandEncoder(); |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 911 | [blitCmdEncoder copyFromTexture: srcTex |
| 912 | sourceSlice: 0 |
| 913 | sourceLevel: 0 |
| 914 | sourceOrigin: MTLOriginMake(srcMtlRect.x(), srcMtlRect.y(), 0) |
| 915 | sourceSize: MTLSizeMake(srcMtlRect.width(), srcMtlRect.height(), 1) |
| 916 | toTexture: dstTex |
| 917 | destinationSlice: 0 |
| 918 | destinationLevel: 0 |
| 919 | destinationOrigin: MTLOriginMake(dstRect.x(), dstRect.y(), 0)]; |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 920 | |
| 921 | return true; |
| 922 | } |
| 923 | |
Timothy Liang | 70c787a | 2018-08-01 09:56:25 -0400 | [diff] [blame] | 924 | bool GrMtlGpu::copySurfaceAsDrawThenBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 925 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
| 926 | const SkIRect& srcRect, const SkIPoint& dstPoint) { |
| 927 | #ifdef SK_DEBUG |
| 928 | int dstSampleCnt = get_surface_sample_cnt(dst); |
| 929 | int srcSampleCnt = get_surface_sample_cnt(src); |
| 930 | SkASSERT(dstSampleCnt == 0); // dst shouldn't be a render target |
| 931 | SkASSERT(!this->mtlCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstOrigin, |
| 932 | src->config(), srcSampleCnt, srcOrigin, |
| 933 | srcRect, dstPoint, dst == src)); |
| 934 | SkASSERT(!this->mtlCaps().canCopyAsDraw(dst->config(), SkToBool(dst->asRenderTarget()), |
| 935 | src->config(), SkToBool(src->asTexture()))); |
| 936 | SkASSERT(this->mtlCaps().canCopyAsDrawThenBlit(dst->config(),src->config(), |
| 937 | SkToBool(src->asTexture()))); |
| 938 | #endif |
| 939 | GrSurfaceDesc surfDesc; |
| 940 | surfDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 941 | surfDesc.fWidth = srcRect.width(); |
| 942 | surfDesc.fHeight = srcRect.height(); |
| 943 | surfDesc.fConfig = dst->config(); |
| 944 | surfDesc.fSampleCnt = 1; |
| 945 | |
| 946 | id<MTLTexture> dstTex = GrGetMTLTextureFromSurface(dst, false); |
| 947 | MTLTextureDescriptor* textureDesc = GrGetMTLTextureDescriptor(dstTex); |
| 948 | textureDesc.width = srcRect.width(); |
| 949 | textureDesc.height = srcRect.height(); |
| 950 | textureDesc.mipmapLevelCount = 1; |
| 951 | textureDesc.usage |= MTLTextureUsageRenderTarget; |
| 952 | |
| 953 | sk_sp<GrMtlTexture> transferTexture = |
| 954 | GrMtlTextureRenderTarget::CreateNewTextureRenderTarget(this, |
| 955 | SkBudgeted::kYes, |
| 956 | surfDesc, |
| 957 | textureDesc, |
| 958 | GrMipMapsStatus::kNotAllocated); |
| 959 | |
| 960 | GrSurfaceOrigin transferOrigin = dstOrigin; |
| 961 | SkASSERT(this->mtlCaps().canCopyAsDraw(transferTexture->config(), |
| 962 | SkToBool(transferTexture->asRenderTarget()), |
| 963 | src->config(), |
| 964 | SkToBool(src->asTexture()))); |
| 965 | // TODO: Eventually we will need to handle resolves either in this function or make a separate |
| 966 | // copySurfaceAsResolveThenBlit(). |
| 967 | if (!this->copySurface(transferTexture.get(), transferOrigin, |
| 968 | src, srcOrigin, |
| 969 | srcRect, SkIPoint::Make(0, 0))) { |
| 970 | return false; |
| 971 | } |
| 972 | |
| 973 | SkIRect transferRect = SkIRect::MakeXYWH(0, 0, srcRect.width(), srcRect.height()); |
| 974 | SkASSERT(this->mtlCaps().canCopyAsBlit(dst->config(), |
| 975 | get_surface_sample_cnt(dst), |
| 976 | dstOrigin, |
| 977 | transferTexture->config(), |
| 978 | get_surface_sample_cnt(transferTexture.get()), |
| 979 | transferOrigin, |
| 980 | transferRect, dstPoint, false)); |
| 981 | if (!this->copySurface(dst, dstOrigin, |
| 982 | transferTexture.get(), transferOrigin, |
| 983 | transferRect, dstPoint)) { |
| 984 | return false; |
| 985 | } |
| 986 | return true; |
| 987 | } |
| 988 | |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 989 | bool GrMtlGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 990 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
| 991 | const SkIRect& srcRect, |
| 992 | const SkIPoint& dstPoint, |
| 993 | bool canDiscardOutsideDstRect) { |
| 994 | |
| 995 | GrPixelConfig dstConfig = dst->config(); |
| 996 | GrPixelConfig srcConfig = src->config(); |
| 997 | |
| 998 | int dstSampleCnt = get_surface_sample_cnt(dst); |
| 999 | int srcSampleCnt = get_surface_sample_cnt(src); |
| 1000 | |
| 1001 | if (dstSampleCnt > 1 || srcSampleCnt > 1) { |
Timothy Liang | 70c787a | 2018-08-01 09:56:25 -0400 | [diff] [blame] | 1002 | SkASSERT(false); // Currently dont support MSAA. TODO: add copySurfaceAsResolve(). |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 1003 | return false; |
| 1004 | } |
| 1005 | |
Jim Van Verth | 61610be | 2019-03-07 15:33:49 -0500 | [diff] [blame] | 1006 | SK_BEGIN_AUTORELEASE_BLOCK |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 1007 | bool success = false; |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 1008 | if (this->mtlCaps().canCopyAsDraw(dst->config(), SkToBool(dst->asRenderTarget()), |
| 1009 | src->config(), SkToBool(src->asTexture()))) { |
| 1010 | success = fCopyManager.copySurfaceAsDraw(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint, |
| 1011 | canDiscardOutsideDstRect); |
| 1012 | } else if (this->mtlCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstOrigin, |
| 1013 | srcConfig, srcSampleCnt, srcOrigin, |
| 1014 | srcRect, dstPoint, dst == src)) { |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 1015 | success = this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint); |
Timothy Liang | 70c787a | 2018-08-01 09:56:25 -0400 | [diff] [blame] | 1016 | } else if (this->mtlCaps().canCopyAsDrawThenBlit(dst->config(), src->config(), |
| 1017 | SkToBool(src->asTexture()))) { |
| 1018 | success = this->copySurfaceAsDrawThenBlit(dst, dstOrigin, src, srcOrigin, |
| 1019 | srcRect, dstPoint); |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 1020 | } |
| 1021 | if (success) { |
| 1022 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.x(), dstPoint.y(), |
| 1023 | srcRect.width(), srcRect.height()); |
| 1024 | this->didWriteToSurface(dst, dstOrigin, &dstRect); |
| 1025 | } |
| 1026 | return success; |
Jim Van Verth | 61610be | 2019-03-07 15:33:49 -0500 | [diff] [blame] | 1027 | SK_END_AUTORELEASE_BLOCK |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 1028 | } |
| 1029 | |
Timothy Liang | a8046af | 2018-07-19 09:58:00 -0400 | [diff] [blame] | 1030 | bool GrMtlGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height, |
| 1031 | GrColorType srcColorType, const GrMipLevel texels[], |
| 1032 | int mipLevelCount) { |
| 1033 | GrMtlTexture* mtlTexture = static_cast<GrMtlTexture*>(surface->asTexture()); |
Jim Van Verth | 686046b | 2019-03-18 15:39:22 -0400 | [diff] [blame] | 1034 | // TODO: In principle we should be able to support pure rendertargets as well, but |
| 1035 | // until we find a use case we'll only support texture rendertargets. |
Timothy Liang | a8046af | 2018-07-19 09:58:00 -0400 | [diff] [blame] | 1036 | if (!mtlTexture) { |
| 1037 | return false; |
| 1038 | } |
| 1039 | if (!mipLevelCount) { |
| 1040 | return false; |
| 1041 | } |
| 1042 | #ifdef SK_DEBUG |
| 1043 | for (int i = 0; i < mipLevelCount; i++) { |
| 1044 | SkASSERT(texels[i].fPixels); |
| 1045 | } |
| 1046 | #endif |
Jim Van Verth | 61610be | 2019-03-07 15:33:49 -0500 | [diff] [blame] | 1047 | SK_BEGIN_AUTORELEASE_BLOCK |
Timothy Liang | a8046af | 2018-07-19 09:58:00 -0400 | [diff] [blame] | 1048 | return this->uploadToTexture(mtlTexture, left, top, width, height, srcColorType, texels, |
| 1049 | mipLevelCount); |
Jim Van Verth | 61610be | 2019-03-07 15:33:49 -0500 | [diff] [blame] | 1050 | SK_END_AUTORELEASE_BLOCK |
Timothy Liang | a8046af | 2018-07-19 09:58:00 -0400 | [diff] [blame] | 1051 | } |
| 1052 | |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 1053 | bool GrMtlGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height, |
| 1054 | GrColorType dstColorType, void* buffer, size_t rowBytes) { |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 1055 | SkASSERT(surface); |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 1056 | if (!check_max_blit_width(width)) { |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 1057 | return false; |
| 1058 | } |
| 1059 | if (GrPixelConfigToColorType(surface->config()) != dstColorType) { |
| 1060 | return false; |
| 1061 | } |
| 1062 | |
Jim Van Verth | 568eb8d | 2019-04-04 14:30:42 -0400 | [diff] [blame] | 1063 | id<MTLBuffer> transferBuffer; |
| 1064 | int bpp = GrColorTypeBytesPerPixel(dstColorType); |
| 1065 | size_t transBufferRowBytes = bpp * width; |
Jim Van Verth | 61610be | 2019-03-07 15:33:49 -0500 | [diff] [blame] | 1066 | SK_BEGIN_AUTORELEASE_BLOCK |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 1067 | bool doResolve = get_surface_sample_cnt(surface) > 1; |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 1068 | id<MTLTexture> mtlTexture = GrGetMTLTextureFromSurface(surface, doResolve); |
Jim Van Verth | 8cc4248 | 2019-04-05 14:16:37 -0400 | [diff] [blame] | 1069 | if (!mtlTexture || [mtlTexture isFramebufferOnly]) { |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 1070 | return false; |
| 1071 | } |
| 1072 | |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 1073 | size_t transBufferImageBytes = transBufferRowBytes * height; |
| 1074 | |
| 1075 | // TODO: implement some way of reusing buffers instead of making a new one every time. |
Jim Van Verth | 8cc4248 | 2019-04-05 14:16:37 -0400 | [diff] [blame] | 1076 | NSUInteger options = 0; |
| 1077 | #ifdef SK_BUILD_FOR_MAC |
| 1078 | options |= MTLResourceStorageModeManaged; |
| 1079 | #else |
| 1080 | options |= MTLResourceStorageModeShared; |
| 1081 | #endif |
Jim Van Verth | 568eb8d | 2019-04-04 14:30:42 -0400 | [diff] [blame] | 1082 | transferBuffer = [fDevice newBufferWithLength: transBufferImageBytes |
Jim Van Verth | 8cc4248 | 2019-04-05 14:16:37 -0400 | [diff] [blame] | 1083 | options: options]; |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 1084 | |
Jim Van Verth | 75c3ae4 | 2019-04-22 17:07:53 -0400 | [diff] [blame] | 1085 | id<MTLBlitCommandEncoder> blitCmdEncoder = this->commandBuffer()->getBlitCommandEncoder(); |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 1086 | [blitCmdEncoder copyFromTexture: mtlTexture |
| 1087 | sourceSlice: 0 |
| 1088 | sourceLevel: 0 |
| 1089 | sourceOrigin: MTLOriginMake(left, top, 0) |
| 1090 | sourceSize: MTLSizeMake(width, height, 1) |
| 1091 | toBuffer: transferBuffer |
| 1092 | destinationOffset: 0 |
| 1093 | destinationBytesPerRow: transBufferRowBytes |
| 1094 | destinationBytesPerImage: transBufferImageBytes]; |
Jim Van Verth | 8cc4248 | 2019-04-05 14:16:37 -0400 | [diff] [blame] | 1095 | #ifdef SK_BUILD_FOR_MAC |
| 1096 | // Sync GPU data back to the CPU |
| 1097 | [blitCmdEncoder synchronizeResource: transferBuffer]; |
| 1098 | #endif |
Jim Van Verth | 568eb8d | 2019-04-04 14:30:42 -0400 | [diff] [blame] | 1099 | SK_END_AUTORELEASE_BLOCK |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 1100 | |
| 1101 | this->submitCommandBuffer(kForce_SyncQueue); |
| 1102 | const void* mappedMemory = transferBuffer.contents; |
| 1103 | |
| 1104 | SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, transBufferRowBytes, height); |
Jim Van Verth | 8cc4248 | 2019-04-05 14:16:37 -0400 | [diff] [blame] | 1105 | |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 1106 | return true; |
Jim Van Verth | 568eb8d | 2019-04-04 14:30:42 -0400 | [diff] [blame] | 1107 | |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 1108 | } |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 1109 | |