Skip to content

Commit

Permalink
Plumb the iOS PlatformViewsController into flow. (flutter#6603)
Browse files Browse the repository at this point in the history
For flow to manipulate the embedded UIViews during the paint traversal
it needs some hook in PaintContext.
This PR introduces a ViewEmbeder interface that is implemented by the
iOS PlatformViewsController and plumbs it into PaintContext.

The ViewEmbedder interface is mainly a place holder at this point, as
this PR is focused on just the plumbing.
  • Loading branch information
amirh authored Oct 26, 2018
1 parent 2bfb893 commit df85722
Show file tree
Hide file tree
Showing 27 changed files with 178 additions and 23 deletions.
1 change: 1 addition & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ORIGIN: ../../../flutter/flow/layers/physical_shape_layer.cc + ../../../LICENSE
TYPE: LicenseType.bsd
FILE: ../../../flutter/flow/debug_print.cc
FILE: ../../../flutter/flow/debug_print.h
FILE: ../../../flutter/flow/embedded_views.h
FILE: ../../../flutter/flow/export_node.h
FILE: ../../../flutter/flow/layers/physical_shape_layer.cc
FILE: ../../../flutter/flow/layers/physical_shape_layer.h
Expand Down
1 change: 1 addition & 0 deletions flow/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ source_set("flow") {
"compositor_context.h",
"debug_print.cc",
"debug_print.h",
"embedded_views.h",
"instrumentation.cc",
"instrumentation.h",
"layers/backdrop_filter_layer.cc",
Expand Down
12 changes: 6 additions & 6 deletions flow/compositor_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ void CompositorContext::EndFrame(ScopedFrame& frame,
std::unique_ptr<CompositorContext::ScopedFrame> CompositorContext::AcquireFrame(
GrContext* gr_context,
SkCanvas* canvas,
ExternalViewEmbedder* view_embedder,
const SkMatrix& root_surface_transformation,
bool instrumentation_enabled) {
return std::make_unique<ScopedFrame>(*this, //
gr_context, //
canvas, //
root_surface_transformation, //
instrumentation_enabled //
);
return std::make_unique<ScopedFrame>(*this, gr_context, canvas, view_embedder,
root_surface_transformation,
instrumentation_enabled);
}

CompositorContext::ScopedFrame::ScopedFrame(
CompositorContext& context,
GrContext* gr_context,
SkCanvas* canvas,
ExternalViewEmbedder* view_embedder,
const SkMatrix& root_surface_transformation,
bool instrumentation_enabled)
: context_(context),
gr_context_(gr_context),
canvas_(canvas),
view_embedder_(view_embedder),
root_surface_transformation_(root_surface_transformation),
instrumentation_enabled_(instrumentation_enabled) {
context_.BeginFrame(*this, instrumentation_enabled_);
Expand Down
6 changes: 6 additions & 0 deletions flow/compositor_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <memory>
#include <string>

#include "flutter/flow/embedded_views.h"
#include "flutter/flow/instrumentation.h"
#include "flutter/flow/raster_cache.h"
#include "flutter/flow/texture.h"
Expand All @@ -26,13 +27,16 @@ class CompositorContext {
ScopedFrame(CompositorContext& context,
GrContext* gr_context,
SkCanvas* canvas,
ExternalViewEmbedder* view_embedder,
const SkMatrix& root_surface_transformation,
bool instrumentation_enabled);

virtual ~ScopedFrame();

SkCanvas* canvas() { return canvas_; }

ExternalViewEmbedder* view_embedder() { return view_embedder_; }

CompositorContext& context() const { return context_; }

const SkMatrix& root_surface_transformation() const {
Expand All @@ -47,6 +51,7 @@ class CompositorContext {
CompositorContext& context_;
GrContext* gr_context_;
SkCanvas* canvas_;
ExternalViewEmbedder* view_embedder_;
const SkMatrix& root_surface_transformation_;
const bool instrumentation_enabled_;

Expand All @@ -60,6 +65,7 @@ class CompositorContext {
virtual std::unique_ptr<ScopedFrame> AcquireFrame(
GrContext* gr_context,
SkCanvas* canvas,
ExternalViewEmbedder* view_embedder,
const SkMatrix& root_surface_transformation,
bool instrumentation_enabled);

Expand Down
34 changes: 34 additions & 0 deletions flow/embedded_views.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2017 The Chromium 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_FLOW_EMBEDDED_VIEWS_H_
#define FLUTTER_FLOW_EMBEDDED_VIEWS_H_

#include "flutter/fml/memory/ref_counted.h"

namespace flow {

class EmbeddedViewParams {
public:
};

// This is only used on iOS when running in a non headless mode,
// in this case ViewEmbedded is a reference to the
// FlutterPlatformViewsController which is owned by FlutterViewController.
class ExternalViewEmbedder {
public:
ExternalViewEmbedder() = default;

// Must be called on the UI thread.
virtual void CompositeEmbeddedView(int view_id,
const EmbeddedViewParams& params) {}

virtual ~ExternalViewEmbedder() = default;

FML_DISALLOW_COPY_AND_ASSIGN(ExternalViewEmbedder);
};

} // namespace flow

#endif // FLUTTER_FLOW_EMBEDDED_VIEWS_H_
2 changes: 2 additions & 0 deletions flow/layers/layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <memory>
#include <vector>

#include "flutter/flow/embedded_views.h"
#include "flutter/flow/instrumentation.h"
#include "flutter/flow/raster_cache.h"
#include "flutter/flow/texture.h"
Expand Down Expand Up @@ -64,6 +65,7 @@ class Layer {

struct PaintContext {
SkCanvas& canvas;
ExternalViewEmbedder* view_embedder;
const Stopwatch& frame_time;
const Stopwatch& engine_time;
TextureRegistry& texture_registry;
Expand Down
4 changes: 3 additions & 1 deletion flow/layers/layer_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void LayerTree::Paint(CompositorContext::ScopedFrame& frame,
TRACE_EVENT0("flutter", "LayerTree::Paint");
Layer::PaintContext context = {
*frame.canvas(),
frame.view_embedder(),
frame.context().frame_time(),
frame.context().engine_time(),
frame.context().texture_registry(),
Expand Down Expand Up @@ -106,7 +107,8 @@ sk_sp<SkPicture> LayerTree::Flatten(const SkRect& bounds) {
};

Layer::PaintContext paint_context = {
*canvas, // canvas
*canvas, // canvas
nullptr,
unused_stopwatch, // frame time (dont care)
unused_stopwatch, // engine time (dont care)
unused_texture_registry, // texture registry (not supported)
Expand Down
11 changes: 9 additions & 2 deletions flow/layers/platform_view_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ void PlatformViewLayer::Preroll(PrerollContext* context,
size_.height()));
}

void PlatformViewLayer::Paint(PaintContext& context) const {}

void PlatformViewLayer::Paint(PaintContext& context) const {
if (context.view_embedder == nullptr) {
FML_LOG(ERROR) << "Trying to embed a platform view but the PaintContext "
"does not support embedding";
return;
}
EmbeddedViewParams params;
context.view_embedder->CompositeEmbeddedView(view_id_, params);
}
} // namespace flow
1 change: 1 addition & 0 deletions flow/raster_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ void RasterCache::Prepare(PrerollContext* context,
[layer, context](SkCanvas* canvas) {
Layer::PaintContext paintContext = {
*canvas,
nullptr,
context->frame_time,
context->engine_time,
context->texture_registry,
Expand Down
13 changes: 8 additions & 5 deletions shell/common/rasterizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ bool Rasterizer::DrawToSurface(flow::LayerTree& layer_tree) {
auto canvas = frame->SkiaCanvas();

auto compositor_frame = compositor_context_->AcquireFrame(
surface_->GetContext(), canvas, surface_->GetRootTransformation(), true);
surface_->GetContext(), canvas, surface_->GetExternalViewEmbedder(),
surface_->GetRootTransformation(), true);

if (canvas) {
canvas->clear(SK_ColorTRANSPARENT);
Expand Down Expand Up @@ -197,9 +198,11 @@ static sk_sp<SkData> ScreenshotLayerTreeAsPicture(
SkMatrix root_surface_transformation;
root_surface_transformation.reset();

auto frame =
compositor_context.AcquireFrame(nullptr, recorder.getRecordingCanvas(),
root_surface_transformation, false);
// TODO(amirh): figure out how to take a screenshot with embedded UIView.
// https://github.com/flutter/flutter/issues/23435
auto frame = compositor_context.AcquireFrame(
nullptr, recorder.getRecordingCanvas(), nullptr,
root_surface_transformation, false);

frame->Raster(*tree, true);

Expand Down Expand Up @@ -249,7 +252,7 @@ static sk_sp<SkData> ScreenshotLayerTreeAsImage(
root_surface_transformation.reset();

auto frame = compositor_context.AcquireFrame(
surface_context, canvas, root_surface_transformation, false);
surface_context, canvas, nullptr, root_surface_transformation, false);
canvas->clear(SK_ColorTRANSPARENT);
frame->Raster(*tree, true);
canvas->flush();
Expand Down
4 changes: 4 additions & 0 deletions shell/common/surface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ Surface::Surface() = default;

Surface::~Surface() = default;

flow::ExternalViewEmbedder* Surface::GetExternalViewEmbedder() {
return nullptr;
}

} // namespace shell
3 changes: 3 additions & 0 deletions shell/common/surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <memory>

#include "flutter/flow/compositor_context.h"
#include "flutter/flow/embedded_views.h"
#include "flutter/fml/macros.h"
#include "third_party/skia/include/core/SkCanvas.h"

Expand Down Expand Up @@ -55,6 +56,8 @@ class Surface {

virtual GrContext* GetContext() = 0;

virtual flow::ExternalViewEmbedder* GetExternalViewEmbedder();

private:
FML_DISALLOW_COPY_AND_ASSIGN(Surface);
};
Expand Down
5 changes: 5 additions & 0 deletions shell/gpu/gpu_surface_gl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,9 @@ GrContext* GPUSurfaceGL::GetContext() {
return context_.get();
}

// |shell::Surface|
flow::ExternalViewEmbedder* GPUSurfaceGL::GetExternalViewEmbedder() {
return delegate_->GetExternalViewEmbedder();
}

} // namespace shell
8 changes: 8 additions & 0 deletions shell/gpu/gpu_surface_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <functional>
#include <memory>

#include "flutter/flow/embedded_views.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/memory/weak_ptr.h"
#include "flutter/shell/common/surface.h"
Expand Down Expand Up @@ -35,6 +36,10 @@ class GPUSurfaceGLDelegate {
return matrix;
}

virtual flow::ExternalViewEmbedder* GetExternalViewEmbedder() {
return nullptr;
}

using GLProcResolver =
std::function<void* /* proc name */ (const char* /* proc address */)>;
virtual GLProcResolver GetGLProcResolver() const { return nullptr; }
Expand All @@ -58,6 +63,9 @@ class GPUSurfaceGL : public Surface {
// |shell::Surface|
GrContext* GetContext() override;

// |shell::Surface|
flow::ExternalViewEmbedder* GetExternalViewEmbedder() override;

private:
GPUSurfaceGLDelegate* delegate_;
GPUSurfaceGLDelegate::GLProcResolver proc_resolver_;
Expand Down
10 changes: 10 additions & 0 deletions shell/gpu/gpu_surface_software.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

namespace shell {

flow::ExternalViewEmbedder*
GPUSurfaceSoftwareDelegate::GetExternalViewEmbedder() {
return nullptr;
}

GPUSurfaceSoftware::GPUSurfaceSoftware(GPUSurfaceSoftwareDelegate* delegate)
: delegate_(delegate), weak_factory_(this) {}

Expand Down Expand Up @@ -75,4 +80,9 @@ GrContext* GPUSurfaceSoftware::GetContext() {
return nullptr;
}

// |shell::Surface|
flow::ExternalViewEmbedder* GPUSurfaceSoftware::GetExternalViewEmbedder() {
return delegate_->GetExternalViewEmbedder();
}

} // namespace shell
6 changes: 6 additions & 0 deletions shell/gpu/gpu_surface_software.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef FLUTTER_SHELL_GPU_GPU_SURFACE_SOFTWARE_H_
#define FLUTTER_SHELL_GPU_GPU_SURFACE_SOFTWARE_H_

#include "flutter/flow/embedded_views.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/memory/weak_ptr.h"
#include "flutter/shell/common/surface.h"
Expand All @@ -17,6 +18,8 @@ class GPUSurfaceSoftwareDelegate {
virtual sk_sp<SkSurface> AcquireBackingStore(const SkISize& size) = 0;

virtual bool PresentBackingStore(sk_sp<SkSurface> backing_store) = 0;

virtual flow::ExternalViewEmbedder* GetExternalViewEmbedder();
};

class GPUSurfaceSoftware : public Surface {
Expand All @@ -37,6 +40,9 @@ class GPUSurfaceSoftware : public Surface {
// |shell::Surface|
GrContext* GetContext() override;

// |shell::Surface|
flow::ExternalViewEmbedder* GetExternalViewEmbedder() override;

private:
GPUSurfaceSoftwareDelegate* delegate_;
fml::WeakPtrFactory<GPUSurfaceSoftware> weak_factory_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,9 @@
fml::scoped_nsobject<NSObject<FlutterPlatformViewFactory>>([factory retain]);
}

void FlutterPlatformViewsController::CompositeEmbeddedView(int view_id,
const flow::EmbeddedViewParams& params) {
// TODO(amirh): implement this.
}

} // namespace shell
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMVIEWS_INTERNAL_H_
#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMVIEWS_INTERNAL_H_

#include "flutter/flow/embedded_views.h"
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
#include "flutter/shell/common/shell.h"
#include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterBinaryMessenger.h"
Expand All @@ -13,12 +14,14 @@

namespace shell {

class FlutterPlatformViewsController {
class FlutterPlatformViewsController : public flow::ExternalViewEmbedder {
public:
FlutterPlatformViewsController(NSObject<FlutterBinaryMessenger>* messenger);

void RegisterViewFactory(NSObject<FlutterPlatformViewFactory>* factory, NSString* factoryId);

void CompositeEmbeddedView(int view_id, const flow::EmbeddedViewParams& params);

private:
fml::scoped_nsobject<FlutterMethodChannel> channel_;
std::map<std::string, fml::scoped_nsobject<NSObject<FlutterPlatformViewFactory>>> factories_;
Expand Down
1 change: 1 addition & 0 deletions shell/platform/darwin/ios/framework/Source/FlutterView.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <memory>

#include "flutter/flow/embedded_views.h"
#include "flutter/fml/memory/weak_ptr.h"
#include "flutter/shell/common/shell.h"
#include "flutter/shell/platform/darwin/ios/ios_surface.h"
Expand Down
Loading

0 comments on commit df85722

Please sign in to comment.