Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
gpu_surface_metal_impeller_unittests.mm
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <Foundation/Foundation.h>
6#include <QuartzCore/QuartzCore.h>
7
8#include "flutter/shell/gpu/gpu_surface_metal_impeller.h"
9#include "gtest/gtest.h"
10#include "impeller/entity/mtl/entity_shaders.h"
11#include "impeller/entity/mtl/framebuffer_blend_shaders.h"
12#include "impeller/entity/mtl/modern_shaders.h"
14
15namespace flutter {
16namespace testing {
17
19 public:
21 layer_ = [[CAMetalLayer alloc] init];
22 }
23
25
26 GPUCAMetalLayerHandle GetCAMetalLayer(const SkISize& frame_info) const override {
27 layer_.drawableSize = CGSizeMake(frame_info.width(), frame_info.height());
28 return (__bridge GPUCAMetalLayerHandle)(layer_);
29 }
30
31 bool PresentDrawable(GrMTLHandle drawable) const override { return true; }
32
33 GPUMTLTextureInfo GetMTLTexture(const SkISize& frame_info) const override { return {}; }
34
35 bool PresentTexture(GPUMTLTextureInfo texture) const override { return true; }
36
37 bool AllowsDrawingWhenGpuDisabled() const override { return true; }
38
39 void SetDevice() { layer_.device = ::MTLCreateSystemDefaultDevice(); }
40
41 private:
42 CAMetalLayer* layer_ = nil;
43};
44
45static std::shared_ptr<impeller::ContextMTL> CreateImpellerContext() {
46 std::vector<std::shared_ptr<fml::Mapping>> shader_mappings = {
47 std::make_shared<fml::NonOwnedMapping>(impeller_entity_shaders_data,
48 impeller_entity_shaders_length),
49 std::make_shared<fml::NonOwnedMapping>(impeller_modern_shaders_data,
50 impeller_modern_shaders_length),
51 std::make_shared<fml::NonOwnedMapping>(impeller_framebuffer_blend_shaders_data,
52 impeller_framebuffer_blend_shaders_length),
53 };
54 auto sync_switch = std::make_shared<fml::SyncSwitch>(false);
55 return impeller::ContextMTL::Create(shader_mappings, sync_switch, "Impeller Library");
56}
57
58TEST(GPUSurfaceMetalImpeller, InvalidImpellerContextCreatesCausesSurfaceToBeInvalid) {
59 auto delegate = std::make_shared<TestGPUSurfaceMetalDelegate>();
60 auto surface = std::make_shared<GPUSurfaceMetalImpeller>(delegate.get(), nullptr);
61
62 ASSERT_FALSE(surface->IsValid());
63}
64
65TEST(GPUSurfaceMetalImpeller, CanCreateValidSurface) {
66 auto delegate = std::make_shared<TestGPUSurfaceMetalDelegate>();
67 auto surface = std::make_shared<GPUSurfaceMetalImpeller>(delegate.get(), CreateImpellerContext());
68
69 ASSERT_TRUE(surface->IsValid());
70}
71
72TEST(GPUSurfaceMetalImpeller, AcquireFrameFromCAMetalLayerNullChecksDrawable) {
73 auto delegate = std::make_shared<TestGPUSurfaceMetalDelegate>();
74 std::shared_ptr<Surface> surface =
75 std::make_shared<GPUSurfaceMetalImpeller>(delegate.get(), CreateImpellerContext());
76
77 ASSERT_TRUE(surface->IsValid());
78
79 auto frame = surface->AcquireFrame(SkISize::Make(100, 100));
80 ASSERT_EQ(frame, nullptr);
81}
82
83TEST(GPUSurfaceMetalImpeller, AcquireFrameFromCAMetalLayerDoesNotRetainThis) {
84 auto delegate = std::make_shared<TestGPUSurfaceMetalDelegate>();
85 delegate->SetDevice();
86 std::unique_ptr<Surface> surface =
87 std::make_unique<GPUSurfaceMetalImpeller>(delegate.get(), CreateImpellerContext());
88
89 ASSERT_TRUE(surface->IsValid());
90
91 auto frame = surface->AcquireFrame(SkISize::Make(100, 100));
92 ASSERT_TRUE(frame);
93
94 // Simulate a rasterizer teardown, e.g. due to going to the background.
95 surface.reset();
96
97 ASSERT_TRUE(frame->Submit());
98}
99
100} // namespace testing
101} // namespace flutter
#define TEST(S, s, D, expected)
const void * GrMTLHandle
Definition GrMtlTypes.h:20
Interface implemented by all platform surfaces that can present a metal backing store to the "screen"...
bool PresentDrawable(GrMTLHandle drawable) const override
Presents the drawable to the "screen". The drawable is obtained from the CAMetalLayer that given by G...
bool AllowsDrawingWhenGpuDisabled() const override
Whether to allow drawing to the surface when the GPU is disabled.
GPUMTLTextureInfo GetMTLTexture(const SkISize &frame_info) const override
Returns the handle to the MTLTexture to render to. This is only called when the specified render targ...
GPUCAMetalLayerHandle GetCAMetalLayer(const SkISize &frame_info) const override
Returns the handle to the CAMetalLayer to render to. This is only called when the specified render ta...
bool PresentTexture(GPUMTLTextureInfo texture) const override
Presents the texture with texture_id to the "screen". texture_id corresponds to a texture that has be...
static std::shared_ptr< ContextMTL > Create(const std::vector< std::string > &shader_library_paths, std::shared_ptr< const fml::SyncSwitch > is_gpu_disabled_sync_switch)
VkSurfaceKHR surface
Definition main.cc:49
double frame
Definition examples.cpp:31
FlTexture * texture
static std::shared_ptr< impeller::ContextMTL > CreateImpellerContext()
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
constexpr int32_t width() const
Definition SkSize.h:36
constexpr int32_t height() const
Definition SkSize.h:37