Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 "include/core/SkSurface.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 9 | #include "include/gpu/GrDirectContext.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 10 | #include "src/gpu/GrDirectContextPriv.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrProxyProvider.h" |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 12 | #include "src/gpu/mtl/GrMtlGpu.h" |
| 13 | #include "tests/Test.h" |
| 14 | |
| 15 | #import <Metal/Metal.h> |
| 16 | #import <MetalKit/MTKView.h> |
| 17 | |
| 18 | #include "src/gpu/mtl/GrMtlCaps.h" |
| 19 | #include "src/gpu/mtl/GrMtlTextureRenderTarget.h" |
| 20 | |
| 21 | DEF_GPUTEST_FOR_METAL_CONTEXT(MtlCopySurfaceTest, reporter, ctxInfo) { |
Jim Van Verth | 8d9e313 | 2021-09-14 08:36:55 -0400 | [diff] [blame] | 22 | if (@available(macOS 11.0, iOS 9.0, *)) { |
| 23 | static const int kWidth = 1024; |
| 24 | static const int kHeight = 768; |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 25 | |
Jim Van Verth | 8d9e313 | 2021-09-14 08:36:55 -0400 | [diff] [blame] | 26 | auto context = ctxInfo.directContext(); |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 27 | |
Jim Van Verth | 8d9e313 | 2021-09-14 08:36:55 -0400 | [diff] [blame] | 28 | // This is a bit weird, but it's the only way to get a framebufferOnly surface |
| 29 | GrMtlGpu* gpu = (GrMtlGpu*) context->priv().getGpu(); |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 30 | |
Jim Van Verth | 8d9e313 | 2021-09-14 08:36:55 -0400 | [diff] [blame] | 31 | MTKView* view = [[MTKView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) |
| 32 | device:gpu->device()]; |
| 33 | id<CAMetalDrawable> drawable = [view currentDrawable]; |
| 34 | REPORTER_ASSERT(reporter, drawable.texture.framebufferOnly); |
| 35 | REPORTER_ASSERT(reporter, drawable.texture.usage & MTLTextureUsageRenderTarget); |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 36 | |
Jim Van Verth | 8d9e313 | 2021-09-14 08:36:55 -0400 | [diff] [blame] | 37 | // Test to see if we can initiate a copy via GrSurfaceProxys |
| 38 | SkSurfaceProps props(0, kRGB_H_SkPixelGeometry); |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 39 | |
Jim Van Verth | 8d9e313 | 2021-09-14 08:36:55 -0400 | [diff] [blame] | 40 | // TODO: check multisampled RT as well |
| 41 | GrMtlTextureInfo fbInfo; |
| 42 | fbInfo.fTexture.retain((__bridge const void*)(drawable.texture)); |
| 43 | GrBackendRenderTarget backendRT(kWidth, kHeight, fbInfo); |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 44 | |
Jim Van Verth | 8d9e313 | 2021-09-14 08:36:55 -0400 | [diff] [blame] | 45 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 46 | sk_sp<GrSurfaceProxy> srcProxy = proxyProvider->wrapBackendRenderTarget(backendRT, nullptr); |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 47 | |
Jim Van Verth | 8d9e313 | 2021-09-14 08:36:55 -0400 | [diff] [blame] | 48 | auto dstProxy = GrSurfaceProxy::Copy(context, |
| 49 | srcProxy, |
| 50 | kTopLeft_GrSurfaceOrigin, |
| 51 | GrMipmapped::kNo, |
| 52 | SkBackingFit::kExact, |
| 53 | SkBudgeted::kYes); |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 54 | |
Jim Van Verth | 8d9e313 | 2021-09-14 08:36:55 -0400 | [diff] [blame] | 55 | // TODO: GrSurfaceProxy::Copy doesn't check to see if the framebufferOnly bit is set yet. |
| 56 | // Update this when it does -- it should fail. |
| 57 | if (!dstProxy) { |
| 58 | ERRORF(reporter, "Expected copy to succeed"); |
| 59 | } |
| 60 | |
| 61 | // Try direct copy via GPU (should fail) |
| 62 | GrBackendFormat backendFormat = GrBackendFormat::MakeMtl(drawable.texture.pixelFormat); |
| 63 | GrSurface* src = srcProxy->peekSurface(); |
| 64 | sk_sp<GrTexture> dst = |
| 65 | gpu->createTexture({kWidth, kHeight}, backendFormat, GrTextureType::k2D, |
| 66 | GrRenderable::kNo, 1, GrMipmapped::kNo, SkBudgeted::kNo, |
| 67 | GrProtected::kNo); |
| 68 | |
| 69 | bool result = gpu->copySurface(dst.get(), src, SkIRect::MakeXYWH(0, 0, kWidth, kHeight), |
| 70 | SkIPoint::Make(0, 0)); |
| 71 | REPORTER_ASSERT(reporter, !result); |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 72 | } |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 73 | } |