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 59 #ifdef USE_M133_SKIA recordingContext()60 GrRecordingContext* recordingContext() const override 61 #else 62 GrRecordingContext* recordingContext() override 63 #endif 64 { 65 return proxy()->recordingContext(); 66 } 67 68 protected: 69 sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override; 70 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override; 71 bool onDoSaveBehind(const SkRect*) override; 72 void onDrawPaint(const SkPaint&) override; 73 void onDrawBehind(const SkPaint&) override; 74 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override; 75 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override; 76 void onDrawRRect(const SkRRect&, const SkPaint&) override; 77 void onDrawRect(const SkRect&, const SkPaint&) override; 78 void onDrawOval(const SkRect&, const SkPaint&) override; 79 void onDrawRegion(const SkRegion&, const SkPaint&) override; 80 void onDrawPath(const SkPath&, const SkPaint&) override; 81 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override; 82 bool onPeekPixels(SkPixmap* pixmap) override; 83 84 void onDrawImage2(const SkImage*, SkScalar, SkScalar, const SkSamplingOptions&, const SkPaint*) override; 85 void onDrawImageRect2(const SkImage*, const SkRect&, const SkRect&, const SkSamplingOptions&, const SkPaint*, 86 SrcRectConstraint) override; 87 void onDrawImageLattice2(const SkImage*, const Lattice&, const SkRect&, SkFilterMode, const SkPaint*) override; 88 void onDrawAtlas2(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int, SkBlendMode, 89 const SkSamplingOptions&, const SkRect*, const SkPaint*) override; 90 bool onAccessTopLayerPixels(SkPixmap* pixmap) override; 91 92 void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override; 93 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], 94 SkBlendMode, const SkPaint& paint) override; 95 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override; 96 void onDrawDrawable(SkDrawable*, const SkMatrix*) override; 97 SkImageInfo onImageInfo() const override; 98 99 #ifdef USE_M133_SKIA 100 void onDrawGlyphRunList(const sktext::GlyphRunList&, const SkPaint&) override; 101 #else 102 void onDrawGlyphRunList(const SkGlyphRunList&, const SkPaint&) override; 103 #endif 104 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) override; 105 void onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) override; 106 void onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) override; 107 108 void onDrawEdgeAAQuad(const SkRect&, const SkPoint[4], QuadAAFlags, const SkColor4f&, SkBlendMode) override; 109 void onDrawEdgeAAImageSet2(const ImageSetEntry[], int count, const SkPoint[], const SkMatrix[], 110 const SkSamplingOptions&, const SkPaint*, SrcRectConstraint) override; 111 112 // draw on actual canvas 113 #ifdef USE_M133_SKIA 114 bool onGetProps(SkSurfaceProps* props, bool top = false) const override; 115 #else 116 bool onGetProps(SkSurfaceProps* props) const override; 117 #endif 118 119 private: 120 bool OpShouldRecord() const; 121 void RecordUnsupportOp(const char* name); 122 void RecordUnsupportOp(const char* name, const SkPaint& paint); 123 bool RecordDrawArea(const SkRect& bounds, const SkPaint& paint, const SkMatrix* matrix = nullptr); 124 void MergeDrawAreaRects(); proxy()125 SkCanvas* proxy() const { return fList[0]; } 126 127 std::vector<SkRect> drawAreaRects_; 128 SkRect unionDrawArea_ = SkRect::MakeEmpty(); 129 SkRect rejectBounds_ = SkRect::MakeEmpty(); 130 SkMatrix originMatrixInvert_; 131 SkMatrix nodeMatrix_; 132 int totalOpNums_ = 0; 133 int64_t totalDrawAreas_ = 0; 134 int percent_ = 0; 135 bool opCanCache_ = true; 136 std::map<std::string, int> debugNotSupportOps_; 137 int calNotSupport_ = 0; 138 }; 139 } // namespace Drawing 140 } // namespace Rosen 141 } // namespace OHOS 142 #endif 143