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