5#include "flutter/shell/platform/android/external_view_embedder/external_view_embedder.h"
6#include "flutter/common/constants.h"
7#include "flutter/fml/synchronization/waitable_event.h"
8#include "flutter/fml/trace_event.h"
14 std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
15 std::shared_ptr<AndroidSurfaceFactory> surface_factory,
18 android_context_(android_context),
19 jni_facade_(
std::move(jni_facade)),
20 surface_factory_(
std::move(surface_factory)),
27 std::unique_ptr<EmbeddedViewParams>
params) {
29 "AndroidExternalViewEmbedder::PrerollCompositeEmbeddedView");
32 std::unique_ptr<EmbedderViewSlice> view;
33 view = std::make_unique<DisplayListEmbedderViewSlice>(view_bounds);
34 slices_.insert_or_assign(view_id, std::move(view));
36 composition_order_.push_back(view_id);
38 if (view_params_.count(view_id) == 1 &&
39 view_params_.at(view_id) == *
params.get()) {
47 if (slices_.count(view_id) == 1) {
48 return slices_.at(view_id)->canvas();
59 params.finalBoundingRect().y(),
60 params.finalBoundingRect().width(),
61 params.finalBoundingRect().height()
67 int64_t flutter_view_id,
69 const std::shared_ptr<impeller::AiksContext>& aiks_context,
70 std::unique_ptr<SurfaceFrame>
frame) {
71 TRACE_EVENT0(
"flutter",
"AndroidExternalViewEmbedder::SubmitFlutterView");
76 if (!FrameHasPlatformLayers()) {
81 std::unordered_map<int64_t, SkRect> overlay_layers;
83 auto current_frame_view_count = composition_order_.size();
89 for (
size_t i = 0;
i < current_frame_view_count;
i++) {
90 int64_t view_id = composition_order_[
i];
92 if (slice->
canvas() ==
nullptr) {
106 for (ssize_t j =
i; j >= 0; j--) {
107 int64_t current_view_id = composition_order_[j];
112 std::vector<SkIRect> intersection_rects =
123 partial_joined_rect.
intersect(current_view_rect);
126 full_joined_rect.
join(partial_joined_rect);
128 if (!full_joined_rect.
isEmpty()) {
136 overlay_layers.insert({view_id, full_joined_rect});
139 background_canvas->
ClipRect(full_joined_rect,
153 auto should_submit_current_frame = previous_frame_view_count_ > 0;
154 if (should_submit_current_frame) {
158 for (int64_t view_id : composition_order_) {
163 jni_facade_->FlutterViewOnDisplayPlatformView(
169 params.sizePoints().width() * device_pixel_ratio_,
170 params.sizePoints().height() * device_pixel_ratio_,
173 std::unordered_map<int64_t, SkRect>::const_iterator overlay =
174 overlay_layers.find(view_id);
175 if (overlay == overlay_layers.end()) {
178 std::unique_ptr<SurfaceFrame>
frame =
179 CreateSurfaceIfNeeded(context,
181 slices_.at(view_id).get(),
184 if (should_submit_current_frame) {
191std::unique_ptr<SurfaceFrame>
192AndroidExternalViewEmbedder::CreateSurfaceIfNeeded(
GrDirectContext* context,
196 std::shared_ptr<OverlayLayer> layer = surface_pool_->GetLayer(
197 context, android_context_, jni_facade_, surface_factory_);
199 std::unique_ptr<SurfaceFrame>
frame =
200 layer->surface->AcquireFrame(frame_size_);
203 jni_facade_->FlutterViewDisplayOverlaySurface(layer->id,
221 if (!FrameHasPlatformLayers()) {
224 if (!raster_thread_merger->
IsMerged()) {
234 raster_thread_merger->
MergeWithLease(kDefaultMergedLeaseDuration);
237 raster_thread_merger->
ExtendLeaseTo(kDefaultMergedLeaseDuration);
240 if (previous_frame_view_count_ == 0) {
246bool AndroidExternalViewEmbedder::FrameHasPlatformLayers() {
247 return !composition_order_.empty();
256void AndroidExternalViewEmbedder::Reset() {
257 previous_frame_view_count_ = composition_order_.size();
259 composition_order_.clear();
269 jni_facade_->FlutterViewBeginFrame();
276 double device_pixel_ratio) {
281 if (frame_size_ != frame_size) {
284 surface_pool_->SetFrameSize(frame_size);
286 frame_size_ = frame_size;
287 device_pixel_ratio_ = device_pixel_ratio;
297 bool should_resubmit_frame,
299 surface_pool_->RecycleLayers();
302 jni_facade_->FlutterViewEndFrame();
317void AndroidExternalViewEmbedder::DestroySurfaces() {
318 if (!surface_pool_->HasLayers()) {
324 surface_pool_->DestroyLayers(jni_facade_);
Holds state that is shared across Android surfaces.
void EndFrame(bool should_resubmit_frame, const fml::RefPtr< fml::RasterThreadMerger > &raster_thread_merger) override
PostPrerollResult PostPrerollAction(const fml::RefPtr< fml::RasterThreadMerger > &raster_thread_merger) override
void PrerollCompositeEmbeddedView(int64_t view_id, std::unique_ptr< flutter::EmbeddedViewParams > params) override
void CancelFrame() override
void SubmitFlutterView(int64_t flutter_view_id, GrDirectContext *context, const std::shared_ptr< impeller::AiksContext > &aiks_context, std::unique_ptr< SurfaceFrame > frame) override
DlCanvas * CompositeEmbeddedView(int64_t view_id) override
void BeginFrame(GrDirectContext *context, const fml::RefPtr< fml::RasterThreadMerger > &raster_thread_merger) override
AndroidExternalViewEmbedder(const AndroidContext &android_context, std::shared_ptr< PlatformViewAndroidJNI > jni_facade, std::shared_ptr< AndroidSurfaceFactory > surface_factory, const TaskRunners &task_runners)
SkRect GetViewRect(int64_t view_id) const
bool SupportsDynamicThreadMerging() override
DlCanvas * GetRootCanvas() override
void PrepareFlutterView(SkISize frame_size, double device_pixel_ratio) override
Developer-facing API for rendering anything within the engine.
virtual void ClipRect(const SkRect &rect, ClipOp clip_op=ClipOp::kIntersect, bool is_aa=false)=0
virtual void Translate(SkScalar tx, SkScalar ty)=0
void Clear(DlColor color)
std::vector< SkIRect > getRects(bool deband=true) const
virtual void render_into(DlCanvas *canvas)=0
virtual DlCanvas * canvas()=0
virtual void end_recording()=0
DlRegion region(const SkRect &query) const
fml::RefPtr< fml::TaskRunner > GetPlatformTaskRunner() const
bool IsOnPlatformThread() const
void ExtendLeaseTo(size_t lease_term)
void MergeWithLease(size_t lease_term)
static void RunNowOrPostTask(const fml::RefPtr< fml::TaskRunner > &runner, const fml::closure &task)
const EmbeddedViewParams * params
TaskRunners task_runners_
#define FML_DCHECK(condition)
sk_sp< SkBlender > blender SkRect rect
constexpr int64_t kFlutterImplicitViewId
static SkRect Make(const SkISize &size)
static constexpr SkRect MakeEmpty()
bool intersect(const SkRect &r)
constexpr float x() const
constexpr float y() const
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
void roundOut(SkIRect *dst) const
constexpr float height() const
constexpr float width() const
void join(const SkRect &r)
void set(const SkIRect &src)
static constexpr DlColor kTransparent()
#define TRACE_EVENT0(category_group, name)