Skip to content

Commit

Permalink
Move FlutterBackingStorePresentInfo into FlutterBackingStore
Browse files Browse the repository at this point in the history
  • Loading branch information
knopp committed Jun 23, 2023
1 parent cd12d34 commit 01d28c5
Show file tree
Hide file tree
Showing 21 changed files with 590 additions and 329 deletions.
4 changes: 4 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -2785,6 +2785,8 @@ ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/TestFlutte
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Source/TestFlutterPlatformView.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/embedder/embedder.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/embedder/embedder.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_backing_store.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_backing_store.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_engine.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_engine.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/embedder/embedder_exports.lst + ../../../flutter/LICENSE
Expand Down Expand Up @@ -5480,6 +5482,8 @@ FILE: ../../../flutter/shell/platform/embedder/assets/EmbedderInfo.plist
FILE: ../../../flutter/shell/platform/embedder/assets/embedder.modulemap
FILE: ../../../flutter/shell/platform/embedder/embedder.cc
FILE: ../../../flutter/shell/platform/embedder/embedder.h
FILE: ../../../flutter/shell/platform/embedder/embedder_backing_store.cc
FILE: ../../../flutter/shell/platform/embedder/embedder_backing_store.h
FILE: ../../../flutter/shell/platform/embedder/embedder_engine.cc
FILE: ../../../flutter/shell/platform/embedder/embedder_engine.h
FILE: ../../../flutter/shell/platform/embedder/embedder_exports.lst
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
info.surface = surface;
info.offset = CGPointMake(layer->offset.x, layer->offset.y);
info.zIndex = i;
FlutterBackingStorePresentInfo* present_info = layer->backing_store_present_info;
FlutterBackingStorePresentInfo* present_info = layer->backing_store->present_info;
if (present_info != nullptr && present_info->paint_region != nullptr) {
auto paint_region = present_info->paint_region;
info.paintRegion = std::vector<FlutterRect>(
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 @@ -58,6 +58,8 @@ template("embedder_source_set") {
source_set(target_name) {
sources = [
"embedder.cc",
"embedder_backing_store.cc",
"embedder_backing_store.h",
"embedder_engine.cc",
"embedder_engine.h",
"embedder_external_texture_resolver.cc",
Expand Down
41 changes: 23 additions & 18 deletions shell/platform/embedder/embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ extern const intptr_t kPlatformStrongDillSize;
#include "flutter/shell/common/rasterizer.h"
#include "flutter/shell/common/switches.h"
#include "flutter/shell/platform/embedder/embedder.h"
#include "flutter/shell/platform/embedder/embedder_backing_store.h"
#include "flutter/shell/platform/embedder/embedder_engine.h"
#include "flutter/shell/platform/embedder/embedder_external_texture_resolver.h"
#include "flutter/shell/platform/embedder/embedder_platform_message_response.h"
Expand Down Expand Up @@ -924,7 +925,7 @@ static sk_sp<SkSurface> MakeSkSurfaceFromBackingStore(

static std::unique_ptr<flutter::EmbedderRenderTarget>
MakeRenderTargetFromBackingStoreImpeller(
FlutterBackingStore backing_store,
flutter::EmbedderBackingStore backing_store,
const fml::closure& on_release,
const std::shared_ptr<impeller::AiksContext>& aiks_context,
const FlutterBackingStoreConfig& config,
Expand Down Expand Up @@ -1046,7 +1047,7 @@ static sk_sp<SkSurface> MakeSkSurfaceFromBackingStore(
}

static std::unique_ptr<flutter::EmbedderRenderTarget>
MakeRenderTargetFromSkSurface(FlutterBackingStore backing_store,
MakeRenderTargetFromSkSurface(flutter::EmbedderBackingStore backing_store,
sk_sp<SkSurface> skia_surface,
fml::closure on_release) {
if (!skia_surface) {
Expand All @@ -1063,8 +1064,7 @@ CreateEmbedderRenderTarget(
GrDirectContext* context,
const std::shared_ptr<impeller::AiksContext>& aiks_context,
bool enable_impeller) {
FlutterBackingStore backing_store = {};
backing_store.struct_size = sizeof(backing_store);
flutter::EmbedderBackingStore backing_store;

// Safe access checks on the compositor struct have been performed in
// InferExternalViewEmbedderFromArgs and are not necessary here.
Expand All @@ -1073,13 +1073,16 @@ CreateEmbedderRenderTarget(

{
TRACE_EVENT0("flutter", "FlutterCompositorCreateBackingStore");
if (!c_create_callback(&config, &backing_store, compositor->user_data)) {
if (!c_create_callback(&config,
static_cast<FlutterBackingStore*>(
backing_store.GetBackingStorePointer()),
compositor->user_data)) {
FML_LOG(ERROR) << "Could not create the embedder backing store.";
return nullptr;
}
}

if (backing_store.struct_size != sizeof(backing_store)) {
if (!backing_store.IsValid()) {
FML_LOG(ERROR) << "Embedder modified the backing store struct size.";
return nullptr;
}
Expand All @@ -1091,28 +1094,30 @@ CreateEmbedderRenderTarget(
fml::ScopedCleanupClosure collect_callback(
[c_collect_callback, backing_store, user_data = compositor->user_data]() {
TRACE_EVENT0("flutter", "FlutterCompositorCollectBackingStore");
c_collect_callback(&backing_store, user_data);
c_collect_callback(static_cast<const FlutterBackingStore*>(
backing_store.GetBackingStorePointer()),
user_data);
});

// No safe access checks on the renderer are necessary since we allocated
// the struct.

std::unique_ptr<flutter::EmbedderRenderTarget> render_target;

switch (backing_store.type) {
switch (backing_store.type()) {
case kFlutterBackingStoreTypeOpenGL: {
switch (backing_store.open_gl.type) {
switch (backing_store.open_gl().type) {
case kFlutterOpenGLTargetTypeTexture: {
auto skia_surface = MakeSkSurfaceFromBackingStore(
context, config, &backing_store.open_gl.texture);
context, config, &backing_store.open_gl().texture);
render_target = MakeRenderTargetFromSkSurface(
backing_store, std::move(skia_surface),
collect_callback.Release());
break;
}
case kFlutterOpenGLTargetTypeFramebuffer: {
auto skia_surface = MakeSkSurfaceFromBackingStore(
context, config, &backing_store.open_gl.framebuffer);
context, config, &backing_store.open_gl().framebuffer);
render_target = MakeRenderTargetFromSkSurface(
backing_store, std::move(skia_surface),
collect_callback.Release());
Expand All @@ -1123,14 +1128,14 @@ CreateEmbedderRenderTarget(
}
case kFlutterBackingStoreTypeSoftware: {
auto skia_surface = MakeSkSurfaceFromBackingStore(
context, config, &backing_store.software);
context, config, &backing_store.software());
render_target = MakeRenderTargetFromSkSurface(
backing_store, std::move(skia_surface), collect_callback.Release());
break;
}
case kFlutterBackingStoreTypeSoftware2: {
auto skia_surface = MakeSkSurfaceFromBackingStore(
context, config, &backing_store.software2);
context, config, &backing_store.software2());
render_target = MakeRenderTargetFromSkSurface(
backing_store, std::move(skia_surface), collect_callback.Release());
break;
Expand All @@ -1139,18 +1144,18 @@ CreateEmbedderRenderTarget(
if (enable_impeller) {
render_target = MakeRenderTargetFromBackingStoreImpeller(
backing_store, collect_callback.Release(), aiks_context, config,
&backing_store.metal);
&backing_store.metal());
} else {
auto skia_surface = MakeSkSurfaceFromBackingStore(context, config,
&backing_store.metal);
auto skia_surface = MakeSkSurfaceFromBackingStore(
context, config, &backing_store.metal());
render_target = MakeRenderTargetFromSkSurface(
backing_store, std::move(skia_surface), collect_callback.Release());
}
break;
}
case kFlutterBackingStoreTypeVulkan: {
auto skia_surface =
MakeSkSurfaceFromBackingStore(context, config, &backing_store.vulkan);
auto skia_surface = MakeSkSurfaceFromBackingStore(
context, config, &backing_store.vulkan());
render_target = MakeRenderTargetFromSkSurface(
backing_store, std::move(skia_surface), collect_callback.Release());
break;
Expand Down
44 changes: 22 additions & 22 deletions shell/platform/embedder/embedder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,24 @@ typedef enum {
kFlutterBackingStoreTypeSoftware2,
} FlutterBackingStoreType;

typedef struct {
/// The size of this struct. Must be sizeof(FlutterRegion).
size_t struct_size;
/// Number of rectangles in the region.
size_t rects_count;
/// The rectangles that make up the region.
FlutterRect* rects;
} FlutterRegion;

typedef struct {
size_t struct_size;

/// The area of the backing store that contains Flutter contents. Pixels
/// outside of this area are transparent and the embedder may choose not
/// to render them. Coordinates are in physical pixels.
FlutterRegion* paint_region;
} FlutterBackingStorePresentInfo;

typedef struct {
/// The size of this struct. Must be sizeof(FlutterBackingStore).
size_t struct_size;
Expand All @@ -1551,6 +1569,10 @@ typedef struct {
// The description of the Vulkan backing store.
FlutterVulkanBackingStore vulkan;
};

/// Extra information set by the engine for the backing store that the
/// embedder may use during presentation.
FlutterBackingStorePresentInfo* present_info;
} FlutterBackingStore;

typedef struct {
Expand All @@ -1568,24 +1590,6 @@ typedef enum {
kFlutterLayerContentTypePlatformView,
} FlutterLayerContentType;

typedef struct {
/// The size of this struct. Must be sizeof(FlutterRegion).
size_t struct_size;
/// Number of rectangles in the region.
size_t rects_count;
/// The rectangles that make up the region.
FlutterRect* rects;
} FlutterRegion;

typedef struct {
size_t struct_size;

/// The area of the backing store that contains Flutter contents. Pixels
/// outside of this area are transparent and the embedder may choose not
/// to render them. Coordinates are in physical pixels.
FlutterRegion* paint_region;
} FlutterBackingStorePresentInfo;

typedef struct {
/// This size of this struct. Must be sizeof(FlutterLayer).
size_t struct_size;
Expand All @@ -1605,10 +1609,6 @@ typedef struct {
FlutterPoint offset;
/// The size of the layer (in physical pixels).
FlutterSize size;

/// Extra information for the backing store that the embedder may
/// use during presentation.
FlutterBackingStorePresentInfo* backing_store_present_info;
} FlutterLayer;

typedef bool (*FlutterBackingStoreCreateCallback)(
Expand Down
17 changes: 17 additions & 0 deletions shell/platform/embedder/embedder_backing_store.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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/embedder_backing_store.h"

namespace flutter {

bool EmbedderBackingStore::SetPresetInfo(FlutterBackingStorePresentInfo* info) {
if (backing_store_.struct_size == sizeof(FlutterBackingStoreVersion2)) {
version2_.present_info = info;
return true;
} else {
return false;
}
}
} // namespace flutter
94 changes: 94 additions & 0 deletions shell/platform/embedder/embedder_backing_store.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 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_EMBEDDER_BACKING_STORE_H_
#define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_BACKING_STORE_H_

#include "flutter/shell/platform/embedder/embedder.h"

namespace flutter {

typedef struct {
/// The size of this struct. Must be sizeof(FlutterBackingStore).
size_t struct_size;
/// A baton that is not interpreted by the engine in any way. The embedder may
/// use this to associate resources that are tied to the lifecycle of the
/// `FlutterBackingStore`.
void* user_data;
/// Specifies the type of backing store.
FlutterBackingStoreType type;
/// Indicates if this backing store was updated since the last time it was
/// associated with a presented layer.
bool did_update;
union {
/// The description of the OpenGL backing store.
FlutterOpenGLBackingStore open_gl;
/// The description of the software backing store.
FlutterSoftwareBackingStore software;
/// The description of the software backing store.
FlutterSoftwareBackingStore2 software2;
// The description of the Metal backing store.
FlutterMetalBackingStore metal;
// The description of the Vulkan backing store.
FlutterVulkanBackingStore vulkan;
};
} FlutterBackingStoreVersion1;

typedef FlutterBackingStore FlutterBackingStoreVersion2;

/// Wrapper around FlutterBackingStore that handles different versions of the
/// struct.
class EmbedderBackingStore {
public:
EmbedderBackingStore() {
// By default assume the oldest version of backing store. Embedder can
// override this by setting the struct_size field.
backing_store_ = {
.struct_size = sizeof(FlutterBackingStoreVersion1),
};
}

bool IsValid() const {
return backing_store_.struct_size == sizeof(FlutterBackingStoreVersion1) ||
backing_store_.struct_size == sizeof(FlutterBackingStoreVersion2);
}

FlutterBackingStoreType type() const { return version1_.type; }

const FlutterOpenGLBackingStore& open_gl() const { return version1_.open_gl; }

const FlutterSoftwareBackingStore& software() const {
return version1_.software;
}

const FlutterSoftwareBackingStore2& software2() const {
return version1_.software2;
}

const FlutterMetalBackingStore& metal() const { return version1_.metal; }

const FlutterVulkanBackingStore& vulkan() const { return version1_.vulkan; }

void SetDidUpdate(bool did_update) { version1_.did_update = true; }

bool SetPresetInfo(FlutterBackingStorePresentInfo* info);

/// Returns pointer to the backing store. This is intentionally opaque to
/// prevent embedder from accessing the fields directly and is meant for
/// passing back to the embedder client.
void* GetBackingStorePointer() { return &backing_store_; }
const void* GetBackingStorePointer() const { return &backing_store_; }

private:
union {
size_t struct_size_;
FlutterBackingStoreVersion1 version1_;
FlutterBackingStoreVersion2 version2_;
FlutterBackingStore backing_store_;
};
};

} // namespace flutter

#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_BACKING_STORE_H_
7 changes: 4 additions & 3 deletions shell/platform/embedder/embedder_layers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ EmbedderLayers::EmbedderLayers(SkISize frame_size,
EmbedderLayers::~EmbedderLayers() = default;

void EmbedderLayers::PushBackingStoreLayer(
const FlutterBackingStore* store,
EmbedderBackingStore* store,
const std::vector<SkIRect>& paint_region_vec) {
FlutterLayer layer = {};

layer.struct_size = sizeof(FlutterLayer);
layer.type = kFlutterLayerContentTypeBackingStore;
layer.backing_store = store;
layer.backing_store =
static_cast<const FlutterBackingStore*>(store->GetBackingStorePointer());

const auto layer_bounds =
SkRect::MakeWH(frame_size_.width(), frame_size_.height());
Expand Down Expand Up @@ -61,7 +62,7 @@ void EmbedderLayers::PushBackingStoreLayer(
present_info->struct_size = sizeof(FlutterBackingStorePresentInfo);
present_info->paint_region = paint_region.get();
regions_referenced_.push_back(std::move(paint_region));
layer.backing_store_present_info = present_info.get();
store->SetPresetInfo(present_info.get());

present_info_referenced_.push_back(std::move(present_info));
presented_layers_.push_back(layer);
Expand Down
3 changes: 2 additions & 1 deletion shell/platform/embedder/embedder_layers.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "flutter/flow/embedded_views.h"
#include "flutter/fml/macros.h"
#include "flutter/shell/platform/embedder/embedder.h"
#include "flutter/shell/platform/embedder/embedder_backing_store.h"
#include "third_party/skia/include/core/SkMatrix.h"
#include "third_party/skia/include/core/SkSize.h"

Expand All @@ -24,7 +25,7 @@ class EmbedderLayers {

~EmbedderLayers();

void PushBackingStoreLayer(const FlutterBackingStore* store,
void PushBackingStoreLayer(EmbedderBackingStore* store,
const std::vector<SkIRect>& drawn_region);

void PushPlatformViewLayer(FlutterPlatformViewIdentifier identifier,
Expand Down
Loading

0 comments on commit 01d28c5

Please sign in to comment.