blob: 00706d2066dd2f40dd8e873586fd63cc3ba5ab76 [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"
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
20DEF_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(),
Greg Daniel3155f7f2020-01-16 16:54:26 -050049 GrColorType::kBGRA_8888,
Jim Van Verth9187e492019-11-11 16:14:13 -050050 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);
Jim Van Verth9187e492019-11-11 16:14:13 -050065 GrSurface* src = srcProxy->peekSurface();
66 sk_sp<GrTexture> dst = gpu->createTexture(desc, backendFormat, GrRenderable::kNo,
67 1, GrMipMapped::kNo, SkBudgeted::kNo,
68 GrProtected::kNo);
69
70 bool result = gpu->copySurface(dst.get(), src, SkIRect::MakeXYWH(0, 0, kWidth, kHeight),
71 SkIPoint::Make(0, 0));
72 REPORTER_ASSERT(reporter, !result);
73}