1 /* 2 * Copyright (c) 2021-2025 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 BRUSH_H 17 #define BRUSH_H 18 19 #include "draw/color.h" 20 #include "effect/color_space.h" 21 #include "effect/blender.h" 22 #include "effect/blur_draw_looper.h" 23 #include "effect/filter.h" 24 #include "effect/shader_effect.h" 25 #include "utils/drawing_macros.h" 26 #include "utils/rect.h" 27 28 namespace OHOS { 29 namespace Rosen { 30 namespace Drawing { 31 class DRAWING_API Brush { 32 public: 33 Brush() noexcept; 34 Brush(const Brush& b) noexcept = default; 35 Brush(const Color& c) noexcept; 36 Brush(const std::shared_ptr<ShaderEffect> e) noexcept; 37 Brush(int rgba) noexcept; 38 ~Brush()39 ~Brush() {} 40 41 /** 42 * @brief Retrieves alpha and RGB, unpremultiplied, packed into 32 bits. 43 * @return unpremultiplied ARGB. 44 */ 45 const Color& GetColor() const; 46 47 /** 48 * @brief Sets alpha and RGB used when stroking and filling. The color is a 32-bit value, 49 * unpremultiplied, packing 8-bit components for alpha, red, blue, and green. 50 * @param color unpremultiplied ARGB 51 */ 52 void SetColor(const Color& c); 53 54 /** 55 * @brief Sets alpha and RGB used when stroking and filling. The color is a 32-bit value, 56 * unpremultiplied, packing 8-bit components for alpha, red, blue, and green. 57 * @param color unpremultiplied ARGB 58 */ 59 void SetColor(uint32_t c); 60 61 /** 62 * @brief Sets color used when drawing solid fills. The color components range from 0 to 255. 63 * The color is unpremultiplied; alpha sets the transparency independent of RGB. 64 * @param a amount of alpha, from fully transparent (0) to fully opaque (255) 65 * @param r amount of red, from no red (0) to full red (255) 66 * @param g amount of green, from no green (0) to full green (255) 67 * @param b amount of blue, from no blue (0) to full blue (255) 68 */ 69 void SetARGB(int a, int r, int g, int b); 70 71 /** 72 * @brief Retrieves alpha and RGB, unpremultiplied, as four floating point values. RGB are 73 * extended sRGB values (sRGB gamut, and encoded with the sRGB transfer function). 74 * @return unpremultiplied RGBA 75 */ 76 const Color4f& GetColor4f(); 77 78 /** 79 * @brief Retrieves a shared pointer to color space of current Brush. 80 * @return a shared pointer to color space of current Brush 81 */ GetColorSpace()82 const std::shared_ptr<ColorSpace> GetColorSpace() const { return colorSpace_; } 83 84 /** 85 * @brief Retrieves a pointer to color space of current Brush. 86 * @return a pointer to color space of current Brush 87 */ GetColorSpacePtr()88 const ColorSpace* GetColorSpacePtr() const { return colorSpace_.get(); } 89 90 /** 91 * @brief Sets alpha and RGB used when stroking and filling. The color is four floating 92 * point values, unpremultiplied. The color values are interpreted as being in 93 * the s. If s is nullptr, then color is assumed to be in the sRGB color space. 94 * @param color unpremultiplied ARGB 95 * @param s ColorSpace describing the encoding of color 96 */ 97 void SetColor(const Color4f& cf, std::shared_ptr<ColorSpace> s); 98 99 /** 100 * @brief Helper that scales the alpha by 255. 101 * @return alpha scaled 255 102 */ GetAlpha()103 inline uint32_t GetAlpha() const 104 { 105 return color_.GetAlpha(); 106 } 107 108 /** 109 * @brief Retrieves alpha from the color used when stroking and filling. 110 * @return alpha ranging from zero, fully transparent, to 255, fully opaque 111 */ GetAlphaF()112 inline scalar GetAlphaF() const 113 { 114 return color_.GetAlphaF(); 115 } 116 117 /** 118 * @brief Retrieves the red component of the color, ranging from 0 to 1. 119 * @return Return red component of color, from zero to 1. 120 */ GetRedF()121 inline scalar GetRedF() const 122 { 123 return color_.GetRedF(); 124 } 125 126 /** 127 * @brief Retrieves the green component of the color, ranging from 0 to 1. 128 * @return Return green component of color, from zero to 1. 129 */ GetGreenF()130 inline scalar GetGreenF() const 131 { 132 return color_.GetGreenF(); 133 } 134 135 /** 136 * @brief Retrieves the blue component of the color, ranging from 0 to 1. 137 * @return Return blue component of color, from zero to 1. 138 */ GetBlueF()139 inline scalar GetBlueF() const 140 { 141 return color_.GetBlueF(); 142 } 143 144 /** 145 * @brief Helper that accepts an int between 0 and 255, and divides it by 255.0. 146 * @param a An int between 0 and 255 147 */ 148 void SetAlpha(uint32_t a); 149 150 /** 151 * @brief Replaces alpha, leaving RGB unchanged. 152 * An out of range value triggers an assert in the debug build. 153 * a is a value from 0.0 to 1.0. a set to zero makes color fully transparent; a set to 154 * 1.0 makes color fully opaque. 155 * @param a alpha component of color 156 */ 157 void SetAlphaF(scalar a); 158 159 /** 160 * @brief Queries the blender. 161 * @return the blender represented as a BlendMode 162 */ GetBlendMode()163 const BlendMode& GetBlendMode() const { return blendMode_; } 164 165 /** 166 * @brief Sets a blender that implements the specified blendmode enum. 167 * @param mode the BlendMode used to set the blender 168 */ 169 void SetBlendMode(const BlendMode& mode); 170 171 /** 172 * @brief Sets Filter to filter. 173 * @param filter Filter to apply to subsequent draw 174 */ 175 void SetFilter(const Filter& filter); 176 177 /** 178 * @brief Returns Filter if set, ot nullptr. 179 * @return Filter if previously set, nullptr otherwise 180 */ 181 const Filter& GetFilter() const; 182 183 /** 184 * @brief Queries the existance of filter. 185 * @return true if the Brush has a filter, otherwise false 186 */ HasFilter()187 bool HasFilter() const { return hasFilter_; } 188 189 /** 190 * @brief Sets optional colors used when filling a path, such as a gradient. 191 * @param e how geometry is filled with color 192 */ 193 void SetShaderEffect(std::shared_ptr<ShaderEffect> e); 194 195 /** 196 * @brief Returns optional colors used when filling a path, such as a gradient. 197 * @return ShaderEffect if previously set, nullptr otherwise 198 */ GetShaderEffect()199 const std::shared_ptr<ShaderEffect> GetShaderEffect() const { return shaderEffect_; } 200 201 /** 202 * @brief Returns optional colors used when filling a path, such as a gradient. 203 * @return ShaderEffect if previously set, nullptr otherwise 204 */ GetShaderEffectPtr()205 const ShaderEffect* GetShaderEffectPtr() const { return shaderEffect_.get(); } 206 207 /** 208 * @brief Sets the current blender, increasing its refcnt, and if a blender is already 209 * present, decreasing that object's refcnt. 210 * @param blender Blender used to set 211 */ 212 void SetBlender(std::shared_ptr<Blender> blender); 213 214 /** 215 * @brief Returns the user-supplied blend function, if one has been set. 216 * @return the Blender assigned to this Brush, otherwise nullptr 217 */ GetBlender()218 const std::shared_ptr<Blender> GetBlender() const { return blender_; } 219 220 /** 221 * @brief Sets the blenderEnabled flag, which determines whether the blender is used. 222 * @param blenderEnabled whether the blender is used. 223 */ 224 void SetBlenderEnabled(bool blenderEnabled); 225 226 /** 227 * @brief Returns the blenderEnabled flag, which determines whether the blender is used. 228 * @return the blenderEnabled flag, whether the blender is used. 229 */ GetBlenderEnabled()230 bool GetBlenderEnabled() const { return blenderEnabled_; }; 231 232 /** 233 * @brief Returns the user-supplied blend function, if one has been set. 234 * @return the Blender assigned to this Brush, otherwise nullptr 235 */ GetBlenderPtr()236 const Blender* GetBlenderPtr() const { return blender_.get(); } 237 238 /** 239 * @brief Returns true if pixels on the active edges of Path may be drawn with partial transparency. 240 * @return antialiasing state 241 */ IsAntiAlias()242 bool IsAntiAlias() const { return antiAlias_; } 243 244 /** 245 * @brief Requests, but does not require, that edge pixels draw opaque or with 246 * partial transparency. 247 * @param aa setting for antialiasing 248 */ 249 void SetAntiAlias(bool aa); 250 251 /** 252 * @brief Returns true if Brush does not include elements requiring extensive computation 253 * to compute bounds of drawn geometry. For instance, Brush with PathEffect 254 * always returns false. 255 * @return true if Brush allows for fast computation of bounds 256 */ 257 bool CanComputeFastBounds(); 258 259 /** 260 * @brief Only call this if CanComputeFastBounds() returned true. This takes a 261 * raw rectangle (the raw bounds of a shape), and adjusts it for stylistic 262 * effects in the Brush (e.g. stroking). If needed, it uses the storage 263 * parameter. It returns the adjusted bounds that can then be used 264 * for CoreCanvas::QuickReject tests. 265 * 266 * The returned Rect will either be orig or storage, thus the caller 267 * should not rely on storage being set to the result, but should always 268 * use the returned value. It is legal for orig and storage to be the same Rect. 269 * @param orig geometry modified by Brush when drawn 270 * @param storage computed bounds of geometry; may not be nullptr 271 * @return fast computed bounds 272 */ 273 const Rect& ComputeFastBounds(const Rect& orig, Rect* storage); 274 275 /** 276 * @brief Queries Whether the current blender can be represented as a BlendMode enum or not. 277 * @return true if can be represented, otherwise false 278 */ 279 bool AsBlendMode(); 280 281 /** 282 * @brief Sets all Brush contents to their initial values. This is equivalent to replacing 283 * Brush with the result of Brush(). 284 */ 285 void Reset(); 286 287 /** 288 * @brief Sets BlurDrawLooper, it will generate two draw operations, which may affect performance. 289 */ 290 void SetLooper(std::shared_ptr<BlurDrawLooper> blurDrawLooper); 291 292 /** 293 * @brief Gets BlurDrawLooper. 294 */ 295 std::shared_ptr<BlurDrawLooper> GetLooper() const; 296 297 friend DRAWING_API bool operator==(const Brush& b1, const Brush& b2); 298 friend DRAWING_API bool operator!=(const Brush& b1, const Brush& b2); 299 300 void Dump(std::string& out) const; 301 302 private: 303 Color color_; 304 BlendMode blendMode_; 305 Filter filter_; 306 std::shared_ptr<ColorSpace> colorSpace_; 307 std::shared_ptr<ShaderEffect> shaderEffect_; 308 std::shared_ptr<Blender> blender_; 309 std::shared_ptr<BlurDrawLooper> blurDrawLooper_; 310 311 bool antiAlias_; 312 bool blenderEnabled_ = true; 313 bool hasFilter_ = false; 314 }; 315 } // namespace Drawing 316 } // namespace Rosen 317 } // namespace OHOS 318 #endif