Flutter iOS Embedder
ios_context.h
Go to the documentation of this file.
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_PLATFORM_DARWIN_IOS_IOS_CONTEXT_H_
6 #define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_CONTEXT_H_
7 
8 #include <memory>
9 
10 #include "flutter/common/graphics/gl_context_switch.h"
11 #include "flutter/common/graphics/msaa_sample_count.h"
12 #include "flutter/common/graphics/texture.h"
13 #include "flutter/fml/concurrent_message_loop.h"
14 #include "flutter/fml/macros.h"
15 #include "flutter/fml/platform/darwin/scoped_nsobject.h"
16 #include "flutter/fml/synchronization/sync_switch.h"
19 #include "third_party/skia/include/gpu/GrDirectContext.h"
20 
21 namespace impeller {
22 class Context;
23 } // namespace impeller
24 
25 namespace flutter {
26 
27 //------------------------------------------------------------------------------
28 /// @brief Manages the lifetime of the on-screen and off-screen rendering
29 /// contexts on iOS. On-screen contexts are used by Flutter for
30 /// rendering into the surface. The lifecycle of this context may be
31 /// tied to the lifecycle of the surface. On the other hand, the
32 /// lifecycle of the off-screen context it tied to that of the
33 /// platform view. This one object used to manage both context
34 /// because GPU handles may need to be shared between the two
35 /// context. To achieve this, context may need references to one
36 /// another at creation time. This one object manages the creation,
37 /// use and collection of both contexts in a client rendering API
38 /// agnostic manner.
39 ///
40 class IOSContext {
41  public:
42  //----------------------------------------------------------------------------
43  /// @brief Create an iOS context object capable of creating the on-screen
44  /// and off-screen GPU context for use by Skia.
45  ///
46  /// In case the engine does not support the specified client
47  /// rendering API, this a `nullptr` may be returned.
48  ///
49  /// @param[in] api A client rendering API supported by the
50  /// engine/platform.
51  /// @param[in] backend A client rendering backend supported by the
52  /// engine/platform.
53  /// @param[in] msaa_samples
54  /// The number of MSAA samples to use. Only supplied to
55  /// Skia, must be either 0, 1, 2, 4, or 8.
56  ///
57  /// @return A valid context on success. `nullptr` on failure.
58  ///
59  static std::unique_ptr<IOSContext> Create(
60  IOSRenderingAPI api,
61  IOSRenderingBackend backend,
62  MsaaSampleCount msaa_samples,
63  const std::shared_ptr<const fml::SyncSwitch>& is_gpu_disabled_sync_switch);
64 
65  //----------------------------------------------------------------------------
66  /// @brief Collects the context object. This must happen on the thread on
67  /// which this object was created.
68  ///
69  virtual ~IOSContext();
70 
71  //----------------------------------------------------------------------------
72  /// @brief Get the rendering backend used by this context.
73  ///
74  /// @return The rendering backend.
75  ///
76  virtual IOSRenderingBackend GetBackend() const;
77 
78  //----------------------------------------------------------------------------
79  /// @brief Create a resource context for use on the IO task runner. This
80  /// resource context is used by Skia to upload texture to
81  /// asynchronously and collect resources that are no longer needed
82  /// on the render task runner.
83  ///
84  /// @attention Client rendering APIs for which a GrDirectContext cannot be realized
85  /// (software rendering), this method will always return null.
86  ///
87  /// @return A non-null Skia context on success. `nullptr` on failure.
88  ///
89  virtual sk_sp<GrDirectContext> CreateResourceContext() = 0;
90 
91  //----------------------------------------------------------------------------
92  /// @brief When using client rendering APIs whose contexts need to be
93  /// bound to a specific thread, the engine will call this method
94  /// to give the on-screen context a chance to bind to the current
95  /// thread.
96  ///
97  /// @attention Client rendering APIs that have no-concept of thread local
98  /// bindings (anything that is not OpenGL) will always return
99  /// `true`.
100  ///
101  /// @attention Client rendering APIs for which a GrDirectContext cannot be created
102  /// (software rendering) will always return `false`.
103  ///
104  /// @attention This binds the on-screen context to the current thread. To
105  /// bind the off-screen context to the thread, use the
106  /// `ResoruceMakeCurrent` method instead.
107  ///
108  /// @attention Only one context may be bound to a thread at any given time.
109  /// Making a binding on a thread, clears the old binding.
110  ///
111  /// @return A GLContextResult that represents the result of the method.
112  /// The GetResult() returns a bool that indicates If the on-screen context could be
113  /// bound to the current
114  /// thread.
115  ///
116  virtual std::unique_ptr<GLContextResult> MakeCurrent() = 0;
117 
118  //----------------------------------------------------------------------------
119  /// @brief Creates an external texture proxy of the appropriate client
120  /// rendering API.
121  ///
122  /// @param[in] texture_id The texture identifier
123  /// @param[in] texture The texture
124  ///
125  /// @return The texture proxy if the rendering backend supports embedder
126  /// provided external textures.
127  ///
128  virtual std::unique_ptr<Texture> CreateExternalTexture(
129  int64_t texture_id,
130  fml::scoped_nsobject<NSObject<FlutterTexture>> texture) = 0;
131 
132  //----------------------------------------------------------------------------
133  /// @brief Accessor for the Skia context associated with IOSSurfaces and
134  /// the raster thread.
135  /// @details There can be any number of resource contexts but this is the
136  /// one context that will be used by surfaces to draw to the
137  /// screen from the raster thread.
138  /// @returns `nullptr` on failure.
139  /// @attention The software context doesn't have a Skia context, so this
140  /// value will be nullptr.
141  /// @see For contexts which are used for offscreen work like loading
142  /// textures see IOSContext::CreateResourceContext.
143  ///
144  virtual sk_sp<GrDirectContext> GetMainContext() const = 0;
145 
146  virtual std::shared_ptr<impeller::Context> GetImpellerContext() const;
147 
148  MsaaSampleCount GetMsaaSampleCount() const { return msaa_samples_; }
149 
150  protected:
151  explicit IOSContext(MsaaSampleCount msaa_samples);
152 
153  private:
154  MsaaSampleCount msaa_samples_ = MsaaSampleCount::kNone;
155  FML_DISALLOW_COPY_AND_ASSIGN(IOSContext);
156 };
157 
158 } // namespace flutter
159 
160 #endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_CONTEXT_H_
flutter::IOSContext::~IOSContext
virtual ~IOSContext()
Collects the context object. This must happen on the thread on which this object was created.
FlutterTexture.h
flutter::IOSContext::GetMsaaSampleCount
MsaaSampleCount GetMsaaSampleCount() const
Definition: ios_context.h:148
flutter::IOSContext::IOSContext
IOSContext(MsaaSampleCount msaa_samples)
Definition: ios_context.mm:18
flutter::IOSContext::Create
static std::unique_ptr< IOSContext > Create(IOSRenderingAPI api, IOSRenderingBackend backend, MsaaSampleCount msaa_samples, const std::shared_ptr< const fml::SyncSwitch > &is_gpu_disabled_sync_switch)
Create an iOS context object capable of creating the on-screen and off-screen GPU context for use by ...
Definition: ios_context.mm:22
flutter::IOSContext
Manages the lifetime of the on-screen and off-screen rendering contexts on iOS. On-screen contexts ar...
Definition: ios_context.h:40
flutter::IOSRenderingBackend
IOSRenderingBackend
Definition: rendering_api_selection.h:19
flutter
Definition: accessibility_bridge.h:28
flutter::IOSRenderingAPI
IOSRenderingAPI
Definition: rendering_api_selection.h:14
flutter::IOSContext::MakeCurrent
virtual std::unique_ptr< GLContextResult > MakeCurrent()=0
When using client rendering APIs whose contexts need to be bound to a specific thread,...
flutter::IOSContext::GetBackend
virtual IOSRenderingBackend GetBackend() const
Get the rendering backend used by this context.
Definition: ios_context.mm:51
flutter::IOSContext::GetMainContext
virtual sk_sp< GrDirectContext > GetMainContext() const =0
Accessor for the Skia context associated with IOSSurfaces and the raster thread.
rendering_api_selection.h
flutter::IOSContext::CreateResourceContext
virtual sk_sp< GrDirectContext > CreateResourceContext()=0
Create a resource context for use on the IO task runner. This resource context is used by Skia to upl...
texture_id
int64_t texture_id
Definition: texture_registrar_unittests.cc:24
impeller
Definition: ios_context.h:21
flutter::IOSContext::CreateExternalTexture
virtual std::unique_ptr< Texture > CreateExternalTexture(int64_t texture_id, fml::scoped_nsobject< NSObject< FlutterTexture >> texture)=0
Creates an external texture proxy of the appropriate client rendering API.
flutter::IOSContext::GetImpellerContext
virtual std::shared_ptr< impeller::Context > GetImpellerContext() const
Definition: ios_context.mm:55