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