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_PROPERTY_DRAWABLE_FOREGROUND_H 17 #define RENDER_SERVICE_BASE_DRAWABLE_RS_PROPERTY_DRAWABLE_FOREGROUND_H 18 19 #include "common/rs_rect.h" 20 #include "common/rs_vector4.h" 21 #include "drawable/rs_property_drawable.h" 22 #include "property/rs_properties.h" 23 #include "property/rs_properties_def.h" 24 25 namespace OHOS::Rosen { 26 class RSFilter; 27 class RSBorder; 28 class RSProperties; 29 namespace Drawing { 30 class RuntimeEffect; 31 class RuntimeShaderBuilder; 32 } // namespace Drawing 33 34 namespace DrawableV2 { 35 namespace { 36 constexpr int MAX_LIGHT_SOURCES = 12; 37 } 38 class RSBinarizationDrawable : public RSDrawable { 39 public: 40 RSBinarizationDrawable() = default; 41 ~RSBinarizationDrawable() override = default; 42 43 static RSDrawable::Ptr OnGenerate(const RSRenderNode& node); 44 bool OnUpdate(const RSRenderNode& node) override; 45 void OnSync() override; 46 Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override; 47 48 private: 49 bool needSync_ = false; 50 std::optional<Vector4f> aiInvert_; 51 std::optional<Vector4f> stagingAiInvert_; 52 }; 53 54 class RSColorFilterDrawable : public RSDrawable { 55 public: 56 RSColorFilterDrawable() = default; 57 ~RSColorFilterDrawable() override = default; 58 59 static RSDrawable::Ptr OnGenerate(const RSRenderNode& node); 60 bool OnUpdate(const RSRenderNode& node) override; 61 void OnSync() override; 62 Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override; 63 64 private: 65 bool needSync_ = false; 66 std::shared_ptr<Drawing::ColorFilter> filter_; 67 std::shared_ptr<Drawing::ColorFilter> stagingFilter_; 68 }; 69 70 class RSLightUpEffectDrawable : public RSDrawable { 71 public: 72 RSLightUpEffectDrawable() = default; 73 ~RSLightUpEffectDrawable() override = default; 74 75 static RSDrawable::Ptr OnGenerate(const RSRenderNode& node); 76 bool OnUpdate(const RSRenderNode& node) override; 77 void OnSync() override; 78 Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override; 79 80 private: 81 bool needSync_ = false; 82 float lightUpEffectDegree_ = 1.0f; 83 float stagingLightUpEffectDegree_ = 1.0f; 84 }; 85 86 class RSDynamicDimDrawable : public RSDrawable { 87 public: 88 RSDynamicDimDrawable() = default; 89 ~RSDynamicDimDrawable() override = default; 90 91 static RSDrawable::Ptr OnGenerate(const RSRenderNode& node); 92 bool OnUpdate(const RSRenderNode& node) override; 93 void OnSync() override; 94 Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override; 95 96 private: 97 bool needSync_ = false; 98 float dynamicDimDegree_ = 1.0f; 99 float stagingDynamicDimDegree_ = 1.0f; 100 }; 101 102 class RSCompositingFilterDrawable : public RSFilterDrawable { 103 public: 104 RSCompositingFilterDrawable() = default; 105 ~RSCompositingFilterDrawable() override = default; 106 107 static RSDrawable::Ptr OnGenerate(const RSRenderNode& node); 108 bool OnUpdate(const RSRenderNode& node) override; IsForeground()109 bool IsForeground() const override 110 { 111 return true; 112 } 113 }; 114 115 // foregroundFilter 116 class RSForegroundFilterDrawable : public RSDrawable { 117 public: 118 RSForegroundFilterDrawable() = default; 119 ~RSForegroundFilterDrawable() override = default; 120 121 static RSDrawable::Ptr OnGenerate(const RSRenderNode& node); 122 bool OnUpdate(const RSRenderNode& node) override; 123 void OnSync() override; 124 Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override; 125 126 private: 127 bool needSync_ = false; 128 RectF boundsRect_; 129 RectF stagingBoundsRect_; 130 }; 131 132 class RSForegroundFilterRestoreDrawable : public RSDrawable { 133 public: 134 RSForegroundFilterRestoreDrawable() = default; 135 ~RSForegroundFilterRestoreDrawable() override = default; 136 137 static RSDrawable::Ptr OnGenerate(const RSRenderNode& node); 138 bool OnUpdate(const RSRenderNode& node) override; 139 void OnSync() override; 140 Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override; 141 142 private: 143 bool needSync_ = false; 144 std::shared_ptr<RSFilter> foregroundFilter_; 145 std::shared_ptr<RSFilter> stagingForegroundFilter_; 146 }; 147 148 class RSForegroundColorDrawable : public RSPropertyDrawable { 149 public: RSForegroundColorDrawable(std::shared_ptr<Drawing::DrawCmdList> && drawCmdList)150 RSForegroundColorDrawable(std::shared_ptr<Drawing::DrawCmdList>&& drawCmdList) 151 : RSPropertyDrawable(std::move(drawCmdList)) 152 {} 153 RSForegroundColorDrawable() = default; 154 static RSDrawable::Ptr OnGenerate(const RSRenderNode& node); 155 bool OnUpdate(const RSRenderNode& node) override; 156 157 private: 158 }; 159 160 class RSPointLightDrawable : public RSDrawable { 161 public: RSPointLightDrawable(const RSProperties & properties)162 RSPointLightDrawable(const RSProperties &properties) : properties_(properties) {} 163 ~RSPointLightDrawable() override = default; 164 void OnSync() override; 165 static RSDrawable::Ptr OnGenerate(const RSRenderNode& node); 166 bool OnUpdate(const RSRenderNode& node) override; 167 Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override; 168 169 private: 170 const RSProperties &properties_; 171 std::vector<std::pair<std::shared_ptr<RSLightSource>, Vector4f>> lightSourcesAndPosVec_; 172 RectI rect_ = {}; 173 IlluminatedType illuminatedType_ = IlluminatedType::INVALID; 174 Drawing::RoundRect borderRRect_ = {}; 175 Drawing::RoundRect contentRRect_ = {}; 176 float borderWidth_ = 0.0f; 177 void DrawLight(Drawing::Canvas* canvas) const; 178 static const std::shared_ptr<Drawing::RuntimeShaderBuilder>& GetPhongShaderBuilder(); 179 void DrawContentLight(Drawing::Canvas& canvas, std::shared_ptr<Drawing::RuntimeShaderBuilder>& lightBuilder, 180 Drawing::Brush& brush, const std::array<float, MAX_LIGHT_SOURCES>& lightIntensityArray) const; 181 void DrawBorderLight(Drawing::Canvas& canvas, std::shared_ptr<Drawing::RuntimeShaderBuilder>& lightBuilder, 182 Drawing::Pen& pen, const std::array<float, MAX_LIGHT_SOURCES>& lightIntensityArray) const; 183 }; 184 185 // ============================================================================ 186 // Border & Outline 187 class RSBorderDrawable : public RSPropertyDrawable { 188 friend class RSOutlineDrawable; 189 190 public: RSBorderDrawable(std::shared_ptr<Drawing::DrawCmdList> && drawCmdList)191 RSBorderDrawable(std::shared_ptr<Drawing::DrawCmdList>&& drawCmdList) : RSPropertyDrawable(std::move(drawCmdList)) 192 {} 193 RSBorderDrawable() = default; 194 static RSDrawable::Ptr OnGenerate(const RSRenderNode& node); 195 bool OnUpdate(const RSRenderNode& node) override; 196 197 private: 198 static void DrawBorder(const RSProperties& properties, Drawing::Canvas& canvas, 199 const std::shared_ptr<RSBorder>& border, const bool& isOutline); 200 }; 201 202 class RSOutlineDrawable : public RSPropertyDrawable { 203 public: RSOutlineDrawable(std::shared_ptr<Drawing::DrawCmdList> && drawCmdList)204 RSOutlineDrawable(std::shared_ptr<Drawing::DrawCmdList>&& drawCmdList) : RSPropertyDrawable(std::move(drawCmdList)) 205 {} 206 RSOutlineDrawable() = default; 207 static RSDrawable::Ptr OnGenerate(const RSRenderNode& node); 208 bool OnUpdate(const RSRenderNode& node) override; 209 210 private: 211 }; 212 213 class RSParticleDrawable : public RSPropertyDrawable { 214 public: RSParticleDrawable(std::shared_ptr<Drawing::DrawCmdList> && drawCmdList)215 RSParticleDrawable(std::shared_ptr<Drawing::DrawCmdList>&& drawCmdList) : RSPropertyDrawable(std::move(drawCmdList)) 216 {} 217 RSParticleDrawable() = default; 218 static RSDrawable::Ptr OnGenerate(const RSRenderNode& node); 219 bool OnUpdate(const RSRenderNode& node) override; 220 221 private: 222 }; 223 224 class RSPixelStretchDrawable : public RSDrawable { 225 public: 226 RSPixelStretchDrawable() = default; 227 ~RSPixelStretchDrawable() override = default; 228 229 static RSDrawable::Ptr OnGenerate(const RSRenderNode& node); 230 bool OnUpdate(const RSRenderNode& node) override; 231 void OnSync() override; 232 Drawing::RecordingCanvas::DrawFunc CreateDrawFunc() const override; 233 void SetPixelStretch(const std::optional<Vector4f>& pixelStretch); 234 235 private: 236 bool needSync_ = false; 237 std::optional<Vector4f> pixelStretch_; 238 std::optional<Vector4f> stagingPixelStretch_; 239 int pixelStretchTileMode_ = 0; 240 int stagePixelStretchTileMode_ = 0; 241 bool boundsGeoValid_ = false; 242 bool stagingBoundsGeoValid_ = false; 243 RectF boundsRect_; 244 RectF stagingBoundsRect_; 245 }; 246 } // namespace DrawableV2 247 } // namespace OHOS::Rosen 248 #endif // RENDER_SERVICE_BASE_DRAWABLE_RS_PROPERTY_DRAWABLE_FOREGROUND_H 249