| 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/common/shell_io_manager.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "flutter/fml/message_loop.h" |
| 10 | #include "flutter/shell/common/context_options.h" |
| 11 | #include "third_party/skia/include/gpu/gl/GrGLInterface.h" |
| 12 | |
| 13 | namespace flutter { |
| 14 | |
| 15 | sk_sp<GrDirectContext> ShellIOManager::CreateCompatibleResourceLoadingContext( |
| 16 | GrBackend backend, |
| 17 | const sk_sp<const GrGLInterface>& gl_interface) { |
| 18 | #if SK_GL |
| 19 | if (backend != GrBackend::kOpenGL_GrBackend) { |
| 20 | return nullptr; |
| 21 | } |
| 22 | |
| 23 | const auto options = MakeDefaultContextOptions(type: ContextType::kResource); |
| 24 | |
| 25 | if (auto context = GrDirectContext::MakeGL(gl_interface, options)) { |
| 26 | // Do not cache textures created by the image decoder. These textures |
| 27 | // should be deleted when they are no longer referenced by an SkImage. |
| 28 | context->setResourceCacheLimit(0); |
| 29 | return context; |
| 30 | } |
| 31 | #endif // SK_GL |
| 32 | |
| 33 | return nullptr; |
| 34 | } |
| 35 | |
| 36 | ShellIOManager::ShellIOManager( |
| 37 | sk_sp<GrDirectContext> resource_context, |
| 38 | std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch, |
| 39 | fml::RefPtr<fml::TaskRunner> unref_queue_task_runner, |
| 40 | std::shared_ptr<impeller::Context> impeller_context, |
| 41 | fml::TimeDelta unref_queue_drain_delay) |
| 42 | : resource_context_(std::move(resource_context)), |
| 43 | resource_context_weak_factory_( |
| 44 | resource_context_ |
| 45 | ? std::make_unique<fml::WeakPtrFactory<GrDirectContext>>( |
| 46 | args: resource_context_.get()) |
| 47 | : nullptr), |
| 48 | unref_queue_(fml::MakeRefCounted<flutter::SkiaUnrefQueue>( |
| 49 | args: std::move(unref_queue_task_runner), |
| 50 | args&: unref_queue_drain_delay, |
| 51 | args&: resource_context_, |
| 52 | /*drain_immediate=*/args: !!impeller_context)), |
| 53 | is_gpu_disabled_sync_switch_(std::move(is_gpu_disabled_sync_switch)), |
| 54 | impeller_context_(std::move(impeller_context)), |
| 55 | weak_factory_(this) { |
| 56 | if (!resource_context_) { |
| 57 | #ifndef OS_FUCHSIA |
| 58 | FML_DLOG(WARNING) << "The IO manager was initialized without a resource " |
| 59 | "context. Async texture uploads will be disabled. " |
| 60 | "Expect performance degradation." ; |
| 61 | #endif // OS_FUCHSIA |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | ShellIOManager::~ShellIOManager() { |
| 66 | // Last chance to drain the IO queue as the platform side reference to the |
| 67 | // underlying OpenGL context may be going away. |
| 68 | is_gpu_disabled_sync_switch_->Execute( |
| 69 | handlers: fml::SyncSwitch::Handlers().SetIfFalse([&] { unref_queue_->Drain(); })); |
| 70 | } |
| 71 | |
| 72 | void ShellIOManager::NotifyResourceContextAvailable( |
| 73 | sk_sp<GrDirectContext> resource_context) { |
| 74 | // The resource context needs to survive as long as we have Dart objects |
| 75 | // referencing. We shouldn't ever need to replace it if we have one - unless |
| 76 | // we've somehow shut down the Dart VM and started a new one fresh. |
| 77 | if (!resource_context_) { |
| 78 | UpdateResourceContext(resource_context: std::move(resource_context)); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void ShellIOManager::UpdateResourceContext( |
| 83 | sk_sp<GrDirectContext> resource_context) { |
| 84 | resource_context_ = std::move(resource_context); |
| 85 | resource_context_weak_factory_ = |
| 86 | resource_context_ |
| 87 | ? std::make_unique<fml::WeakPtrFactory<GrDirectContext>>( |
| 88 | args: resource_context_.get()) |
| 89 | : nullptr; |
| 90 | unref_queue_->UpdateResourceContext(context: resource_context_); |
| 91 | } |
| 92 | |
| 93 | fml::WeakPtr<ShellIOManager> ShellIOManager::GetWeakPtr() { |
| 94 | return weak_factory_.GetWeakPtr(); |
| 95 | } |
| 96 | |
| 97 | // |IOManager| |
| 98 | fml::WeakPtr<GrDirectContext> ShellIOManager::GetResourceContext() const { |
| 99 | return resource_context_weak_factory_ |
| 100 | ? resource_context_weak_factory_->GetWeakPtr() |
| 101 | : fml::WeakPtr<GrDirectContext>(); |
| 102 | } |
| 103 | |
| 104 | // |IOManager| |
| 105 | fml::RefPtr<flutter::SkiaUnrefQueue> ShellIOManager::GetSkiaUnrefQueue() const { |
| 106 | return unref_queue_; |
| 107 | } |
| 108 | |
| 109 | // |IOManager| |
| 110 | fml::WeakPtr<IOManager> ShellIOManager::GetWeakIOManager() const { |
| 111 | return weak_factory_.GetWeakPtr(); |
| 112 | } |
| 113 | |
| 114 | // |IOManager| |
| 115 | std::shared_ptr<const fml::SyncSwitch> |
| 116 | ShellIOManager::GetIsGpuDisabledSyncSwitch() { |
| 117 | return is_gpu_disabled_sync_switch_; |
| 118 | } |
| 119 | |
| 120 | // |IOManager| |
| 121 | std::shared_ptr<impeller::Context> ShellIOManager::GetImpellerContext() const { |
| 122 | return impeller_context_; |
| 123 | } |
| 124 | |
| 125 | } // namespace flutter |
| 126 | |