Avoid root surface acquisition during custom composition with software renderer. (flutter/engine#11394)

Uses the same technique used during OpenGL composition to elide root surface access. The refactoring of this approach is tracked in https://github.com/flutter/flutter/issues/38466

Fixes https://github.com/flutter/flutter/issues/39009.
This commit is contained in:
Chinmay Garde
2019-08-23 13:52:06 -07:00
committed by GitHub
parent db3c15416c
commit 7132a8a12d
10 changed files with 75 additions and 9 deletions

View File

@@ -9,8 +9,11 @@
namespace flutter {
GPUSurfaceSoftware::GPUSurfaceSoftware(GPUSurfaceSoftwareDelegate* delegate)
: delegate_(delegate), weak_factory_(this) {}
GPUSurfaceSoftware::GPUSurfaceSoftware(GPUSurfaceSoftwareDelegate* delegate,
bool render_to_surface)
: delegate_(delegate),
render_to_surface_(render_to_surface),
weak_factory_(this) {}
GPUSurfaceSoftware::~GPUSurfaceSoftware() = default;
@@ -22,6 +25,15 @@ bool GPUSurfaceSoftware::IsValid() {
// |Surface|
std::unique_ptr<SurfaceFrame> GPUSurfaceSoftware::AcquireFrame(
const SkISize& logical_size) {
// TODO(38466): Refactor GPU surface APIs take into account the fact that an
// external view embedder may want to render to the root surface.
if (!render_to_surface_) {
return std::make_unique<SurfaceFrame>(
nullptr, [](const SurfaceFrame& surface_frame, SkCanvas* canvas) {
return true;
});
}
if (!IsValid()) {
return nullptr;
}

View File

@@ -14,7 +14,8 @@ namespace flutter {
class GPUSurfaceSoftware : public Surface {
public:
GPUSurfaceSoftware(GPUSurfaceSoftwareDelegate* delegate);
GPUSurfaceSoftware(GPUSurfaceSoftwareDelegate* delegate,
bool render_to_surface);
~GPUSurfaceSoftware() override;
@@ -35,6 +36,11 @@ class GPUSurfaceSoftware : public Surface {
private:
GPUSurfaceSoftwareDelegate* delegate_;
// 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.
const bool render_to_surface_;
fml::WeakPtrFactory<GPUSurfaceSoftware> weak_factory_;
FML_DISALLOW_COPY_AND_ASSIGN(GPUSurfaceSoftware);

View File

@@ -61,7 +61,8 @@ std::unique_ptr<Surface> AndroidSurfaceSoftware::CreateGPUSurface() {
return nullptr;
}
auto surface = std::make_unique<GPUSurfaceSoftware>(this);
auto surface =
std::make_unique<GPUSurfaceSoftware>(this, true /* render to surface */);
if (!surface->IsValid()) {
return nullptr;

View File

@@ -43,7 +43,7 @@ std::unique_ptr<Surface> IOSSurfaceSoftware::CreateGPUSurface() {
return nullptr;
}
auto surface = std::make_unique<GPUSurfaceSoftware>(this);
auto surface = std::make_unique<GPUSurfaceSoftware>(this, true /* render to surface */);
if (!surface->IsValid()) {
return nullptr;

View File

@@ -81,7 +81,7 @@ EmbedderSurfaceGL::GLProcResolver EmbedderSurfaceGL::GetGLProcResolver() const {
// |EmbedderSurface|
std::unique_ptr<Surface> EmbedderSurfaceGL::CreateGPUSurface() {
bool render_to_surface = !external_view_embedder_;
const bool render_to_surface = !external_view_embedder_;
return std::make_unique<GPUSurfaceGL>(this, // GPU surface GL delegate
render_to_surface // render to surface

View File

@@ -32,8 +32,8 @@ std::unique_ptr<Surface> EmbedderSurfaceSoftware::CreateGPUSurface() {
if (!IsValid()) {
return nullptr;
}
auto surface = std::make_unique<GPUSurfaceSoftware>(this);
const bool render_to_surface = !external_view_embedder_;
auto surface = std::make_unique<GPUSurfaceSoftware>(this, render_to_surface);
if (!surface->IsValid()) {
return nullptr;

View File

@@ -5,6 +5,7 @@
#include "flutter/shell/platform/embedder/tests/embedder_config_builder.h"
#include "flutter/shell/platform/embedder/embedder.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace flutter {
namespace testing {
@@ -48,7 +49,21 @@ EmbedderConfigBuilder::EmbedderConfigBuilder(
software_renderer_config_.struct_size = sizeof(FlutterSoftwareRendererConfig);
software_renderer_config_.surface_present_callback =
[](void*, const void*, size_t, size_t) { return true; };
[](void* context, const void* allocation, size_t row_bytes,
size_t height) {
auto image_info =
SkImageInfo::MakeN32Premul(SkISize::Make(row_bytes / 4, height));
SkBitmap bitmap;
if (!bitmap.installPixels(image_info, const_cast<void*>(allocation),
row_bytes)) {
FML_LOG(ERROR) << "Could not copy pixels for the software "
"composition from the engine.";
return false;
}
bitmap.setImmutable();
return reinterpret_cast<EmbedderTestContext*>(context)->SofwarePresent(
SkImage::MakeFromBitmap(bitmap));
};
if (preference == InitializationPreference::kInitialize) {
SetSoftwareRendererConfig();

View File

@@ -143,6 +143,7 @@ bool EmbedderTestContext::GLClearCurrent() {
}
bool EmbedderTestContext::GLPresent() {
gl_surface_present_count_++;
FML_CHECK(gl_surface_) << "GL surface must be initialized.";
if (next_scene_callback_) {
@@ -200,5 +201,19 @@ void EmbedderTestContext::SetNextSceneCallback(
next_scene_callback_ = next_scene_callback;
}
bool EmbedderTestContext::SofwarePresent(sk_sp<SkImage> image) {
software_surface_present_count_++;
software_surface_ = std::move(image);
return software_surface_ != nullptr;
}
size_t EmbedderTestContext::GetGLSurfacePresentCount() const {
return gl_surface_present_count_;
}
size_t EmbedderTestContext::GetSoftwareSurfacePresentCount() const {
return software_surface_present_count_;
}
} // namespace testing
} // namespace flutter

View File

@@ -61,6 +61,10 @@ class EmbedderTestContext {
using NextSceneCallback = std::function<void(sk_sp<SkImage> image)>;
void SetNextSceneCallback(NextSceneCallback next_scene_callback);
size_t GetGLSurfacePresentCount() const;
size_t GetSoftwareSurfacePresentCount() const;
private:
// This allows the builder to access the hooks.
friend class EmbedderConfigBuilder;
@@ -76,8 +80,11 @@ class EmbedderTestContext {
SemanticsActionCallback update_semantics_custom_action_callback_;
std::function<void(const FlutterPlatformMessage*)> platform_message_callback_;
std::unique_ptr<TestGLSurface> gl_surface_;
sk_sp<SkImage> software_surface_;
std::unique_ptr<EmbedderTestCompositor> compositor_;
NextSceneCallback next_scene_callback_;
size_t gl_surface_present_count_ = 0;
size_t software_surface_present_count_ = 0;
static VoidCallback GetIsolateCreateCallbackHook();
@@ -107,6 +114,8 @@ class EmbedderTestContext {
void PlatformMessageCallback(const FlutterPlatformMessage* message);
bool SofwarePresent(sk_sp<SkImage> image);
FML_DISALLOW_COPY_AND_ASSIGN(EmbedderTestContext);
};

View File

@@ -1105,6 +1105,10 @@ TEST_F(EmbedderTest, CompositorMustBeAbleToRenderKnownScene) {
latch.Wait();
ASSERT_TRUE(ImageMatchesFixture("compositor.png", scene_image));
// There should no present calls on the root surface.
ASSERT_EQ(context.GetSoftwareSurfacePresentCount(), 0u);
ASSERT_EQ(context.GetGLSurfacePresentCount(), 0u);
}
//------------------------------------------------------------------------------
@@ -1279,6 +1283,10 @@ TEST_F(EmbedderTest,
latch.Wait();
ASSERT_TRUE(ImageMatchesFixture("compositor_software.png", scene_image));
// There should no present calls on the root surface.
ASSERT_EQ(context.GetSoftwareSurfacePresentCount(), 0u);
ASSERT_EQ(context.GetGLSurfacePresentCount(), 0u);
}
//------------------------------------------------------------------------------