Add Metal to the FlutterCompositor struct in the Embedder API (flutter/engine#25612)
This commit is contained in:
@@ -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();
|
||||
@@ -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;
|
||||
|
||||
@@ -19,10 +19,13 @@ static_assert(!__has_feature(objc_arc), "ARC must be disabled.");
|
||||
|
||||
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();
|
||||
@@ -55,6 +58,11 @@ std::unique_ptr<SurfaceFrame> GPUSurfaceMetal::AcquireFrame(const SkISize& frame
|
||||
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_) {
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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,
|
||||
@@ -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) {
|
||||
|
||||
@@ -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.
|
||||
@@ -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.
|
||||
@@ -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 {
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -39,7 +39,8 @@ std::unique_ptr<Surface> EmbedderSurfaceMetal::CreateGPUSurface() {
|
||||
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;
|
||||
|
||||
@@ -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)) {
|
||||
@@ -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;
|
||||
@@ -216,6 +228,8 @@ inline std::string FlutterBackingStoreTypeToString(
|
||||
return "kFlutterBackingStoreTypeOpenGL";
|
||||
case kFlutterBackingStoreTypeSoftware:
|
||||
return "kFlutterBackingStoreTypeSoftware";
|
||||
case kFlutterBackingStoreTypeMetal:
|
||||
return "kFlutterBackingStoreTypeMetal";
|
||||
}
|
||||
return "Unknown";
|
||||
}
|
||||
@@ -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) {
|
||||
@@ -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
|
||||
@@ -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;
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
@@ -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
|
||||
|
||||
@@ -5,11 +5,15 @@
|
||||
#ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_TEST_BACKINGSTORE_PRODUCER_H_
|
||||
#define FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_TEST_BACKINGSTORE_PRODUCER_H_
|
||||
|
||||
#include <memory>
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "flutter/shell/platform/embedder/embedder.h"
|
||||
|
||||
#include "third_party/skia/include/gpu/GrDirectContext.h"
|
||||
|
||||
#ifdef SHELL_ENABLE_METAL
|
||||
#include "flutter/testing/test_metal_context.h"
|
||||
#endif
|
||||
|
||||
namespace flutter {
|
||||
namespace testing {
|
||||
|
||||
@@ -19,6 +23,7 @@ class EmbedderTestBackingStoreProducer {
|
||||
kSoftwareBuffer,
|
||||
kOpenGLFramebuffer,
|
||||
kOpenGLTexture,
|
||||
kMetalTexture,
|
||||
};
|
||||
|
||||
EmbedderTestBackingStoreProducer(sk_sp<GrDirectContext> context,
|
||||
@@ -38,9 +43,16 @@ class EmbedderTestBackingStoreProducer {
|
||||
bool CreateSoftware(const FlutterBackingStoreConfig* config,
|
||||
FlutterBackingStore* backing_store_out);
|
||||
|
||||
bool CreateMTLTexture(const FlutterBackingStoreConfig* config,
|
||||
FlutterBackingStore* renderer_out);
|
||||
|
||||
sk_sp<GrDirectContext> context_;
|
||||
RenderTargetType type_;
|
||||
|
||||
#ifdef SHELL_ENABLE_METAL
|
||||
std::unique_ptr<TestMetalContext> test_metal_context_;
|
||||
#endif
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(EmbedderTestBackingStoreProducer);
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flutter/shell/platform/embedder/tests/embedder_test_compositor_metal.h"
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "flutter/shell/platform/embedder/tests/embedder_assertions.h"
|
||||
#include "third_party/skia/include/core/SkSurface.h"
|
||||
|
||||
namespace flutter {
|
||||
namespace testing {
|
||||
|
||||
EmbedderTestCompositorMetal::EmbedderTestCompositorMetal(
|
||||
SkISize surface_size,
|
||||
sk_sp<GrDirectContext> context)
|
||||
: EmbedderTestCompositor(surface_size, context) {}
|
||||
|
||||
EmbedderTestCompositorMetal::~EmbedderTestCompositorMetal() = default;
|
||||
|
||||
bool EmbedderTestCompositorMetal::UpdateOffscrenComposition(
|
||||
const FlutterLayer** layers,
|
||||
size_t layers_count) {
|
||||
last_composition_ = nullptr;
|
||||
|
||||
const auto image_info = SkImageInfo::MakeN32Premul(surface_size_);
|
||||
|
||||
auto surface =
|
||||
SkSurface::MakeRenderTarget(context_.get(), // context
|
||||
SkBudgeted::kNo, // budgeted
|
||||
image_info, // image info
|
||||
1, // sample count
|
||||
kTopLeft_GrSurfaceOrigin, // surface origin
|
||||
nullptr, // surface properties
|
||||
false // create mipmaps
|
||||
);
|
||||
|
||||
if (!surface) {
|
||||
FML_LOG(ERROR) << "Could not update the off-screen composition.";
|
||||
return false;
|
||||
}
|
||||
|
||||
auto canvas = surface->getCanvas();
|
||||
|
||||
// This has to be transparent because we are going to be compositing this
|
||||
// sub-hierarchy onto the on-screen surface.
|
||||
canvas->clear(SK_ColorTRANSPARENT);
|
||||
|
||||
for (size_t i = 0; i < layers_count; ++i) {
|
||||
const auto* layer = layers[i];
|
||||
|
||||
sk_sp<SkImage> platform_rendered_contents;
|
||||
|
||||
sk_sp<SkImage> layer_image;
|
||||
SkIPoint canvas_offset = SkIPoint::Make(0, 0);
|
||||
|
||||
switch (layer->type) {
|
||||
case kFlutterLayerContentTypeBackingStore:
|
||||
layer_image =
|
||||
reinterpret_cast<SkSurface*>(layer->backing_store->user_data)
|
||||
->makeImageSnapshot();
|
||||
break;
|
||||
case kFlutterLayerContentTypePlatformView:
|
||||
layer_image =
|
||||
platform_view_renderer_callback_
|
||||
? platform_view_renderer_callback_(*layer, context_.get())
|
||||
: nullptr;
|
||||
canvas_offset = SkIPoint::Make(layer->offset.x, layer->offset.y);
|
||||
break;
|
||||
};
|
||||
|
||||
// If the layer is not a platform view but the engine did not specify an
|
||||
// image for the backing store, it is an error.
|
||||
if (!layer_image && layer->type != kFlutterLayerContentTypePlatformView) {
|
||||
FML_LOG(ERROR) << "Could not snapshot layer in test compositor: "
|
||||
<< *layer;
|
||||
return false;
|
||||
}
|
||||
|
||||
// The test could have just specified no contents to be rendered in place of
|
||||
// a platform view. This is not an error.
|
||||
if (layer_image) {
|
||||
// The image rendered by Flutter already has the correct offset and
|
||||
// transformation applied. The layers offset is meant for the platform.
|
||||
canvas->drawImage(layer_image.get(), canvas_offset.x(),
|
||||
canvas_offset.y());
|
||||
}
|
||||
}
|
||||
|
||||
last_composition_ = surface->makeImageSnapshot();
|
||||
|
||||
if (!last_composition_) {
|
||||
FML_LOG(ERROR) << "Could not update the contents of the sub-composition.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (next_scene_callback_) {
|
||||
auto last_composition_snapshot = last_composition_->makeRasterImage();
|
||||
FML_CHECK(last_composition_snapshot);
|
||||
auto callback = next_scene_callback_;
|
||||
next_scene_callback_ = nullptr;
|
||||
callback(std::move(last_composition_snapshot));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright 2013 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_TEST_COMPOSITOR_METAL_H_
|
||||
#define FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_TEST_COMPOSITOR_METAL_H_
|
||||
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "flutter/shell/platform/embedder/embedder.h"
|
||||
#include "flutter/shell/platform/embedder/tests/embedder_test_compositor.h"
|
||||
|
||||
namespace flutter {
|
||||
namespace testing {
|
||||
|
||||
class EmbedderTestCompositorMetal : public EmbedderTestCompositor {
|
||||
public:
|
||||
EmbedderTestCompositorMetal(SkISize surface_size,
|
||||
sk_sp<GrDirectContext> context);
|
||||
|
||||
~EmbedderTestCompositorMetal() override;
|
||||
|
||||
private:
|
||||
bool UpdateOffscrenComposition(const FlutterLayer** layers,
|
||||
size_t layers_count) override;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(EmbedderTestCompositorMetal);
|
||||
};
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
|
||||
#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_TEST_COMPOSITOR_METAL_H_
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "embedder.h"
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "flutter/shell/platform/embedder/tests/embedder_test_compositor_metal.h"
|
||||
|
||||
namespace flutter {
|
||||
namespace testing {
|
||||
@@ -22,6 +23,7 @@ EmbedderTestContextMetal::~EmbedderTestContextMetal() {}
|
||||
void EmbedderTestContextMetal::SetupSurface(SkISize surface_size) {
|
||||
FML_CHECK(surface_size_.isEmpty());
|
||||
surface_size_ = surface_size;
|
||||
metal_surface_ = TestMetalSurface::Create(*metal_context_, surface_size_);
|
||||
}
|
||||
|
||||
size_t EmbedderTestContextMetal::GetSurfacePresentCount() const {
|
||||
@@ -33,7 +35,11 @@ EmbedderTestContextType EmbedderTestContextMetal::GetContextType() const {
|
||||
}
|
||||
|
||||
void EmbedderTestContextMetal::SetupCompositor() {
|
||||
FML_CHECK(false) << "Compositor rendering not supported in metal.";
|
||||
FML_CHECK(!compositor_) << "Already set up a compositor in this context.";
|
||||
FML_CHECK(metal_surface_)
|
||||
<< "Set up the Metal surface before setting up a compositor.";
|
||||
compositor_ = std::make_unique<EmbedderTestCompositorMetal>(
|
||||
surface_size_, metal_surface_->GetGrContext());
|
||||
}
|
||||
|
||||
TestMetalContext* EmbedderTestContextMetal::GetTestMetalContext() {
|
||||
@@ -41,11 +47,8 @@ TestMetalContext* EmbedderTestContextMetal::GetTestMetalContext() {
|
||||
}
|
||||
|
||||
bool EmbedderTestContextMetal::Present(int64_t texture_id) {
|
||||
FireRootSurfacePresentCallbackIfPresent([&]() {
|
||||
auto metal_surface_ =
|
||||
TestMetalSurface::Create(*metal_context_, texture_id, surface_size_);
|
||||
return metal_surface_->GetRasterSurfaceSnapshot();
|
||||
});
|
||||
FireRootSurfacePresentCallbackIfPresent(
|
||||
[&]() { return metal_surface_->GetRasterSurfaceSnapshot(); });
|
||||
present_count_++;
|
||||
return metal_context_->Present(texture_id);
|
||||
}
|
||||
@@ -67,5 +70,17 @@ bool EmbedderTestContextMetal::PopulateExternalTexture(
|
||||
}
|
||||
}
|
||||
|
||||
FlutterMetalTexture EmbedderTestContextMetal::GetNextDrawable(
|
||||
const FlutterFrameInfo* frame_info) {
|
||||
auto texture_info = metal_surface_->GetTextureInfo();
|
||||
|
||||
FlutterMetalTexture texture;
|
||||
texture.struct_size = sizeof(FlutterMetalTexture);
|
||||
texture.texture_id = texture_info.texture_id;
|
||||
texture.texture =
|
||||
reinterpret_cast<FlutterMetalTextureHandle>(texture_info.texture);
|
||||
return texture;
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
|
||||
@@ -45,6 +45,8 @@ class EmbedderTestContextMetal : public EmbedderTestContext {
|
||||
|
||||
TestMetalContext* GetTestMetalContext();
|
||||
|
||||
FlutterMetalTexture GetNextDrawable(const FlutterFrameInfo* frame_info);
|
||||
|
||||
private:
|
||||
// This allows the builder to access the hooks.
|
||||
friend class EmbedderConfigBuilder;
|
||||
@@ -52,6 +54,7 @@ class EmbedderTestContextMetal : public EmbedderTestContext {
|
||||
TestExternalTextureCallback external_texture_frame_callback_ = nullptr;
|
||||
SkISize surface_size_ = SkISize::MakeEmpty();
|
||||
std::unique_ptr<TestMetalContext> metal_context_;
|
||||
std::unique_ptr<TestMetalSurface> metal_surface_;
|
||||
size_t present_count_ = 0;
|
||||
|
||||
void SetupSurface(SkISize surface_size) override;
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#import <Metal/Metal.h>
|
||||
|
||||
#include "embedder.h"
|
||||
#include "flutter/fml/synchronization/count_down_latch.h"
|
||||
#include "flutter/shell/platform/embedder/tests/embedder_assertions.h"
|
||||
#include "flutter/shell/platform/embedder/tests/embedder_config_builder.h"
|
||||
#include "flutter/shell/platform/embedder/tests/embedder_test.h"
|
||||
@@ -116,5 +117,267 @@ TEST_F(EmbedderTest, ExternalTextureMetal) {
|
||||
ASSERT_TRUE(ImageMatchesFixture("external_texture_metal.png", rendered_scene));
|
||||
}
|
||||
|
||||
TEST_F(EmbedderTest, MetalCompositorMustBeAbleToRenderPlatformViews) {
|
||||
auto& context = GetEmbedderContext(EmbedderTestContextType::kMetalContext);
|
||||
|
||||
EmbedderConfigBuilder builder(context);
|
||||
builder.SetMetalRendererConfig(SkISize::Make(800, 600));
|
||||
builder.SetCompositor();
|
||||
builder.SetDartEntrypoint("can_composite_platform_views");
|
||||
|
||||
builder.SetRenderTargetType(EmbedderTestBackingStoreProducer::RenderTargetType::kMetalTexture);
|
||||
|
||||
fml::CountDownLatch latch(3);
|
||||
context.GetCompositor().SetNextPresentCallback(
|
||||
[&](const FlutterLayer** layers, size_t layers_count) {
|
||||
ASSERT_EQ(layers_count, 3u);
|
||||
|
||||
{
|
||||
FlutterBackingStore backing_store = *layers[0]->backing_store;
|
||||
backing_store.struct_size = sizeof(backing_store);
|
||||
backing_store.type = kFlutterBackingStoreTypeMetal;
|
||||
backing_store.did_update = true;
|
||||
|
||||
FlutterLayer layer = {};
|
||||
layer.struct_size = sizeof(layer);
|
||||
layer.type = kFlutterLayerContentTypeBackingStore;
|
||||
layer.backing_store = &backing_store;
|
||||
layer.size = FlutterSizeMake(800.0, 600.0);
|
||||
layer.offset = FlutterPointMake(0, 0);
|
||||
|
||||
ASSERT_EQ(*layers[0], layer);
|
||||
}
|
||||
|
||||
{
|
||||
FlutterPlatformView platform_view = *layers[1]->platform_view;
|
||||
platform_view.struct_size = sizeof(platform_view);
|
||||
platform_view.identifier = 42;
|
||||
|
||||
FlutterLayer layer = {};
|
||||
layer.struct_size = sizeof(layer);
|
||||
layer.type = kFlutterLayerContentTypePlatformView;
|
||||
layer.platform_view = &platform_view;
|
||||
layer.size = FlutterSizeMake(123.0, 456.0);
|
||||
layer.offset = FlutterPointMake(1.0, 2.0);
|
||||
|
||||
ASSERT_EQ(*layers[1], layer);
|
||||
}
|
||||
|
||||
{
|
||||
FlutterBackingStore backing_store = *layers[2]->backing_store;
|
||||
backing_store.struct_size = sizeof(backing_store);
|
||||
backing_store.type = kFlutterBackingStoreTypeMetal;
|
||||
backing_store.did_update = true;
|
||||
|
||||
FlutterLayer layer = {};
|
||||
layer.struct_size = sizeof(layer);
|
||||
layer.type = kFlutterLayerContentTypeBackingStore;
|
||||
layer.backing_store = &backing_store;
|
||||
layer.size = FlutterSizeMake(800.0, 600.0);
|
||||
layer.offset = FlutterPointMake(0.0, 0.0);
|
||||
|
||||
ASSERT_EQ(*layers[2], layer);
|
||||
}
|
||||
|
||||
latch.CountDown();
|
||||
});
|
||||
|
||||
context.AddNativeCallback(
|
||||
"SignalNativeTest",
|
||||
CREATE_NATIVE_ENTRY([&latch](Dart_NativeArguments args) { latch.CountDown(); }));
|
||||
|
||||
auto engine = builder.LaunchEngine();
|
||||
|
||||
// Send a window metrics events so frames may be scheduled.
|
||||
FlutterWindowMetricsEvent event = {};
|
||||
event.struct_size = sizeof(event);
|
||||
event.width = 800;
|
||||
event.height = 600;
|
||||
event.pixel_ratio = 1.0;
|
||||
ASSERT_EQ(FlutterEngineSendWindowMetricsEvent(engine.get(), &event), kSuccess);
|
||||
ASSERT_TRUE(engine.is_valid());
|
||||
|
||||
latch.Wait();
|
||||
}
|
||||
|
||||
TEST_F(EmbedderTest, CanRenderSceneWithoutCustomCompositorMetal) {
|
||||
auto& context = GetEmbedderContext(EmbedderTestContextType::kMetalContext);
|
||||
|
||||
EmbedderConfigBuilder builder(context);
|
||||
|
||||
builder.SetDartEntrypoint("can_render_scene_without_custom_compositor");
|
||||
builder.SetMetalRendererConfig(SkISize::Make(800, 600));
|
||||
|
||||
auto rendered_scene = context.GetNextSceneImage();
|
||||
|
||||
auto engine = builder.LaunchEngine();
|
||||
ASSERT_TRUE(engine.is_valid());
|
||||
|
||||
// Send a window metrics events so frames may be scheduled.
|
||||
FlutterWindowMetricsEvent event = {};
|
||||
event.struct_size = sizeof(event);
|
||||
event.width = 800;
|
||||
event.height = 600;
|
||||
event.pixel_ratio = 1.0;
|
||||
ASSERT_EQ(FlutterEngineSendWindowMetricsEvent(engine.get(), &event), kSuccess);
|
||||
|
||||
ASSERT_TRUE(ImageMatchesFixture("scene_without_custom_compositor.png", rendered_scene));
|
||||
}
|
||||
|
||||
TEST_F(EmbedderTest, CompositorMustBeAbleToRenderKnownSceneMetal) {
|
||||
auto& context = GetEmbedderContext(EmbedderTestContextType::kMetalContext);
|
||||
|
||||
EmbedderConfigBuilder builder(context);
|
||||
builder.SetMetalRendererConfig(SkISize::Make(800, 600));
|
||||
builder.SetCompositor();
|
||||
builder.SetDartEntrypoint("can_composite_platform_views_with_known_scene");
|
||||
|
||||
builder.SetRenderTargetType(EmbedderTestBackingStoreProducer::RenderTargetType::kMetalTexture);
|
||||
|
||||
fml::CountDownLatch latch(5);
|
||||
|
||||
auto scene_image = context.GetNextSceneImage();
|
||||
|
||||
context.GetCompositor().SetNextPresentCallback(
|
||||
[&](const FlutterLayer** layers, size_t layers_count) {
|
||||
ASSERT_EQ(layers_count, 5u);
|
||||
|
||||
// Layer Root
|
||||
{
|
||||
FlutterBackingStore backing_store = *layers[0]->backing_store;
|
||||
backing_store.type = kFlutterBackingStoreTypeMetal;
|
||||
backing_store.did_update = true;
|
||||
|
||||
FlutterLayer layer = {};
|
||||
layer.struct_size = sizeof(layer);
|
||||
layer.type = kFlutterLayerContentTypeBackingStore;
|
||||
layer.backing_store = &backing_store;
|
||||
layer.size = FlutterSizeMake(800.0, 600.0);
|
||||
layer.offset = FlutterPointMake(0.0, 0.0);
|
||||
|
||||
ASSERT_EQ(*layers[0], layer);
|
||||
}
|
||||
|
||||
// Layer 1
|
||||
{
|
||||
FlutterPlatformView platform_view = *layers[1]->platform_view;
|
||||
platform_view.struct_size = sizeof(platform_view);
|
||||
platform_view.identifier = 1;
|
||||
|
||||
FlutterLayer layer = {};
|
||||
layer.struct_size = sizeof(layer);
|
||||
layer.type = kFlutterLayerContentTypePlatformView;
|
||||
layer.platform_view = &platform_view;
|
||||
layer.size = FlutterSizeMake(50.0, 150.0);
|
||||
layer.offset = FlutterPointMake(20.0, 20.0);
|
||||
|
||||
ASSERT_EQ(*layers[1], layer);
|
||||
}
|
||||
|
||||
// Layer 2
|
||||
{
|
||||
FlutterBackingStore backing_store = *layers[2]->backing_store;
|
||||
backing_store.type = kFlutterBackingStoreTypeMetal;
|
||||
backing_store.did_update = true;
|
||||
|
||||
FlutterLayer layer = {};
|
||||
layer.struct_size = sizeof(layer);
|
||||
layer.type = kFlutterLayerContentTypeBackingStore;
|
||||
layer.backing_store = &backing_store;
|
||||
layer.size = FlutterSizeMake(800.0, 600.0);
|
||||
layer.offset = FlutterPointMake(0.0, 0.0);
|
||||
|
||||
ASSERT_EQ(*layers[2], layer);
|
||||
}
|
||||
|
||||
// Layer 3
|
||||
{
|
||||
FlutterPlatformView platform_view = *layers[3]->platform_view;
|
||||
platform_view.struct_size = sizeof(platform_view);
|
||||
platform_view.identifier = 2;
|
||||
|
||||
FlutterLayer layer = {};
|
||||
layer.struct_size = sizeof(layer);
|
||||
layer.type = kFlutterLayerContentTypePlatformView;
|
||||
layer.platform_view = &platform_view;
|
||||
layer.size = FlutterSizeMake(50.0, 150.0);
|
||||
layer.offset = FlutterPointMake(40.0, 40.0);
|
||||
|
||||
ASSERT_EQ(*layers[3], layer);
|
||||
}
|
||||
|
||||
// Layer 4
|
||||
{
|
||||
FlutterBackingStore backing_store = *layers[4]->backing_store;
|
||||
backing_store.type = kFlutterBackingStoreTypeMetal;
|
||||
backing_store.did_update = true;
|
||||
|
||||
FlutterLayer layer = {};
|
||||
layer.struct_size = sizeof(layer);
|
||||
layer.type = kFlutterLayerContentTypeBackingStore;
|
||||
layer.backing_store = &backing_store;
|
||||
layer.size = FlutterSizeMake(800.0, 600.0);
|
||||
layer.offset = FlutterPointMake(0.0, 0.0);
|
||||
|
||||
ASSERT_EQ(*layers[4], layer);
|
||||
}
|
||||
|
||||
latch.CountDown();
|
||||
});
|
||||
|
||||
context.GetCompositor().SetPlatformViewRendererCallback(
|
||||
[&](const FlutterLayer& layer, GrDirectContext* context) -> sk_sp<SkImage> {
|
||||
auto surface = CreateRenderSurface(layer, context);
|
||||
auto canvas = surface->getCanvas();
|
||||
FML_CHECK(canvas != nullptr);
|
||||
|
||||
switch (layer.platform_view->identifier) {
|
||||
case 1: {
|
||||
SkPaint paint;
|
||||
// See dart test for total order.
|
||||
paint.setColor(SK_ColorGREEN);
|
||||
paint.setAlpha(127);
|
||||
const auto& rect = SkRect::MakeWH(layer.size.width, layer.size.height);
|
||||
canvas->drawRect(rect, paint);
|
||||
latch.CountDown();
|
||||
} break;
|
||||
case 2: {
|
||||
SkPaint paint;
|
||||
// See dart test for total order.
|
||||
paint.setColor(SK_ColorMAGENTA);
|
||||
paint.setAlpha(127);
|
||||
const auto& rect = SkRect::MakeWH(layer.size.width, layer.size.height);
|
||||
canvas->drawRect(rect, paint);
|
||||
latch.CountDown();
|
||||
} break;
|
||||
default:
|
||||
// Asked to render an unknown platform view.
|
||||
FML_CHECK(false) << "Test was asked to composite an unknown platform view.";
|
||||
}
|
||||
|
||||
return surface->makeImageSnapshot();
|
||||
});
|
||||
|
||||
context.AddNativeCallback(
|
||||
"SignalNativeTest",
|
||||
CREATE_NATIVE_ENTRY([&latch](Dart_NativeArguments args) { latch.CountDown(); }));
|
||||
|
||||
auto engine = builder.LaunchEngine();
|
||||
|
||||
// Send a window metrics events so frames may be scheduled.
|
||||
FlutterWindowMetricsEvent event = {};
|
||||
event.struct_size = sizeof(event);
|
||||
// Flutter still thinks it is 800 x 600. Only the root surface is rotated.
|
||||
event.width = 800;
|
||||
event.height = 600;
|
||||
event.pixel_ratio = 1.0;
|
||||
ASSERT_EQ(FlutterEngineSendWindowMetricsEvent(engine.get(), &event), kSuccess);
|
||||
ASSERT_TRUE(engine.is_valid());
|
||||
|
||||
latch.Wait();
|
||||
|
||||
ASSERT_TRUE(ImageMatchesFixture("compositor.png", scene_image));
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
|
||||
@@ -48,4 +48,8 @@ sk_sp<SkImage> TestMetalSurface::GetRasterSurfaceSnapshot() {
|
||||
return impl_ ? impl_->GetRasterSurfaceSnapshot() : nullptr;
|
||||
}
|
||||
|
||||
TestMetalContext::TextureInfo TestMetalSurface::GetTextureInfo() {
|
||||
return impl_ ? impl_->GetTextureInfo() : TestMetalContext::TextureInfo();
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
@@ -40,6 +40,8 @@ class TestMetalSurface {
|
||||
|
||||
virtual sk_sp<SkImage> GetRasterSurfaceSnapshot();
|
||||
|
||||
virtual TestMetalContext::TextureInfo GetTextureInfo();
|
||||
|
||||
protected:
|
||||
TestMetalSurface();
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ class TestMetalSurfaceImpl : public TestMetalSurface {
|
||||
const TestMetalContext& test_metal_context_;
|
||||
bool is_valid_ = false;
|
||||
sk_sp<SkSurface> surface_;
|
||||
TestMetalContext::TextureInfo texture_info_;
|
||||
|
||||
// |TestMetalSurface|
|
||||
bool IsValid() const override;
|
||||
@@ -43,6 +44,9 @@ class TestMetalSurfaceImpl : public TestMetalSurface {
|
||||
// |TestMetalSurface|
|
||||
sk_sp<SkImage> GetRasterSurfaceSnapshot() override;
|
||||
|
||||
// |TestMetalSurface|
|
||||
TestMetalContext::TextureInfo GetTextureInfo() override;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(TestMetalSurfaceImpl);
|
||||
};
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
|
||||
#include "flutter/testing/test_metal_context.h"
|
||||
#include "third_party/skia/include/core/SkCanvas.h"
|
||||
#include "third_party/skia/include/core/SkSurface.h"
|
||||
|
||||
namespace flutter {
|
||||
@@ -48,6 +49,7 @@ void TestMetalSurfaceImpl::Init(const TestMetalContext::TextureInfo& texture_inf
|
||||
}
|
||||
|
||||
surface_ = std::move(surface);
|
||||
texture_info_ = std::move(texture_info);
|
||||
is_valid_ = true;
|
||||
}
|
||||
|
||||
@@ -120,4 +122,9 @@ sk_sp<SkSurface> TestMetalSurfaceImpl::GetSurface() const {
|
||||
return IsValid() ? surface_ : nullptr;
|
||||
}
|
||||
|
||||
// |TestMetalSurface|
|
||||
TestMetalContext::TextureInfo TestMetalSurfaceImpl::GetTextureInfo() {
|
||||
return IsValid() ? texture_info_ : TestMetalContext::TextureInfo();
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
Reference in New Issue
Block a user