mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
This patch allows embedders to split the Flutter layer tree into multiple chunks. These chunks are meant to be composed one on top of another. This gives embedders a chance to interleave their own contents between these chunks. The Flutter embedder API already provides hooks for the specification of textures for the Flutter engine to compose within its own hierarchy (for camera feeds, video, etc..). However, not all embedders can render the contents of such sources into textures the Flutter engine can accept. Moreover, this composition model may have overheads that are non-trivial for certain use cases. In such cases, the embedder may choose to specify multiple render target for Flutter to render into instead of just one. The use of this API allows embedders to perform composition very similar to the iOS embedder. This composition model is used on that platform for the embedding of UIKit view such and web view and map views within the Flutter hierarchy. However, do note that iOS also has threading configurations that are currently not available to custom embedders. The embedder API updates in this patch are ABI stable and existing embedders will continue to work are normal. For embedders that want to enable this composition mode, the API is designed to make it easy to opt into the same in an incremental manner. Rendering of contents into the “root” rendering surface remains unchanged. However, now the application can push “platform views” via a scene builder. These platform views need to handled by a FlutterCompositor specified in a new field at the end of the FlutterProjectArgs struct. When a new platform view in introduced within the layer tree, the compositor will ask the embedder to create a new render target for that platform view. Render targets can currently be OpenGL framebuffers, OpenGL textures or software buffers. The type of the render target returned by the embedder must be compatible with the root render surface. That is, if the root render surface is an OpenGL framebuffer, the render target for each platform view must either be a texture or a framebuffer in the same OpenGL context. New render target types as well as root renderers for newer APIs like Metal & Vulkan can and will be added in the future. The addition of these APIs will be done in an ABI & API stable manner. As Flutter renders frames, it gives the embedder a callback with information about the position of the various platform views in the effective hierarchy. The embedder is then meant to put the contents of the render targets that it setup and had previously given to the engine onto the screen (of course interleaving the contents of the platform views). Unit-tests have been added that test not only the structure and properties of layer hierarchy given to the compositor, but also the contents of the texels rendered by a test compositor using both the OpenGL and software rendering backends. Fixes b/132812775 Fixes flutter/flutter#35410
239 lines
7.2 KiB
C++
239 lines
7.2 KiB
C++
// 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_FLOW_EMBEDDED_VIEWS_H_
|
|
#define FLUTTER_FLOW_EMBEDDED_VIEWS_H_
|
|
|
|
#include <vector>
|
|
|
|
#include "flutter/fml/gpu_thread_merger.h"
|
|
#include "flutter/fml/memory/ref_counted.h"
|
|
#include "third_party/skia/include/core/SkCanvas.h"
|
|
#include "third_party/skia/include/core/SkPath.h"
|
|
#include "third_party/skia/include/core/SkPoint.h"
|
|
#include "third_party/skia/include/core/SkRRect.h"
|
|
#include "third_party/skia/include/core/SkRect.h"
|
|
#include "third_party/skia/include/core/SkSize.h"
|
|
#include "third_party/skia/include/core/SkSurface.h"
|
|
|
|
namespace flutter {
|
|
|
|
enum MutatorType { clip_rect, clip_rrect, clip_path, transform, opacity };
|
|
|
|
// Stores mutation information like clipping or transform.
|
|
//
|
|
// The `type` indicates the type of the mutation: clip_rect, transform and etc.
|
|
// Each `type` is paired with an object that supports the mutation. For example,
|
|
// if the `type` is clip_rect, `rect()` is used the represent the rect to be
|
|
// clipped. One mutation object must only contain one type of mutation.
|
|
class Mutator {
|
|
public:
|
|
Mutator(const Mutator& other) {
|
|
type_ = other.type_;
|
|
switch (other.type_) {
|
|
case clip_rect:
|
|
rect_ = other.rect_;
|
|
break;
|
|
case clip_rrect:
|
|
rrect_ = other.rrect_;
|
|
break;
|
|
case clip_path:
|
|
path_ = new SkPath(*other.path_);
|
|
break;
|
|
case transform:
|
|
matrix_ = other.matrix_;
|
|
break;
|
|
case opacity:
|
|
alpha_ = other.alpha_;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
explicit Mutator(const SkRect& rect) : type_(clip_rect), rect_(rect) {}
|
|
explicit Mutator(const SkRRect& rrect) : type_(clip_rrect), rrect_(rrect) {}
|
|
explicit Mutator(const SkPath& path)
|
|
: type_(clip_path), path_(new SkPath(path)) {}
|
|
explicit Mutator(const SkMatrix& matrix)
|
|
: type_(transform), matrix_(matrix) {}
|
|
explicit Mutator(const int& alpha) : type_(opacity), alpha_(alpha) {}
|
|
|
|
const MutatorType& GetType() const { return type_; }
|
|
const SkRect& GetRect() const { return rect_; }
|
|
const SkRRect& GetRRect() const { return rrect_; }
|
|
const SkPath& GetPath() const { return *path_; }
|
|
const SkMatrix& GetMatrix() const { return matrix_; }
|
|
const int& GetAlpha() const { return alpha_; }
|
|
float GetAlphaFloat() const { return (alpha_ / 255.0); }
|
|
|
|
bool operator==(const Mutator& other) const {
|
|
if (type_ != other.type_) {
|
|
return false;
|
|
}
|
|
switch (type_) {
|
|
case clip_rect:
|
|
return rect_ == other.rect_;
|
|
case clip_rrect:
|
|
return rrect_ == other.rrect_;
|
|
case clip_path:
|
|
return *path_ == *other.path_;
|
|
case transform:
|
|
return matrix_ == other.matrix_;
|
|
case opacity:
|
|
return alpha_ == other.alpha_;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
bool operator!=(const Mutator& other) const { return !operator==(other); }
|
|
|
|
bool IsClipType() {
|
|
return type_ == clip_rect || type_ == clip_rrect || type_ == clip_path;
|
|
}
|
|
|
|
~Mutator() {
|
|
if (type_ == clip_path) {
|
|
delete path_;
|
|
}
|
|
};
|
|
|
|
private:
|
|
MutatorType type_;
|
|
|
|
union {
|
|
SkRect rect_;
|
|
SkRRect rrect_;
|
|
SkMatrix matrix_;
|
|
SkPath* path_;
|
|
int alpha_;
|
|
};
|
|
|
|
}; // Mutator
|
|
|
|
// A stack of mutators that can be applied to an embedded platform view.
|
|
//
|
|
// The stack may include mutators like transforms and clips, each mutator
|
|
// applies to all the mutators that are below it in the stack and to the
|
|
// embedded view.
|
|
//
|
|
// For example consider the following stack: [T1, T2, T3], where T1 is the top
|
|
// of the stack and T3 is the bottom of the stack. Applying this mutators stack
|
|
// to a platform view P1 will result in T1(T2(T2(P1))).
|
|
class MutatorsStack {
|
|
public:
|
|
MutatorsStack() = default;
|
|
|
|
void PushClipRect(const SkRect& rect);
|
|
void PushClipRRect(const SkRRect& rrect);
|
|
void PushClipPath(const SkPath& path);
|
|
void PushTransform(const SkMatrix& matrix);
|
|
void PushOpacity(const int& alpha);
|
|
|
|
// Removes the `Mutator` on the top of the stack
|
|
// and destroys it.
|
|
void Pop();
|
|
|
|
// Returns an iterator pointing to the top of the stack.
|
|
const std::vector<std::shared_ptr<Mutator>>::const_reverse_iterator Top()
|
|
const;
|
|
// Returns an iterator pointing to the bottom of the stack.
|
|
const std::vector<std::shared_ptr<Mutator>>::const_reverse_iterator Bottom()
|
|
const;
|
|
|
|
bool operator==(const MutatorsStack& other) const {
|
|
if (vector_.size() != other.vector_.size()) {
|
|
return false;
|
|
}
|
|
for (size_t i = 0; i < vector_.size(); i++) {
|
|
if (*vector_[i] != *other.vector_[i]) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool operator!=(const MutatorsStack& other) const {
|
|
return !operator==(other);
|
|
}
|
|
|
|
private:
|
|
std::vector<std::shared_ptr<Mutator>> vector_;
|
|
}; // MutatorsStack
|
|
|
|
class EmbeddedViewParams {
|
|
public:
|
|
EmbeddedViewParams() = default;
|
|
|
|
EmbeddedViewParams(const EmbeddedViewParams& other) {
|
|
offsetPixels = other.offsetPixels;
|
|
sizePoints = other.sizePoints;
|
|
mutatorsStack = other.mutatorsStack;
|
|
};
|
|
|
|
SkPoint offsetPixels;
|
|
SkSize sizePoints;
|
|
MutatorsStack mutatorsStack;
|
|
|
|
bool operator==(const EmbeddedViewParams& other) const {
|
|
return offsetPixels == other.offsetPixels &&
|
|
sizePoints == other.sizePoints &&
|
|
mutatorsStack == other.mutatorsStack;
|
|
}
|
|
};
|
|
|
|
enum class PostPrerollResult { kResubmitFrame, kSuccess };
|
|
|
|
// This is only used on iOS when running in a non headless mode,
|
|
// in this case ExternalViewEmbedder is a reference to the
|
|
// FlutterPlatformViewsController which is owned by FlutterViewController.
|
|
class ExternalViewEmbedder {
|
|
// TODO(cyanglaz): Make embedder own the `EmbeddedViewParams`.
|
|
|
|
public:
|
|
ExternalViewEmbedder() = default;
|
|
|
|
virtual ~ExternalViewEmbedder() = default;
|
|
|
|
// Usually, the root surface is not owned by the view embedder. However, if
|
|
// the view embedder wants to provide a surface to the rasterizer, it may
|
|
// return one here. This surface takes priority over the surface materialized
|
|
// from the on-screen render target.
|
|
virtual sk_sp<SkSurface> GetRootSurface() = 0;
|
|
|
|
// Call this in-lieu of |SubmitFrame| to clear pre-roll state and
|
|
// sets the stage for the next pre-roll.
|
|
virtual void CancelFrame() = 0;
|
|
|
|
virtual void BeginFrame(SkISize frame_size, GrContext* context) = 0;
|
|
|
|
virtual void PrerollCompositeEmbeddedView(
|
|
int view_id,
|
|
std::unique_ptr<EmbeddedViewParams> params) = 0;
|
|
|
|
// This needs to get called after |Preroll| finishes on the layer tree.
|
|
// Returns kResubmitFrame if the frame needs to be processed again, this is
|
|
// after it does any requisite tasks needed to bring itself to a valid state.
|
|
// Returns kSuccess if the view embedder is already in a valid state.
|
|
virtual PostPrerollResult PostPrerollAction(
|
|
fml::RefPtr<fml::GpuThreadMerger> gpu_thread_merger) {
|
|
return PostPrerollResult::kSuccess;
|
|
}
|
|
|
|
virtual std::vector<SkCanvas*> GetCurrentCanvases() = 0;
|
|
|
|
// Must be called on the UI thread.
|
|
virtual SkCanvas* CompositeEmbeddedView(int view_id) = 0;
|
|
|
|
virtual bool SubmitFrame(GrContext* context);
|
|
|
|
FML_DISALLOW_COPY_AND_ASSIGN(ExternalViewEmbedder);
|
|
|
|
}; // ExternalViewEmbedder
|
|
|
|
} // namespace flutter
|
|
|
|
#endif // FLUTTER_FLOW_EMBEDDED_VIEWS_H_
|