1 /*
2  * Copyright (c) 2022-2024 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 SKIACANVAS_AUTOCACHE_H
17 #define SKIACANVAS_AUTOCACHE_H
18 
19 #include <vector>
20 #include <map>
21 #include <string>
22 #include "skia_canvas_op.h"
23 #include "include/utils/SkNWayCanvas.h"
24 #include "include/core/SkCanvasVirtualEnforcer.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace Drawing {
29 class SkiaCanvasAutoCache : public SkiaCanvasOp {
30 public:
31     explicit SkiaCanvasAutoCache(SkCanvas* canvas);
32 
33     // drawing func
34     bool OpCanCache(const SkRect& bound) override;
35     std::vector<SkRect>& GetOpListDrawArea() override;
36     SkRect& GetOpUnionRect() override;
37 
GetOpsNum()38     int GetOpsNum() override
39     {
40         return totalOpNums_;
41     }
42 
GetOpsPercent()43     int GetOpsPercent() override
44     {
45         return percent_;
46     }
47 
48     void Init(const SkMatrix& m);
49 
50     // dfx
51     void ShowDrawResult(const SkRect &bound);
52 
53     // skia func
getBaseLayerSize()54     SkISize getBaseLayerSize() const override
55     {
56         return proxy()->getBaseLayerSize();
57     }
58 
recordingContext()59     GrRecordingContext* recordingContext() override
60     {
61         return proxy()->recordingContext();
62     }
63 
64 protected:
65     sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
66     SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
67     bool onDoSaveBehind(const SkRect*) override;
68     void onDrawPaint(const SkPaint&) override;
69     void onDrawBehind(const SkPaint&) override;
70     void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
71     void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
72     void onDrawRRect(const SkRRect&, const SkPaint&) override;
73     void onDrawRect(const SkRect&, const SkPaint&) override;
74     void onDrawOval(const SkRect&, const SkPaint&) override;
75     void onDrawRegion(const SkRegion&, const SkPaint&) override;
76     void onDrawPath(const SkPath&, const SkPaint&) override;
77     void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
78     bool onPeekPixels(SkPixmap* pixmap) override;
79 
80     void onDrawImage2(const SkImage*, SkScalar, SkScalar, const SkSamplingOptions&, const SkPaint*) override;
81     void onDrawImageRect2(const SkImage*, const SkRect&, const SkRect&, const SkSamplingOptions&, const SkPaint*,
82         SrcRectConstraint) override;
83     void onDrawImageLattice2(const SkImage*, const Lattice&, const SkRect&, SkFilterMode, const SkPaint*) override;
84     void onDrawAtlas2(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int, SkBlendMode,
85         const SkSamplingOptions&, const SkRect*, const SkPaint*) override;
86     bool onAccessTopLayerPixels(SkPixmap* pixmap) override;
87 
88     void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
89     void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4],
90         SkBlendMode, const SkPaint& paint) override;
91     void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
92     void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
93     SkImageInfo onImageInfo() const override;
94 
95     void onDrawGlyphRunList(const SkGlyphRunList&, const SkPaint&) override;
96     void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) override;
97     void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) override;
98     void onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) override;
99 
100     void onDrawEdgeAAQuad(const SkRect&, const SkPoint[4], QuadAAFlags, const SkColor4f&, SkBlendMode) override;
101     void onDrawEdgeAAImageSet2(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[],
102         const SkSamplingOptions&, const SkPaint*, SrcRectConstraint) override;
103 
104     // draw on actual canvas
105     bool onGetProps(SkSurfaceProps* props) const override;
106 
107 private:
108     bool OpShouldRecord() const;
109     void RecordUnsupportOp(const char* name);
110     void RecordUnsupportOp(const char* name, const SkPaint& paint);
111     bool RecordDrawArea(const SkRect& bounds, const SkPaint& paint, const SkMatrix* matrix = nullptr);
112     void MergeDrawAreaRects();
proxy()113     SkCanvas* proxy() const { return fList[0]; }
114 
115     std::vector<SkRect> drawAreaRects_;
116     SkRect unionDrawArea_ = SkRect::MakeEmpty();
117     SkRect rejectBounds_ = SkRect::MakeEmpty();
118     SkMatrix originMatrixInvert_;
119     SkMatrix nodeMatrix_;
120     int totalOpNums_ = 0;
121     int64_t totalDrawAreas_ = 0;
122     int percent_ = 0;
123     bool opCanCache_ = true;
124     std::map<std::string, int> debugNotSupportOps_;
125     int calNotSupport_ = 0;
126 };
127 } // namespace Drawing
128 } // namespace Rosen
129 } // namespace OHOS
130 #endif
131