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" |
| 9 | #include "include/gpu/GrContext.h" |
| 10 | #include "src/gpu/GrContextPriv.h" |
| 11 | #include "src/gpu/mtl/GrMtlGpu.h" |
| 12 | #include "tests/Test.h" |
| 13 | |
| 14 | #import <Metal/Metal.h> |
| 15 | #import <MetalKit/MTKView.h> |
| 16 | |
| 17 | #include "src/gpu/mtl/GrMtlCaps.h" |
| 18 | #include "src/gpu/mtl/GrMtlTextureRenderTarget.h" |
| 19 | |
| 20 | DEF_GPUTEST_FOR_METAL_CONTEXT(MtlCopySurfaceTest, reporter, ctxInfo) { |
| 21 | static const int kWidth = 1024; |
| 22 | static const int kHeight = 768; |
| 23 | |
| 24 | GrContext* context = ctxInfo.grContext(); |
| 25 | |
| 26 | // This is a bit weird, but it's the only way to get a framebufferOnly surface |
| 27 | GrMtlGpu* gpu = (GrMtlGpu*) context->priv().getGpu(); |
| 28 | |
| 29 | MTKView* view = [[MTKView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) |
| 30 | device:gpu->device()]; |
| 31 | id<CAMetalDrawable> drawable = [view currentDrawable]; |
| 32 | REPORTER_ASSERT(reporter, drawable.texture.framebufferOnly); |
| 33 | REPORTER_ASSERT(reporter, drawable.texture.usage & MTLTextureUsageRenderTarget); |
| 34 | |
| 35 | // Test to see if we can initiate a copy via GrSurfaceProxys |
| 36 | SkSurfaceProps props(0, kRGB_H_SkPixelGeometry); |
| 37 | |
| 38 | // TODO: check multisampled RT as well |
| 39 | GrMtlTextureInfo fbInfo; |
| 40 | fbInfo.fTexture.retain((__bridge const void*)(drawable.texture)); |
| 41 | GrBackendRenderTarget backendRT(kWidth, kHeight, 1, fbInfo); |
| 42 | |
| 43 | GrProxyProvider* proxyProvider = context->priv().proxyProvider(); |
| 44 | sk_sp<GrSurfaceProxy> srcProxy = proxyProvider->wrapBackendRenderTarget( |
| 45 | backendRT, GrColorType::kBGRA_8888, |
| 46 | kTopLeft_GrSurfaceOrigin); |
| 47 | |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 48 | GrSurfaceProxyView dstView = GrSurfaceProxy::Copy(context, srcProxy.get(), |
| 49 | kTopLeft_GrSurfaceOrigin, |
| 50 | GrColorType::kBGRA_8888, |
| 51 | GrMipMapped::kNo, |
| 52 | SkBackingFit::kExact, |
| 53 | SkBudgeted::kYes); |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 54 | |
| 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. |
Greg Daniel | 40903af | 2020-01-30 14:55:05 -0500 | [diff] [blame] | 57 | if (!dstView.proxy()) { |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 58 | ERRORF(reporter, "Expected copy to succeed"); |
| 59 | } |
| 60 | |
| 61 | // Try direct copy via GPU (should fail) |
| 62 | GrSurfaceDesc desc; |
| 63 | desc.fWidth = kWidth; |
| 64 | desc.fHeight = kHeight; |
| 65 | GrBackendFormat backendFormat = GrBackendFormat::MakeMtl(drawable.texture.pixelFormat); |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 66 | GrSurface* src = srcProxy->peekSurface(); |
| 67 | sk_sp<GrTexture> dst = gpu->createTexture(desc, backendFormat, GrRenderable::kNo, |
| 68 | 1, GrMipMapped::kNo, SkBudgeted::kNo, |
| 69 | GrProtected::kNo); |
| 70 | |
| 71 | bool result = gpu->copySurface(dst.get(), src, SkIRect::MakeXYWH(0, 0, kWidth, kHeight), |
| 72 | SkIPoint::Make(0, 0)); |
| 73 | REPORTER_ASSERT(reporter, !result); |
| 74 | } |