Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
raster_cache_util.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_FLOW_RASTER_CACHE_UTIL_H_
6#define FLUTTER_FLOW_RASTER_CACHE_UTIL_H_
7
8#include "flutter/fml/logging.h"
11#include "include/core/SkRect.h"
12
13namespace flutter {
14
16 // The default max number of picture and display list raster caches to be
17 // generated per frame. Generating too many caches in one frame may cause jank
18 // on that frame. This limit allows us to throttle the cache and distribute
19 // the work across multiple frames.
21
22 // The ImageFilterLayer might cache the filtered output of this layer
23 // if the layer remains stable (if it is not animating for instance).
24 // If the ImageFilterLayer is not the same between rendered frames,
25 // though, it will cache its children instead and filter their cached
26 // output on the fly.
27 // Caching just the children saves the time to render them and also
28 // avoids a rendering surface switch to draw them.
29 // Caching the layer itself avoids all of that and additionally avoids
30 // the cost of applying the filter, but can be worse than caching the
31 // children if the filter itself is not stable from frame to frame.
32 // This constant controls how many times we will Preroll and Paint this
33 // same ImageFilterLayer before we consider the layer and filter to be
34 // stable enough to switch from caching the children to caching the
35 // filtered output of this layer.
37
38 static bool CanRasterizeRect(const SkRect& cull_rect) {
39 if (cull_rect.isEmpty()) {
40 // No point in ever rasterizing an empty display list.
41 return false;
42 }
43
44 if (!cull_rect.isFinite()) {
45 // Cannot attempt to rasterize into an infinitely large surface.
46 FML_LOG(INFO) << "Attempted to raster cache non-finite display list";
47 return false;
48 }
49
50 return true;
51 }
52
53 static SkRect GetDeviceBounds(const SkRect& rect, const SkMatrix& ctm) {
54 SkRect device_rect;
55 ctm.mapRect(&device_rect, rect);
56 return device_rect;
57 }
58
60 const SkMatrix& ctm) {
61 SkRect device_rect;
62 ctm.mapRect(&device_rect, rect);
63 device_rect.roundOut(&device_rect);
64 return device_rect;
65 }
66
67 /**
68 * @brief Snap the translation components of the matrix to integers.
69 *
70 * The snapping will only happen if the matrix only has scale and translation
71 * transformations. This is used, along with GetRoundedOutDeviceBounds, to
72 * ensure that the textures drawn by the raster cache are exactly aligned to
73 * physical pixels. Any layers that participate in raster caching must align
74 * themselves to physical pixels even when not cached to prevent a change in
75 * apparent location if caching is later applied.
76 *
77 * @param ctm the current transformation matrix.
78 * @return SkMatrix the snapped transformation matrix.
79 */
81 SkMatrix integral;
82 return ComputeIntegralTransCTM(ctm, &integral) ? integral : ctm;
83 }
84
85 /**
86 * @brief Snap the translation components of the |in| matrix to integers
87 * and store the snapped matrix in |out|.
88 *
89 * The snapping will only happen if the matrix only has scale and translation
90 * transformations. This is used, along with GetRoundedOutDeviceBounds, to
91 * ensure that the textures drawn by the raster cache are exactly aligned to
92 * physical pixels. Any layers that participate in raster caching must align
93 * themselves to physical pixels even when not cached to prevent a change in
94 * apparent location if caching is later applied.
95 *
96 * The |out| matrix will not be modified if this method returns false.
97 *
98 * @param in the current transformation matrix.
99 * @param out the storage for the snapped matrix.
100 * @return true if the integral modification was needed, false otherwise.
101 */
102 static bool ComputeIntegralTransCTM(const SkMatrix& in, SkMatrix* out);
103
104 /**
105 * @brief Snap the translation components of the matrix to integers.
106 *
107 * The snapping will only happen if the matrix only has scale and translation
108 * transformations. This is used, along with GetRoundedOutDeviceBounds, to
109 * ensure that the textures drawn by the raster cache are exactly aligned to
110 * physical pixels. Any layers that participate in raster caching must align
111 * themselves to physical pixels even when not cached to prevent a change in
112 * apparent location if caching is later applied.
113 *
114 * @param ctm the current transformation matrix.
115 * @return SkM44 the snapped transformation matrix.
116 */
117 static SkM44 GetIntegralTransCTM(const SkM44& ctm) {
118 SkM44 integral;
119 return ComputeIntegralTransCTM(ctm, &integral) ? integral : ctm;
120 }
121
122 /**
123 * @brief Snap the translation components of the |in| matrix to integers
124 * and store the snapped matrix in |out|.
125 *
126 * The snapping will only happen if the matrix only has scale and translation
127 * transformations. This is used, along with GetRoundedOutDeviceBounds, to
128 * ensure that the textures drawn by the raster cache are exactly aligned to
129 * physical pixels. Any layers that participate in raster caching must align
130 * themselves to physical pixels even when not cached to prevent a change in
131 * apparent location if caching is later applied.
132 *
133 * The |out| matrix will not be modified if this method returns false.
134 *
135 * @param in the current transformation matrix.
136 * @param out the storage for the snapped matrix.
137 * @return true if the integral modification was needed, false otherwise.
138 */
139 static bool ComputeIntegralTransCTM(const SkM44& in, SkM44* out);
140};
141
142} // namespace flutter
143
144#endif // FLUTTER_FLOW_RASTER_CACHE_UTIL_H_
Definition SkM44.h:150
bool mapRect(SkRect *dst, const SkRect &src, SkApplyPerspectiveClip pc=SkApplyPerspectiveClip::kYes) const
#define FML_LOG(severity)
Definition logging.h:82
bool isFinite() const
Definition SkRect.h:711
void roundOut(SkIRect *dst) const
Definition SkRect.h:1241
bool isEmpty() const
Definition SkRect.h:693
static bool CanRasterizeRect(const SkRect &cull_rect)
static bool ComputeIntegralTransCTM(const SkMatrix &in, SkMatrix *out)
Snap the translation components of the |in| matrix to integers and store the snapped matrix in |out|.
static SkMatrix GetIntegralTransCTM(const SkMatrix &ctm)
Snap the translation components of the matrix to integers.
static SkRect GetRoundedOutDeviceBounds(const SkRect &rect, const SkMatrix &ctm)
static constexpr int kDefaultPictureAndDisplayListCacheLimitPerFrame
static constexpr int kMinimumRendersBeforeCachingFilterLayer
static SkM44 GetIntegralTransCTM(const SkM44 &ctm)
Snap the translation components of the matrix to integers.
static SkRect GetDeviceBounds(const SkRect &rect, const SkMatrix &ctm)