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 | |
| 48 | sk_sp<GrTextureProxy> dstProxy = GrSurfaceProxy::Copy(context, srcProxy.get(), |
Brian Salomon | 078e8fa | 2019-11-22 04:10:18 +0000 | [diff] [blame^] | 49 | GrColorType::kBGRA_8888, |
Jim Van Verth | 9187e49 | 2019-11-11 16:14:13 -0500 | [diff] [blame] | 50 | GrMipMapped::kNo, |
| 51 | SkBackingFit::kExact, |
| 52 | SkBudgeted::kYes); |
| 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. |
| 56 | if (!dstProxy) { |
| 57 | ERRORF(reporter, "Expected copy to succeed"); |
| 58 | } |
| 59 | |
| 60 | // Try direct copy via GPU (should fail) |
| 61 | GrSurfaceDesc desc; |
| 62 | desc.fWidth = kWidth; |
| 63 | desc.fHeight = kHeight; |
| 64 | GrBackendFormat backendFormat = GrBackendFormat::MakeMtl(drawable.texture.pixelFormat); |
| 65 | desc.fConfig = gpu->caps()->getConfigFromBackendFormat(backendFormat, |
| 66 | GrColorType::kBGRA_8888); |
| 67 | GrSurface* src = srcProxy->peekSurface(); |
| 68 | sk_sp<GrTexture> dst = gpu->createTexture(desc, backendFormat, GrRenderable::kNo, |
| 69 | 1, GrMipMapped::kNo, SkBudgeted::kNo, |
| 70 | GrProtected::kNo); |
| 71 | |
| 72 | bool result = gpu->copySurface(dst.get(), src, SkIRect::MakeXYWH(0, 0, kWidth, kHeight), |
| 73 | SkIPoint::Make(0, 0)); |
| 74 | REPORTER_ASSERT(reporter, !result); |
| 75 | } |