5#ifndef FLUTTER_SHELL_COMMON_RASTERIZER_H_
6#define FLUTTER_SHELL_COMMON_RASTERIZER_H_
10#include <unordered_map>
12#include "flutter/common/settings.h"
13#include "flutter/common/task_runners.h"
14#include "flutter/display_list/image/dl_image.h"
15#include "flutter/flow/compositor_context.h"
16#include "flutter/flow/embedded_views.h"
17#include "flutter/flow/frame_timings.h"
18#include "flutter/flow/layers/layer_tree.h"
19#include "flutter/flow/surface.h"
20#include "flutter/fml/closure.h"
21#include "flutter/fml/memory/weak_ptr.h"
22#include "flutter/fml/raster_thread_merger.h"
23#include "flutter/fml/synchronization/sync_switch.h"
24#include "flutter/fml/synchronization/waitable_event.h"
25#include "flutter/fml/time/time_delta.h"
26#include "flutter/fml/time/time_point.h"
27#if IMPELLER_SUPPORTS_RENDERING
33#include "flutter/lib/ui/snapshot_delegate.h"
34#include "flutter/shell/common/pipeline.h"
35#include "flutter/shell/common/snapshot_controller.h"
36#include "flutter/shell/common/snapshot_surface_producer.h"
43#if !IMPELLER_SUPPORTS_RENDERING
90 FrameItem(std::vector<std::unique_ptr<LayerTreeTask>> tasks,
305 std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder);
439 const std::string& p_format,
496 const std::shared_ptr<ExternalViewEmbedder>& view_embedder);
507 std::unique_ptr<SnapshotSurfaceProducer> producer);
516 return compositor_context_.get();
606 enum class DoDrawStatus {
620 struct DoDrawResult {
624 DoDrawStatus status = DoDrawStatus::kDone;
632 std::unique_ptr<FrameItem> resubmitted_item;
636 std::unique_ptr<LayerTreeTask> last_successful_task;
637 std::optional<DrawSurfaceStatus> last_draw_status;
647 SkISize picture_size)
override;
654 const std::shared_ptr<impeller::RuntimeStage>& runtime_stage)
override;
663 const std::unique_ptr<Surface>&
GetSurface()
const override {
669#if IMPELLER_SUPPORTS_RENDERING
671 return surface_->GetAiksContext();
673 if (
auto context = impeller_context_.lock()) {
674 return std::make_shared<impeller::AiksContext>(
684 return snapshot_surface_producer_;
702 std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder,
703 std::vector<std::unique_ptr<LayerTreeTask>> tasks);
706 DoDrawResult DrawToSurfaces(
708 std::vector<std::unique_ptr<LayerTreeTask>> tasks);
720 std::unique_ptr<FrameItem> DrawToSurfacesUnsafe(
722 std::vector<std::unique_ptr<LayerTreeTask>> tasks);
732 float device_pixel_ratio,
733 std::optional<fml::TimePoint> presentation_time);
735 ViewRecord& EnsureViewRecord(int64_t view_id);
737 void FireNextFrameCallbackIfPresent();
739 static bool ShouldResubmitFrame(
const DoDrawResult&
result);
740 static DrawStatus ToDrawStatus(DoDrawStatus status);
742 bool is_torn_down_ =
false;
745 std::weak_ptr<impeller::Context> impeller_context_;
746 std::unique_ptr<Surface> surface_;
747 std::unique_ptr<SnapshotSurfaceProducer> snapshot_surface_producer_;
748 std::unique_ptr<flutter::CompositorContext> compositor_context_;
749 std::unordered_map<int64_t, ViewRecord> view_records_;
751 bool user_override_resource_cache_bytes_ =
false;
752 std::optional<size_t> max_cache_bytes_;
754 std::shared_ptr<ExternalViewEmbedder> external_view_embedder_;
755 std::unique_ptr<SnapshotController> snapshot_controller_;
Used to forward events from the rasterizer to interested subsystems. Currently, the shell sets itself...
virtual const TaskRunners & GetTaskRunners() const =0
Task runners used by the shell.
virtual void OnFrameRasterized(const FrameTiming &frame_timing)=0
Notifies the delegate that a frame has been rendered. The rasterizer collects profiling information f...
virtual const Settings & GetSettings() const =0
virtual std::shared_ptr< const fml::SyncSwitch > GetIsGpuDisabledSyncSwitch() const =0
virtual const fml::RefPtr< fml::RasterThreadMerger > GetParentRasterThreadMerger() const =0
The raster thread merger from parent shell's rasterizer.
virtual bool ShouldDiscardLayerTree(int64_t view_id, const flutter::LayerTree &tree)=0
virtual fml::Milliseconds GetFrameBudget()=0
virtual fml::TimePoint GetLatestFrameTargetTime() const =0
const std::unique_ptr< Surface > & GetSurface() const override
void SetNextFrameCallback(const fml::closure &callback)
Sets a callback that will be executed when the next layer tree in rendered to the on-screen surface....
fml::RefPtr< fml::RasterThreadMerger > GetRasterThreadMerger()
Returns the raster thread merger used by this rasterizer. This may be nullptr.
bool IsTornDown()
Returns whether TearDown has been called.
std::shared_ptr< impeller::AiksContext > GetAiksContext() const override
void SetSnapshotSurfaceProducer(std::unique_ptr< SnapshotSurfaceProducer > producer)
Set the snapshot surface producer. This is done on shell initialization. This is non-null on platform...
flutter::LayerTree * GetLastLayerTree(int64_t view_id)
Returns the last successfully drawn layer tree for the given view, or nullptr if there isn't any....
ScreenshotType
The type of the screenshot to obtain of the previously rendered layer tree.
void DisableThreadMergerIfNeeded()
Disables the thread merger if the external view embedder supports dynamic thread merging.
void CacheRuntimeStage(const std::shared_ptr< impeller::RuntimeStage > &runtime_stage) override
~Rasterizer()
Destroys the rasterizer. This must happen on the raster task runner. All GPU resources are collected ...
sk_sp< DlImage > MakeRasterSnapshot(sk_sp< DisplayList > display_list, SkISize picture_size) override
const std::unique_ptr< SnapshotSurfaceProducer > & GetSnapshotSurfaceProducer() const override
DrawStatus Draw(const std::shared_ptr< FramePipeline > &pipeline)
Takes the next item from the layer tree pipeline and executes the raster thread frame workload for th...
fml::TaskRunnerAffineWeakPtr< Rasterizer > GetWeakPtr() const
Gets a weak pointer to the rasterizer. The rasterizer may only be accessed on the raster task runner.
void SetResourceCacheMaxBytes(size_t max_bytes, bool from_user)
Skia has no notion of time. To work around the performance implications of this, it may cache GPU res...
std::shared_ptr< const fml::SyncSwitch > GetIsGpuDisabledSyncSwitch() const override
void DrawLastLayerTrees(std::unique_ptr< FrameTimingsRecorder > frame_timings_recorder)
Draws the last layer trees with their last configuration. This may seem entirely redundant at first g...
MakeGpuImageBehavior
How to handle calls to MakeSkiaGpuImage.
@ kGpu
MakeSkiaGpuImage returns a GPU resident image, if possible.
void TeardownExternalViewEmbedder()
Releases any resource used by the external view embedder. For example, overlay surfaces or Android vi...
void EnableThreadMergerIfNeeded()
Enables the thread merger if the external view embedder supports dynamic thread merging.
fml::Milliseconds GetFrameBudget() const override
sk_sp< SkImage > ConvertToRasterImage(sk_sp< SkImage > image) override
fml::TaskRunnerAffineWeakPtr< SnapshotDelegate > GetSnapshotDelegate() const
std::shared_ptr< flutter::TextureRegistry > GetTextureRegistry() override
Gets the registry of external textures currently in use by the rasterizer. These textures may be upda...
std::optional< size_t > GetResourceCacheMaxBytes() const
The current value of Skia's resource cache size, if a surface is present.
void Teardown()
Releases the previously set up on-screen render surface and collects associated resources....
std::unique_ptr< GpuImageResult > MakeSkiaGpuImage(sk_sp< DisplayList > display_list, const SkImageInfo &image_info) override
Attempts to create a GrBackendTexture for the specified DisplayList. May result in a raster bitmap if...
GrDirectContext * GetGrContext() override
void CollectView(int64_t view_id)
Deallocate the resources for displaying a view.
void Setup(std::unique_ptr< Surface > surface)
Rasterizers may be created well before an on-screen surface is available for rendering....
std::optional< DrawSurfaceStatus > GetLastDrawStatus(int64_t view_id)
Returns the last status of drawing the specific view.
void NotifyLowMemoryWarning() const
Notifies the rasterizer that there is a low memory situation and it must purge as many unnecessary re...
void SetExternalViewEmbedder(const std::shared_ptr< ExternalViewEmbedder > &view_embedder)
Set the External View Embedder. This is done on shell initialization. This is non-null on platforms t...
Screenshot ScreenshotLastLayerTree(ScreenshotType type, bool base64_encode)
Screenshots the last layer tree to one of the supported screenshot types and optionally Base 64 encod...
void SetImpellerContext(std::weak_ptr< impeller::Context > impeller_context)
flutter::CompositorContext * compositor_context()
Returns a pointer to the compositor context used by this rasterizer. This pointer will never be nullp...
The refresh rate interface for Stopwatch.
static std::shared_ptr< TypographerContext > Make()
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
std::chrono::duration< double, std::milli > Milliseconds
std::function< void()> closure
static constexpr SkISize MakeEmpty()
std::unique_ptr< FrameTimingsRecorder > frame_timings_recorder
FrameItem(std::vector< std::unique_ptr< LayerTreeTask > > tasks, std::unique_ptr< FrameTimingsRecorder > frame_timings_recorder)
std::vector< std::unique_ptr< LayerTreeTask > > layer_tree_tasks
A POD type used to return the screenshot data along with the size of the frame.
ScreenshotFormat pixel_format
Screenshot(const Screenshot &other)
The copy constructor for a screenshot.
Screenshot()
Creates an empty screenshot.
~Screenshot()
Destroys the screenshot object and releases underlying data.