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