1 /* 2 * Copyright (c) 2024 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 #ifndef RENDER_SERVICE_BASE_DRAWABLE_RS_DRAWABLE_H 17 #define RENDER_SERVICE_BASE_DRAWABLE_RS_DRAWABLE_H 18 19 #include <bitset> 20 #include <cstdint> 21 #include <functional> 22 #include <memory> 23 #include <unordered_set> 24 25 #include "recording/recording_canvas.h" 26 27 #include "modifier/rs_modifier_type.h" 28 29 namespace OHOS::Rosen { 30 class RSRenderNode; 31 class RSRenderContent; 32 33 // NOTE: MUST update DrawableGeneratorLut in rs_drawable_content.cpp when new slots are added 34 enum class RSDrawableSlot : int8_t { 35 INVALID = -1, 36 SAVE_ALL = 0, 37 38 // Bounds Geometry 39 MASK, 40 TRANSITION, 41 ENV_FOREGROUND_COLOR, 42 SHADOW, 43 FOREGROUND_FILTER, 44 OUTLINE, 45 46 // BG properties in Bounds Clip 47 BG_SAVE_BOUNDS, 48 CLIP_TO_BOUNDS, 49 BLENDER, 50 BACKGROUND_COLOR, 51 BACKGROUND_SHADER, 52 BACKGROUND_IMAGE, 53 BACKGROUND_FILTER, 54 USE_EFFECT, 55 BACKGROUND_STYLE, 56 DYNAMIC_LIGHT_UP, 57 ENV_FOREGROUND_COLOR_STRATEGY, 58 BG_RESTORE_BOUNDS, 59 60 // Frame Geometry 61 SAVE_FRAME, 62 FRAME_OFFSET, 63 CLIP_TO_FRAME, 64 CONTENT_STYLE, 65 CHILDREN, 66 FOREGROUND_STYLE, 67 RESTORE_FRAME, 68 69 // FG properties in Bounds clip 70 FG_SAVE_BOUNDS, 71 FG_CLIP_TO_BOUNDS, 72 BINARIZATION, 73 COLOR_FILTER, 74 LIGHT_UP_EFFECT, 75 DYNAMIC_DIM, 76 COMPOSITING_FILTER, 77 FOREGROUND_COLOR, 78 FG_RESTORE_BOUNDS, 79 80 // No clip (unless ClipToBounds is set) 81 POINT_LIGHT, 82 BORDER, 83 OVERLAY, 84 PARTICLE_EFFECT, 85 PIXEL_STRETCH, 86 87 // Restore state 88 RESTORE_BLENDER, 89 RESTORE_FOREGROUND_FILTER, 90 RESTORE_ALL, 91 92 // Annotations: Please remember to update this when new slots are added. 93 // properties before Background, not clipped 94 TRANSITION_PROPERTIES_BEGIN = SHADOW, 95 TRANSITION_PROPERTIES_END = OUTLINE, 96 // background properties, clipped by bounds by default 97 BG_PROPERTIES_BEGIN = BLENDER, 98 BG_PROPERTIES_END = ENV_FOREGROUND_COLOR_STRATEGY, 99 // content properties, can be clipped by ClipToFrame and ClipToBounds 100 CONTENT_BEGIN = FRAME_OFFSET, 101 CONTENT_END = FOREGROUND_STYLE, 102 // foreground properties, clipped by bounds by default 103 FG_PROPERTIES_BEGIN = BINARIZATION, 104 FG_PROPERTIES_END = FOREGROUND_COLOR, 105 // post-foreground properties, can be clipped by ClipToBounds 106 EXTRA_PROPERTIES_BEGIN = POINT_LIGHT, 107 EXTRA_PROPERTIES_END = PIXEL_STRETCH, 108 109 MAX = RESTORE_ALL + 1, 110 }; 111 112 // pure virtual base class 113 class RSDrawable : public std::enable_shared_from_this<RSDrawable> { 114 public: 115 RSDrawable() = default; 116 virtual ~RSDrawable() = default; 117 118 // Not copyable and moveable 119 RSDrawable(const RSDrawable&) = delete; 120 RSDrawable(const RSDrawable&&) = delete; 121 RSDrawable& operator=(const RSDrawable&) = delete; 122 RSDrawable& operator=(const RSDrawable&&) = delete; 123 124 // Type definitions 125 using Ptr = std::shared_ptr<RSDrawable>; 126 using Vec = std::array<Ptr, static_cast<size_t>(RSDrawableSlot::MAX)>; 127 using Generator = std::function<Ptr(const RSRenderNode&)>; 128 129 // UI methods: OnUpdate and OnGenerate (static method defined in every subclass) can only access the UI (staging) 130 // members, else may cause crash. 131 // OnUpdate and OnGenerate will be invoked if related property has changed, if false is returned, the drawable will 132 // be erased. OnUpdate(const RSRenderNode & content)133 virtual bool OnUpdate(const RSRenderNode& content) 134 { 135 return true; 136 } 137 138 // Render helper methods: This func is called in UI thread, but the generated DrawFunc will be called in RT thread, 139 // they can only access the RT members, else may cause crash 140 virtual Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const = 0; 141 142 // Sync methods, then can access all members and do UI->RT sync 143 virtual void OnSync() = 0; 144 OnPurge()145 virtual void OnPurge() {}; 146 147 // static generate & update helper methods 148 // Step 1, calculate dirtySlots based on dirty modifiers 149 static std::unordered_set<RSDrawableSlot> CalculateDirtySlots( 150 const ModifierDirtyTypes& dirtyTypes, const Vec& drawableVec); 151 // Step 2, for every dirtySlot, update or generate RSDrawable 152 static bool UpdateDirtySlots( 153 const RSRenderNode& node, Vec& drawableVec, std::unordered_set<RSDrawableSlot>& dirtySlots); 154 // Step 2-1 (optional), fuze some drawables 155 static bool FuzeDrawableSlots(const RSRenderNode& node, Vec& drawableVec); 156 // Step 3, insert necessary Clip/Save/Restore into drawableVec 157 static void UpdateSaveRestore(RSRenderNode& node, Vec& drawableVec, uint8_t& drawableVecStatus); 158 }; 159 } // namespace OHOS::Rosen 160 #endif // RENDER_SERVICE_BASE_DRAWABLE_RS_DRAWABLE_H 161