| 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 "flutter/fml/macros.h"
12 #include "impeller/geometry/rect.h"
13 #include "impeller/renderer/context.h"
14 #include "impeller/renderer/surface.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 std::optional<IRect> clip_rect = std::nullopt);
46
47 static std::unique_ptr<SurfaceMTL> MakeFromTexture(
48 const std::shared_ptr<Context>& context,
49 id<MTLTexture> texture,
50 std::optional<IRect> clip_rect,
51 id<CAMetalDrawable> drawable = nil);
52 #pragma GCC diagnostic pop
53
54 // |Surface|
55 ~SurfaceMTL() override;
56
57 id<MTLDrawable> drawable() const { return drawable_; }
58
59 // Returns a Rect defining the area of the surface in device pixels
60 IRect coverage() const;
61
62 // |Surface|
63 bool Present() const override;
64
65 private:
66 std::weak_ptr<Context> context_;
67 std::shared_ptr<Texture> resolve_texture_;
68 id<CAMetalDrawable> drawable_ = nil;
69 std::shared_ptr<Texture> source_texture_;
70 std::shared_ptr<Texture> destination_texture_;
71 bool requires_blit_ = false;
72 std::optional<IRect> clip_rect_;
73
74 static bool ShouldPerformPartialRepaint(std::optional<IRect> damage_rect);
75
76 SurfaceMTL(const std::weak_ptr<Context>& context,
77 const RenderTarget& target,
78 std::shared_ptr<Texture> resolve_texture,
79 id<CAMetalDrawable> drawable,
80 std::shared_ptr<Texture> source_texture,
81 std::shared_ptr<Texture> destination_texture,
82 bool requires_blit,
83 std::optional<IRect> clip_rect);
84
85 SurfaceMTL(const SurfaceMTL&) = delete;
86
87 SurfaceMTL& operator=(const SurfaceMTL&) = delete;
88 };
89
90 } // namespace impeller
91
92 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_SURFACE_MTL_H_
impeller::SurfaceMTL
Definition: surface_mtl.h:18
impeller::SurfaceMTL::coverage
IRect coverage() const
Definition: surface_mtl.mm:217
impeller::Surface
Definition: surface.h:18
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:119
impeller::RenderTarget
Definition: render_target.h:49
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:127
impeller::SurfaceMTL::drawable
id< MTLDrawable > drawable() const
Definition: surface_mtl.h:57
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:21
context.h
impeller
Definition: aiks_context.cc:10
impeller::TRect
Definition: rect.h:22
impeller::SurfaceMTL::Present
bool Present() const override
Definition: surface_mtl.mm:222