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;
641 std::unique_ptr<GpuImageResult> MakeSkiaGpuImage(
646 void MakeRasterSnapshot(
653 SkISize picture_size)
override;
659 void CacheRuntimeStage(
660 const std::shared_ptr<impeller::RuntimeStage>& runtime_stage)
override;
669 const std::unique_ptr<Surface>& GetSurface()
const override {
674 std::shared_ptr<impeller::AiksContext> GetAiksContext()
const override {
675#if IMPELLER_SUPPORTS_RENDERING
677 return surface_->GetAiksContext();
679 if (
auto context = impeller_context_.lock()) {
680 return std::make_shared<impeller::AiksContext>(
688 const std::unique_ptr<SnapshotSurfaceProducer>& GetSnapshotSurfaceProducer()
690 return snapshot_surface_producer_;
694 std::shared_ptr<const fml::SyncSwitch> GetIsGpuDisabledSyncSwitch()
708 std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder,
709 std::vector<std::unique_ptr<LayerTreeTask>> tasks);
712 DoDrawResult DrawToSurfaces(
713 FrameTimingsRecorder& frame_timings_recorder,
714 std::vector<std::unique_ptr<LayerTreeTask>> tasks);
726 std::unique_ptr<FrameItem> DrawToSurfacesUnsafe(
727 FrameTimingsRecorder& frame_timings_recorder,
728 std::vector<std::unique_ptr<LayerTreeTask>> tasks);
738 float device_pixel_ratio,
739 std::optional<fml::TimePoint> presentation_time);
741 ViewRecord& EnsureViewRecord(int64_t view_id);
743 void FireNextFrameCallbackIfPresent();
745 static bool ShouldResubmitFrame(
const DoDrawResult&
result);
746 static DrawStatus ToDrawStatus(DoDrawStatus status);
748 bool is_torn_down_ =
false;
751 std::weak_ptr<impeller::Context> impeller_context_;
752 std::unique_ptr<Surface> surface_;
753 std::unique_ptr<SnapshotSurfaceProducer> snapshot_surface_producer_;
754 std::unique_ptr<flutter::CompositorContext> compositor_context_;
755 std::unordered_map<int64_t, ViewRecord> view_records_;
757 bool user_override_resource_cache_bytes_ =
false;
758 std::optional<size_t> max_cache_bytes_;
760 std::shared_ptr<ExternalViewEmbedder> external_view_embedder_;
761 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
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.
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.
~Rasterizer()
Destroys the rasterizer. This must happen on the raster task runner. All GPU resources are collected ...
Rasterizer(Delegate &delegate, MakeGpuImageBehavior gpu_image_behavior=MakeGpuImageBehavior::kGpu)
Creates a new instance of a rasterizer. Rasterizers may only be created on the raster task runner....
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...
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::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....
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
Dart_NativeFunction function
sk_sp< const SkImage > image
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.