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 "draw/canvas.h" 20 #include "include/core/SkBitmap.h" 21 #include "include/core/SkCanvas.h" 22 #include "include/core/HMSymbol.h" 23 #include "include/core/SkImage.h" 24 #include "include/core/SkMatrix.h" 25 #include "include/core/SkPaint.h" 26 #include "include/core/SkPicture.h" 27 #include "include/core/SkPoint3.h" 28 #include "include/core/SkRegion.h" 29 #include "include/core/SkRRect.h" 30 #include "include/effects/SkRuntimeEffect.h" 31 #include "include/utils/SkShadowUtils.h" 32 #include "skia_bitmap.h" 33 #include "skia_image.h" 34 #include "skia_matrix.h" 35 #include "skia_paint.h" 36 #include "skia_picture.h" 37 #include "skia_region.h" 38 #include "skia_vertices.h" 39 #include "skia_canvas_op.h" 40 41 #include "common/rs_macros.h" 42 #include "impl_interface/core_canvas_impl.h" 43 44 namespace OHOS { 45 namespace Rosen { 46 namespace Drawing { 47 class DRAWING_API SkiaCanvas : public CoreCanvasImpl { 48 public: 49 static inline constexpr AdapterType TYPE = AdapterType::SKIA_ADAPTER; 50 51 SkiaCanvas(); 52 explicit SkiaCanvas(DrawingType type); 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 RectI GetRoundInDeviceClipBounds() const override; 68 #ifdef RS_ENABLE_GPU 69 std::shared_ptr<GPUContext> GetGPUContext() const override; 70 #endif 71 int32_t GetWidth() const override; 72 int32_t GetHeight() const override; 73 ImageInfo GetImageInfo() override; 74 bool ReadPixels(const ImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes, 75 int srcX, int srcY) override; 76 bool ReadPixels(const Bitmap& dstBitmap, int srcX, int srcY) override; 77 78 // shapes 79 void DrawSdf(const SDFShapeBase& shape) override; 80 void DrawPoint(const Point& point, const Paint& paint) override; 81 void DrawPoints(PointMode mode, size_t count, const Point pts[], const Paint& paint) override; 82 void DrawLine(const Point& startPt, const Point& endPt, const Paint& paint) override; 83 void DrawRect(const Rect& rect, const Paint& paint) override; 84 void DrawRoundRect(const RoundRect& roundRect, const Paint& paint) override; 85 void DrawNestedRoundRect(const RoundRect& outer, const RoundRect& inner, const Paint& paint) override; 86 void DrawArc(const Rect& oval, scalar startAngle, scalar sweepAngle, const Paint& paint) override; 87 void DrawPie(const Rect& oval, scalar startAngle, scalar sweepAngle, const Paint& paint) override; 88 void DrawOval(const Rect& oval, const Paint& paint) override; 89 void DrawCircle(const Point& centerPt, scalar radius, const Paint& paint) override; 90 void DrawPath(const Path& path, const Paint& paint) override; 91 void DrawPathWithStencil(const Path& path, uint32_t stencilVal, const Paint& paint) override; 92 void DrawBackground(const Brush& brush) override; 93 void DrawShadow(const Path& path, const Point3& planeParams, const Point3& devLightPos, scalar lightRadius, 94 Color ambientColor, Color spotColor, ShadowFlags flag) override; 95 void DrawShadowStyle(const Path& path, const Point3& planeParams, const Point3& devLightPos, scalar lightRadius, 96 Color ambientColor, Color spotColor, ShadowFlags flag, bool isLimitElevation) override; 97 void DrawRegion(const Region& region, const Paint& paint) override; 98 void DrawPatch(const Point cubics[12], const ColorQuad colors[4], 99 const Point texCoords[4], BlendMode mode, const Paint& paint) override; 100 void DrawVertices(const Vertices& vertices, BlendMode mode, const Paint& paint) override; 101 102 void DrawImageNine(const Image* image, const RectI& center, const Rect& dst, 103 FilterMode filter, const Brush* brush = nullptr) override; 104 void DrawImageLattice(const Image* image, const Lattice& lattice, const Rect& dst, 105 FilterMode filter, const Paint& paint) override; 106 107 // color 108 void DrawColor(ColorQuad color, BlendMode mode) override; 109 110 bool OpCalculateBefore(const Matrix& matrix) override; 111 std::shared_ptr<Drawing::OpListHandle> OpCalculateAfter(const Rect& bound) override; 112 113 // image 114 void DrawAtlas(const Image* atlas, const RSXform xform[], const Rect tex[], const ColorQuad colors[], int count, 115 BlendMode mode, const SamplingOptions& sampling, const Rect* cullRect, const Paint& paint) override; 116 void DrawBitmap(const Bitmap& bitmap, const scalar px, const scalar py, const Paint& paint) override; 117 void DrawImage(const Image& image, const scalar px, const scalar py, const SamplingOptions& sampling, 118 const Paint& paint) override; 119 void DrawImageWithStencil(const Image& image, const scalar px, const scalar py, 120 const SamplingOptions& sampling, uint32_t stencilVal, const Paint& paint) override; 121 void DrawImageRect(const Image& image, const Rect& src, const Rect& dst, const SamplingOptions& sampling, 122 SrcRectConstraint constraint, const Paint& paint) override; 123 void DrawImageRect(const Image& image, const Rect& dst, const SamplingOptions& sampling, 124 const Paint& paint) override; 125 void DrawPicture(const Picture& picture) override; 126 127 void DrawSVGDOM(const sk_sp<SkSVGDOM>& svgDom) override; 128 129 // text 130 void DrawTextBlob(const TextBlob* blob, const scalar x, const scalar y, const Paint& paint) override; 131 132 // symbol 133 void DrawSymbol(const DrawingHMSymbolData& symbol, Point locate, const Paint& paint) override; 134 135 // stencil culling 136 void ClearStencil(const RectI& rect, uint32_t stencilVal) override; 137 138 // clip 139 void ClipRect(const Rect& rect, ClipOp op, bool doAntiAlias) override; 140 void ClipIRect(const RectI& rect, ClipOp op = ClipOp::INTERSECT) override; 141 void ClipRoundRect(const RoundRect& roundRect, ClipOp op, bool doAntiAlias) override; 142 void ClipRoundRect(const Rect& rect, std::vector<Point>& pts, bool doAntiAlias) override; 143 void ClipPath(const Path& path, ClipOp op, bool doAntiAlias) override; 144 void ClipRegion(const Region& region, ClipOp op = ClipOp::INTERSECT) override; 145 bool IsClipEmpty() override; 146 bool IsClipRect() override; 147 void ResetClip() override; 148 bool QuickReject(const Path& path) override; 149 bool QuickReject(const Rect& rect) override; 150 151 // transform 152 void SetMatrix(const Matrix& matrix) override; 153 void ResetMatrix() override; 154 void ConcatMatrix(const Matrix& matrix) override; 155 void Translate(scalar dx, scalar dy) override; 156 void Scale(scalar sx, scalar sy) override; 157 void Rotate(scalar deg, scalar sx, scalar sy) override; 158 void Shear(scalar sx, scalar sy) override; 159 160 // state 161 void Flush() override; 162 void Clear(ColorQuad color) override; 163 uint32_t Save() override; 164 void SaveLayer(const SaveLayerOps& saveLayerOps) override; 165 void Restore() override; 166 uint32_t GetSaveCount() const override; 167 void Discard() override; 168 169 SkCanvas* ExportSkCanvas() const; 170 void ImportSkCanvas(SkCanvas* skCanvas); 171 172 void BuildOverDraw(std::shared_ptr<Canvas> canvas) override; 173 174 void BuildNoDraw(int32_t width, int32_t height) override; 175 176 void Reset(int32_t width, int32_t height) override; 177 178 void SetGrContextToSkiaImage(SkiaImage* skiaImage); 179 180 bool DrawBlurImage(const Image& image, const Drawing::HpsBlurParameter& blurParams) override; 181 182 std::array<int, 2> CalcHpsBluredImageDimension(const Drawing::HpsBlurParameter& blurParams) override; 183 184 private: 185 void RoundRectCastToSkRRect(const RoundRect& roundRect, SkRRect& skRRect) const; 186 bool ConvertToHMSymbolData(const DrawingHMSymbolData& symbol, HMSymbolData& skSymbol); 187 bool AddSdfPara(SkRuntimeShaderBuilder& builder, const SDFShapeBase& shape); 188 std::shared_ptr<SkCanvas> skiaCanvas_; 189 SkCanvas* skCanvas_; 190 SkCanvas* skCanvasBackup_ = nullptr; 191 std::shared_ptr<SkiaCanvasOp> skiaCanvasOp_ = nullptr; 192 SkPaint defaultPaint_; 193 SkPaint skPaint_; 194 }; 195 } // namespace Drawing 196 } // namespace Rosen 197 } // namespace OHOS 198 #endif 199