Flutter Engine
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_RESOURCE_CONTEXT_H_
6 #define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_RESOURCE_CONTEXT_H_
7 
8 #include <memory>
9 
10 #include "flutter/flow/gl_context_switch.h"
11 #include "flutter/flow/texture.h"
12 #include "flutter/fml/macros.h"
13 #include "flutter/fml/platform/darwin/scoped_nsobject.h"
14 #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h"
15 #import "flutter/shell/platform/darwin/ios/rendering_api_selection.h"
16 #include "third_party/skia/include/gpu/GrDirectContext.h"
17 
18 namespace flutter {
19 
20 //------------------------------------------------------------------------------
21 /// @brief Manages the lifetime of the on-screen and off-screen rendering
22 /// contexts on iOS. On-screen contexts are used by Flutter for
23 /// rendering into the surface. The lifecycle of this context may be
24 /// tied to the lifecycle of the surface. On the other hand, the
25 /// lifecycle of the the off-screen context it tied to that of the
26 /// platform view. This one object used to manage both context
27 /// because GPU handles may need to be shared between the two
28 /// context. To achieve this, context may need references to one
29 /// another at creation time. This one object manages the creation,
30 /// use and collection of both contexts in a client rendering API
31 /// agnostic manner.
32 ///
33 class IOSContext {
34  public:
35  //----------------------------------------------------------------------------
36  /// @brief Create an iOS context object capable of creating the on-screen
37  /// and off-screen GPU context for use by Skia.
38  ///
39  /// In case the engine does not support the specified client
40  /// rendering API, this a `nullptr` may be returned.
41  ///
42  /// @param[in] rendering_api A client rendering API supported by the
43  /// engine/platform.
44  ///
45  /// @return A valid context on success. `nullptr` on failure.
46  ///
47  static std::unique_ptr<IOSContext> Create(IOSRenderingAPI rendering_api);
48 
49  //----------------------------------------------------------------------------
50  /// @brief Collects the context object. This must happen on the thread on
51  /// which this object was created.
52  ///
53  virtual ~IOSContext();
54 
55  //----------------------------------------------------------------------------
56  /// @brief Create a resource context for use on the IO task runner. This
57  /// resource context is used by Skia to upload texture to
58  /// asynchronously and collect resources that are no longer needed
59  /// on the render task runner.
60  ///
61  /// @attention Client rendering APIs for which a GrDirectContext cannot be realized
62  /// (software rendering), this method will always return null.
63  ///
64  /// @return A non-null Skia context on success. `nullptr` on failure.
65  ///
66  virtual sk_sp<GrDirectContext> CreateResourceContext() = 0;
67 
68  //----------------------------------------------------------------------------
69  /// @brief When using client rendering APIs whose contexts need to be
70  /// bound to a specific thread, the engine will call this method
71  /// to give the on-screen context a chance to bind to the current
72  /// thread.
73  ///
74  /// @attention Client rendering APIs that have no-concept of thread local
75  /// bindings (anything that is not OpenGL) will always return
76  /// `true`.
77  ///
78  /// @attention Client rendering APIs for which a GrDirectContext cannot be created
79  /// (software rendering) will always return `false`.
80  ///
81  /// @attention This binds the on-screen context to the current thread. To
82  /// bind the off-screen context to the thread, use the
83  /// `ResoruceMakeCurrent` method instead.
84  ///
85  /// @attention Only one context may be bound to a thread at any given time.
86  /// Making a binding on a thread, clears the old binding.
87  ///
88  /// @return A GLContextResult that represents the result of the method.
89  /// The GetResult() returns a bool that indicates If the on-screen context could be
90  /// bound to the current
91  /// thread.
92  ///
93  virtual std::unique_ptr<GLContextResult> MakeCurrent() = 0;
94 
95  //----------------------------------------------------------------------------
96  /// @brief Creates an external texture proxy of the appropriate client
97  /// rendering API.
98  ///
99  /// @param[in] texture_id The texture identifier
100  /// @param[in] texture The texture
101  ///
102  /// @return The texture proxy if the rendering backend supports embedder
103  /// provided external textures.
104  ///
105  virtual std::unique_ptr<Texture> CreateExternalTexture(
106  int64_t texture_id,
107  fml::scoped_nsobject<NSObject<FlutterTexture>> texture) = 0;
108 
109  protected:
110  IOSContext();
111 
112  private:
113  FML_DISALLOW_COPY_AND_ASSIGN(IOSContext);
114 };
115 
116 } // namespace flutter
117 
118 #endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_IOS_RESOURCE_CONTEXT_H_
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.
virtual std::unique_ptr< GLContextResult > MakeCurrent()=0
When using client rendering APIs whose contexts need to be bound to a specific thread, the engine will call this method to give the on-screen context a chance to bind to the current thread.
static std::unique_ptr< IOSContext > Create(IOSRenderingAPI rendering_api)
Create an iOS context object capable of creating the on-screen and off-screen GPU context for use by ...
Definition: ios_context.mm:21
Manages the lifetime of the on-screen and off-screen rendering contexts on iOS. On-screen contexts ar...
Definition: ios_context.h:33
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...
virtual ~IOSContext()
Collects the context object. This must happen on the thread on which this object was created...