|
| 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 |
0 commit comments