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