5#include "flutter/flow/raster_cache.h"
10#include "flutter/common/constants.h"
11#include "flutter/display_list/skia/dl_sk_dispatcher.h"
12#include "flutter/flow/layers/container_layer.h"
13#include "flutter/flow/layers/layer.h"
14#include "flutter/flow/paint_utils.h"
15#include "flutter/flow/raster_cache_util.h"
16#include "flutter/fml/logging.h"
17#include "flutter/fml/trace_event.h"
28 const SkRect& logical_rect,
32 logical_rect_(logical_rect),
34 rtree_(
std::move(rtree)) {}
38 bool preserve_rtree)
const {
44 FML_DCHECK(std::abs(bounds.width() - image_->dimensions().width()) <= 1 &&
45 std::abs(bounds.height() - image_->dimensions().height()) <= 1);
48 if (!preserve_rtree || !rtree_) {
49 canvas.
DrawImage(image_, {bounds.fLeft, bounds.fTop},
55 auto rects = rtree_->region().getRects(
true);
57 canvas.
Translate(bounds.fLeft, bounds.fTop);
61 for (
auto rect : rects) {
72 size_t display_list_cache_limit_per_frame)
73 : access_threshold_(access_threshold),
74 display_list_cache_limit_per_frame_(display_list_cache_limit_per_frame) {}
80 const std::function<
void(
DlCanvas*)>& draw_function,
81 const std::function<
void(
DlCanvas*,
const SkRect& rect)>& draw_checkerboard)
105 draw_function(&canvas);
107 if (checkerboard_images_) {
112 return std::make_unique<RasterCacheResult>(
118 const Context& raster_cache_context,
119 const std::function<
void(
DlCanvas*)>& render_function,
122 Entry& entry = cache_[
key];
125 entry.image =
Rasterize(raster_cache_context, std::move(rtree),
126 render_function, func);
127 if (entry.image !=
nullptr) {
130 display_list_cached_this_frame_++;
139 return entry.image !=
nullptr;
144 bool visible)
const {
146 Entry& entry = cache_[
key];
147 entry.encountered_this_frame =
true;
148 entry.visible_this_frame = visible;
149 if (visible || entry.accesses_since_visible > 0) {
150 entry.accesses_since_visible++;
152 return {entry.accesses_since_visible, entry.image !=
nullptr};
158 auto entry = cache_.find(
key);
159 if (entry != cache_.cend()) {
160 return entry->second.accesses_since_visible;
168 if (cache_.find(
key) != cache_.cend()) {
177 bool preserve_rtree)
const {
179 if (it == cache_.end()) {
183 Entry& entry = it->second;
186 entry.image->draw(canvas,
paint, preserve_rtree);
194 display_list_cached_this_frame_ = 0;
195 picture_metrics_ = {};
199void RasterCache::UpdateMetrics() {
200 for (
auto it = cache_.begin(); it != cache_.end(); ++it) {
201 Entry& entry = it->second;
208 entry.encountered_this_frame =
false;
213 std::vector<RasterCacheKey::Map<Entry>::iterator> dead;
215 for (
auto it = cache_.begin(); it != cache_.end(); ++it) {
216 Entry& entry = it->second;
217 if (!entry.encountered_this_frame) {
222 for (
auto it : dead) {
223 if (it->second.image) {
234 TraceStatsToTimeline();
239 picture_metrics_ = {};
244 return cache_.size();
248 size_t layer_cached_entries_count = 0;
249 for (
const auto& item : cache_) {
251 layer_cached_entries_count++;
254 return layer_cached_entries_count;
258 size_t display_list_cached_entries_count = 0;
259 for (
const auto& item : cache_) {
261 display_list_cached_entries_count++;
264 return display_list_cached_entries_count;
279void RasterCache::TraceStatsToTimeline()
const {
283 "RasterCache",
reinterpret_cast<int64_t
>(
this),
293 size_t layer_cache_bytes = 0;
294 for (
const auto& item : cache_) {
297 layer_cache_bytes += item.second.image->image_bytes();
300 return layer_cache_bytes;
304 size_t picture_cache_bytes = 0;
305 for (
const auto& item : cache_) {
308 picture_cache_bytes += item.second.image->image_bytes();
311 return picture_cache_bytes;
317 return picture_metrics_;
319 return layer_metrics_;
Developer-facing API for rendering anything within the engine.
virtual void TransformReset()=0
virtual void Translate(SkScalar tx, SkScalar ty)=0
void Clear(DlColor color)
virtual void DrawImageRect(const sk_sp< DlImage > &image, const SkRect &src, const SkRect &dst, DlImageSampling sampling, const DlPaint *paint=nullptr, SrcRectConstraint constraint=SrcRectConstraint::kFast)=0
virtual SkMatrix GetTransform() const =0
virtual void DrawImage(const sk_sp< DlImage > &image, const SkPoint point, DlImageSampling sampling, const DlPaint *paint=nullptr)=0
static sk_sp< DlImage > Make(const SkImage *image)
Backend implementation of |DlCanvas| for |SkCanvas|.
void Transform(const SkMatrix *matrix) override
void Translate(SkScalar tx, SkScalar ty) override
RasterCacheResult(sk_sp< DlImage > image, const SkRect &logical_rect, const char *type, sk_sp< const DlRTree > rtree=nullptr)
virtual void draw(DlCanvas &canvas, const DlPaint *paint, bool preserve_rtree) const
bool Draw(const RasterCacheKeyID &id, DlCanvas &canvas, const DlPaint *paint, bool preserve_rtree=false) const
void SetCheckboardCacheImages(bool checkerboard)
CacheInfo MarkSeen(const RasterCacheKeyID &id, const SkMatrix &matrix, bool visible) const
The entry whose RasterCacheKey is generated by RasterCacheKeyID and matrix is marked as encountered b...
size_t EstimatePictureCacheByteSize() const
Estimate how much memory is used by picture raster cache entries in bytes.
bool HasEntry(const RasterCacheKeyID &id, const SkMatrix &) const
bool UpdateCacheEntry(const RasterCacheKeyID &id, const Context &raster_cache_context, const std::function< void(DlCanvas *)> &render_function, sk_sp< const DlRTree > rtree=nullptr) const
RasterCache(size_t access_threshold=3, size_t picture_and_display_list_cache_limit_per_frame=RasterCacheUtil::kDefaultPictureAndDisplayListCacheLimitPerFrame)
int GetAccessCount(const RasterCacheKeyID &id, const SkMatrix &matrix) const
size_t GetCachedEntriesCount() const
size_t GetPictureCachedEntriesCount() const
size_t EstimateLayerCacheByteSize() const
Estimate how much memory is used by layer raster cache entries in bytes.
size_t GetLayerCachedEntriesCount() const
std::unique_ptr< RasterCacheResult > Rasterize(const RasterCache::Context &context, sk_sp< const DlRTree > rtree, const std::function< void(DlCanvas *)> &draw_function, const std::function< void(DlCanvas *, const SkRect &rect)> &draw_checkerboard) const
void EvictUnusedCacheEntries()
void Step(const char *label=nullptr) const
#define FML_DCHECK(condition)
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
constexpr double kMegaByteSizeInBytes
void DrawCheckerboard(DlCanvas *canvas, const SkRect &rect)
static SkImageInfo MakeN32Premul(int width, int height)
static SkRect Make(const SkISize &size)
constexpr float left() const
constexpr float top() const
SkScalar fLeft
smaller x-axis bounds
void offset(float dx, float dy)
constexpr float height() const
constexpr float width() const
SkScalar fTop
smaller y-axis bounds
static constexpr DlColor kTransparent()
size_t total_count() const
size_t total_bytes() const
static SkMatrix GetIntegralTransCTM(const SkMatrix &ctm)
Snap the translation components of the matrix to integers.
static SkRect GetRoundedOutDeviceBounds(const SkRect &rect, const SkMatrix &ctm)
const SkRect & logical_rect
GrDirectContext * gr_context
const sk_sp< SkColorSpace > dst_color_space
#define FML_TRACE_COUNTER(category_group, name, counter_id, arg1,...)
static void checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)