Flutter Engine
gl_context_switch.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_FLOW_GL_CONTEXT_SWITCH_H_
6 #define FLUTTER_FLOW_GL_CONTEXT_SWITCH_H_
7 
8 #include <functional>
9 #include <memory>
10 #include <vector>
11 
12 #include "flutter/fml/logging.h"
13 #include "flutter/fml/macros.h"
14 
15 namespace flutter {
16 
17 // This interface represents a gl context that can be switched by
18 // |GLContextSwitch|.
19 //
20 // The implementation should wrap a "Context" object inside this class. For
21 // example, in iOS while using a GL rendering surface, the implementation should
22 // wrap an |EAGLContext|.
24  public:
26 
27  virtual ~SwitchableGLContext();
28 
29  // Implement this to set the context wrapped by this |SwitchableGLContext|
30  // object to the current context.
31  virtual bool SetCurrent() = 0;
32 
33  // Implement this to remove the context wrapped by this |SwitchableGLContext|
34  // object from current context;
35  virtual bool RemoveCurrent() = 0;
36 
38 };
39 
40 // Represents the result of setting a gl context.
41 //
42 // This class exists because context results are used in places applies to all
43 // the platforms. On certain platforms(for example lower end iOS devices that
44 // uses gl), a |GLContextSwitch| is required to protect flutter's gl contect
45 // from being polluted by other programs(embedded platform views). A
46 // |GLContextSwitch| is a subclass of |GLContextResult|, which can be returned
47 // on platforms that requires context switching. A |GLContextDefaultResult| is
48 // also a subclass of |GLContextResult|, which can be returned on platforms
49 // that doesn't require context switching.
51  public:
53  virtual ~GLContextResult();
54 
55  //----------------------------------------------------------------------------
56  // Returns true if the gl context is set successfully.
57  bool GetResult();
58 
59  protected:
60  GLContextResult(bool static_result);
61  bool result_;
62 
64 };
65 
66 //------------------------------------------------------------------------------
67 /// The default implementation of |GLContextResult|.
68 ///
69 /// Use this class on platforms that doesn't require gl context switching.
70 /// * See also |GLContextSwitch| if the platform requires gl context switching.
72  public:
73  //----------------------------------------------------------------------------
74  /// Constructs a |GLContextDefaultResult| with a static result.
75  ///
76  /// Used this on platforms that doesn't require gl context switching. (For
77  /// example, metal on iOS)
78  ///
79  /// @param static_result a static value that will be returned from
80  /// |GetResult|
81  GLContextDefaultResult(bool static_result);
82 
83  ~GLContextDefaultResult() override;
84 
86 };
87 
88 //------------------------------------------------------------------------------
89 /// Switches the gl context to the a context that is passed in the
90 /// constructor.
91 ///
92 /// In destruction, it should restore the current context to what was
93 /// before the construction of this switch.
94 class GLContextSwitch final : public GLContextResult {
95  public:
96  //----------------------------------------------------------------------------
97  /// Constructs a |GLContextSwitch|.
98  ///
99  /// @param context The context that is going to be set as the current
100  /// context. The |GLContextSwitch| should not outlive the owner of the gl
101  /// context wrapped inside the `context`.
102  GLContextSwitch(std::unique_ptr<SwitchableGLContext> context);
103 
104  ~GLContextSwitch() override;
105 
106  private:
107  std::unique_ptr<SwitchableGLContext> context_;
108 
110 };
111 
112 } // namespace flutter
113 
114 #endif // FLUTTER_FLOW_GL_CONTEXT_SWITCH_H_
virtual bool SetCurrent()=0
virtual bool RemoveCurrent()=0
FML_DISALLOW_COPY_AND_ASSIGN(SwitchableGLContext)