1 /* 2 * Copyright (c) 2021-2023 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 RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RECORDING_CANVAS_H 17 #define RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RECORDING_CANVAS_H 18 19 #ifndef USE_ROSEN_DRAWING 20 21 #include "common/rs_macros.h" 22 #include "include/core/SkBitmap.h" 23 #include "include/core/SkCanvas.h" 24 #include "include/core/SkCanvasVirtualEnforcer.h" 25 #include "include/core/SkDrawable.h" 26 #include "include/core/SkPaint.h" 27 #include "include/core/SkPath.h" 28 #include "include/core/SkRect.h" 29 #include "include/utils/SkNoDrawCanvas.h" 30 #include "pipeline/rs_draw_cmd_list.h" 31 #include "property/rs_properties_def.h" 32 #include "render/rs_image.h" 33 #ifdef NEW_SKIA 34 #include "src/core/SkVerticesPriv.h" 35 #endif 36 #ifdef ROSEN_OHOS 37 #include "surface_buffer.h" 38 #endif 39 40 namespace OHOS { 41 namespace Media { 42 class PixelMap; 43 } 44 namespace Rosen { 45 class DrawCmdList; 46 class OpItem; 47 #ifdef ROSEN_OHOS 48 struct RSSurfaceBufferInfo { 49 RSSurfaceBufferInfo() = default; RSSurfaceBufferInfoRSSurfaceBufferInfo50 RSSurfaceBufferInfo( 51 const sptr<SurfaceBuffer>& surfaceBuffer, int offSetX, int offSetY, int width, int height) 52 : surfaceBuffer_(surfaceBuffer), offSetX_(offSetX), offSetY_(offSetY), width_(width), height_(height) 53 {} 54 sptr<SurfaceBuffer> surfaceBuffer_ = nullptr; 55 int offSetX_ = 0; 56 int offSetY_ = 0; 57 int width_ = 0; 58 int height_ = 0; 59 }; 60 #endif 61 class RSB_EXPORT RSRecordingCanvas : public SkCanvasVirtualEnforcer<SkNoDrawCanvas> { 62 public: 63 RSRecordingCanvas(int width, int height); 64 virtual ~RSRecordingCanvas(); 65 std::shared_ptr<DrawCmdList> GetDrawCmdList() const; 66 void Clear() const; 67 void AddOp(std::unique_ptr<OpItem>&& opItem); 68 #ifdef NEW_SKIA 69 GrRecordingContext* recordingContext() override; 70 void SetGrRecordingContext(GrRecordingContext* context); 71 void didConcat44(const SkM44&) override; 72 void didSetM44(const SkM44&) override; 73 void didScale(SkScalar, SkScalar) override; 74 void onDrawGlyphRunList(const SkGlyphRunList& glyphRunList, const SkPaint& paint) override; 75 void onDrawImage2(const SkImage* img, SkScalar dx, SkScalar dy, const SkSamplingOptions& samplingOptions, 76 const SkPaint* paint) override; 77 void onDrawImageRect2(const SkImage* img, const SkRect& src, const SkRect& dst, 78 const SkSamplingOptions& samplingOptions, const SkPaint* paint, SrcRectConstraint constraint) override; 79 void onDrawImageLattice2(const SkImage* img, const Lattice& lattice, const SkRect& dst, 80 SkFilterMode mode, const SkPaint* paint) override; 81 void onDrawAtlas2(const SkImage*, const SkRSXform[], const SkRect src[], 82 const SkColor[], int count, SkBlendMode mode, const SkSamplingOptions& samplingOptions, 83 const SkRect* cull, const SkPaint* paint) override; 84 void onDrawEdgeAAImageSet2(const ImageSetEntry imageSet[], int count, 85 const SkPoint dstClips[], const SkMatrix preViewMatrices[], 86 const SkSamplingOptions& samplingOptions, const SkPaint* paint, 87 SrcRectConstraint constraint) override; 88 void onDrawVerticesObject(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) override; 89 void onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], QuadAAFlags aaFlags, 90 const SkColor4f& color, SkBlendMode mode) override; 91 void DrawPixelMapRect(const std::shared_ptr<Media::PixelMap>& pixelmap, const SkRect& src, const SkRect& dst, 92 const SkSamplingOptions& samplingOptions, const SkPaint* paint, 93 SrcRectConstraint constraint = kStrict_SrcRectConstraint); 94 void DrawPixelMapRect(const std::shared_ptr<Media::PixelMap>& pixelmap, const SkRect& dst, 95 const SkSamplingOptions& samplingOptions, const SkPaint* paint); 96 void DrawPixelMap(const std::shared_ptr<Media::PixelMap>& pixelmap, SkScalar x, SkScalar y, 97 const SkSamplingOptions& samplingOptions, const SkPaint* paint = nullptr); 98 void DrawImageWithParm(const sk_sp<SkImage> image, const sk_sp<SkData> data, 99 const Rosen::RsImageInfo& rsImageInfo, const SkSamplingOptions& samplingOptions, const SkPaint& paint); 100 void DrawPixelMapWithParm( 101 const std::shared_ptr<Media::PixelMap>& pixelmap, 102 const Rosen::RsImageInfo& rsImageInfo, const SkSamplingOptions& samplingOptions, const SkPaint& paint); 103 #else 104 GrContext* getGrContext() override; 105 void SetGrContext(GrContext* grContext); 106 void didConcat(const SkMatrix& matrix) override; 107 void didSetMatrix(const SkMatrix& matrix) override; 108 void onDrawBitmap(const SkBitmap& bm, SkScalar x, SkScalar y, const SkPaint* paint) override; 109 void onDrawBitmapLattice(const SkBitmap& bm, const SkCanvas::Lattice& lattice, const SkRect& dst, 110 const SkPaint* paint) override; 111 void onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center, const SkRect& dst, const SkPaint* paint) override; 112 void onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& dst, 113 const SkPaint* paint, SrcRectConstraint constraint) override; 114 void onDrawImage(const SkImage* img, SkScalar x, SkScalar y, const SkPaint* paint)override; 115 void onDrawImageLattice(const SkImage* img, const SkCanvas::Lattice& lattice, 116 const SkRect& dst, const SkPaint* paint) override; 117 void onDrawImageNine(const SkImage* img, const SkIRect& center, const SkRect& dst, const SkPaint* paint) override; 118 void onDrawImageRect(const SkImage* img, const SkRect* src, const SkRect& dst, const SkPaint* paint, 119 SrcRectConstraint constraint) override; 120 void onDrawVerticesObject( 121 const SkVertices*, const SkVertices::Bone bones[], int boneCount, SkBlendMode, const SkPaint&) override; 122 void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int, SkBlendMode, 123 const SkRect*, const SkPaint*) override; onDrawEdgeAAQuad(const SkRect &,const SkPoint[4],QuadAAFlags,SkColor,SkBlendMode)124 void onDrawEdgeAAQuad(const SkRect&, const SkPoint[4], QuadAAFlags, SkColor, 125 SkBlendMode) override {} onDrawEdgeAAImageSet(const ImageSetEntry[],int,const SkPoint[],const SkMatrix[],const SkPaint *,SrcRectConstraint)126 void onDrawEdgeAAImageSet(const ImageSetEntry[], int, const SkPoint[], 127 const SkMatrix[], const SkPaint*, SrcRectConstraint) override {} 128 void DrawPixelMapRect(const std::shared_ptr<Media::PixelMap>& pixelmap, const SkRect& src, const SkRect& dst, 129 const SkPaint* paint, SrcRectConstraint constraint = kStrict_SrcRectConstraint); 130 void DrawPixelMapRect(const std::shared_ptr<Media::PixelMap>& pixelmap, const SkRect& dst, const SkPaint* paint); 131 void DrawPixelMap(const std::shared_ptr<Media::PixelMap>& pixelmap, SkScalar x, SkScalar y, 132 const SkPaint* paint = nullptr); 133 void DrawImageWithParm(const sk_sp<SkImage> image, const sk_sp<SkData> data, 134 const Rosen::RsImageInfo& rsImageInfo, const SkPaint& paint); 135 void DrawPixelMapWithParm( 136 const std::shared_ptr<Media::PixelMap>& pixelmap, const Rosen::RsImageInfo& rsImageInfo, const SkPaint& paint); 137 #endif 138 sk_sp<SkSurface> onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) override; 139 140 void willSave() override; 141 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& rec) override; 142 void willRestore() override; 143 144 void onFlush() override; 145 146 void didTranslate(SkScalar dx, SkScalar dy) override; 147 148 void onClipRect(const SkRect& rect, SkClipOp clipOp, ClipEdgeStyle style) override; 149 void onClipRRect(const SkRRect& rect, SkClipOp clipOp, ClipEdgeStyle style) override; 150 void onClipPath(const SkPath& path, SkClipOp clipOp, ClipEdgeStyle style) override; 151 void onClipRegion(const SkRegion& region, SkClipOp clipop) override; 152 153 void onDrawPaint(const SkPaint& paint) override; 154 void onDrawBehind(const SkPaint&) override; 155 void onDrawPath(const SkPath& path, const SkPaint& paint) override; 156 void onDrawRect(const SkRect& rect, const SkPaint& paint) override; 157 void onDrawRegion(const SkRegion& region, const SkPaint& paint) override; 158 void onDrawOval(const SkRect& oval, const SkPaint& paint) override; 159 void onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, 160 bool useCenter, const SkPaint& paint) override; 161 void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) override; 162 void onDrawDRRect(const SkRRect& out, const SkRRect& in, const SkPaint& paint) override; 163 164 void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override; 165 void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) override; 166 void onDrawAnnotation(const SkRect&, const char[], SkData*) override; 167 168 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) override; 169 170 void onDrawPatch(const SkPoint[12], const SkColor[4], const SkPoint[4], SkBlendMode, const SkPaint&) override; 171 void onDrawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint) override; 172 void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override; 173 174 void ClipOutsetRect(float dx, float dy); 175 void DrawAdaptiveRRect(float radius, const SkPaint& paint); 176 void DrawAdaptiveRRectScale(float radiusRatio, const SkPaint& paint); 177 void ClipAdaptiveRRect(const SkVector radius[]); 178 #ifdef ROSEN_OHOS 179 void DrawSurfaceBuffer(const RSSurfaceBufferInfo& surfaceBufferInfo); 180 #endif 181 void MultiplyAlpha(float alpha); 182 void SaveAlpha(); 183 void RestoreAlpha(); 184 185 void SetIsCustomTextType(bool isCustomTextType); 186 bool IsCustomTextType() const; 187 private: 188 void DrawImageLatticeAsBitmap( 189 const SkImage* image, const SkCanvas::Lattice& lattice, const SkRect& dst, const SkPaint* paint); 190 191 std::shared_ptr<DrawCmdList> drawCmdList_ { nullptr }; 192 int saveCount_ = 0; 193 bool isCustomTextType_ = false; 194 #ifdef NEW_SKIA 195 GrRecordingContext* grContext_ = nullptr; 196 #else 197 GrContext* grContext_ = nullptr; 198 #endif 199 }; 200 } // namespace Rosen 201 } // namespace OHOS 202 203 #endif // USE_ROSEN_DRAWING 204 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RECORDING_CANVAS_H 205