|
| 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" |
| 13 | +#include "impeller/renderer/backend/metal/context_mtl.h" |
| 14 | + |
| 15 | +namespace flutter { |
| 16 | +namespace testing { |
| 17 | + |
| 18 | +class TestGPUSurfaceMetalDelegate : public GPUSurfaceMetalDelegate { |
| 19 | + public: |
| 20 | + TestGPUSurfaceMetalDelegate() : GPUSurfaceMetalDelegate(MTLRenderTargetType::kCAMetalLayer) { |
| 21 | + layer_ = [[CAMetalLayer alloc] init]; |
| 22 | + } |
| 23 | + |
| 24 | + ~TestGPUSurfaceMetalDelegate() = default; |
| 25 | + |
| 26 | + GPUCAMetalLayerHandle GetCAMetalLayer(const SkISize& frame_info) const override { |
| 27 | + return (__bridge GPUCAMetalLayerHandle)(layer_); |
| 28 | + } |
| 29 | + |
| 30 | + bool PresentDrawable(GrMTLHandle drawable) const override { return true; } |
| 31 | + |
| 32 | + GPUMTLTextureInfo GetMTLTexture(const SkISize& frame_info) const override { return {}; } |
| 33 | + |
| 34 | + bool PresentTexture(GPUMTLTextureInfo texture) const override { return true; } |
| 35 | + |
| 36 | + bool AllowsDrawingWhenGpuDisabled() const override { return true; } |
| 37 | + |
| 38 | + private: |
| 39 | + CAMetalLayer* layer_ = nil; |
| 40 | +}; |
| 41 | + |
| 42 | +static std::shared_ptr<impeller::ContextMTL> CreateImpellerContext() { |
| 43 | + std::vector<std::shared_ptr<fml::Mapping>> shader_mappings = { |
| 44 | + std::make_shared<fml::NonOwnedMapping>(impeller_entity_shaders_data, |
| 45 | + impeller_entity_shaders_length), |
| 46 | + std::make_shared<fml::NonOwnedMapping>(impeller_modern_shaders_data, |
| 47 | + impeller_modern_shaders_length), |
| 48 | + std::make_shared<fml::NonOwnedMapping>(impeller_framebuffer_blend_shaders_data, |
| 49 | + impeller_framebuffer_blend_shaders_length), |
| 50 | + }; |
| 51 | + auto sync_switch = std::make_shared<fml::SyncSwitch>(false); |
| 52 | + return impeller::ContextMTL::Create(shader_mappings, sync_switch, "Impeller Library"); |
| 53 | +} |
| 54 | + |
| 55 | +TEST(GPUSurfaceMetalImpeller, InvalidImpellerContextCreatesCausesSurfaceToBeInvalid) { |
| 56 | + auto delegate = std::make_shared<TestGPUSurfaceMetalDelegate>(); |
| 57 | + auto surface = std::make_shared<GPUSurfaceMetalImpeller>(delegate.get(), nullptr); |
| 58 | + |
| 59 | + ASSERT_FALSE(surface->IsValid()); |
| 60 | +} |
| 61 | + |
| 62 | +TEST(GPUSurfaceMetalImpeller, CanCreateValidSurface) { |
| 63 | + auto delegate = std::make_shared<TestGPUSurfaceMetalDelegate>(); |
| 64 | + auto surface = std::make_shared<GPUSurfaceMetalImpeller>(delegate.get(), CreateImpellerContext()); |
| 65 | + |
| 66 | + ASSERT_TRUE(surface->IsValid()); |
| 67 | +} |
| 68 | + |
| 69 | +TEST(GPUSurfaceMetalImpeller, AcquireFrameFromCAMetalLayerNullChecksDrawable) { |
| 70 | + auto delegate = std::make_shared<TestGPUSurfaceMetalDelegate>(); |
| 71 | + std::shared_ptr<Surface> surface = |
| 72 | + std::make_shared<GPUSurfaceMetalImpeller>(delegate.get(), CreateImpellerContext()); |
| 73 | + |
| 74 | + ASSERT_TRUE(surface->IsValid()); |
| 75 | + |
| 76 | + auto frame = surface->AcquireFrame(SkISize::Make(100, 100)); |
| 77 | + ASSERT_EQ(frame, nullptr); |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace testing |
| 81 | +} // namespace flutter |
0 commit comments