Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
rasterizer.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_SHELL_COMMON_RASTERIZER_H_
6#define FLUTTER_SHELL_COMMON_RASTERIZER_H_
7
8#include <memory>
9#include <optional>
10#include <unordered_map>
11
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
28#include "impeller/aiks/aiks_context.h" // nogncheck
29#include "impeller/core/formats.h" // nogncheck
30#include "impeller/renderer/context.h" // nogncheck
32#endif // 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"
42
43#if !IMPELLER_SUPPORTS_RENDERING
44namespace impeller {
45class Context;
46class AiksContext;
47} // namespace impeller
48#endif // !IMPELLER_SUPPORTS_RENDERING
49
50namespace flutter {
51
52// The result status of Rasterizer::Draw. This is only used for unit tests.
53enum class DrawStatus {
54 // The drawing was done without any specified status.
55 kDone,
56 // Failed to rasterize the frame because the Rasterizer is not set up.
58 // Nothing was done, because the call was not on the raster thread. Yielded to
59 // let this frame be serviced on the right thread.
61 // Nothing was done, because the pipeline was empty.
63 // Nothing was done, because the GPU was unavailable.
65};
66
67// The result status of drawing to a view. This is only used for unit tests.
69 // The layer tree was successfully rasterized.
71 // The layer tree must be submitted again.
72 //
73 // This can occur on Android when switching the background surface to
74 // FlutterImageView. On Android, the first frame doesn't make the image
75 // available to the ImageReader right away. The second frame does.
76 // TODO(egarciad): https://github.com/flutter/flutter/issues/65652
77 //
78 // This can also occur when the frame is dropped to wait for the thread
79 // merger to merge the raster and platform threads.
80 kRetry,
81 // Failed to rasterize the frame.
82 kFailed,
83 // Layer tree was discarded because its size does not match the view size.
84 // This typically occurs during resizing.
86};
87
88// The information to draw to all views of a frame.
89struct FrameItem {
90 FrameItem(std::vector<std::unique_ptr<LayerTreeTask>> tasks,
91 std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder)
92 : layer_tree_tasks(std::move(tasks)),
94 std::vector<std::unique_ptr<LayerTreeTask>> layer_tree_tasks;
95 std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder;
96};
97
99
100//------------------------------------------------------------------------------
101/// The rasterizer is a component owned by the shell that resides on the raster
102/// task runner. Each shell owns exactly one instance of a rasterizer. The
103/// rasterizer may only be created, used and collected on the raster task
104/// runner.
105///
106/// The rasterizer owns the instance of the currently active on-screen render
107/// surface. On this surface, it renders the contents of layer trees submitted
108/// to it by the `Engine` (which lives on the UI task runner).
109///
110/// The primary components owned by the rasterizer are the compositor context
111/// and the on-screen render surface. The compositor context has all the GPU
112/// state necessary to render frames to the render surface.
113///
114class Rasterizer final : public SnapshotDelegate,
117 public:
118 //----------------------------------------------------------------------------
119 /// @brief Used to forward events from the rasterizer to interested
120 /// subsystems. Currently, the shell sets itself up as the
121 /// rasterizer delegate to listen for frame rasterization events.
122 /// It can then forward these events to the engine.
123 ///
124 /// Like all rasterizer operation, the rasterizer delegate call
125 /// are made on the raster task runner. Any delegate must ensure
126 /// that they can handle the threading implications.
127 ///
128 class Delegate {
129 public:
130 //--------------------------------------------------------------------------
131 /// @brief Notifies the delegate that a frame has been rendered. The
132 /// rasterizer collects profiling information for each part of
133 /// the frame workload. This profiling information is made
134 /// available to the delegate for forwarding to subsystems
135 /// interested in collecting such profiles. Currently, the shell
136 /// (the delegate) forwards this to the engine where Dart code
137 /// can react to this information.
138 ///
139 /// @see `FrameTiming`
140 ///
141 /// @param[in] frame_timing Instrumentation information for each phase of
142 /// the frame workload.
143 ///
144 virtual void OnFrameRasterized(const FrameTiming& frame_timing) = 0;
145
146 /// Time limit for a smooth frame.
147 ///
148 /// See: `DisplayManager::GetMainDisplayRefreshRate`.
150
151 /// Target time for the latest frame. See also `Shell::OnAnimatorBeginFrame`
152 /// for when this time gets updated.
154
155 /// Task runners used by the shell.
156 virtual const TaskRunners& GetTaskRunners() const = 0;
157
158 /// The raster thread merger from parent shell's rasterizer.
161
162 /// Accessor for the shell's GPU sync switch, which determines whether GPU
163 /// operations are allowed on the current thread.
164 ///
165 /// For example, on some platforms when the application is backgrounded it
166 /// is critical that GPU operations are not processed.
167 virtual std::shared_ptr<const fml::SyncSwitch> GetIsGpuDisabledSyncSwitch()
168 const = 0;
169
170 virtual const Settings& GetSettings() const = 0;
171
172 virtual bool ShouldDiscardLayerTree(int64_t view_id,
173 const flutter::LayerTree& tree) = 0;
174 };
175
176 //----------------------------------------------------------------------------
177 /// @brief How to handle calls to MakeSkiaGpuImage.
179 /// MakeSkiaGpuImage returns a GPU resident image, if possible.
180 kGpu,
181 /// MakeSkiaGpuImage returns a checkerboard bitmap. This is useful in test
182 /// contexts where no GPU surface is available.
183 kBitmap,
184 };
185
186 //----------------------------------------------------------------------------
187 /// @brief Creates a new instance of a rasterizer. Rasterizers may only
188 /// be created on the raster task runner. Rasterizers are
189 /// currently only created by the shell (which also sets itself up
190 /// as the rasterizer delegate).
191 ///
192 /// @param[in] delegate The rasterizer delegate.
193 /// @param[in] gpu_image_behavior How to handle calls to
194 /// MakeSkiaGpuImage.
195 ///
196 explicit Rasterizer(
197 Delegate& delegate,
199
200 //----------------------------------------------------------------------------
201 /// @brief Destroys the rasterizer. This must happen on the raster task
202 /// runner. All GPU resources are collected before this call
203 /// returns. Any context set up by the embedder to hold these
204 /// resources can be immediately collected as well.
205 ///
207
208 void SetImpellerContext(std::weak_ptr<impeller::Context> impeller_context);
209
210 //----------------------------------------------------------------------------
211 /// @brief Rasterizers may be created well before an on-screen surface is
212 /// available for rendering. Shells usually create a rasterizer in
213 /// their constructors. Once an on-screen surface is available
214 /// however, one may be provided to the rasterizer using this
215 /// call. No rendering may occur before this call. The surface is
216 /// held till the balancing call to `Rasterizer::Teardown` is
217 /// made. Calling a setup before tearing down the previous surface
218 /// (if this is not the first time the surface has been set up) is
219 /// user error.
220 ///
221 /// @see `Rasterizer::Teardown`
222 ///
223 /// @param[in] surface The on-screen render surface.
224 ///
225 void Setup(std::unique_ptr<Surface> surface);
226
227 //----------------------------------------------------------------------------
228 /// @brief Releases the previously set up on-screen render surface and
229 /// collects associated resources. No more rendering may occur
230 /// till the next call to `Rasterizer::Setup` with a new render
231 /// surface. Calling a teardown without a setup is user error.
232 /// Calling this method multiple times is safe.
233 ///
234 void Teardown();
235
236 //----------------------------------------------------------------------------
237 /// @brief Releases any resource used by the external view embedder.
238 /// For example, overlay surfaces or Android views.
239 /// On Android, this method post a task to the platform thread,
240 /// and waits until it completes.
242
243 //----------------------------------------------------------------------------
244 /// @brief Notifies the rasterizer that there is a low memory situation
245 /// and it must purge as many unnecessary resources as possible.
246 /// Currently, the Skia context associated with onscreen rendering
247 /// is told to free GPU resources.
248 ///
249 void NotifyLowMemoryWarning() const;
250
251 //----------------------------------------------------------------------------
252 /// @brief Gets a weak pointer to the rasterizer. The rasterizer may only
253 /// be accessed on the raster task runner.
254 ///
255 /// @return The weak pointer to the rasterizer.
256 ///
258
260
261 //----------------------------------------------------------------------------
262 /// @brief Deallocate the resources for displaying a view.
263 ///
264 /// This method must be called on the raster task runner when a
265 /// view is removed from the engine.
266 ///
267 /// When the rasterizer is requested to draw an unrecognized view,
268 /// it implicitly allocates necessary resources. These resources
269 /// must be explicitly deallocated.
270 ///
271 /// @param[in] view_id The ID of the view.
272 ///
273 void CollectView(int64_t view_id);
274
275 //----------------------------------------------------------------------------
276 /// @brief Returns the last successfully drawn layer tree for the given
277 /// view, or nullptr if there isn't any. This is useful during
278 /// `DrawLastLayerTrees` and computing frame damage.
279 ///
280 /// @bug https://github.com/flutter/flutter/issues/33939
281 ///
282 /// @return A pointer to the last layer or `nullptr` if this rasterizer
283 /// has never rendered a frame to the given view.
284 ///
285 flutter::LayerTree* GetLastLayerTree(int64_t view_id);
286
287 //----------------------------------------------------------------------------
288 /// @brief Draws the last layer trees with their last configuration. This
289 /// may seem entirely redundant at first glance. After all, on
290 /// surface loss and re-acquisition, the framework generates a new
291 /// layer tree. Otherwise, why render the same contents to the
292 /// screen again? This is used as an optimization in cases where
293 /// there are external textures (video or camera streams for
294 /// example) in referenced in the layer tree. These textures may
295 /// be updated at a cadence different from that of the Flutter
296 /// application. Flutter can re-render the layer tree with just
297 /// the updated textures instead of waiting for the framework to
298 /// do the work to generate the layer tree describing the same
299 /// contents.
300 ///
301 /// Calling this method clears all last layer trees
302 /// (GetLastLayerTree).
303 ///
305 std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder);
306
307 // |SnapshotDelegate|
308 GrDirectContext* GetGrContext() override;
309
310 std::shared_ptr<flutter::TextureRegistry> GetTextureRegistry() override;
311
312 //----------------------------------------------------------------------------
313 /// @brief Takes the next item from the layer tree pipeline and executes
314 /// the raster thread frame workload for that pipeline item to
315 /// render a frame on the on-screen surface.
316 ///
317 /// Why does the draw call take a layer tree pipeline and not the
318 /// layer tree directly?
319 ///
320 /// The pipeline is the way book-keeping of frame workloads
321 /// distributed across the multiple threads is managed. The
322 /// rasterizer deals with the pipelines directly (instead of layer
323 /// trees which is what it actually renders) because the pipeline
324 /// consumer's workload must be accounted for within the pipeline
325 /// itself. If the rasterizer took the layer tree directly, it
326 /// would have to be taken out of the pipeline. That would signal
327 /// the end of the frame workload and the pipeline would be ready
328 /// for new frames. But the last frame has not been rendered by
329 /// the frame yet! On the other hand, the pipeline must own the
330 /// layer tree it renders because it keeps a reference to the last
331 /// layer tree around till a new frame is rendered. So a simple
332 /// reference wont work either. The `Rasterizer::DoDraw` method
333 /// actually performs the GPU operations within the layer tree
334 /// pipeline.
335 ///
336 /// @see `Rasterizer::DoDraw`
337 ///
338 /// @param[in] pipeline The layer tree pipeline to take the next layer tree
339 /// to render from.
340 ///
341 DrawStatus Draw(const std::shared_ptr<FramePipeline>& pipeline);
342
343 //----------------------------------------------------------------------------
344 /// @brief The type of the screenshot to obtain of the previously
345 /// rendered layer tree.
346 ///
347 enum class ScreenshotType {
348 // NOLINTBEGIN(readability-identifier-naming)
349 //--------------------------------------------------------------------------
350 /// A format used to denote a Skia picture. A Skia picture is a serialized
351 /// representation of an `SkPicture` that can be used to introspect the
352 /// series of commands used to draw that picture.
353 ///
354 /// Skia pictures are typically stored as files with the .skp extension on
355 /// disk. These files may be viewed in an interactive debugger available at
356 /// https://debugger.skia.org/
357 ///
359
360 //--------------------------------------------------------------------------
361 /// A format used to denote uncompressed image data. For Skia, this format
362 /// is 32 bits per pixel, 8 bits per component and
363 /// denoted by the `kN32_SkColorType ` Skia color type. For Impeller, its
364 /// format is specified in Screenshot::pixel_format.
365 ///
367
368 //--------------------------------------------------------------------------
369 /// A format used to denote compressed image data. The PNG compressed
370 /// container is used.
371 ///
373
374 //--------------------------------------------------------------------------
375 /// Reads the data directly from the Rasterizer's surface. The pixel format
376 /// is determined from the surface. This is the only way to read wide gamut
377 /// color data, but isn't supported everywhere.
379 // NOLINTEND(readability-identifier-naming)
380 };
381
382 // Specifies the format of pixel data in a Screenshot.
383 enum class ScreenshotFormat {
384 // Unknown format, or Skia default.
385 kUnknown,
386 // RGBA 8 bits per channel.
388 // BGRA 8 bits per channel.
390 // RGBA 16 bit floating point per channel.
392 };
393
394 //----------------------------------------------------------------------------
395 /// @brief A POD type used to return the screenshot data along with the
396 /// size of the frame.
397 ///
398 struct Screenshot {
399 //--------------------------------------------------------------------------
400 /// The data used to describe the screenshot. The data format depends on the
401 /// type of screenshot taken and any further encoding done to the same.
402 ///
403 /// @see `ScreenshotType`
404 ///
406
407 //--------------------------------------------------------------------------
408 /// The size of the screenshot in texels.
409 ///
411
412 //--------------------------------------------------------------------------
413 /// Characterization of the format of the data in `data`.
414 ///
415 std::string format;
416
417 //--------------------------------------------------------------------------
418 /// The pixel format of the data in `data`.
419 ///
420 /// If the impeller backend is not used, this value is always kUnknown and
421 /// the data is in RGBA8888 format.
423
424 //--------------------------------------------------------------------------
425 /// @brief Creates an empty screenshot
426 ///
427 Screenshot();
428
429 //--------------------------------------------------------------------------
430 /// @brief Creates a screenshot with the specified data and size.
431 ///
432 /// @param[in] p_data The screenshot data
433 /// @param[in] p_size The screenshot size.
434 /// @param[in] p_format The screenshot format.
435 /// @param[in] p_pixel_format The screenshot format.
436 ///
438 SkISize p_size,
439 const std::string& p_format,
440 ScreenshotFormat p_pixel_format);
441
442 //--------------------------------------------------------------------------
443 /// @brief The copy constructor for a screenshot.
444 ///
445 /// @param[in] other The screenshot to copy from.
446 ///
447 Screenshot(const Screenshot& other);
448
449 //--------------------------------------------------------------------------
450 /// @brief Destroys the screenshot object and releases underlying data.
451 ///
453 };
454
455 //----------------------------------------------------------------------------
456 /// @brief Screenshots the last layer tree to one of the supported
457 /// screenshot types and optionally Base 64 encodes that data for
458 /// easier transmission and packaging (usually over the service
459 /// protocol for instrumentation tools running on the host).
460 ///
461 /// @param[in] type The type of the screenshot to gather.
462 /// @param[in] base64_encode Whether Base 64 encoding must be applied to the
463 /// data after a screenshot has been captured.
464 ///
465 /// @return A non-empty screenshot if one could be captured. A screenshot
466 /// capture may fail if there were no layer trees previously
467 /// rendered by this rasterizer, or, due to an unspecified
468 /// internal error. Internal error will be logged to the console.
469 ///
471
472 //----------------------------------------------------------------------------
473 /// @brief Sets a callback that will be executed when the next layer tree
474 /// in rendered to the on-screen surface. This is used by
475 /// embedders to listen for one time operations like listening for
476 /// when the first frame is rendered so that they may hide splash
477 /// screens.
478 ///
479 /// The callback is only executed once and dropped on the GPU
480 /// thread when executed (lambda captures must be able to deal
481 /// with the threading repercussions of this behavior).
482 ///
483 /// @param[in] callback The callback to execute when the next layer tree is
484 /// rendered on-screen.
485 ///
487
488 //----------------------------------------------------------------------------
489 /// @brief Set the External View Embedder. This is done on shell
490 /// initialization. This is non-null on platforms that support
491 /// embedding externally composited views.
492 ///
493 /// @param[in] view_embedder The external view embedder object.
494 ///
496 const std::shared_ptr<ExternalViewEmbedder>& view_embedder);
497
498 //----------------------------------------------------------------------------
499 /// @brief Set the snapshot surface producer. This is done on shell
500 /// initialization. This is non-null on platforms that support taking
501 /// GPU accelerated raster snapshots in the background.
502 ///
503 /// @param[in] producer A surface producer for raster snapshotting when the
504 /// onscreen surface is not available.
505 ///
507 std::unique_ptr<SnapshotSurfaceProducer> producer);
508
509 //----------------------------------------------------------------------------
510 /// @brief Returns a pointer to the compositor context used by this
511 /// rasterizer. This pointer will never be `nullptr`.
512 ///
513 /// @return The compositor context used by this rasterizer.
514 ///
516 return compositor_context_.get();
517 }
518
519 //----------------------------------------------------------------------------
520 /// @brief Returns the raster thread merger used by this rasterizer.
521 /// This may be `nullptr`.
522 ///
523 /// @return The raster thread merger used by this rasterizer.
524 ///
526
527 //----------------------------------------------------------------------------
528 /// @brief Skia has no notion of time. To work around the performance
529 /// implications of this, it may cache GPU resources to reference
530 /// them from one frame to the next. Using this call, embedders
531 /// may set the maximum bytes cached by Skia in its caches
532 /// dedicated to on-screen rendering.
533 ///
534 /// @attention This cache setting will be invalidated when the surface is
535 /// torn down via `Rasterizer::Teardown`. This call must be made
536 /// again with new limits after surface re-acquisition.
537 ///
538 /// @attention This cache does not describe the entirety of GPU resources
539 /// that may be cached. The `RasterCache` also holds very large
540 /// GPU resources.
541 ///
542 /// @see `RasterCache`
543 ///
544 /// @param[in] max_bytes The maximum byte size of resource that may be
545 /// cached for GPU rendering.
546 /// @param[in] from_user Whether this request was from user code, e.g. via
547 /// the flutter/skia message channel, in which case
548 /// it should not be overridden by the platform.
549 ///
550 void SetResourceCacheMaxBytes(size_t max_bytes, bool from_user);
551
552 //----------------------------------------------------------------------------
553 /// @brief The current value of Skia's resource cache size, if a surface
554 /// is present.
555 ///
556 /// @attention This cache does not describe the entirety of GPU resources
557 /// that may be cached. The `RasterCache` also holds very large
558 /// GPU resources.
559 ///
560 /// @see `RasterCache`
561 ///
562 /// @return The size of Skia's resource cache, if available.
563 ///
564 std::optional<size_t> GetResourceCacheMaxBytes() const;
565
566 //----------------------------------------------------------------------------
567 /// @brief Enables the thread merger if the external view embedder
568 /// supports dynamic thread merging.
569 ///
570 /// @attention This method is thread-safe. When the thread merger is enabled,
571 /// the raster task queue can run in the platform thread at any
572 /// time.
573 ///
574 /// @see `ExternalViewEmbedder`
575 ///
577
578 //----------------------------------------------------------------------------
579 /// @brief Disables the thread merger if the external view embedder
580 /// supports dynamic thread merging.
581 ///
582 /// @attention This method is thread-safe. When the thread merger is
583 /// disabled, the raster task queue will continue to run in the
584 /// same thread until |EnableThreadMergerIfNeeded| is called.
585 ///
586 /// @see `ExternalViewEmbedder`
587 ///
589
590 //----------------------------------------------------------------------------
591 /// @brief Returns whether TearDown has been called.
592 ///
593 /// This method is used only in unit tests.
594 ///
595 bool IsTornDown();
596
597 //----------------------------------------------------------------------------
598 /// @brief Returns the last status of drawing the specific view.
599 ///
600 /// This method is used only in unit tests.
601 ///
602 std::optional<DrawSurfaceStatus> GetLastDrawStatus(int64_t view_id);
603
604 private:
605 // The result status of DoDraw, DrawToSurfaces, and DrawToSurfacesUnsafe.
606 enum class DoDrawStatus {
607 // The drawing was done without any specified status.
608 kDone,
609 // Frame has been successfully rasterized, but there are additional items
610 // in the pipeline waiting to be consumed. This is currently only used when
611 // thread configuration change occurs.
612 kEnqueuePipeline,
613 // Failed to rasterize the frame because the Rasterizer is not set up.
614 kNotSetUp,
615 // Nothing was done, because GPU was unavailable.
617 };
618
619 // The result of DoDraw.
620 struct DoDrawResult {
621 // The overall status of the drawing process.
622 //
623 // The status of drawing a specific view is available at GetLastDrawStatus.
624 DoDrawStatus status = DoDrawStatus::kDone;
625
626 // The frame item that needs to be submitted again.
627 //
628 // See RasterStatus::kResubmit and kSkipAndRetry for when it happens.
629 //
630 // If `resubmitted_item` is not null, its `tasks` is guaranteed to be
631 // non-empty.
632 std::unique_ptr<FrameItem> resubmitted_item;
633 };
634
635 struct ViewRecord {
636 std::unique_ptr<LayerTreeTask> last_successful_task;
637 std::optional<DrawSurfaceStatus> last_draw_status;
638 };
639
640 // |SnapshotDelegate|
641 std::unique_ptr<GpuImageResult> MakeSkiaGpuImage(
642 sk_sp<DisplayList> display_list,
643 const SkImageInfo& image_info) override;
644
645 // |SnapshotDelegate|
647 SkISize picture_size) override;
648
649 // |SnapshotDelegate|
651
652 // |SnapshotDelegate|
654 const std::shared_ptr<impeller::RuntimeStage>& runtime_stage) override;
655
656 // |Stopwatch::Delegate|
657 /// Time limit for a smooth frame.
658 ///
659 /// See: `DisplayManager::GetMainDisplayRefreshRate`.
660 fml::Milliseconds GetFrameBudget() const override;
661
662 // |SnapshotController::Delegate|
663 const std::unique_ptr<Surface>& GetSurface() const override {
664 return surface_;
665 }
666
667 // |SnapshotController::Delegate|
668 std::shared_ptr<impeller::AiksContext> GetAiksContext() const override {
669#if IMPELLER_SUPPORTS_RENDERING
670 if (surface_) {
671 return surface_->GetAiksContext();
672 }
673 if (auto context = impeller_context_.lock()) {
674 return std::make_shared<impeller::AiksContext>(
676 }
677#endif
678 return nullptr;
679 }
680
681 // |SnapshotController::Delegate|
682 const std::unique_ptr<SnapshotSurfaceProducer>& GetSnapshotSurfaceProducer()
683 const override {
684 return snapshot_surface_producer_;
685 }
686
687 // |SnapshotController::Delegate|
688 std::shared_ptr<const fml::SyncSwitch> GetIsGpuDisabledSyncSwitch()
689 const override {
690 return delegate_.GetIsGpuDisabledSyncSwitch();
691 }
692
693 std::pair<sk_sp<SkData>, ScreenshotFormat> ScreenshotLayerTreeAsImage(
694 flutter::LayerTree* tree,
696 bool compressed);
697
698 // This method starts with the frame timing recorder at build end. This
699 // method might push it to raster end and get the recorded time, or abort in
700 // the middle and not get the recorded time.
701 DoDrawResult DoDraw(
702 std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder,
703 std::vector<std::unique_ptr<LayerTreeTask>> tasks);
704
705 // This method pushes the frame timing recorder from build end to raster end.
706 DoDrawResult DrawToSurfaces(
707 FrameTimingsRecorder& frame_timings_recorder,
708 std::vector<std::unique_ptr<LayerTreeTask>> tasks);
709
710 // Draws the specified layer trees to views, assuming we have access to the
711 // GPU.
712 //
713 // If any layer trees need resubmitting, this method returns the frame item to
714 // be resubmitted. Otherwise, it returns nullptr.
715 //
716 // Unsafe because it assumes we have access to the GPU which isn't the case
717 // when iOS is backgrounded, for example.
718 //
719 // This method pushes the frame timing recorder from build end to raster end.
720 std::unique_ptr<FrameItem> DrawToSurfacesUnsafe(
721 FrameTimingsRecorder& frame_timings_recorder,
722 std::vector<std::unique_ptr<LayerTreeTask>> tasks);
723
724 // Draws the layer tree to the specified view, assuming we have access to the
725 // GPU.
726 //
727 // This method is not affiliated with the frame timing recorder, but must be
728 // included between the RasterStart and RasterEnd.
729 DrawSurfaceStatus DrawToSurfaceUnsafe(
730 int64_t view_id,
731 flutter::LayerTree& layer_tree,
732 float device_pixel_ratio,
733 std::optional<fml::TimePoint> presentation_time);
734
735 ViewRecord& EnsureViewRecord(int64_t view_id);
736
737 void FireNextFrameCallbackIfPresent();
738
739 static bool ShouldResubmitFrame(const DoDrawResult& result);
740 static DrawStatus ToDrawStatus(DoDrawStatus status);
741
742 bool is_torn_down_ = false;
743 Delegate& delegate_;
744 MakeGpuImageBehavior gpu_image_behavior_;
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_;
750 fml::closure next_frame_callback_;
751 bool user_override_resource_cache_bytes_ = false;
752 std::optional<size_t> max_cache_bytes_;
753 fml::RefPtr<fml::RasterThreadMerger> raster_thread_merger_;
754 std::shared_ptr<ExternalViewEmbedder> external_view_embedder_;
755 std::unique_ptr<SnapshotController> snapshot_controller_;
756
757 // WeakPtrFactory must be the last member.
760};
761
762} // namespace flutter
763
764#endif // FLUTTER_SHELL_COMMON_RASTERIZER_H_
Used to forward events from the rasterizer to interested subsystems. Currently, the shell sets itself...
Definition rasterizer.h:128
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
Definition rasterizer.h:663
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
Definition rasterizer.h:668
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.
Definition rasterizer.h:347
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
Definition rasterizer.h:682
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.
Definition rasterizer.cc:65
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
Definition rasterizer.h:688
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.
Definition rasterizer.h:178
@ 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
Definition rasterizer.cc:69
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....
Definition rasterizer.cc:79
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)
Definition rasterizer.cc:74
flutter::CompositorContext * compositor_context()
Returns a pointer to the compositor context used by this rasterizer. This pointer will never be nullp...
Definition rasterizer.h:515
The refresh rate interface for Stopwatch.
Definition stopwatch.h:20
static std::shared_ptr< TypographerContext > Make()
VkSurfaceKHR surface
Definition main.cc:49
sk_sp< SkImage > image
Definition examples.cpp:29
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
GAsyncResult * result
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
DrawSurfaceStatus
Definition rasterizer.h:68
std::chrono::duration< double, std::milli > Milliseconds
Definition time_delta.h:18
std::function< void()> closure
Definition closure.h:14
Definition ref_ptr.h:256
static constexpr SkISize MakeEmpty()
Definition SkSize.h:22
std::unique_ptr< FrameTimingsRecorder > frame_timings_recorder
Definition rasterizer.h:95
FrameItem(std::vector< std::unique_ptr< LayerTreeTask > > tasks, std::unique_ptr< FrameTimingsRecorder > frame_timings_recorder)
Definition rasterizer.h:90
std::vector< std::unique_ptr< LayerTreeTask > > layer_tree_tasks
Definition rasterizer.h:94
A POD type used to return the screenshot data along with the size of the frame.
Definition rasterizer.h:398
Screenshot(const Screenshot &other)
The copy constructor for a screenshot.
Screenshot()
Creates an empty screenshot.
~Screenshot()
Destroys the screenshot object and releases underlying data.