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 SKIACANVAS_H 17 #define SKIACANVAS_H 18 19 #include "include/core/SkBitmap.h" 20 #include "include/core/SkCanvas.h" 21 #include "include/core/HMSymbol.h" 22 #include "include/core/SkImage.h" 23 #include "include/core/SkMatrix.h" 24 #include "include/core/SkPaint.h" 25 #include "include/core/SkPicture.h" 26 #include "include/core/SkPoint3.h" 27 #include "include/core/SkRegion.h" 28 #include "include/core/SkRRect.h" 29 #include "include/utils/SkShadowUtils.h" 30 #include "skia_bitmap.h" 31 #include "skia_image.h" 32 #include "skia_matrix.h" 33 #include "skia_paint.h" 34 #include "skia_picture.h" 35 #include "skia_region.h" 36 #include "skia_vertices.h" 37 38 #include "common/rs_macros.h" 39 #include "impl_interface/core_canvas_impl.h" 40 41 namespace OHOS { 42 namespace Rosen { 43 namespace Drawing { 44 #ifndef USE_ROSEN_DRAWING 45 class RS_EXPORT SkiaCanvas : public CoreCanvasImpl { 46 #else 47 class DRAWING_API SkiaCanvas : public CoreCanvasImpl { 48 #endif 49 public: 50 static inline constexpr AdapterType TYPE = AdapterType::SKIA_ADAPTER; 51 52 SkiaCanvas(); 53 explicit SkiaCanvas(const std::shared_ptr<SkCanvas>& skCanvas); 54 SkiaCanvas(int32_t width, int32_t height); ~SkiaCanvas()55 ~SkiaCanvas() override {}; 56 GetType()57 AdapterType GetType() const override 58 { 59 return AdapterType::SKIA_ADAPTER; 60 } 61 62 void Bind(const Bitmap& bitmap) override; 63 64 Matrix GetTotalMatrix() const override; 65 Rect GetLocalClipBounds() const override; 66 RectI GetDeviceClipBounds() const override; 67 #ifdef ACE_ENABLE_GPU 68 std::shared_ptr<GPUContext> GetGPUContext() const override; 69 #endif 70 int32_t GetWidth() const override; 71 int32_t GetHeight() const override; 72 ImageInfo GetImageInfo() override; 73 bool ReadPixels(const ImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, 74 int srcX, int srcY) override; 75 bool ReadPixels(const Bitmap& dstBitmap, int srcX, int srcY) override; 76 77 // shapes 78 void DrawPoint(const Point& point) override; 79 void DrawPoints(PointMode mode, size_t count, const Point pts[]) override; 80 void DrawLine(const Point& startPt, const Point& endPt) override; 81 void DrawRect(const Rect& rect) override; 82 void DrawRoundRect(const RoundRect& roundRect) override; 83 void DrawNestedRoundRect(const RoundRect& outer, const RoundRect& inner) override; 84 void DrawArc(const Rect& oval, scalar startAngle, scalar sweepAngle) override; 85 void DrawPie(const Rect& oval, scalar startAngle, scalar sweepAngle) override; 86 void DrawOval(const Rect& oval) override; 87 void DrawCircle(const Point& centerPt, scalar radius) override; 88 void DrawPath(const Path& path) override; 89 void DrawBackground(const Brush& brush) override; 90 void DrawShadow(const Path& path, const Point3& planeParams, const Point3& devLightPos, scalar lightRadius, 91 Color ambientColor, Color spotColor, ShadowFlags flag) override; 92 void DrawRegion(const Region& region) override; 93 void DrawPatch(const Point cubics[12], const ColorQuad colors[4], 94 const Point texCoords[4], BlendMode mode) override; 95 void DrawVertices(const Vertices& vertices, BlendMode mode) override; 96 97 void DrawImageNine(const Image* image, const RectI& center, const Rect& dst, 98 FilterMode filter, const Brush* brush = nullptr) override; 99 void DrawImageLattice(const Image* image, const Lattice& lattice, const Rect& dst, 100 FilterMode filter, const Brush* brush = nullptr) override; 101 102 // color 103 void DrawColor(ColorQuad color, BlendMode mode) override; 104 105 // image 106 void DrawBitmap(const Bitmap& bitmap, const scalar px, const scalar py) override; 107 void DrawBitmap(Media::PixelMap& pixelMap, const scalar px, const scalar py) override; 108 void DrawImage(const Image& image, const scalar px, const scalar py, const SamplingOptions& sampling) override; 109 void DrawImageRect(const Image& image, const Rect& src, const Rect& dst, const SamplingOptions& sampling, 110 SrcRectConstraint constraint) override; 111 void DrawImageRect(const Image& image, const Rect& dst, const SamplingOptions& sampling) override; 112 void DrawPicture(const Picture& picture) override; 113 114 void DrawSVGDOM(const sk_sp<SkSVGDOM>& svgDom) override; 115 116 // text 117 void DrawTextBlob(const TextBlob* blob, const scalar x, const scalar y) override; 118 119 // symbol 120 void DrawSymbol(const DrawingHMSymbolData& symbol, Point locate) override; 121 122 // clip 123 void ClipRect(const Rect& rect, ClipOp op, bool doAntiAlias) override; 124 void ClipIRect(const RectI& rect, ClipOp op = ClipOp::INTERSECT) override; 125 void ClipRoundRect(const RoundRect& roundRect, ClipOp op, bool doAntiAlias) override; 126 void ClipRoundRect(const Rect& rect, std::vector<Point>& pts, bool doAntiAlias) override; 127 void ClipPath(const Path& path, ClipOp op, bool doAntiAlias) override; 128 void ClipRegion(const Region& region, ClipOp op = ClipOp::INTERSECT) override; 129 bool IsClipEmpty() override; 130 bool IsClipRect() override; 131 bool QuickReject(const Rect& rect) override; 132 133 // transform 134 void SetMatrix(const Matrix& matrix) override; 135 void ResetMatrix() override; 136 void ConcatMatrix(const Matrix& matrix) override; 137 void Translate(scalar dx, scalar dy) override; 138 void Scale(scalar sx, scalar sy) override; 139 void Rotate(scalar deg, scalar sx, scalar sy) override; 140 void Shear(scalar sx, scalar sy) override; 141 142 // state 143 void Flush() override; 144 void Clear(ColorQuad color) override; 145 uint32_t Save() override; 146 void SaveLayer(const SaveLayerOps& saveLayerOps) override; 147 void Restore() override; 148 uint32_t GetSaveCount() const override; 149 void Discard() override; 150 151 // paint 152 void AttachPaint(const Paint& paint) override; 153 154 SkCanvas* ExportSkCanvas() const; 155 void ImportSkCanvas(SkCanvas* skCanvas); 156 157 void BuildOverDraw(std::shared_ptr<Canvas> canvas) override; 158 159 private: 160 void RoundRectCastToSkRRect(const RoundRect& roundRect, SkRRect& skRRect) const; 161 bool ConvertToHMSymbolData(const DrawingHMSymbolData& symbol, HMSymbolData& skSymbol); 162 std::shared_ptr<SkCanvas> skiaCanvas_; 163 SkCanvas* skCanvas_; 164 SkiaPaint skiaPaint_; 165 }; 166 } // namespace Drawing 167 } // namespace Rosen 168 } // namespace OHOS 169 #endif 170