1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "property/rs_property_drawable.h"
17
18 #include "pipeline/rs_render_node.h"
19 #include "platform/common/rs_log.h"
20 #include "property/rs_properties.h"
21 #include "property/rs_property_drawable_bounds_geometry.h"
22 #include "property/rs_property_drawable_frame_geometry.h"
23 #include "property/rs_property_drawable_utilities.h"
24
25 namespace OHOS::Rosen {
26 namespace {
27 template<RSModifierType T>
CustomModifierAdapter(const RSRenderContent &)28 inline RSModifierDrawable::DrawablePtr CustomModifierAdapter(const RSRenderContent&)
29 {
30 return std::make_unique<RSModifierDrawable>(T);
31 }
32
GenerateAlias(RSPropertyDrawableSlot slot)33 inline RSPropertyDrawable::DrawablePtr GenerateAlias(RSPropertyDrawableSlot slot)
34 {
35 return std::make_unique<RSAliasDrawable>(slot);
36 }
37
GenerateSaveRestore(RSPaintFilterCanvas::SaveType type=RSPaintFilterCanvas::kCanvas)38 inline std::pair<RSPropertyDrawable::DrawablePtr, RSPropertyDrawable::DrawablePtr> GenerateSaveRestore(
39 RSPaintFilterCanvas::SaveType type = RSPaintFilterCanvas::kCanvas)
40 {
41 if (type == RSPaintFilterCanvas::kNone) {
42 return {};
43 } else if (type == RSPaintFilterCanvas::kCanvas) {
44 auto count = std::make_shared<uint32_t>(0);
45 return { std::make_unique<RSSaveDrawable>(count), std::make_unique<RSRestoreDrawable>(count) };
46 } else {
47 auto status = std::make_shared<RSPaintFilterCanvas::SaveStatus>();
48 return { std::make_unique<RSCustomSaveDrawable>(status, type),
49 std::make_unique<RSCustomRestoreDrawable>(status) };
50 }
51 }
52
SaveRestoreHelper(RSPropertyDrawable::DrawableVec & drawableVec,RSPropertyDrawableSlot slot1,RSPropertyDrawableSlot slot2,RSPaintFilterCanvas::SaveType saveType)53 inline void SaveRestoreHelper(RSPropertyDrawable::DrawableVec& drawableVec, RSPropertyDrawableSlot slot1,
54 RSPropertyDrawableSlot slot2, RSPaintFilterCanvas::SaveType saveType)
55 {
56 std::tie(drawableVec[static_cast<size_t>(slot1)], drawableVec[static_cast<size_t>(slot2)]) =
57 GenerateSaveRestore(saveType);
58 }
59
60 // key = RSModifierType, value = RSPropertyDrawableType
61 static const std::unordered_map<RSModifierType, RSPropertyDrawableSlot> g_propertyToDrawableLut = {
62 { RSModifierType::INVALID, RSPropertyDrawableSlot::INVALID },
63 { RSModifierType::BOUNDS, RSPropertyDrawableSlot::BOUNDS_MATRIX },
64 { RSModifierType::FRAME, RSPropertyDrawableSlot::FRAME_OFFSET },
65 { RSModifierType::POSITION_Z, RSPropertyDrawableSlot::BOUNDS_MATRIX },
66 { RSModifierType::PIVOT, RSPropertyDrawableSlot::BOUNDS_MATRIX },
67 { RSModifierType::PIVOT_Z, RSPropertyDrawableSlot::BOUNDS_MATRIX },
68 { RSModifierType::QUATERNION, RSPropertyDrawableSlot::BOUNDS_MATRIX },
69 { RSModifierType::ROTATION, RSPropertyDrawableSlot::BOUNDS_MATRIX },
70 { RSModifierType::ROTATION_X, RSPropertyDrawableSlot::BOUNDS_MATRIX },
71 { RSModifierType::ROTATION_Y, RSPropertyDrawableSlot::BOUNDS_MATRIX },
72 { RSModifierType::CAMERA_DISTANCE, RSPropertyDrawableSlot::BOUNDS_MATRIX },
73 { RSModifierType::SCALE, RSPropertyDrawableSlot::BOUNDS_MATRIX },
74 { RSModifierType::SKEW, RSPropertyDrawableSlot::BOUNDS_MATRIX },
75 { RSModifierType::PERSP, RSPropertyDrawableSlot::BOUNDS_MATRIX },
76 { RSModifierType::TRANSLATE, RSPropertyDrawableSlot::BOUNDS_MATRIX },
77 { RSModifierType::TRANSLATE_Z, RSPropertyDrawableSlot::BOUNDS_MATRIX },
78 { RSModifierType::SUBLAYER_TRANSFORM, RSPropertyDrawableSlot::INVALID },
79 { RSModifierType::CORNER_RADIUS, RSPropertyDrawableSlot::INVALID },
80 { RSModifierType::ALPHA, RSPropertyDrawableSlot::ALPHA },
81 { RSModifierType::ALPHA_OFFSCREEN, RSPropertyDrawableSlot::ALPHA },
82 { RSModifierType::FOREGROUND_COLOR, RSPropertyDrawableSlot::FOREGROUND_COLOR },
83 { RSModifierType::BACKGROUND_COLOR, RSPropertyDrawableSlot::BACKGROUND_COLOR },
84 { RSModifierType::BACKGROUND_SHADER, RSPropertyDrawableSlot::BACKGROUND_SHADER },
85 { RSModifierType::BG_IMAGE, RSPropertyDrawableSlot::BACKGROUND_IMAGE },
86 { RSModifierType::BG_IMAGE_WIDTH, RSPropertyDrawableSlot::BACKGROUND_IMAGE },
87 { RSModifierType::BG_IMAGE_HEIGHT, RSPropertyDrawableSlot::BACKGROUND_IMAGE },
88 { RSModifierType::BG_IMAGE_POSITION_X, RSPropertyDrawableSlot::BACKGROUND_IMAGE },
89 { RSModifierType::BG_IMAGE_POSITION_Y, RSPropertyDrawableSlot::BACKGROUND_IMAGE },
90 { RSModifierType::SURFACE_BG_COLOR, RSPropertyDrawableSlot::INVALID },
91 { RSModifierType::BORDER_COLOR, RSPropertyDrawableSlot::BORDER },
92 { RSModifierType::BORDER_WIDTH, RSPropertyDrawableSlot::BORDER },
93 { RSModifierType::BORDER_STYLE, RSPropertyDrawableSlot::BORDER },
94 { RSModifierType::BORDER_DASH_WIDTH, RSPropertyDrawableSlot::BORDER },
95 { RSModifierType::BORDER_DASH_GAP, RSPropertyDrawableSlot::BORDER },
96 { RSModifierType::FILTER, RSPropertyDrawableSlot::COMPOSITING_FILTER },
97 { RSModifierType::BACKGROUND_FILTER, RSPropertyDrawableSlot::BACKGROUND_FILTER },
98 { RSModifierType::LINEAR_GRADIENT_BLUR_PARA, RSPropertyDrawableSlot::COMPOSITING_FILTER },
99 { RSModifierType::FOREGROUND_EFFECT_RADIUS, RSPropertyDrawableSlot::FOREGROUND_FILTER },
100 { RSModifierType::MOTION_BLUR_PARA, RSPropertyDrawableSlot::FOREGROUND_FILTER },
101 { RSModifierType::FLY_OUT_DEGREE, RSPropertyDrawableSlot::FOREGROUND_FILTER },
102 { RSModifierType::FLY_OUT_PARAMS, RSPropertyDrawableSlot::FOREGROUND_FILTER },
103 { RSModifierType::DISTORTION_K, RSPropertyDrawableSlot::FOREGROUND_FILTER },
104 { RSModifierType::MAGNIFIER_PARA, RSPropertyDrawableSlot::BACKGROUND_FILTER },
105 { RSModifierType::DYNAMIC_LIGHT_UP_RATE, RSPropertyDrawableSlot::DYNAMIC_LIGHT_UP },
106 { RSModifierType::DYNAMIC_LIGHT_UP_DEGREE, RSPropertyDrawableSlot::DYNAMIC_LIGHT_UP },
107 { RSModifierType::FRAME_GRAVITY, RSPropertyDrawableSlot::FRAME_OFFSET },
108 { RSModifierType::CLIP_RRECT, RSPropertyDrawableSlot::CLIP_TO_BOUNDS },
109 { RSModifierType::CLIP_BOUNDS, RSPropertyDrawableSlot::CLIP_TO_BOUNDS },
110 { RSModifierType::CLIP_TO_BOUNDS, RSPropertyDrawableSlot::CLIP_TO_BOUNDS },
111 { RSModifierType::CLIP_TO_FRAME, RSPropertyDrawableSlot::CLIP_TO_FRAME },
112 { RSModifierType::VISIBLE, RSPropertyDrawableSlot::INVALID },
113 { RSModifierType::SHADOW_COLOR, RSPropertyDrawableSlot::SHADOW },
114 { RSModifierType::SHADOW_OFFSET_X, RSPropertyDrawableSlot::SHADOW },
115 { RSModifierType::SHADOW_OFFSET_Y, RSPropertyDrawableSlot::SHADOW },
116 { RSModifierType::SHADOW_ALPHA, RSPropertyDrawableSlot::SHADOW },
117 { RSModifierType::SHADOW_ELEVATION, RSPropertyDrawableSlot::SHADOW },
118 { RSModifierType::SHADOW_RADIUS, RSPropertyDrawableSlot::SHADOW },
119 { RSModifierType::SHADOW_PATH, RSPropertyDrawableSlot::SHADOW },
120 { RSModifierType::SHADOW_MASK, RSPropertyDrawableSlot::SHADOW },
121 { RSModifierType::MASK, RSPropertyDrawableSlot::MASK },
122 { RSModifierType::SPHERIZE, RSPropertyDrawableSlot::INVALID },
123 { RSModifierType::LIGHT_UP_EFFECT, RSPropertyDrawableSlot::LIGHT_UP_EFFECT },
124 { RSModifierType::AIINVERT, RSPropertyDrawableSlot::BINARIZATION },
125 { RSModifierType::SYSTEMBAREFFECT, RSPropertyDrawableSlot::BACKGROUND_FILTER },
126 { RSModifierType::WATER_RIPPLE_PROGRESS, RSPropertyDrawableSlot::BACKGROUND_FILTER },
127 { RSModifierType::WATER_RIPPLE_PARAMS, RSPropertyDrawableSlot::BACKGROUND_FILTER },
128 { RSModifierType::PIXEL_STRETCH, RSPropertyDrawableSlot::PIXEL_STRETCH },
129 { RSModifierType::PIXEL_STRETCH_PERCENT, RSPropertyDrawableSlot::PIXEL_STRETCH },
130 { RSModifierType::PIXEL_STRETCH_TILE_MODE, RSPropertyDrawableSlot::PIXEL_STRETCH },
131 { RSModifierType::USE_EFFECT, RSPropertyDrawableSlot::USE_EFFECT },
132 { RSModifierType::SANDBOX, RSPropertyDrawableSlot::BOUNDS_MATRIX },
133 { RSModifierType::GRAY_SCALE, RSPropertyDrawableSlot::COLOR_FILTER },
134 { RSModifierType::BRIGHTNESS, RSPropertyDrawableSlot::COLOR_FILTER },
135 { RSModifierType::CONTRAST, RSPropertyDrawableSlot::COLOR_FILTER },
136 { RSModifierType::SATURATE, RSPropertyDrawableSlot::COLOR_FILTER },
137 { RSModifierType::SEPIA, RSPropertyDrawableSlot::COLOR_FILTER },
138 { RSModifierType::INVERT, RSPropertyDrawableSlot::COLOR_FILTER },
139 { RSModifierType::HUE_ROTATE, RSPropertyDrawableSlot::COLOR_FILTER },
140 { RSModifierType::COLOR_BLEND, RSPropertyDrawableSlot::COLOR_FILTER },
141 { RSModifierType::PARTICLE, RSPropertyDrawableSlot::PARTICLE_EFFECT },
142 { RSModifierType::SHADOW_IS_FILLED, RSPropertyDrawableSlot::INVALID },
143 { RSModifierType::OUTLINE_COLOR, RSPropertyDrawableSlot::OUTLINE },
144 { RSModifierType::OUTLINE_WIDTH, RSPropertyDrawableSlot::OUTLINE },
145 { RSModifierType::OUTLINE_STYLE, RSPropertyDrawableSlot::OUTLINE },
146 { RSModifierType::OUTLINE_DASH_WIDTH, RSPropertyDrawableSlot::OUTLINE },
147 { RSModifierType::OUTLINE_DASH_GAP, RSPropertyDrawableSlot::OUTLINE },
148 { RSModifierType::OUTLINE_RADIUS, RSPropertyDrawableSlot::OUTLINE },
149 { RSModifierType::COLOR_BLEND_MODE, RSPropertyDrawableSlot::BLEND_MODE },
150 { RSModifierType::COLOR_BLEND_APPLY_TYPE, RSPropertyDrawableSlot::BLEND_MODE },
151 { RSModifierType::LIGHT_INTENSITY, RSPropertyDrawableSlot::POINT_LIGHT },
152 { RSModifierType::LIGHT_COLOR, RSPropertyDrawableSlot::POINT_LIGHT },
153 { RSModifierType::LIGHT_POSITION, RSPropertyDrawableSlot::POINT_LIGHT },
154 { RSModifierType::ILLUMINATED_TYPE, RSPropertyDrawableSlot::POINT_LIGHT },
155 { RSModifierType::BLOOM, RSPropertyDrawableSlot::POINT_LIGHT },
156 { RSModifierType::BACKGROUND_BLUR_RADIUS, RSPropertyDrawableSlot::BACKGROUND_FILTER },
157 { RSModifierType::BACKGROUND_BLUR_SATURATION, RSPropertyDrawableSlot::BACKGROUND_FILTER },
158 { RSModifierType::BACKGROUND_BLUR_BRIGHTNESS, RSPropertyDrawableSlot::BACKGROUND_FILTER },
159 { RSModifierType::BACKGROUND_BLUR_MASK_COLOR, RSPropertyDrawableSlot::BACKGROUND_FILTER },
160 { RSModifierType::BACKGROUND_BLUR_COLOR_MODE, RSPropertyDrawableSlot::BACKGROUND_FILTER },
161 { RSModifierType::BACKGROUND_BLUR_RADIUS_X, RSPropertyDrawableSlot::BACKGROUND_FILTER },
162 { RSModifierType::BACKGROUND_BLUR_RADIUS_Y, RSPropertyDrawableSlot::BACKGROUND_FILTER },
163 { RSModifierType::FOREGROUND_BLUR_RADIUS, RSPropertyDrawableSlot::COMPOSITING_FILTER },
164 { RSModifierType::FOREGROUND_BLUR_SATURATION, RSPropertyDrawableSlot::COMPOSITING_FILTER },
165 { RSModifierType::FOREGROUND_BLUR_BRIGHTNESS, RSPropertyDrawableSlot::COMPOSITING_FILTER },
166 { RSModifierType::FOREGROUND_BLUR_MASK_COLOR, RSPropertyDrawableSlot::COMPOSITING_FILTER },
167 { RSModifierType::FOREGROUND_BLUR_COLOR_MODE, RSPropertyDrawableSlot::COMPOSITING_FILTER },
168 { RSModifierType::FOREGROUND_BLUR_RADIUS_X, RSPropertyDrawableSlot::COMPOSITING_FILTER },
169 { RSModifierType::FOREGROUND_BLUR_RADIUS_Y, RSPropertyDrawableSlot::COMPOSITING_FILTER },
170 { RSModifierType::CUSTOM, RSPropertyDrawableSlot::INVALID },
171 { RSModifierType::EXTENDED, RSPropertyDrawableSlot::INVALID },
172 { RSModifierType::TRANSITION, RSPropertyDrawableSlot::TRANSITION },
173 { RSModifierType::BACKGROUND_STYLE, RSPropertyDrawableSlot::BACKGROUND_STYLE },
174 { RSModifierType::CONTENT_STYLE, RSPropertyDrawableSlot::CONTENT_STYLE },
175 { RSModifierType::FOREGROUND_STYLE, RSPropertyDrawableSlot::FOREGROUND_STYLE },
176 { RSModifierType::OVERLAY_STYLE, RSPropertyDrawableSlot::OVERLAY },
177 { RSModifierType::NODE_MODIFIER, RSPropertyDrawableSlot::INVALID },
178 { RSModifierType::ENV_FOREGROUND_COLOR, RSPropertyDrawableSlot::ENV_FOREGROUND_COLOR },
179 { RSModifierType::ENV_FOREGROUND_COLOR_STRATEGY, RSPropertyDrawableSlot::ENV_FOREGROUND_COLOR_STRATEGY },
180 { RSModifierType::PARTICLE_EMITTER_UPDATER, RSPropertyDrawableSlot::PARTICLE_EFFECT },
181 { RSModifierType::PARTICLE_NOISE_FIELD, RSPropertyDrawableSlot::PARTICLE_EFFECT },
182 { RSModifierType::DYNAMIC_DIM_DEGREE, RSPropertyDrawableSlot::DYNAMIC_DIM },
183 { RSModifierType::GEOMETRYTRANS, RSPropertyDrawableSlot::INVALID },
184 };
185
186 // NOTE: This LUT should always the same size as RSPropertyDrawableSlot
187 // index = RSPropertyDrawableType, value = DrawableGenerator
188 constexpr int LUT_SIZE = static_cast<int>(RSPropertyDrawableSlot::MAX);
189 static const std::array<RSPropertyDrawable::DrawableGenerator, LUT_SIZE> g_drawableGeneratorLut = {
190 nullptr, // INVALID = 0
191 nullptr, // SAVE_ALL
192
193 // Bounds Geometry
194 nullptr, // BOUNDS_MATRIX
195 RSAlphaDrawable::Generate, // ALPHA
196 RSMaskDrawable::Generate, // MASK
197 CustomModifierAdapter<RSModifierType::TRANSITION>, // TRANSITION
198 CustomModifierAdapter<RSModifierType::ENV_FOREGROUND_COLOR>, // ENV_FOREGROUND_COLOR
199 RSShadowDrawable::Generate, // SHADOW
200 RSForegroundFilterDrawable::Generate, // FOREGROUND_FILTER
201 RSOutlineDrawable::Generate, // OUTLINE
202
203 // BG properties in Bounds Clip
204 nullptr, // BG_SAVE_BOUNDS
205 nullptr, // CLIP_TO_BOUNDS
206 BlendSaveDrawableGenerate, // BLEND_MODE
207 RSBackgroundColorDrawable::Generate, // BACKGROUND_COLOR
208 RSBackgroundShaderDrawable::Generate, // BACKGROUND_SHADER
209 RSBackgroundImageDrawable::Generate, // BACKGROUND_IMAGE
210 RSBackgroundFilterDrawable::Generate, // BACKGROUND_FILTER
211 RSEffectDataApplyDrawable::Generate, // USE_EFFECT
212 CustomModifierAdapter<RSModifierType::BACKGROUND_STYLE>, // BACKGROUND_STYLE
213 RSDynamicLightUpDrawable::Generate, // DYNAMIC_LIGHT_UP
214 CustomModifierAdapter<RSModifierType::ENV_FOREGROUND_COLOR_STRATEGY>, // ENV_FOREGROUND_COLOR_STRATEGY
215 nullptr, // BG_RESTORE_BOUNDS
216
217 // Frame Geometry
218 nullptr, // SAVE_FRAME
219 nullptr, // FRAME_OFFSET
220 RSClipFrameDrawable::Generate, // CLIP_TO_FRAME
221 CustomModifierAdapter<RSModifierType::CONTENT_STYLE>, // CONTENT_STYLE
222 nullptr, // CHILDREN
223 CustomModifierAdapter<RSModifierType::FOREGROUND_STYLE>, // FOREGROUND_STYLE
224 nullptr, // RESTORE_FRAME
225
226 // FG properties in Bounds clip
227 nullptr, // FG_SAVE_BOUNDS
228 nullptr, // EXTRA_CLIP_TO_BOUNDS
229 RSBinarizationDrawable::Generate, // BINARIZATION,
230 RSColorFilterDrawable::Generate, // COLOR_FILTER
231 RSDynamicDimDrawable::Generate, // DYNAMIC_DIM
232 RSLightUpEffectDrawable::Generate, // LIGHT_UP_EFFECT
233 RSCompositingFilterDrawable::Generate, // COMPOSITING_FILTER
234 RSForegroundColorDrawable::Generate, // FOREGROUND_COLOR
235 nullptr, // FG_RESTORE_BOUNDS
236
237 // No clip (unless ClipToBounds is set)
238 RSPointLightDrawable::Generate, // POINT_LIGHT
239 RSBorderDrawable::Generate, // BORDER
240 CustomModifierAdapter<RSModifierType::OVERLAY_STYLE>, // OVERLAY
241 RSParticleDrawable::Generate, // PARTICLE_EFFECT
242 RSPixelStretchDrawable::Generate, // PIXEL_STRETCH
243
244 BlendRestoreDrawableGenerate, // RESTORE_BLEND
245 RSForegroundFilterRestoreDrawable::Generate, // RESTORE_FOREGROUND_FILTER
246 nullptr, // RESTORE_ALL
247 };
248
249 enum DrawableVecStatus : uint8_t {
250 CLIP_TO_BOUNDS = 1 << 0,
251 BG_BOUNDS_PROPERTY = 1 << 1,
252 FG_BOUNDS_PROPERTY = 1 << 2,
253 CLIP_TO_FRAME = 1 << 3,
254 FRAME_PROPERTY = 1 << 4,
255 HAS_CHILDREN = 1 << 5,
256 BOUNDS_MASK = CLIP_TO_BOUNDS | BG_BOUNDS_PROPERTY | FG_BOUNDS_PROPERTY,
257 FRAME_MASK = CLIP_TO_FRAME | FRAME_PROPERTY | HAS_CHILDREN,
258 };
259 } // namespace
260
GenerateDirtySlots(const RSProperties & properties,const ModifierDirtyTypes & dirtyTypes)261 std::unordered_set<RSPropertyDrawableSlot> RSPropertyDrawable::GenerateDirtySlots(
262 const RSProperties& properties, const ModifierDirtyTypes& dirtyTypes)
263 {
264 // Step 1.1: collect dirty slots
265 std::unordered_set<RSPropertyDrawableSlot> dirtySlots;
266 for (uint8_t type = 0; type < static_cast<size_t>(RSModifierType::MAX_RS_MODIFIER_TYPE); type++) {
267 if (dirtyTypes[type]) {
268 auto it = g_propertyToDrawableLut.find(static_cast<RSModifierType>(type));
269 if (it == g_propertyToDrawableLut.end() || it->second == RSPropertyDrawableSlot::INVALID) {
270 continue;
271 }
272 dirtySlots.emplace(it->second);
273 }
274 }
275
276 // Step 1.2: expand dirty slots if needed
277 if (dirtyTypes.test(static_cast<size_t>(RSModifierType::BOUNDS))) {
278 if (properties.GetPixelStretch().has_value()) {
279 dirtySlots.emplace(RSPropertyDrawableSlot::PIXEL_STRETCH);
280 }
281 if (properties.GetBorder() != nullptr) {
282 dirtySlots.emplace(RSPropertyDrawableSlot::BORDER);
283 }
284 if (properties.GetOutline() != nullptr) {
285 dirtySlots.emplace(RSPropertyDrawableSlot::OUTLINE);
286 }
287 // PLANNING: add other slots: ClipToFrame, ColorFilter
288 }
289 if (dirtyTypes.test(static_cast<size_t>(RSModifierType::CORNER_RADIUS))) {
290 // border may should be updated with corner radius
291 if (properties.GetBorder() != nullptr) {
292 dirtySlots.emplace(RSPropertyDrawableSlot::BORDER);
293 }
294
295 if (properties.GetOutline() != nullptr) {
296 dirtySlots.emplace(RSPropertyDrawableSlot::OUTLINE);
297 }
298 }
299 if (dirtySlots.count(RSPropertyDrawableSlot::BLEND_MODE)) {
300 // BlendMode Restore should be regenerated with BlendMode
301 dirtySlots.emplace(RSPropertyDrawableSlot::RESTORE_BLEND_MODE);
302 }
303 if (dirtySlots.count(RSPropertyDrawableSlot::FOREGROUND_FILTER)) {
304 // ForegroundFilter Restore should be regenerated with ForegroundFilter
305 dirtySlots.emplace(RSPropertyDrawableSlot::RESTORE_FOREGROUND_FILTER);
306 }
307
308 return dirtySlots;
309 }
310
UpdateDrawableVec(const RSRenderContent & content,DrawableVec & drawableVec,std::unordered_set<RSPropertyDrawableSlot> & dirtySlots)311 bool RSPropertyDrawable::UpdateDrawableVec(
312 const RSRenderContent& content, DrawableVec& drawableVec, std::unordered_set<RSPropertyDrawableSlot>& dirtySlots)
313 {
314 if (dirtySlots.empty()) {
315 return false;
316 }
317 // ====================================================================
318 // Step 2.1: re-generate drawables for all dirty slots
319 auto drawableSlotChanged = false;
320 for (const auto& slot : dirtySlots) {
321 auto& origDrawable = drawableVec[static_cast<size_t>(slot)];
322 if (origDrawable != nullptr && origDrawable->Update(content)) {
323 continue;
324 }
325 auto& generator = g_drawableGeneratorLut[static_cast<int>(slot)];
326 if (!generator) {
327 continue;
328 }
329 auto drawable = generator(content);
330 if (bool(origDrawable) != bool(drawable)) {
331 // drawable slot changed (nullptr to non-nullptr or vice versa)
332 drawableSlotChanged = true;
333 }
334 origDrawable = std::move(drawable);
335 }
336
337 // Step 2.2: post-generate hooks (PLANNING: refactor this into a separate function)
338
339 // Temporary fix, change of clipToBounds should trigger UpdateSaveRestore
340 if (!drawableSlotChanged && dirtySlots.count(RSPropertyDrawableSlot::CLIP_TO_BOUNDS)) {
341 drawableSlotChanged = true;
342 }
343
344 return drawableSlotChanged;
345 }
346
347 namespace {
HasPropertyDrawableInRange(const RSPropertyDrawable::DrawableVec & drawableVec,RSPropertyDrawableSlot begin,RSPropertyDrawableSlot end)348 inline bool HasPropertyDrawableInRange(
349 const RSPropertyDrawable::DrawableVec& drawableVec, RSPropertyDrawableSlot begin, RSPropertyDrawableSlot end)
350 {
351 return std::any_of(drawableVec.begin() + static_cast<size_t>(begin), drawableVec.begin() + static_cast<size_t>(end),
352 [](const auto& drawablePtr) { return drawablePtr != nullptr; });
353 }
354
CalculateDrawableVecStatus(RSRenderContent & content,const RSPropertyDrawable::DrawableVec & drawableVec)355 uint8_t CalculateDrawableVecStatus(RSRenderContent& content, const RSPropertyDrawable::DrawableVec& drawableVec)
356 {
357 uint8_t result = 0;
358 auto& properties = content.GetRenderProperties();
359
360 // color blend mode has implicit dependency on clipToBounds
361 if (properties.GetClipToBounds() || properties.GetClipToRRect() || properties.GetClipBounds() != nullptr ||
362 properties.GetColorBlendMode()) {
363 result |= DrawableVecStatus::CLIP_TO_BOUNDS;
364 }
365 if (properties.GetClipToFrame()) {
366 result |= DrawableVecStatus::CLIP_TO_FRAME;
367 }
368
369 if (HasPropertyDrawableInRange(
370 drawableVec, RSPropertyDrawableSlot::BG_PROPERTIES_BEGIN, RSPropertyDrawableSlot::BG_PROPERTIES_END)) {
371 result |= DrawableVecStatus::BG_BOUNDS_PROPERTY;
372 }
373 if (HasPropertyDrawableInRange(
374 drawableVec, RSPropertyDrawableSlot::FG_PROPERTIES_BEGIN, RSPropertyDrawableSlot::FG_PROPERTIES_END)) {
375 result |= DrawableVecStatus::FG_BOUNDS_PROPERTY;
376 }
377 if (HasPropertyDrawableInRange(drawableVec, RSPropertyDrawableSlot::CONTENT_PROPERTIES_BEGIN,
378 RSPropertyDrawableSlot::CONTENT_PROPERTIES_END)) {
379 result |= DrawableVecStatus::FRAME_PROPERTY;
380 }
381
382 return result;
383 }
384
385 constexpr std::array boundsSlotsToErase = {
386 RSPropertyDrawableSlot::BG_SAVE_BOUNDS,
387 RSPropertyDrawableSlot::CLIP_TO_BOUNDS,
388 RSPropertyDrawableSlot::BG_RESTORE_BOUNDS,
389 RSPropertyDrawableSlot::FG_SAVE_BOUNDS,
390 RSPropertyDrawableSlot::FG_CLIP_TO_BOUNDS,
391 RSPropertyDrawableSlot::FG_RESTORE_BOUNDS,
392 };
393
394 constexpr std::array frameSlotsToErase = {
395 RSPropertyDrawableSlot::SAVE_FRAME,
396 RSPropertyDrawableSlot::RESTORE_FRAME,
397 };
398
OptimizeBoundsSaveRestore(RSRenderContent & content,RSPropertyDrawable::DrawableVec & drawableVec,uint8_t flags)399 void OptimizeBoundsSaveRestore(RSRenderContent& content, RSPropertyDrawable::DrawableVec& drawableVec, uint8_t flags)
400 {
401 // Erase existing save/clip/restore before re-generating
402 for (auto& slot : boundsSlotsToErase) {
403 drawableVec[static_cast<size_t>(slot)] = nullptr;
404 }
405
406 if (flags & DrawableVecStatus::CLIP_TO_BOUNDS) {
407 // case 1: ClipToBounds set.
408 // add one clip, and reuse SAVE_ALL and RESTORE_ALL.
409 drawableVec[static_cast<size_t>(RSPropertyDrawableSlot::CLIP_TO_BOUNDS)] =
410 RSClipBoundsDrawable::Generate(content);
411 return;
412 }
413
414 if ((flags & DrawableVecStatus::BG_BOUNDS_PROPERTY) && (flags & DrawableVecStatus::FG_BOUNDS_PROPERTY)) {
415 // case 2: ClipToBounds not set and we have bounds properties both BG and FG.
416 // add two sets of save/clip/restore before & after content.
417
418 // part 1: before children
419 SaveRestoreHelper(drawableVec, RSPropertyDrawableSlot::BG_SAVE_BOUNDS,
420 RSPropertyDrawableSlot::BG_RESTORE_BOUNDS, RSPaintFilterCanvas::kCanvas);
421 drawableVec[static_cast<size_t>(RSPropertyDrawableSlot::CLIP_TO_BOUNDS)] =
422 RSClipBoundsDrawable::Generate(content);
423
424 // part 2: after children, add aliases
425 drawableVec[static_cast<size_t>(RSPropertyDrawableSlot::FG_SAVE_BOUNDS)] =
426 GenerateAlias(RSPropertyDrawableSlot::BG_SAVE_BOUNDS);
427 drawableVec[static_cast<size_t>(RSPropertyDrawableSlot::FG_CLIP_TO_BOUNDS)] =
428 GenerateAlias(RSPropertyDrawableSlot::CLIP_TO_BOUNDS);
429 drawableVec[static_cast<size_t>(RSPropertyDrawableSlot::FG_RESTORE_BOUNDS)] =
430 GenerateAlias(RSPropertyDrawableSlot::BG_RESTORE_BOUNDS);
431 return;
432 }
433
434 if (flags & DrawableVecStatus::BG_BOUNDS_PROPERTY) {
435 // case 3: ClipToBounds not set and we have background bounds properties.
436 SaveRestoreHelper(drawableVec, RSPropertyDrawableSlot::BG_SAVE_BOUNDS,
437 RSPropertyDrawableSlot::BG_RESTORE_BOUNDS, RSPaintFilterCanvas::kCanvas);
438
439 drawableVec[static_cast<size_t>(RSPropertyDrawableSlot::CLIP_TO_BOUNDS)] =
440 RSClipBoundsDrawable::Generate(content);
441 return;
442 }
443
444 if (flags & DrawableVecStatus::FG_BOUNDS_PROPERTY) {
445 // case 4: ClipToBounds not set and we have foreground bounds properties.
446 SaveRestoreHelper(drawableVec, RSPropertyDrawableSlot::FG_SAVE_BOUNDS,
447 RSPropertyDrawableSlot::FG_RESTORE_BOUNDS, RSPaintFilterCanvas::kCanvas);
448
449 drawableVec[static_cast<size_t>(RSPropertyDrawableSlot::FG_CLIP_TO_BOUNDS)] =
450 RSClipBoundsDrawable::Generate(content);
451 return;
452 }
453 // case 5: ClipToBounds not set and no bounds properties, no need to save/clip/restore.
454 // nothing to do
455 }
456
OptimizeFrameSaveRestore(RSRenderContent & content,RSPropertyDrawable::DrawableVec & drawableVec,uint8_t flags)457 void OptimizeFrameSaveRestore(RSRenderContent& content, RSPropertyDrawable::DrawableVec& drawableVec, uint8_t flags)
458 {
459 // Erase existing save/clip/restore before re-generating
460 for (auto& slot : frameSlotsToErase) {
461 drawableVec[static_cast<size_t>(slot)] = nullptr;
462 }
463
464 // PLANNING: if both clipToFrame and clipToBounds are set, and frame == bounds, we don't need an extra clip
465 if (flags & DrawableVecStatus::FRAME_PROPERTY) {
466 // save/restore
467 SaveRestoreHelper(drawableVec, RSPropertyDrawableSlot::SAVE_FRAME,
468 RSPropertyDrawableSlot::RESTORE_FRAME, RSPaintFilterCanvas::kCanvas);
469 } else {
470 // no need to save/clip/restore
471 }
472 }
473 } // namespace
474
InitializeSaveRestore(const RSRenderContent & content,DrawableVec & drawableVec)475 void RSPropertyDrawable::InitializeSaveRestore(const RSRenderContent& content, DrawableVec& drawableVec)
476 {
477 SaveRestoreHelper(
478 drawableVec, RSPropertyDrawableSlot::SAVE_ALL, RSPropertyDrawableSlot::RESTORE_ALL, RSPaintFilterCanvas::kAll);
479 drawableVec[static_cast<size_t>(RSPropertyDrawableSlot::BOUNDS_MATRIX)] =
480 RSBoundsGeometryDrawable::Generate(content);
481 drawableVec[static_cast<size_t>(RSPropertyDrawableSlot::FRAME_OFFSET)] = RSFrameGeometryDrawable::Generate(content);
482 }
483
UpdateSaveRestore(RSRenderContent & content,DrawableVec & drawableVec,uint8_t & drawableVecStatus)484 void RSPropertyDrawable::UpdateSaveRestore(
485 RSRenderContent& content, DrawableVec& drawableVec, uint8_t& drawableVecStatus)
486 {
487 // ====================================================================
488 // Step 3: Universal save/clip/restore optimization
489
490 // calculate new drawable map status
491 auto drawableVecStatusNew = CalculateDrawableVecStatus(content, drawableVec);
492
493 // calculate changed bits
494 uint8_t changedBits = drawableVecStatus ^ drawableVecStatusNew;
495 if (changedBits & BOUNDS_MASK) {
496 // update bounds save/clip if need
497 OptimizeBoundsSaveRestore(content, drawableVec, drawableVecStatusNew);
498 }
499 if (changedBits & FRAME_MASK) {
500 // update frame save/clip if need
501 OptimizeFrameSaveRestore(content, drawableVec, drawableVecStatusNew);
502 }
503 drawableVecStatus = drawableVecStatusNew;
504 }
505
506 } // namespace OHOS::Rosen
507