Skip to content

Commit

Permalink
Add Metal to the FlutterCompositor struct in the Embedder API (flutte…
Browse files Browse the repository at this point in the history
  • Loading branch information
George Wright authored Apr 23, 2021
1 parent fbdddfc commit 5407bdd
Show file tree
Hide file tree
Showing 19 changed files with 644 additions and 25 deletions.
8 changes: 7 additions & 1 deletion shell/gpu/gpu_surface_metal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace flutter {
class SK_API_AVAILABLE_CA_METAL_LAYER GPUSurfaceMetal : public Surface {
public:
GPUSurfaceMetal(GPUSurfaceMetalDelegate* delegate,
sk_sp<GrDirectContext> context);
sk_sp<GrDirectContext> context,
bool render_to_surface = true);

// |Surface|
~GPUSurfaceMetal();
Expand All @@ -30,6 +31,11 @@ class SK_API_AVAILABLE_CA_METAL_LAYER GPUSurfaceMetal : public Surface {
GrMTLHandle next_drawable_ = nullptr;
sk_sp<GrDirectContext> context_;
GrDirectContext* precompiled_sksl_context_ = nullptr;
// TODO(38466): Refactor GPU surface APIs take into account the fact that an
// external view embedder may want to render to the root surface. This is a
// hack to make avoid allocating resources for the root surface when an
// external view embedder is present.
bool render_to_surface_;

// |Surface|
std::unique_ptr<SurfaceFrame> AcquireFrame(const SkISize& size) override;
Expand Down
12 changes: 10 additions & 2 deletions shell/gpu/gpu_surface_metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@

namespace flutter {

GPUSurfaceMetal::GPUSurfaceMetal(GPUSurfaceMetalDelegate* delegate, sk_sp<GrDirectContext> context)
GPUSurfaceMetal::GPUSurfaceMetal(GPUSurfaceMetalDelegate* delegate,
sk_sp<GrDirectContext> context,
bool render_to_surface)
: delegate_(delegate),
render_target_type_(delegate->GetRenderTargetType()),
context_(std::move(context)) {}
context_(std::move(context)),
render_to_surface_(render_to_surface) {}

GPUSurfaceMetal::~GPUSurfaceMetal() {
ReleaseUnusedDrawableIfNecessary();
Expand Down Expand Up @@ -55,6 +58,11 @@
return nullptr;
}

if (!render_to_surface_) {
return std::make_unique<SurfaceFrame>(
nullptr, true, [](const SurfaceFrame& surface_frame, SkCanvas* canvas) { return true; });
}

PrecompileKnownSkSLsIfNecessary();

switch (render_target_type_) {
Expand Down
2 changes: 2 additions & 0 deletions shell/platform/embedder/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ if (enable_unittests) {

if (test_enable_metal) {
sources += [
"tests/embedder_test_compositor_metal.cc",
"tests/embedder_test_compositor_metal.h",
"tests/embedder_test_context_metal.cc",
"tests/embedder_test_context_metal.h",
"tests/embedder_unittests_metal.mm",
Expand Down
49 changes: 49 additions & 0 deletions shell/platform/embedder/embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,51 @@ static sk_sp<SkSurface> MakeSkSurfaceFromBackingStore(
return surface;
}

static sk_sp<SkSurface> MakeSkSurfaceFromBackingStore(
GrDirectContext* context,
const FlutterBackingStoreConfig& config,
const FlutterMetalBackingStore* metal) {
#ifdef SHELL_ENABLE_METAL
GrMtlTextureInfo texture_info;
if (!metal->texture.texture) {
FML_LOG(ERROR) << "Embedder supplied null Metal texture.";
return nullptr;
}
sk_cf_obj<FlutterMetalTextureHandle> mtl_texture;
mtl_texture.retain(metal->texture.texture);
texture_info.fTexture = mtl_texture;
GrBackendTexture backend_texture(config.size.width, //
config.size.height, //
GrMipMapped::kNo, //
texture_info //
);

SkSurfaceProps surface_properties(0, kUnknown_SkPixelGeometry);

auto surface = SkSurface::MakeFromBackendTexture(
context, // context
backend_texture, // back-end texture
kTopLeft_GrSurfaceOrigin, // surface origin
1, // sample count
kBGRA_8888_SkColorType, // color type
nullptr, // color space
&surface_properties, // surface properties
static_cast<SkSurface::TextureReleaseProc>(
metal->texture.destruction_callback), // release proc
metal->texture.user_data // release context
);

if (!surface) {
FML_LOG(ERROR) << "Could not wrap embedder supplied Metal render texture.";
return nullptr;
}

return surface;
#else
return nullptr;
#endif
}

static std::unique_ptr<flutter::EmbedderRenderTarget>
CreateEmbedderRenderTarget(const FlutterCompositor* compositor,
const FlutterBackingStoreConfig& config,
Expand Down Expand Up @@ -622,6 +667,10 @@ CreateEmbedderRenderTarget(const FlutterCompositor* compositor,
render_surface = MakeSkSurfaceFromBackingStore(context, config,
&backing_store.software);
break;
case kFlutterBackingStoreTypeMetal:
render_surface =
MakeSkSurfaceFromBackingStore(context, config, &backing_store.metal);
break;
};

if (!render_surface) {
Expand Down
22 changes: 22 additions & 0 deletions shell/platform/embedder/embedder.h
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,13 @@ typedef struct {
/// Handle to the MTLTexture that is owned by the embedder. Engine will render
/// the frame into this texture.
FlutterMetalTextureHandle texture;
/// A baton that is not interpreted by the engine in any way. It will be given
/// back to the embedder in the destruction callback below. Embedder resources
/// may be associated with this baton.
void* user_data;
/// The callback invoked by the engine when it no longer needs this backing
/// store.
VoidCallback destruction_callback;
} FlutterMetalTexture;

/// Callback for when a metal texture is requested.
Expand Down Expand Up @@ -944,6 +951,17 @@ typedef struct {
VoidCallback destruction_callback;
} FlutterSoftwareBackingStore;

typedef struct {
/// The size of this struct. Must be sizeof(FlutterMetalBackingStore).
size_t struct_size;
union {
// A Metal texture for Flutter to render into. Ownership is not transferred
// to Flutter; the texture is CFRetained on successfully being passed in and
// CFReleased when no longer used.
FlutterMetalTexture texture;
};
} FlutterMetalBackingStore;

typedef enum {
/// Indicates that the Flutter application requested that an opacity be
/// applied to the platform view.
Expand Down Expand Up @@ -1001,6 +1019,8 @@ typedef enum {
kFlutterBackingStoreTypeOpenGL,
/// Specified an software allocation for Flutter to render into using the CPU.
kFlutterBackingStoreTypeSoftware,
/// Specifies a Metal backing store. This is backed by a Metal texture.
kFlutterBackingStoreTypeMetal,
} FlutterBackingStoreType;

typedef struct {
Expand All @@ -1020,6 +1040,8 @@ typedef struct {
FlutterOpenGLBackingStore open_gl;
/// The description of the software backing store.
FlutterSoftwareBackingStore software;
// The description of the Metal backing store.
FlutterMetalBackingStore metal;
};
} FlutterBackingStore;

Expand Down
3 changes: 2 additions & 1 deletion shell/platform/embedder/embedder_surface_metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
return nullptr;
}

auto surface = std::make_unique<GPUSurfaceMetal>(this, main_context_);
const bool render_to_surface = !external_view_embedder_;
auto surface = std::make_unique<GPUSurfaceMetal>(this, main_context_, render_to_surface);

if (!surface->IsValid()) {
return nullptr;
Expand Down
29 changes: 29 additions & 0 deletions shell/platform/embedder/tests/embedder_assertions.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ inline bool operator==(const FlutterOpenGLFramebuffer& a,
a.destruction_callback == b.destruction_callback;
}

inline bool operator==(const FlutterMetalTexture& a,
const FlutterMetalTexture& b) {
return a.texture_id == b.texture_id && a.texture == b.texture;
}

inline bool operator==(const FlutterMetalBackingStore& a,
const FlutterMetalBackingStore& b) {
return a.texture == b.texture;
}

inline bool operator==(const FlutterOpenGLBackingStore& a,
const FlutterOpenGLBackingStore& b) {
if (!(a.type == b.type)) {
Expand Down Expand Up @@ -100,6 +110,8 @@ inline bool operator==(const FlutterBackingStore& a,
return a.open_gl == b.open_gl;
case kFlutterBackingStoreTypeSoftware:
return a.software == b.software;
case kFlutterBackingStoreTypeMetal:
return a.metal == b.metal;
}

return false;
Expand Down Expand Up @@ -216,6 +228,8 @@ inline std::string FlutterBackingStoreTypeToString(
return "kFlutterBackingStoreTypeOpenGL";
case kFlutterBackingStoreTypeSoftware:
return "kFlutterBackingStoreTypeSoftware";
case kFlutterBackingStoreTypeMetal:
return "kFlutterBackingStoreTypeMetal";
}
return "Unknown";
}
Expand All @@ -236,6 +250,12 @@ inline std::ostream& operator<<(std::ostream& out,
<< reinterpret_cast<void*>(item.destruction_callback);
}

inline std::ostream& operator<<(std::ostream& out,
const FlutterMetalTexture& item) {
return out << "(FlutterMetalTexture) Texture ID: " << std::hex
<< item.texture_id << std::dec << " Handle: 0x" << std::hex
<< item.texture;
}
inline std::string FlutterPlatformViewMutationTypeToString(
FlutterPlatformViewMutationType type) {
switch (type) {
Expand Down Expand Up @@ -322,6 +342,11 @@ inline std::ostream& operator<<(std::ostream& out,
<< reinterpret_cast<void*>(item.destruction_callback);
}

inline std::ostream& operator<<(std::ostream& out,
const FlutterMetalBackingStore& item) {
return out << "(FlutterMetalBackingStore) Texture: " << item.texture;
}

inline std::ostream& operator<<(std::ostream& out,
const FlutterBackingStore& backing_store) {
out << "(FlutterBackingStore) Struct size: " << backing_store.struct_size
Expand All @@ -337,6 +362,10 @@ inline std::ostream& operator<<(std::ostream& out,
case kFlutterBackingStoreTypeSoftware:
out << backing_store.software;
break;

case kFlutterBackingStoreTypeMetal:
out << backing_store.metal;
break;
}

return out;
Expand Down
15 changes: 2 additions & 13 deletions shell/platform/embedder/tests/embedder_config_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -400,19 +400,8 @@ void EmbedderConfigBuilder::InitializeMetalRendererConfig() {
metal_context.GetTestMetalContext()->GetMetalCommandQueue();
metal_renderer_config_.get_next_drawable_callback =
[](void* user_data, const FlutterFrameInfo* frame_info) {
EmbedderTestContextMetal* metal_context =
reinterpret_cast<EmbedderTestContextMetal*>(user_data);
SkISize surface_size =
SkISize::Make(frame_info->size.width, frame_info->size.height);
TestMetalContext::TextureInfo texture_info =
metal_context->GetTestMetalContext()->CreateMetalTexture(
surface_size);
FlutterMetalTexture texture;
texture.struct_size = sizeof(FlutterMetalTexture);
texture.texture_id = texture_info.texture_id;
texture.texture =
reinterpret_cast<FlutterMetalTextureHandle>(texture_info.texture);
return texture;
return reinterpret_cast<EmbedderTestContextMetal*>(user_data)
->GetNextDrawable(frame_info);
};
metal_renderer_config_.present_drawable_callback =
[](void* user_data, const FlutterMetalTexture* texture) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@
#include "flutter/shell/platform/embedder/tests/embedder_test_backingstore_producer.h"

#include "flutter/fml/logging.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkSize.h"
#include "third_party/skia/include/core/SkSurface.h"

#include <memory>

namespace flutter {
namespace testing {

EmbedderTestBackingStoreProducer::EmbedderTestBackingStoreProducer(
sk_sp<GrDirectContext> context,
RenderTargetType type)
: context_(context), type_(type) {}
: context_(context),
type_(type)
#ifdef SHELL_ENABLE_METAL
,
test_metal_context_(std::make_unique<TestMetalContext>())
#endif
{
}

EmbedderTestBackingStoreProducer::~EmbedderTestBackingStoreProducer() = default;

Expand All @@ -28,6 +39,10 @@ bool EmbedderTestBackingStoreProducer::Create(
return CreateTexture(config, renderer_out);
case RenderTargetType::kOpenGLFramebuffer:
return CreateFramebuffer(config, renderer_out);
#endif
#ifdef SHELL_ENABLE_METAL
case RenderTargetType::kMetalTexture:
return CreateMTLTexture(config, renderer_out);
#endif
default:
return false;
Expand Down Expand Up @@ -174,5 +189,52 @@ bool EmbedderTestBackingStoreProducer::CreateSoftware(
return true;
}

bool EmbedderTestBackingStoreProducer::CreateMTLTexture(
const FlutterBackingStoreConfig* config,
FlutterBackingStore* backing_store_out) {
#ifdef SHELL_ENABLE_METAL
// TODO(gw280): Use SkSurface::MakeRenderTarget instead of generating our
// own MTLTexture and wrapping it.
auto surface_size = SkISize::Make(config->size.width, config->size.height);
auto texture_info = test_metal_context_->CreateMetalTexture(surface_size);
sk_cf_obj<FlutterMetalTextureHandle> texture;
texture.retain(texture_info.texture);

GrMtlTextureInfo skia_texture_info;
skia_texture_info.fTexture = texture;
GrBackendTexture backend_texture(surface_size.width(), surface_size.height(),
GrMipmapped::kNo, skia_texture_info);

SkSurface::TextureReleaseProc release_mtltexture = [](void* user_data) {
SkCFSafeRelease(user_data);
};

sk_sp<SkSurface> surface = SkSurface::MakeFromBackendTexture(
context_.get(), backend_texture, kTopLeft_GrSurfaceOrigin, 1,
kBGRA_8888_SkColorType, nullptr, nullptr, release_mtltexture,
texture_info.texture);

if (!surface) {
FML_LOG(ERROR) << "Could not create Skia surface from a Metal texture.";
return false;
}

backing_store_out->type = kFlutterBackingStoreTypeMetal;
backing_store_out->user_data = surface.get();
backing_store_out->metal.texture.texture = texture_info.texture;
// The balancing unref is in the destruction callback.
surface->ref();
backing_store_out->metal.struct_size = sizeof(FlutterMetalBackingStore);
backing_store_out->metal.texture.user_data = surface.get();
backing_store_out->metal.texture.destruction_callback = [](void* user_data) {
reinterpret_cast<SkSurface*>(user_data)->unref();
};

return true;
#else
return false;
#endif
}

} // namespace testing
} // namespace flutter
Loading

0 comments on commit 5407bdd

Please sign in to comment.