Flutter Impeller
surface_mtl.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_IMPELLER_RENDERER_BACKEND_METAL_SURFACE_MTL_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_SURFACE_MTL_H_
7 
8 #include <QuartzCore/CAMetalLayer.h>
9 #include <memory>
10 
11 #include "impeller/geometry/rect.h"
15 
16 namespace impeller {
17 
18 class SurfaceMTL final : public Surface {
19  public:
20 #pragma GCC diagnostic push
21  // Disable the diagnostic for iOS Simulators. Metal without emulation isn't
22  // available prior to iOS 13 and that's what the simulator headers say when
23  // support for CAMetalLayer begins. CAMetalLayer is available on iOS 8.0 and
24  // above which is well below Flutters support level.
25 #pragma GCC diagnostic ignored "-Wunguarded-availability-new"
26  //----------------------------------------------------------------------------
27  /// @brief Wraps the current drawable of the given Metal layer to create
28  /// a surface Impeller can render to. The surface must be created
29  /// as late as possible and discarded immediately after rendering
30  /// to it.
31  ///
32  /// @param[in] context The context
33  /// @param[in] layer The layer whose current drawable to wrap to create a
34  /// surface.
35  ///
36  /// @return A pointer to the wrapped surface or null.
37  ///
38  static id<CAMetalDrawable> GetMetalDrawableAndValidate(
39  const std::shared_ptr<Context>& context,
40  CAMetalLayer* layer);
41 
42  static std::unique_ptr<SurfaceMTL> MakeFromMetalLayerDrawable(
43  const std::shared_ptr<Context>& context,
44  id<CAMetalDrawable> drawable,
45  const std::shared_ptr<SwapchainTransientsMTL>& transients,
46  std::optional<IRect> clip_rect = std::nullopt);
47 
48  static std::unique_ptr<SurfaceMTL> MakeFromTexture(
49  const std::shared_ptr<Context>& context,
50  id<MTLTexture> texture,
51  const std::shared_ptr<SwapchainTransientsMTL>& transients,
52  std::optional<IRect> clip_rect,
53  id<CAMetalDrawable> drawable = nil);
54 #pragma GCC diagnostic pop
55 
56  // |Surface|
57  ~SurfaceMTL() override;
58 
59  id<MTLDrawable> drawable() const { return drawable_; }
60 
61  // Returns a Rect defining the area of the surface in device pixels
62  IRect coverage() const;
63 
64  /// Mark this surface as presenting with a transaction.
65  ///
66  /// If true, [Present] will block on the scheduling of a command buffer.
67  void PresentWithTransaction(bool present_with_transaction) {
68  present_with_transaction_ = present_with_transaction;
69  }
70 
71  /// @brief Perform the final blit and trigger end of frame workloads.
72  bool PreparePresent() const;
73 
74  // |Surface|
75  bool Present() const override;
76 
77  void SetFrameBoundary(bool frame_boundary) {
78  frame_boundary_ = frame_boundary;
79  }
80 
81  private:
82  std::weak_ptr<Context> context_;
83  std::shared_ptr<Texture> resolve_texture_;
84  id<CAMetalDrawable> drawable_ = nil;
85  std::shared_ptr<Texture> source_texture_;
86  std::shared_ptr<Texture> destination_texture_;
87  bool requires_blit_ = false;
88  std::optional<IRect> clip_rect_;
89  bool frame_boundary_ = false;
90  bool present_with_transaction_ = false;
91  mutable bool prepared_ = false;
92 
93  static bool ShouldPerformPartialRepaint(std::optional<IRect> damage_rect);
94 
95  SurfaceMTL(const std::weak_ptr<Context>& context,
96  const RenderTarget& target,
97  std::shared_ptr<Texture> resolve_texture,
98  id<CAMetalDrawable> drawable,
99  std::shared_ptr<Texture> source_texture,
100  std::shared_ptr<Texture> destination_texture,
101  bool requires_blit,
102  std::optional<IRect> clip_rect);
103 
104  SurfaceMTL(const SurfaceMTL&) = delete;
105 
106  SurfaceMTL& operator=(const SurfaceMTL&) = delete;
107 };
108 
109 } // namespace impeller
110 
111 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_SURFACE_MTL_H_
bool Present() const override
Definition: surface_mtl.mm:258
IRect coverage() const
Definition: surface_mtl.mm:212
static std::unique_ptr< SurfaceMTL > MakeFromMetalLayerDrawable(const std::shared_ptr< Context > &context, id< CAMetalDrawable > drawable, const std::shared_ptr< SwapchainTransientsMTL > &transients, std::optional< IRect > clip_rect=std::nullopt)
Definition: surface_mtl.mm:113
static id< CAMetalDrawable > GetMetalDrawableAndValidate(const std::shared_ptr< Context > &context, CAMetalLayer *layer)
Wraps the current drawable of the given Metal layer to create a surface Impeller can render to....
Definition: surface_mtl.mm:29
void PresentWithTransaction(bool present_with_transaction)
Definition: surface_mtl.h:67
void SetFrameBoundary(bool frame_boundary)
Definition: surface_mtl.h:77
bool PreparePresent() const
Perform the final blit and trigger end of frame workloads.
Definition: surface_mtl.mm:216
~SurfaceMTL() override
static std::unique_ptr< SurfaceMTL > MakeFromTexture(const std::shared_ptr< Context > &context, id< MTLTexture > texture, const std::shared_ptr< SwapchainTransientsMTL > &transients, std::optional< IRect > clip_rect, id< CAMetalDrawable > drawable=nil)
Definition: surface_mtl.mm:122
id< MTLDrawable > drawable() const
Definition: surface_mtl.h:59