Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
tester_context_vk_factory.cc
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#define FML_USED_ON_EMBEDDER
6
8
9#include <vulkan/vulkan.h>
10
11#include <vector>
12
13#include "flutter/fml/mapping.h"
14#include "flutter/fml/paths.h"
16#include "impeller/entity/vk/entity_shaders_vk.h"
17#include "impeller/entity/vk/framebuffer_blend_shaders_vk.h"
18#include "impeller/entity/vk/modern_shaders_vk.h"
21#include "impeller/renderer/vk/compute_shaders_vk.h"
23
24namespace flutter {
25
26namespace {
27std::vector<std::shared_ptr<fml::Mapping>> ShaderLibraryMappings() {
28 return {
29 std::make_shared<fml::NonOwnedMapping>(impeller_entity_shaders_vk_data,
30 impeller_entity_shaders_vk_length),
31 std::make_shared<fml::NonOwnedMapping>(impeller_modern_shaders_vk_data,
32 impeller_modern_shaders_vk_length),
33 std::make_shared<fml::NonOwnedMapping>(
34 impeller_framebuffer_blend_shaders_vk_data,
35 impeller_framebuffer_blend_shaders_vk_length),
36 std::make_shared<fml::NonOwnedMapping>(
37 impeller_compute_shaders_vk_data, impeller_compute_shaders_vk_length),
38 };
39}
40
41class TesterContextVK : public TesterContext {
42 public:
43 TesterContextVK() = default;
44
45 ~TesterContextVK() override {
46 if (context_) {
47 context_->Shutdown();
48 }
49 }
50
51 bool Initialize(bool enable_validation) {
52 impeller::ContextVK::Settings context_settings;
53 context_settings.proc_address_callback = &vkGetInstanceProcAddr;
54 context_settings.shader_libraries_data = ShaderLibraryMappings();
56 context_settings.enable_validation = enable_validation;
57
58 context_ = impeller::ContextVK::Create(std::move(context_settings));
59 if (!context_ || !context_->IsValid()) {
60 VALIDATION_LOG << "Could not create Vulkan context.";
61 return false;
62 }
63
64 impeller::vk::SurfaceKHR vk_surface;
65 impeller::vk::HeadlessSurfaceCreateInfoEXT surface_create_info;
66 auto res = context_->GetInstance().createHeadlessSurfaceEXT(
67 &surface_create_info, // surface create info
68 nullptr, // allocator
69 &vk_surface // surface
70 );
71 if (res != impeller::vk::Result::eSuccess) {
72 VALIDATION_LOG << "Could not create surface for tester "
73 << impeller::vk::to_string(res);
74 return false;
75 }
76
77 impeller::vk::UniqueSurfaceKHR surface{vk_surface, context_->GetInstance()};
78 surface_context_ = context_->CreateSurfaceContext();
79 if (!surface_context_->SetWindowSurface(std::move(surface),
80 impeller::ISize{1, 1})) {
81 VALIDATION_LOG << "Could not set up surface for context.";
82 return false;
83 }
84 return true;
85 }
86
87 // |TesterContext|
88 std::shared_ptr<impeller::Context> GetImpellerContext() const override {
89 return context_;
90 }
91
92 // |TesterContext|
93 std::unique_ptr<Surface> CreateRenderingSurface() override {
94 FML_DCHECK(context_);
95 auto surface =
96 std::make_unique<GPUSurfaceVulkanImpeller>(nullptr, surface_context_);
97 FML_DCHECK(surface->IsValid());
98 return surface;
99 }
100
101 private:
102 std::shared_ptr<impeller::ContextVK> context_;
103 std::shared_ptr<impeller::SurfaceContextVK> surface_context_;
104};
105
106} // namespace
107
108std::unique_ptr<TesterContext> TesterContextVKFactory::Create(
109 bool enable_validation) {
110 auto context = std::make_unique<TesterContextVK>();
111 if (!context->Initialize(enable_validation)) {
112 return nullptr;
113 }
114 return context;
115}
116
117} // namespace flutter
static std::unique_ptr< TesterContext > Create(bool enable_validation)
static std::shared_ptr< ContextVK > Create(Settings settings)
VkSurfaceKHR surface
Definition main.cc:65
#define FML_DCHECK(condition)
Definition logging.h:122
fml::UniqueFD GetCachesDirectory()
std::vector< std::shared_ptr< fml::Mapping > > shader_libraries_data
Definition context_vk.h:81
PFN_vkGetInstanceProcAddr proc_address_callback
Definition context_vk.h:80
#define VALIDATION_LOG
Definition validation.h:91