1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2019 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#include "include/core/SkSurface.h" 9cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h" 10cb93a386Sopenharmony_ci#include "src/gpu/GrDirectContextPriv.h" 11cb93a386Sopenharmony_ci#include "src/gpu/GrProxyProvider.h" 12cb93a386Sopenharmony_ci#include "src/gpu/mtl/GrMtlGpu.h" 13cb93a386Sopenharmony_ci#include "tests/Test.h" 14cb93a386Sopenharmony_ci 15cb93a386Sopenharmony_ci#import <Metal/Metal.h> 16cb93a386Sopenharmony_ci#import <MetalKit/MTKView.h> 17cb93a386Sopenharmony_ci 18cb93a386Sopenharmony_ci#include "src/gpu/mtl/GrMtlCaps.h" 19cb93a386Sopenharmony_ci#include "src/gpu/mtl/GrMtlTextureRenderTarget.h" 20cb93a386Sopenharmony_ci 21cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_METAL_CONTEXT(MtlCopySurfaceTest, reporter, ctxInfo) { 22cb93a386Sopenharmony_ci if (@available(macOS 11.0, iOS 9.0, *)) { 23cb93a386Sopenharmony_ci static const int kWidth = 1024; 24cb93a386Sopenharmony_ci static const int kHeight = 768; 25cb93a386Sopenharmony_ci 26cb93a386Sopenharmony_ci auto context = ctxInfo.directContext(); 27cb93a386Sopenharmony_ci 28cb93a386Sopenharmony_ci // This is a bit weird, but it's the only way to get a framebufferOnly surface 29cb93a386Sopenharmony_ci GrMtlGpu* gpu = (GrMtlGpu*) context->priv().getGpu(); 30cb93a386Sopenharmony_ci 31cb93a386Sopenharmony_ci MTKView* view = [[MTKView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) 32cb93a386Sopenharmony_ci device:gpu->device()]; 33cb93a386Sopenharmony_ci id<CAMetalDrawable> drawable = [view currentDrawable]; 34cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, drawable.texture.framebufferOnly); 35cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, drawable.texture.usage & MTLTextureUsageRenderTarget); 36cb93a386Sopenharmony_ci 37cb93a386Sopenharmony_ci // Test to see if we can initiate a copy via GrSurfaceProxys 38cb93a386Sopenharmony_ci SkSurfaceProps props(0, kRGB_H_SkPixelGeometry); 39cb93a386Sopenharmony_ci 40cb93a386Sopenharmony_ci // TODO: check multisampled RT as well 41cb93a386Sopenharmony_ci GrMtlTextureInfo fbInfo; 42cb93a386Sopenharmony_ci fbInfo.fTexture.retain((__bridge const void*)(drawable.texture)); 43cb93a386Sopenharmony_ci GrBackendRenderTarget backendRT(kWidth, kHeight, fbInfo); 44cb93a386Sopenharmony_ci 45cb93a386Sopenharmony_ci GrProxyProvider* proxyProvider = context->priv().proxyProvider(); 46cb93a386Sopenharmony_ci sk_sp<GrSurfaceProxy> srcProxy = proxyProvider->wrapBackendRenderTarget(backendRT, nullptr); 47cb93a386Sopenharmony_ci 48cb93a386Sopenharmony_ci auto dstProxy = GrSurfaceProxy::Copy(context, 49cb93a386Sopenharmony_ci srcProxy, 50cb93a386Sopenharmony_ci kTopLeft_GrSurfaceOrigin, 51cb93a386Sopenharmony_ci GrMipmapped::kNo, 52cb93a386Sopenharmony_ci SkBackingFit::kExact, 53cb93a386Sopenharmony_ci SkBudgeted::kYes); 54cb93a386Sopenharmony_ci 55cb93a386Sopenharmony_ci // TODO: GrSurfaceProxy::Copy doesn't check to see if the framebufferOnly bit is set yet. 56cb93a386Sopenharmony_ci // Update this when it does -- it should fail. 57cb93a386Sopenharmony_ci if (!dstProxy) { 58cb93a386Sopenharmony_ci ERRORF(reporter, "Expected copy to succeed"); 59cb93a386Sopenharmony_ci } 60cb93a386Sopenharmony_ci 61cb93a386Sopenharmony_ci // Try direct copy via GPU (should fail) 62cb93a386Sopenharmony_ci GrBackendFormat backendFormat = GrBackendFormat::MakeMtl(drawable.texture.pixelFormat); 63cb93a386Sopenharmony_ci GrSurface* src = srcProxy->peekSurface(); 64cb93a386Sopenharmony_ci sk_sp<GrTexture> dst = 65cb93a386Sopenharmony_ci gpu->createTexture({kWidth, kHeight}, backendFormat, GrTextureType::k2D, 66cb93a386Sopenharmony_ci GrRenderable::kNo, 1, GrMipmapped::kNo, SkBudgeted::kNo, 67cb93a386Sopenharmony_ci GrProtected::kNo); 68cb93a386Sopenharmony_ci 69cb93a386Sopenharmony_ci bool result = gpu->copySurface(dst.get(), src, SkIRect::MakeXYWH(0, 0, kWidth, kHeight), 70cb93a386Sopenharmony_ci SkIPoint::Make(0, 0)); 71cb93a386Sopenharmony_ci REPORTER_ASSERT(reporter, !result); 72cb93a386Sopenharmony_ci } 73cb93a386Sopenharmony_ci} 74