Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit c7b2230

Browse files
authored
[Impeller] Wire-up AndroidSurfaceImpellerVulkan (#37249)
1 parent edbba91 commit c7b2230

13 files changed

+213
-10
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,8 @@ FILE: ../../../flutter/shell/platform/android/android_surface_gl_skia.cc
21442144
FILE: ../../../flutter/shell/platform/android/android_surface_gl_skia.h
21452145
FILE: ../../../flutter/shell/platform/android/android_surface_software.cc
21462146
FILE: ../../../flutter/shell/platform/android/android_surface_software.h
2147+
FILE: ../../../flutter/shell/platform/android/android_surface_vulkan_impeller.cc
2148+
FILE: ../../../flutter/shell/platform/android/android_surface_vulkan_impeller.h
21472149
FILE: ../../../flutter/shell/platform/android/apk_asset_provider.cc
21482150
FILE: ../../../flutter/shell/platform/android/apk_asset_provider.h
21492151
FILE: ../../../flutter/shell/platform/android/apk_asset_provider_unittests.cc

impeller/renderer/backend/vulkan/context_vk.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "impeller/renderer/backend/vulkan/allocator_vk.h"
2121
#include "impeller/renderer/backend/vulkan/capabilities_vk.h"
2222
#include "impeller/renderer/backend/vulkan/command_buffer_vk.h"
23+
#include "impeller/renderer/backend/vulkan/formats_vk.h"
2324
#include "impeller/renderer/backend/vulkan/surface_producer_vk.h"
2425
#include "impeller/renderer/backend/vulkan/swapchain_details_vk.h"
2526
#include "impeller/renderer/backend/vulkan/vk.h"
@@ -563,6 +564,7 @@ void ContextVK::SetupSwapchain(vk::UniqueSurfaceKHR surface) {
563564
if (!swapchain_details) {
564565
return;
565566
}
567+
surface_format_ = swapchain_details->PickSurfaceFormat().format;
566568
swapchain_ = SwapchainVK::Create(*device_, *surface_, *swapchain_details);
567569
auto weak_this = weak_from_this();
568570
surface_producer_ = SurfaceProducerVK::Create(
@@ -582,4 +584,8 @@ std::shared_ptr<DescriptorPoolVK> ContextVK::GetDescriptorPool() const {
582584
return descriptor_pool_;
583585
}
584586

587+
PixelFormat ContextVK::GetColorAttachmentPixelFormat() const {
588+
return ToPixelFormat(surface_format_);
589+
}
590+
585591
} // namespace impeller

impeller/renderer/backend/vulkan/context_vk.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "impeller/renderer/backend/vulkan/swapchain_vk.h"
2020
#include "impeller/renderer/backend/vulkan/vk.h"
2121
#include "impeller/renderer/context.h"
22+
#include "impeller/renderer/formats.h"
2223

2324
namespace impeller {
2425

@@ -85,6 +86,7 @@ class ContextVK final : public Context, public BackendCast<ContextVK, Context> {
8586
vk::Queue transfer_queue_;
8687
vk::Queue present_queue_;
8788
vk::UniqueSurfaceKHR surface_;
89+
vk::Format surface_format_;
8890
std::unique_ptr<SwapchainVK> swapchain_;
8991
std::unique_ptr<CommandPoolVK> graphics_command_pool_;
9092
std::unique_ptr<SurfaceProducerVK> surface_producer_;
@@ -114,6 +116,9 @@ class ContextVK final : public Context, public BackendCast<ContextVK, Context> {
114116
// |Context|
115117
std::shared_ptr<CommandBuffer> CreateCommandBuffer() const override;
116118

119+
// |Context|
120+
PixelFormat GetColorAttachmentPixelFormat() const override;
121+
117122
// |Context|
118123
std::shared_ptr<WorkQueue> GetWorkQueue() const override;
119124

impeller/renderer/backend/vulkan/pipeline_library_vk.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ std::optional<vk::UniqueRenderPass> PipelineLibraryVK::CreateRenderPass(
147147
std::vector<vk::AttachmentDescription> render_pass_attachments;
148148
const auto sample_count = desc.GetSampleCount();
149149
// Set the color attachment.
150+
const auto& format = desc.GetColorAttachmentDescriptor(0)->format;
150151
render_pass_attachments.push_back(CreatePlaceholderAttachmentDescription(
151-
vk::Format::eB8G8R8A8Unorm, sample_count, true));
152+
ToVKImageFormat(format), sample_count, true));
152153

153154
std::vector<vk::AttachmentReference> color_attachment_references;
154155
std::vector<vk::AttachmentReference> resolve_attachment_references;

impeller/renderer/backend/vulkan/surface_producer_vk.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ std::unique_ptr<Surface> SurfaceProducerVK::AcquireSurface(
6565
return nullptr;
6666
}
6767

68-
if (acuire_image_res == vk::Result::eSuboptimalKHR) {
69-
VALIDATION_LOG << "Suboptimal image acquired.";
70-
}
71-
7268
SurfaceVK::SwapCallback swap_callback = [this, current_frame, image_index]() {
7369
return Present(current_frame, image_index);
7470
};

impeller/renderer/context.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ std::shared_ptr<GPUTracer> Context::GetGPUTracer() const {
1818
return nullptr;
1919
}
2020

21+
PixelFormat Context::GetColorAttachmentPixelFormat() const {
22+
return PixelFormat::kDefaultColor;
23+
}
24+
2125
} // namespace impeller

impeller/renderer/context.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string>
99

1010
#include "flutter/fml/macros.h"
11+
#include "impeller/renderer/formats.h"
1112

1213
namespace impeller {
1314

@@ -45,6 +46,8 @@ class Context : public std::enable_shared_from_this<Context> {
4546
///
4647
virtual std::shared_ptr<GPUTracer> GetGPUTracer() const;
4748

49+
virtual PixelFormat GetColorAttachmentPixelFormat() const;
50+
4851
virtual bool HasThreadingRestrictions() const;
4952

5053
virtual bool SupportsOffscreenMSAA() const = 0;

impeller/renderer/pipeline_builder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ struct PipelineBuilder {
117117
// Configure the sole color attachments pixel format. This is by
118118
// convention.
119119
ColorAttachmentDescriptor color0;
120-
color0.format = PixelFormat::kDefaultColor;
120+
color0.format = context.GetColorAttachmentPixelFormat();
121121
color0.blending_enabled = true;
122-
desc.SetColorAttachmentDescriptor(0u, std::move(color0));
122+
desc.SetColorAttachmentDescriptor(0u, color0);
123123
}
124124

125125
// Setup default stencil buffer descriptions.

shell/platform/android/BUILD.gn

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import("//flutter/shell/version/version.gni")
1313
shell_gpu_configuration("android_gpu_configuration") {
1414
enable_software = true
1515
enable_gl = true
16-
enable_vulkan = false
16+
enable_vulkan = true
1717
enable_metal = false
1818
}
1919

@@ -85,6 +85,8 @@ source_set("flutter_shell_native_src") {
8585
"android_surface_gl_skia.h",
8686
"android_surface_software.cc",
8787
"android_surface_software.h",
88+
"android_surface_vulkan_impeller.cc",
89+
"android_surface_vulkan_impeller.h",
8890
"apk_asset_provider.cc",
8991
"apk_asset_provider.h",
9092
"flutter_main.cc",
@@ -123,6 +125,7 @@ source_set("flutter_shell_native_src") {
123125
"//flutter/shell/platform/android/platform_view_android_delegate",
124126
"//flutter/shell/platform/android/surface",
125127
"//flutter/shell/platform/android/surface:native_window",
128+
"//flutter/vulkan",
126129
"//third_party/skia",
127130
]
128131

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 "flutter/shell/platform/android/android_surface_vulkan_impeller.h"
6+
7+
#include <memory>
8+
#include <utility>
9+
10+
#include "flutter/fml/concurrent_message_loop.h"
11+
#include "flutter/fml/logging.h"
12+
#include "flutter/fml/memory/ref_ptr.h"
13+
#include "flutter/impeller/renderer/backend/vulkan/context_vk.h"
14+
#include "flutter/shell/gpu/gpu_surface_vulkan_impeller.h"
15+
#include "flutter/vulkan/vulkan_native_surface_android.h"
16+
#include "impeller/entity/vk/entity_shaders_vk.h"
17+
18+
namespace flutter {
19+
20+
std::shared_ptr<impeller::Context> CreateImpellerContext(
21+
const fml::RefPtr<vulkan::VulkanProcTable>& proc_table,
22+
const std::shared_ptr<fml::ConcurrentMessageLoop>& concurrent_loop) {
23+
std::vector<std::shared_ptr<fml::Mapping>> shader_mappings = {
24+
std::make_shared<fml::NonOwnedMapping>(impeller_entity_shaders_vk_data,
25+
impeller_entity_shaders_vk_length),
26+
};
27+
28+
PFN_vkGetInstanceProcAddr instance_proc_addr =
29+
proc_table->NativeGetInstanceProcAddr();
30+
31+
auto context =
32+
impeller::ContextVK::Create(instance_proc_addr, //
33+
shader_mappings, //
34+
nullptr, //
35+
concurrent_loop->GetTaskRunner(), //
36+
"Android Impeller Vulkan Lib" //
37+
);
38+
39+
return context;
40+
}
41+
42+
AndroidSurfaceVulkanImpeller::AndroidSurfaceVulkanImpeller(
43+
const std::shared_ptr<AndroidContext>& android_context,
44+
const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade)
45+
: AndroidSurface(android_context),
46+
proc_table_(fml::MakeRefCounted<vulkan::VulkanProcTable>()),
47+
workers_(fml::ConcurrentMessageLoop::Create()) {
48+
impeller_context_ = CreateImpellerContext(proc_table_, workers_);
49+
is_valid_ =
50+
proc_table_->HasAcquiredMandatoryProcAddresses() && impeller_context_;
51+
}
52+
53+
AndroidSurfaceVulkanImpeller::~AndroidSurfaceVulkanImpeller() = default;
54+
55+
bool AndroidSurfaceVulkanImpeller::IsValid() const {
56+
return is_valid_;
57+
}
58+
59+
void AndroidSurfaceVulkanImpeller::TeardownOnScreenContext() {
60+
// Nothing to do.
61+
}
62+
63+
std::unique_ptr<Surface> AndroidSurfaceVulkanImpeller::CreateGPUSurface(
64+
GrDirectContext* gr_context) {
65+
if (!IsValid()) {
66+
return nullptr;
67+
}
68+
69+
if (!native_window_ || !native_window_->IsValid()) {
70+
return nullptr;
71+
}
72+
73+
std::unique_ptr<GPUSurfaceVulkanImpeller> gpu_surface =
74+
std::make_unique<GPUSurfaceVulkanImpeller>(impeller_context_);
75+
76+
if (!gpu_surface->IsValid()) {
77+
return nullptr;
78+
}
79+
80+
return gpu_surface;
81+
}
82+
83+
bool AndroidSurfaceVulkanImpeller::OnScreenSurfaceResize(const SkISize& size) {
84+
return true;
85+
}
86+
87+
bool AndroidSurfaceVulkanImpeller::ResourceContextMakeCurrent() {
88+
FML_DLOG(ERROR) << "The vulkan backend does not support resource contexts.";
89+
return false;
90+
}
91+
92+
bool AndroidSurfaceVulkanImpeller::ResourceContextClearCurrent() {
93+
FML_DLOG(ERROR) << "The vulkan backend does not support resource contexts.";
94+
return false;
95+
}
96+
97+
bool AndroidSurfaceVulkanImpeller::SetNativeWindow(
98+
fml::RefPtr<AndroidNativeWindow> window) {
99+
native_window_ = std::move(window);
100+
bool success = native_window_ && native_window_->IsValid();
101+
102+
if (success) {
103+
auto& context_vk = impeller::ContextVK::Cast(*impeller_context_);
104+
auto surface = context_vk.CreateAndroidSurface(native_window_->handle());
105+
106+
if (!surface) {
107+
FML_LOG(ERROR) << "Could not create a vulkan surface.";
108+
return false;
109+
}
110+
111+
context_vk.SetupSwapchain(std::move(surface));
112+
return true;
113+
}
114+
115+
native_window_ = nullptr;
116+
return false;
117+
}
118+
119+
} // namespace flutter
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
#pragma once
6+
7+
#include "flutter/fml/concurrent_message_loop.h"
8+
#include "flutter/fml/macros.h"
9+
#include "flutter/impeller/renderer/context.h"
10+
#include "flutter/shell/platform/android/surface/android_native_window.h"
11+
#include "flutter/shell/platform/android/surface/android_surface.h"
12+
#include "flutter/vulkan/procs/vulkan_proc_table.h"
13+
14+
namespace flutter {
15+
16+
class AndroidSurfaceVulkanImpeller : public AndroidSurface {
17+
public:
18+
AndroidSurfaceVulkanImpeller(
19+
const std::shared_ptr<AndroidContext>& android_context,
20+
const std::shared_ptr<PlatformViewAndroidJNI>& jni_facade);
21+
22+
~AndroidSurfaceVulkanImpeller() override;
23+
24+
// |AndroidSurface|
25+
bool IsValid() const override;
26+
27+
// |AndroidSurface|
28+
std::unique_ptr<Surface> CreateGPUSurface(
29+
GrDirectContext* gr_context) override;
30+
31+
// |AndroidSurface|
32+
void TeardownOnScreenContext() override;
33+
34+
// |AndroidSurface|
35+
bool OnScreenSurfaceResize(const SkISize& size) override;
36+
37+
// |AndroidSurface|
38+
bool ResourceContextMakeCurrent() override;
39+
40+
// |AndroidSurface|
41+
bool ResourceContextClearCurrent() override;
42+
43+
// |AndroidSurface|
44+
bool SetNativeWindow(fml::RefPtr<AndroidNativeWindow> window) override;
45+
46+
private:
47+
fml::RefPtr<vulkan::VulkanProcTable> proc_table_;
48+
fml::RefPtr<AndroidNativeWindow> native_window_;
49+
std::shared_ptr<fml::ConcurrentMessageLoop> workers_;
50+
std::shared_ptr<impeller::Context> impeller_context_;
51+
bool is_valid_ = false;
52+
53+
FML_DISALLOW_COPY_AND_ASSIGN(AndroidSurfaceVulkanImpeller);
54+
};
55+
56+
} // namespace flutter

shell/platform/android/platform_view_android.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "flutter/shell/platform/android/android_surface_gl_impeller.h"
1717
#include "flutter/shell/platform/android/android_surface_gl_skia.h"
1818
#include "flutter/shell/platform/android/android_surface_software.h"
19+
#include "flutter/shell/platform/android/android_surface_vulkan_impeller.h"
1920
#include "flutter/shell/platform/android/context/android_context.h"
2021
#include "flutter/shell/platform/android/external_view_embedder/external_view_embedder.h"
2122
#include "flutter/shell/platform/android/jni/platform_view_android_jni.h"
@@ -43,8 +44,15 @@ std::unique_ptr<AndroidSurface> AndroidSurfaceFactoryImpl::CreateSurface() {
4344
jni_facade_);
4445
case AndroidRenderingAPI::kOpenGLES:
4546
if (enable_impeller_) {
47+
// TODO(kaushikiska@): Enable this after wiring a preference for Vulkan backend.
48+
#if false
49+
return std::make_unique<AndroidSurfaceVulkanImpeller>(android_context_,
50+
jni_facade_);
51+
52+
#else
4653
return std::make_unique<AndroidSurfaceGLImpeller>(android_context_,
4754
jni_facade_);
55+
#endif
4856
} else {
4957
return std::make_unique<AndroidSurfaceGLSkia>(android_context_,
5058
jni_facade_);

tools/gn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,10 +436,10 @@ def to_gn_args(args):
436436
gn_args['skia_use_metal'] = True
437437
gn_args['shell_enable_metal'] = True
438438

439-
# Enable Vulkan on all platforms except for Android and iOS. This is just
439+
# Enable Vulkan on all platforms except for iOS. This is just
440440
# to save on mobile binary size, as there's no reason the Vulkan embedder
441441
# features can't work on these platforms.
442-
if args.target_os not in ['android', 'ios']:
442+
if args.target_os not in ['ios']:
443443
gn_args['skia_use_vulkan'] = True
444444
gn_args['skia_use_vma'] = False
445445
gn_args['shell_enable_vulkan'] = True

0 commit comments

Comments
 (0)