|
| 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_GL_CONTEXT_SWITCH_MANAGER_H_ |
| 6 | +#define FLUTTER_SHELL_COMMON_GL_CONTEXT_SWITCH_MANAGER_H_ |
| 7 | + |
| 8 | +#include <memory> |
| 9 | +#include "flutter/fml/macros.h" |
| 10 | + |
| 11 | +namespace flutter { |
| 12 | + |
| 13 | +//------------------------------------------------------------------------------ |
| 14 | +/// Manages `RendererContextSwitch`. |
| 15 | +/// |
| 16 | +/// Should be subclassed for platforms that uses GL and requires context |
| 17 | +/// switching. Always use `MakeCurrent` and `ResourceMakeCurrent` in the |
| 18 | +/// `RendererContextSwitchManager` to set gl contexts. |
| 19 | +/// |
| 20 | +class RendererContextSwitchManager { |
| 21 | + public: |
| 22 | + //------------------------------------------------------------------------------ |
| 23 | + /// Switches the gl context to the flutter's contexts. |
| 24 | + /// |
| 25 | + /// Should be subclassed for each platform embedder that uses GL. |
| 26 | + /// In construction, it should set the current context to a flutter's context |
| 27 | + /// In destruction, it should rest the current context. |
| 28 | + /// |
| 29 | + class RendererContextSwitch { |
| 30 | + public: |
| 31 | + RendererContextSwitch(); |
| 32 | + |
| 33 | + virtual ~RendererContextSwitch(); |
| 34 | + |
| 35 | + virtual bool GetSwitchResult() = 0; |
| 36 | + |
| 37 | + FML_DISALLOW_COPY_AND_ASSIGN(RendererContextSwitch); |
| 38 | + }; |
| 39 | + |
| 40 | + RendererContextSwitchManager(); |
| 41 | + ~RendererContextSwitchManager(); |
| 42 | + |
| 43 | + //---------------------------------------------------------------------------- |
| 44 | + /// @brief Creates a shell instance using the provided settings. The |
| 45 | + /// callbacks to create the various shell subcomponents will be |
| 46 | + /// called on the appropriate threads before this method returns. |
| 47 | + /// If this is the first instance of a shell in the process, this |
| 48 | + /// call also bootstraps the Dart VM. |
| 49 | + /// |
| 50 | + /// @param[in] task_runners The task runners |
| 51 | + /// @param[in] settings The settings |
| 52 | + /// @param[in] on_create_platform_view The callback that must return a |
| 53 | + /// platform view. This will be called on |
| 54 | + /// the platform task runner before this |
| 55 | + /// method returns. |
| 56 | + /// @param[in] on_create_rasterizer That callback that must provide a |
| 57 | + /// valid rasterizer. This will be called |
| 58 | + /// on the render task runner before this |
| 59 | + /// method returns. |
| 60 | + /// |
| 61 | + /// @return A full initialized shell if the settings and callbacks are |
| 62 | + /// valid. The root isolate has been created but not yet launched. |
| 63 | + /// It may be launched by obtaining the engine weak pointer and |
| 64 | + /// posting a task onto the UI task runner with a valid run |
| 65 | + /// configuration to run the isolate. The embedder must always |
| 66 | + /// check the validity of the shell (using the IsSetup call) |
| 67 | + /// immediately after getting a pointer to it. |
| 68 | + /// |
| 69 | + |
| 70 | + //---------------------------------------------------------------------------- |
| 71 | + /// @brief Make the flutter's context as current context. |
| 72 | + /// |
| 73 | + /// @return A `RendererContextSwitch` with `GetSwitchResult` returning |
| 74 | + /// true if the setting process is succesful. |
| 75 | + virtual std::unique_ptr<RendererContextSwitch> MakeCurrent() = 0; |
| 76 | + |
| 77 | + //---------------------------------------------------------------------------- |
| 78 | + /// @brief Make the flutter's resources context as current context. |
| 79 | + /// |
| 80 | + /// @return A `RendererContextSwitch` with `GetSwitchResult` returning |
| 81 | + /// true if the setting process is succesful. |
| 82 | + virtual std::unique_ptr<RendererContextSwitch> ResourceMakeCurrent() = 0; |
| 83 | + |
| 84 | + //------------------------------------------------------------------------------ |
| 85 | + /// A representation of a `RendererContextSwitch` that doesn't require actual |
| 86 | + /// context switching. |
| 87 | + /// |
| 88 | + class RendererContextSwitchPureResult final : public RendererContextSwitch { |
| 89 | + public: |
| 90 | + // Constructor that creates an `RendererContextSwitchPureResult`. |
| 91 | + // The `GetSwitchResult` will return the same value as `switch_result`. |
| 92 | + |
| 93 | + //---------------------------------------------------------------------------- |
| 94 | + /// @brief Constructs a `RendererContextSwitchPureResult`. |
| 95 | + /// |
| 96 | + /// @param[in] switch_result the switch result that will be returned |
| 97 | + /// in `GetSwitchResult()` |
| 98 | + /// |
| 99 | + RendererContextSwitchPureResult(bool switch_result); |
| 100 | + |
| 101 | + ~RendererContextSwitchPureResult(); |
| 102 | + |
| 103 | + bool GetSwitchResult() override; |
| 104 | + |
| 105 | + private: |
| 106 | + bool switch_result_; |
| 107 | + |
| 108 | + FML_DISALLOW_COPY_AND_ASSIGN(RendererContextSwitchPureResult); |
| 109 | + }; |
| 110 | + |
| 111 | + FML_DISALLOW_COPY_AND_ASSIGN(RendererContextSwitchManager); |
| 112 | +}; |
| 113 | + |
| 114 | +} // namespace flutter |
| 115 | + |
| 116 | +#endif |
0 commit comments