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/SkImage.h" 22 #include "include/core/SkMatrix.h" 23 #include "include/core/SkPaint.h" 24 #include "include/core/SkPicture.h" 25 #include "include/core/SkPoint3.h" 26 #include "include/core/SkRegion.h" 27 #include "include/core/SkRRect.h" 28 #include "include/utils/SkShadowUtils.h" 29 #include "skia_bitmap.h" 30 #include "skia_image.h" 31 #include "skia_matrix.h" 32 #include "skia_paint.h" 33 #include "skia_picture.h" 34 #include "skia_region.h" 35 36 #include "impl_interface/core_canvas_impl.h" 37 38 namespace OHOS { 39 namespace Rosen { 40 namespace Drawing { 41 class SkiaCanvas : public CoreCanvasImpl { 42 public: 43 static inline constexpr AdapterType TYPE = AdapterType::SKIA_ADAPTER; 44 SkiaCanvas(); 45 explicit SkiaCanvas(const std::shared_ptr<SkCanvas>& skCanvas); 46 SkiaCanvas(int32_t width, int32_t height); ~SkiaCanvas()47 ~SkiaCanvas() override {}; GetType()48 AdapterType GetType() const override 49 { 50 return AdapterType::SKIA_ADAPTER; 51 } 52 53 void Bind(const Bitmap& bitmap) override; 54 55 Matrix GetTotalMatrix() const override; 56 Rect GetLocalClipBounds() const override; 57 RectI GetDeviceClipBounds() const override; 58 #ifdef ACE_ENABLE_GPU 59 std::shared_ptr<GPUContext> GetGPUContext() const override; 60 #endif 61 int32_t GetWidth() const override; 62 int32_t GetHeight() const override; 63 64 // shapes 65 void DrawPoint(const Point& point) override; 66 void DrawLine(const Point& startPt, const Point& endPt) override; 67 void DrawRect(const Rect& rect) override; 68 void DrawRoundRect(const RoundRect& roundRect) override; 69 void DrawNestedRoundRect(const RoundRect& outer, const RoundRect& inner) override; 70 void DrawArc(const Rect& oval, scalar startAngle, scalar sweepAngle) override; 71 void DrawPie(const Rect& oval, scalar startAngle, scalar sweepAngle) override; 72 void DrawOval(const Rect& oval) override; 73 void DrawCircle(const Point& centerPt, scalar radius) override; 74 void DrawPath(const Path& path) override; 75 void DrawBackground(const Brush& brush) override; 76 void DrawShadow(const Path& path, const Point3& planeParams, const Point3& devLightPos, scalar lightRadius, 77 Color ambientColor, Color spotColor, ShadowFlags flag) override; 78 void DrawRegion(const Region& region) override; 79 80 // image 81 void DrawBitmap(const Bitmap& bitmap, const scalar px, const scalar py) override; 82 void DrawBitmap(Media::PixelMap& pixelMap, const scalar px, const scalar py) override; 83 void DrawImage(const Image& image, const scalar px, const scalar py, const SamplingOptions& sampling) override; 84 void DrawImageRect(const Image& image, const Rect& src, const Rect& dst, const SamplingOptions& sampling, 85 SrcRectConstraint constraint) override; 86 void DrawImageRect(const Image& image, const Rect& dst, const SamplingOptions& sampling) override; 87 void DrawPicture(const Picture& picture) override; 88 89 void DrawSVGDOM(const sk_sp<SkSVGDOM>& svgDom) override; 90 91 // clip 92 void ClipRect(const Rect& rect, ClipOp op, bool doAntiAlias) override; 93 void ClipRoundRect(const RoundRect& roundRect, ClipOp op, bool doAntiAlias) override; 94 void ClipPath(const Path& path, ClipOp op, bool doAntiAlias) override; 95 96 // transform 97 void SetMatrix(const Matrix& matrix) override; 98 void ResetMatrix() override; 99 void ConcatMatrix(const Matrix& matrix) override; 100 void Translate(scalar dx, scalar dy) override; 101 void Scale(scalar sx, scalar sy) override; 102 void Rotate(scalar deg, scalar sx, scalar sy) override; 103 void Shear(scalar sx, scalar sy) override; 104 105 // state 106 void Flush() override; 107 void Clear(ColorQuad color) override; 108 void Save() override; 109 void SaveLayer(const SaveLayerOps& saveLayerOps) override; 110 void Restore() override; 111 uint32_t GetSaveCount() const override; 112 113 // paint 114 void AttachPen(const Pen& pen) override; 115 void AttachBrush(const Brush& brush) override; 116 void DetachPen() override; 117 void DetachBrush() override; 118 119 SkCanvas* ExportSkCanvas() const; 120 void ImportSkCanvas(SkCanvas* skCanvas); 121 122 private: 123 void RoundRectCastToSkRRect(const RoundRect& roundRect, SkRRect& skRRect) const; 124 std::shared_ptr<SkCanvas> skiaCanvas_; 125 SkCanvas* skCanvas_; 126 SkiaPaint skiaPaint_; 127 }; 128 } // namespace Drawing 129 } // namespace Rosen 130 } // namespace OHOS 131 #endif 132