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#ifndef FLUTTER_SHELL_COMMON_SHELL_IO_MANAGER_H_
6#define FLUTTER_SHELL_COMMON_SHELL_IO_MANAGER_H_
7
8#include <memory>
9
10#include "flutter/flow/skia_gpu_object.h"
11#include "flutter/fml/macros.h"
12#include "flutter/fml/memory/weak_ptr.h"
13#include "flutter/lib/ui/io_manager.h"
14#include "third_party/skia/include/gpu/GrDirectContext.h"
15
16namespace flutter {
17
18class ShellIOManager final : public IOManager {
19 public:
20 // Convenience methods for platforms to create a GrDirectContext used to
21 // supply to the IOManager. The platforms may create the context themselves if
22 // they so desire.
23 static sk_sp<GrDirectContext> CreateCompatibleResourceLoadingContext(
24 GrBackend backend,
25 sk_sp<const GrGLInterface> gl_interface);
26
27 ShellIOManager(sk_sp<GrDirectContext> resource_context,
28 std::shared_ptr<fml::SyncSwitch> is_gpu_disabled_sync_switch,
29 fml::RefPtr<fml::TaskRunner> unref_queue_task_runner);
30
31 ~ShellIOManager() override;
32
33 // This method should be called when a resource_context first becomes
34 // available. It is safe to call multiple times, and will only update
35 // the held resource context if it has not already been set.
36 void NotifyResourceContextAvailable(sk_sp<GrDirectContext> resource_context);
37
38 // This method should be called if you want to force the IOManager to
39 // update its resource context reference. It should not be called
40 // if there are any Dart objects that have a reference to the old
41 // resource context, but may be called if the Dart VM is restarted.
42 void UpdateResourceContext(sk_sp<GrDirectContext> resource_context);
43
44 fml::WeakPtr<ShellIOManager> GetWeakPtr();
45
46 // |IOManager|
47 fml::WeakPtr<IOManager> GetWeakIOManager() const override;
48
49 // |IOManager|
50 fml::WeakPtr<GrDirectContext> GetResourceContext() const override;
51
52 // |IOManager|
53 fml::RefPtr<flutter::SkiaUnrefQueue> GetSkiaUnrefQueue() const override;
54
55 // |IOManager|
56 std::shared_ptr<fml::SyncSwitch> GetIsGpuDisabledSyncSwitch() override;
57
58 private:
59 // Resource context management.
60 sk_sp<GrDirectContext> resource_context_;
61 std::unique_ptr<fml::WeakPtrFactory<GrDirectContext>>
62 resource_context_weak_factory_;
63
64 // Unref queue management.
65 fml::RefPtr<flutter::SkiaUnrefQueue> unref_queue_;
66
67 fml::WeakPtrFactory<ShellIOManager> weak_factory_;
68
69 std::shared_ptr<fml::SyncSwitch> is_gpu_disabled_sync_switch_;
70
71 FML_DISALLOW_COPY_AND_ASSIGN(ShellIOManager);
72};
73
74} // namespace flutter
75
76#endif // FLUTTER_SHELL_COMMON_SHELL_IO_MANAGER_H_
77