1 /* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.. All rights reserved. 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 CORE_CANVAS_H 17 #define CORE_CANVAS_H 18 19 #include <memory> 20 21 #include "common/rs_macros.h" 22 #include "drawing/engine_adapter/impl_interface/core_canvas_impl.h" 23 #include "utils/drawing_macros.h" 24 25 namespace OHOS { 26 namespace Rosen { 27 namespace Drawing { 28 enum class SrcRectConstraint { 29 STRICT_SRC_RECT_CONSTRAINT, 30 FAST_SRC_RECT_CONSTRAINT, 31 }; 32 33 /* 34 * @brief PointMode: Selects if an array of points are drawn as discrete points, as lines, or as 35 * an open polygon. 36 */ 37 enum class PointMode { 38 POINTS_POINTMODE, // draw each point separately 39 LINES_POINTMODE, // draw each pair of points as a line segment 40 POLYGON_POINTMODE, // draw the array of points as a open polygon 41 }; 42 43 enum class QuadAAFlags { 44 LEFT_QUADAAFLAG = 1, 45 TOP_QUADAAFLAG = 2, 46 RIGHT_QUADAAFLAG = 4, 47 BOTTOM_QUADAAFLAG = 8, 48 NONE_QUADAAFLAGS = 0, 49 ALL_QUADAAFLAGS = 15, 50 }; 51 52 const int DIVES_SIZE = 2; 53 #undef TRANSPARENT 54 struct Lattice { 55 enum RectType : uint8_t { 56 DEFAULT = 0, 57 TRANSPARENT, 58 FIXEDCOLOR, 59 }; 60 int fXDivs[DIVES_SIZE]; 61 int fYDivs[DIVES_SIZE]; 62 RectType fRectTypes = RectType::DEFAULT; 63 int fXCount; 64 int fYCount; 65 RectI fBounds; 66 Color fColors; 67 }; 68 69 enum CacheType : uint8_t { 70 UNDEFINED, // do not change current cache status 71 ENABLED, // explicitly enable cache 72 DISABLED, // explicitly disable cache 73 OFFSCREEN, // offscreen rendering 74 }; 75 76 class Surface; 77 78 /* 79 * @brief Contains the option used to create the layer. 80 */ 81 #ifndef USE_ROSEN_DRAWING 82 class RS_EXPORT SaveLayerOps { 83 #else 84 class DRAWING_API SaveLayerOps { 85 #endif 86 public: 87 // How to allocate layer 88 enum Flags { 89 INIT_WITH_PREVIOUS = 1 << 1, // create with previous contents 90 }; 91 SaveLayerOps()92 SaveLayerOps() : bounds_(nullptr), brush_(nullptr), saveLayerFlags_(0) {} 93 94 /* 95 * @param bounds The bounds of layer, may be nullptr. 96 * @param brush When restoring the current layer, attach this brush, may be nullptr. 97 * @param saveLayerFlags How to allocate layer. 98 */ 99 SaveLayerOps(const Rect* bounds, const Brush* brush, uint32_t saveLayerFlags = 0) bounds_(bounds)100 : bounds_(bounds), brush_(brush), saveLayerFlags_(saveLayerFlags) {} ~SaveLayerOps()101 ~SaveLayerOps() {} 102 103 /* 104 * @brief Gets the bounds of layer, may be nullptr. 105 */ GetBounds()106 const Rect* GetBounds() const 107 { 108 return bounds_; 109 } 110 111 /* 112 * @brief Gets the brush of layer, may be nullptr. 113 */ GetBrush()114 const Brush* GetBrush() const 115 { 116 return brush_; 117 } 118 119 /* 120 * @brief Gets the options to modify layer. 121 */ GetSaveLayerFlags()122 uint32_t GetSaveLayerFlags() const 123 { 124 return saveLayerFlags_; 125 } 126 127 private: 128 const Rect* bounds_; 129 const Brush* brush_; 130 uint32_t saveLayerFlags_; 131 }; 132 133 #ifndef USE_ROSEN_DRAWING 134 class RS_EXPORT CoreCanvas { 135 #else 136 class DRAWING_API CoreCanvas { 137 #endif 138 public: 139 CoreCanvas(); 140 explicit CoreCanvas(void* rawCanvas); ~CoreCanvas()141 virtual ~CoreCanvas() {} 142 void Bind(const Bitmap& bitmap); 143 144 void BuildOverDraw(std::shared_ptr<Canvas> canvas); 145 GetDrawingType()146 virtual DrawingType GetDrawingType() const 147 { 148 return DrawingType::COMMON; 149 } 150 151 /* 152 * @brief Gets the total matrix of Canvas to device. 153 */ 154 virtual Matrix GetTotalMatrix() const; 155 156 /* 157 * @brief Gets bounds of clip in local coordinates. 158 */ 159 virtual Rect GetLocalClipBounds() const; 160 161 /* 162 * @brief Gets bounds of clip in device coordinates. 163 */ 164 virtual RectI GetDeviceClipBounds() const; 165 166 /* 167 * @brief Gets GPU context of the GPU surface associated with Canvas. 168 */ 169 #ifdef ACE_ENABLE_GPU 170 virtual std::shared_ptr<GPUContext> GetGPUContext(); 171 #endif 172 173 /* 174 * @brief Gets width of Canvas. 175 */ 176 int32_t GetWidth() const; 177 178 /* 179 * @brief Gets height of Canvas. 180 */ 181 int32_t GetHeight() const; 182 183 /* 184 * @brief Gets ImageInfo of Canvas. 185 */ 186 ImageInfo GetImageInfo(); 187 188 bool ReadPixels(const ImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, 189 int srcX, int srcY); 190 191 bool ReadPixels(const Bitmap& dstBitmap, int srcX, int srcY); 192 193 // shapes 194 virtual void DrawPoint(const Point& point); 195 virtual void DrawPoints(PointMode mode, size_t count, const Point pts[]); 196 virtual void DrawLine(const Point& startPt, const Point& endPt); 197 virtual void DrawRect(const Rect& rect); 198 virtual void DrawRoundRect(const RoundRect& roundRect); 199 virtual void DrawNestedRoundRect(const RoundRect& outer, const RoundRect& inner); 200 virtual void DrawArc(const Rect& oval, scalar startAngle, scalar sweepAngle); 201 virtual void DrawPie(const Rect& oval, scalar startAngle, scalar sweepAngle); 202 virtual void DrawOval(const Rect& oval); 203 virtual void DrawCircle(const Point& centerPt, scalar radius); 204 virtual void DrawPath(const Path& path); 205 virtual void DrawBackground(const Brush& brush); 206 virtual void DrawShadow(const Path& path, const Point3& planeParams, const Point3& devLightPos, scalar lightRadius, 207 Color ambientColor, Color spotColor, ShadowFlags flag); 208 209 // color 210 virtual void DrawColor(ColorQuad color, BlendMode mode = BlendMode::SRC_OVER); 211 212 /* 213 * @brief Draws Region on the Canvas. 214 * @param region Region to draw. 215 */ 216 virtual void DrawRegion(const Region& region); 217 218 virtual void DrawPatch(const Point cubics[12], const ColorQuad colors[4], 219 const Point texCoords[4], BlendMode mode); 220 virtual void DrawVertices(const Vertices& vertices, BlendMode mode); 221 222 virtual void DrawImageNine(const Image* image, const RectI& center, const Rect& dst, 223 FilterMode filter, const Brush* brush = nullptr); 224 virtual void DrawImageLattice(const Image* image, const Lattice& lattice, const Rect& dst, 225 FilterMode filter, const Brush* brush = nullptr); 226 227 // image 228 virtual void DrawBitmap(const Bitmap& bitmap, const scalar px, const scalar py); 229 virtual void DrawBitmap(Media::PixelMap& pixelMap, const scalar px, const scalar py); 230 virtual void DrawImage(const Image& image, const scalar px, const scalar py, const SamplingOptions& sampling); 231 virtual void DrawImageRect(const Image& image, const Rect& src, const Rect& dst, const SamplingOptions& sampling, 232 SrcRectConstraint constraint = SrcRectConstraint::STRICT_SRC_RECT_CONSTRAINT); 233 virtual void DrawImageRect(const Image& image, const Rect& dst, const SamplingOptions& sampling); 234 virtual void DrawPicture(const Picture& picture); 235 236 // temporary interface. Support drawing of SkSVGDOM 237 virtual void DrawSVGDOM(const sk_sp<SkSVGDOM>& svgDom); 238 239 // text 240 virtual void DrawTextBlob(const TextBlob* blob, const scalar x, const scalar y); 241 242 // symbol 243 virtual void DrawSymbol(const DrawingHMSymbolData& symbol, Point locate); 244 245 // clip 246 /* 247 * @brief Replace the clipping area with the intersection or difference between the 248 current clipping area and Rect, and use a clipping edge that is aliased or anti-aliased. 249 * @param rect To combine with clipping area. 250 * @param op To apply to clip. 251 * @param doAntiAlias true if clip is to be anti-aliased. The default value is false. 252 */ 253 virtual void ClipRect(const Rect& rect, ClipOp op = ClipOp::INTERSECT, bool doAntiAlias = false); 254 255 /* 256 * @brief Replace the clipping area with the intersection or difference between the 257 current clipping area and RectI, and use a clipping edge that is aliased or anti-aliased. 258 * @param rect To combine with clipping area. 259 * @param op To apply to clip. 260 * @param doAntiAlias true if clip is to be anti-aliased. The default value is false. 261 */ 262 virtual void ClipIRect(const RectI& rect, ClipOp op = ClipOp::INTERSECT); 263 264 /* 265 * @brief Replace the clipping area with the intersection or difference of the 266 current clipping area and Rect, and use a clipping edge that is aliased or anti-aliased. 267 * @param roundRect To combine with clip. 268 * @param op To apply to clip. 269 * @param doAntiAlias true if clip is to be anti-aliased. The default value is false. 270 */ 271 virtual void ClipRoundRect(const RoundRect& roundRect, ClipOp op = ClipOp::INTERSECT, bool doAntiAlias = false); 272 273 virtual void ClipRoundRect(const Rect& rect, std::vector<Point>& pts, bool doAntiAlias = false); 274 275 /* 276 * @brief Replace the clipping area with the intersection or difference of the 277 current clipping area and Path, and use a clipping edge that is aliased or anti-aliased. 278 * @param path To combine with clip. 279 * @param op To apply to clip. 280 * @param doAntiAlias true if clip is to be anti-aliased. The default value is false. 281 */ 282 virtual void ClipPath(const Path& path, ClipOp op = ClipOp::INTERSECT, bool doAntiAlias = false); 283 284 /* 285 * @brief Replace the clipping area with the intersection or difference of the 286 current clipping area and Region, and use a clipping edge that is aliased or anti-aliased. 287 * @param region To combine with clip. 288 * @param op To apply to clip.The default value is ClipOp::INTERSECT 289 */ 290 virtual void ClipRegion(const Region& region, ClipOp op = ClipOp::INTERSECT); 291 292 /* 293 * @brief Returns true if clip is empty. 294 */ 295 virtual bool IsClipEmpty(); 296 297 /* 298 * @brief Returns true if clip is SkRect and not empty. 299 */ 300 virtual bool IsClipRect(); 301 302 /* 303 * @brief Returns true if clip is emptySkRect rect, transformed by SkMatrix, 304 * can be quickly determined to be outside of clip. 305 */ 306 virtual bool QuickReject(const Rect& rect); 307 308 // transform 309 virtual void SetMatrix(const Matrix& matrix); 310 virtual void ResetMatrix(); 311 virtual void ConcatMatrix(const Matrix& matrix); 312 virtual void Translate(scalar dx, scalar dy); 313 virtual void Scale(scalar sx, scalar sy); 314 315 /* 316 * @brief Rotates Matrix by degrees. 317 * @param deg Amount to rotate, in degrees. 318 */ Rotate(scalar deg)319 void Rotate(scalar deg) 320 { 321 Rotate(deg, 0, 0); 322 } 323 virtual void Rotate(scalar deg, scalar sx, scalar sy); 324 virtual void Shear(scalar sx, scalar sy); 325 326 // state 327 virtual void Flush(); 328 virtual void Clear(ColorQuad color); 329 330 /* 331 * @brief Saves Matrix and clipping area, return the number of saved states. 332 */ 333 virtual uint32_t Save(); 334 335 /* 336 * @brief Saves Matrix and clipping area, and allocates Surface for subsequent drawing. 337 * @param saveLayerOps Contains the option used to create the layer. 338 */ 339 virtual void SaveLayer(const SaveLayerOps& saveLayerOps); 340 virtual void Restore(); 341 342 /* 343 * @brief Returns the number of saved states, each containing Matrix and clipping area. 344 */ 345 virtual uint32_t GetSaveCount() const; 346 347 /* 348 * @brief Makes Canvas contents undefined. 349 */ 350 virtual void Discard(); 351 352 // paint 353 virtual CoreCanvas& AttachPen(const Pen& pen); 354 virtual CoreCanvas& AttachBrush(const Brush& brush); 355 virtual CoreCanvas& AttachPaint(const Paint& paint); 356 virtual CoreCanvas& DetachPen(); 357 virtual CoreCanvas& DetachBrush(); 358 virtual CoreCanvas& DetachPaint(); 359 360 virtual ColorQuad GetEnvForegroundColor() const; 361 virtual bool isHighContrastEnabled() const; 362 virtual Drawing::CacheType GetCacheType() const; 363 virtual Drawing::Surface* GetSurface() const; 364 365 virtual float GetAlpha() const; 366 virtual int GetAlphaSaveCount() const; 367 368 template<typename T> GetImpl()369 T* GetImpl() const 370 { 371 return impl_->DowncastingTo<T>(); 372 } 373 std::shared_ptr<CoreCanvasImpl> GetCanvasData() const; 374 375 protected: 376 CoreCanvas(int32_t width, int32_t height); 377 Paint paintBrush_; 378 Paint paintPen_; 379 380 private: 381 void AttachPaint(); 382 std::shared_ptr<CoreCanvasImpl> impl_; 383 #ifdef ACE_ENABLE_GPU 384 std::shared_ptr<GPUContext> gpuContext_; 385 #endif 386 }; 387 } // namespace Drawing 388 } // namespace Rosen 389 } // namespace OHOS 390 #endif 391