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