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 <lib/async/cpp/time.h> 8 #include <lib/async/default.h> 9 #include <lib/syslog/global.h> 10 11 #include "flutter/flow/scene_update_context.h" 12 #include "flutter/fml/macros.h" 13 #include "flutter/vulkan/vulkan_application.h" 14 #include "flutter/vulkan/vulkan_device.h" 15 #include "flutter/vulkan/vulkan_proc_table.h" 16 #include "flutter/vulkan/vulkan_provider.h" 17 #include "lib/ui/scenic/cpp/resources.h" 18 #include "lib/ui/scenic/cpp/session.h" 19 #include "logging.h" 20 #include "vulkan_surface.h" 21 #include "vulkan_surface_pool.h" 22 23 namespace flutter_runner { 24 25 class VulkanSurfaceProducer final 26 : public flutter::SceneUpdateContext::SurfaceProducer, 27 public vulkan::VulkanProvider { 28 public: 29 VulkanSurfaceProducer(scenic::Session* scenic_session); 30 31 ~VulkanSurfaceProducer(); 32 IsValid()33 bool IsValid() const { return valid_; } 34 35 // |flutter::SceneUpdateContext::SurfaceProducer| 36 std::unique_ptr<flutter::SceneUpdateContext::SurfaceProducerSurface> 37 ProduceSurface(const SkISize& size, 38 const flutter::LayerRasterCacheKey& layer_key, 39 std::unique_ptr<scenic::EntityNode> entity_node) override; 40 41 // |flutter::SceneUpdateContext::SurfaceProducer| 42 void SubmitSurface( 43 std::unique_ptr<flutter::SceneUpdateContext::SurfaceProducerSurface> 44 surface) override; 45 46 // |flutter::SceneUpdateContext::HasRetainedNode| HasRetainedNode(const flutter::LayerRasterCacheKey & key)47 bool HasRetainedNode(const flutter::LayerRasterCacheKey& key) const override { 48 return surface_pool_->HasRetainedNode(key); 49 } 50 51 // |flutter::SceneUpdateContext::GetRetainedNode| GetRetainedNode(const flutter::LayerRasterCacheKey & key)52 const scenic::EntityNode& GetRetainedNode( 53 const flutter::LayerRasterCacheKey& key) override { 54 return surface_pool_->GetRetainedNode(key); 55 } 56 57 void OnSurfacesPresented( 58 std::vector< 59 std::unique_ptr<flutter::SceneUpdateContext::SurfaceProducerSurface>> 60 surfaces); 61 OnSessionSizeChangeHint(float width_change_factor,float height_change_factor)62 void OnSessionSizeChangeHint(float width_change_factor, 63 float height_change_factor) { 64 FX_LOGF(INFO, LOG_TAG, 65 "VulkanSurfaceProducer:OnSessionSizeChangeHint %f, %f", 66 width_change_factor, height_change_factor); 67 } 68 69 private: 70 // VulkanProvider vk()71 const vulkan::VulkanProcTable& vk() override { return *vk_.get(); } vk_device()72 const vulkan::VulkanHandle<VkDevice>& vk_device() override { 73 return logical_device_->GetHandle(); 74 } 75 76 bool TransitionSurfacesToExternal( 77 const std::vector< 78 std::unique_ptr<flutter::SceneUpdateContext::SurfaceProducerSurface>>& 79 surfaces); 80 81 // Note: the order here is very important. The proctable must be destroyed 82 // last because it contains the function pointers for VkDestroyDevice and 83 // VkDestroyInstance. 84 fml::RefPtr<vulkan::VulkanProcTable> vk_; 85 std::unique_ptr<vulkan::VulkanApplication> application_; 86 std::unique_ptr<vulkan::VulkanDevice> logical_device_; 87 sk_sp<GrContext> context_; 88 std::unique_ptr<VulkanSurfacePool> surface_pool_; 89 bool valid_ = false; 90 91 // Keep track of the last time we produced a surface. This is used to 92 // determine whether it is safe to shrink |surface_pool_| or not. 93 zx::time last_produce_time_ = async::Now(async_get_default_dispatcher()); 94 fml::WeakPtrFactory<VulkanSurfaceProducer> weak_factory_{this}; 95 96 bool Initialize(scenic::Session* scenic_session); 97 98 // Disallow copy and assignment. 99 VulkanSurfaceProducer(const VulkanSurfaceProducer&) = delete; 100 VulkanSurfaceProducer& operator=(const VulkanSurfaceProducer&) = delete; 101 }; 102 103 } // namespace flutter_runner 104