blob: 41a0f62b85718e0f0ac26d88bb40b1dd791c3380 [file] [log] [blame]
Jim Van Verth9187e492019-11-11 16:14:13 -05001/*
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 Phillips6d344c32020-07-06 10:56:46 -04009#include "include/gpu/GrDirectContext.h"
Jim Van Verth9187e492019-11-11 16:14:13 -050010#include "src/gpu/GrContextPriv.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040011#include "src/gpu/GrProxyProvider.h"
Jim Van Verth9187e492019-11-11 16:14:13 -050012#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
21DEF_GPUTEST_FOR_METAL_CONTEXT(MtlCopySurfaceTest, reporter, ctxInfo) {
22 static const int kWidth = 1024;
23 static const int kHeight = 768;
24
Robert Phillips6d344c32020-07-06 10:56:46 -040025 auto context = ctxInfo.directContext();
Jim Van Verth9187e492019-11-11 16:14:13 -050026
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 Phillipsa1121332020-06-29 13:05:29 -040045 sk_sp<GrSurfaceProxy> srcProxy = proxyProvider->wrapBackendRenderTarget(backendRT, nullptr);
Jim Van Verth9187e492019-11-11 16:14:13 -050046
Brian Salomonc5243782020-04-02 12:50:34 -040047 auto dstProxy = GrSurfaceProxy::Copy(context,
48 srcProxy.get(),
49 kTopLeft_GrSurfaceOrigin,
50 GrMipMapped::kNo,
51 SkBackingFit::kExact,
52 SkBudgeted::kYes);
Jim Van Verth9187e492019-11-11 16:14:13 -050053
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 Salomonc5243782020-04-02 12:50:34 -040056 if (!dstProxy) {
Jim Van Verth9187e492019-11-11 16:14:13 -050057 ERRORF(reporter, "Expected copy to succeed");
58 }
59
60 // Try direct copy via GPU (should fail)
Jim Van Verth9187e492019-11-11 16:14:13 -050061 GrBackendFormat backendFormat = GrBackendFormat::MakeMtl(drawable.texture.pixelFormat);
Jim Van Verth9187e492019-11-11 16:14:13 -050062 GrSurface* src = srcProxy->peekSurface();
Brian Salomona56a7462020-02-07 14:17:25 -050063 sk_sp<GrTexture> dst =
64 gpu->createTexture({kWidth, kHeight}, backendFormat, GrRenderable::kNo, 1,
65 GrMipMapped::kNo, SkBudgeted::kNo, GrProtected::kNo);
Jim Van Verth9187e492019-11-11 16:14:13 -050066
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}