• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkBitmap.h"
22 #include "include/core/SkCanvas.h"
23 #include "include/core/SkCanvasVirtualEnforcer.h"
24 #include "include/core/SkDrawable.h"
25 #include "include/core/SkPaint.h"
26 #include "include/core/SkPath.h"
27 #include "include/core/SkRect.h"
28 #include "include/utils/SkNoDrawCanvas.h"
29 
30 #include "common/rs_macros.h"
31 #include "pipeline/rs_draw_cmd_list.h"
32 #include "property/rs_properties_def.h"
33 #include "render/rs_image.h"
34 
35 #ifdef NEW_SKIA
36 #include "src/core/SkVerticesPriv.h"
37 #endif
38 #ifdef ROSEN_OHOS
39 #include "surface_buffer.h"
40 #endif
41 
42 #include "include/core/HMSymbol.h"
43 
44 namespace OHOS {
45 namespace Media {
46 class PixelMap;
47 }
48 namespace Rosen {
49 class DrawCmdList;
50 class OpItem;
51 #ifdef ROSEN_OHOS
52 struct RSSurfaceBufferInfo {
53     RSSurfaceBufferInfo() = default;
RSSurfaceBufferInfoRSSurfaceBufferInfo54     RSSurfaceBufferInfo(
55         const sptr<SurfaceBuffer>& surfaceBuffer, int offSetX, int offSetY, int width, int height)
56         : surfaceBuffer_(surfaceBuffer), offSetX_(offSetX), offSetY_(offSetY), width_(width), height_(height)
57     {}
58     sptr<SurfaceBuffer> surfaceBuffer_ = nullptr;
59     int offSetX_ = 0;
60     int offSetY_ = 0;
61     int width_ = 0;
62     int height_ = 0;
63 };
64 #endif
65 class RSB_EXPORT RSRecordingCanvas : public SkCanvasVirtualEnforcer<SkNoDrawCanvas> {
66 public:
67     RSRecordingCanvas(int width, int height);
68     virtual ~RSRecordingCanvas();
69     std::shared_ptr<DrawCmdList> GetDrawCmdList() const;
70     void Clear() const;
71     void AddOp(std::unique_ptr<OpItem>&& opItem);
72 #ifdef NEW_SKIA
73     GrRecordingContext* recordingContext() override;
74     void SetGrRecordingContext(GrRecordingContext* context);
75     void didConcat44(const SkM44&) override;
76     void didSetM44(const SkM44&) override;
77     void didScale(SkScalar, SkScalar) override;
78     void onDrawGlyphRunList(const SkGlyphRunList& glyphRunList, const SkPaint& paint) override;
79     void onDrawImage2(const SkImage* img, SkScalar dx, SkScalar dy, const SkSamplingOptions& samplingOptions,
80         const SkPaint* paint) override;
81     void onDrawImageRect2(const SkImage* img, const SkRect& src, const SkRect& dst,
82         const SkSamplingOptions& samplingOptions, const SkPaint* paint, SrcRectConstraint constraint) override;
83     void onDrawImageLattice2(const SkImage* img, const Lattice& lattice, const SkRect& dst,
84                                         SkFilterMode mode, const SkPaint* paint) override;
85     void onDrawAtlas2(const SkImage*, const SkRSXform[], const SkRect src[],
86         const SkColor[], int count, SkBlendMode mode, const SkSamplingOptions& samplingOptions,
87         const SkRect* cull, const SkPaint* paint) override;
88     void onDrawEdgeAAImageSet2(const ImageSetEntry imageSet[], int count,
89         const SkPoint dstClips[], const SkMatrix preViewMatrices[],
90         const SkSamplingOptions& samplingOptions, const SkPaint* paint,
91         SrcRectConstraint constraint) override;
92     void onDrawVerticesObject(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) override;
93     void onDrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], QuadAAFlags aaFlags,
94         const SkColor4f& color, SkBlendMode mode) override;
95     void DrawPixelMapRect(const std::shared_ptr<Media::PixelMap>& pixelmap, const SkRect& src, const SkRect& dst,
96         const SkSamplingOptions& samplingOptions, const SkPaint* paint,
97         SrcRectConstraint constraint = kStrict_SrcRectConstraint);
98     void DrawPixelMapRect(const std::shared_ptr<Media::PixelMap>& pixelmap, const SkRect& dst,
99         const SkSamplingOptions& samplingOptions, const SkPaint* paint);
100     void DrawRsImage(const std::shared_ptr<RSImageBase>& rsImage, const SkSamplingOptions& samplingOptions,
101         const SkPaint& paint, SrcRectConstraint constraint = kStrict_SrcRectConstraint);
102     void DrawPixelMap(const std::shared_ptr<Media::PixelMap>& pixelmap, SkScalar x, SkScalar y,
103         const SkSamplingOptions& samplingOptions, const SkPaint* paint = nullptr);
104     void DrawImageWithParm(const sk_sp<SkImage> image, const sk_sp<SkData> data,
105         const Rosen::RsImageInfo& rsImageInfo, const SkSamplingOptions& samplingOptions, const SkPaint& paint);
106     void DrawPixelMapWithParm(
107         const std::shared_ptr<Media::PixelMap>& pixelmap,
108         const Rosen::RsImageInfo& rsImageInfo, const SkSamplingOptions& samplingOptions, const SkPaint& paint);
109     void DrawImageNine(const std::shared_ptr<Media::PixelMap>& pixelmap, const SkIRect& center,
110         const SkRect& dst, SkFilterMode filter, const SkPaint* paint);
111     using DrawFunc  = std::function<void(RSPaintFilterCanvas& canvas, const SkRect*)>;
112     void DrawDrawFunc(DrawFunc && drawFunc);
113 #else
114     GrContext* getGrContext() override;
115     void SetGrContext(GrContext* grContext);
116     void didConcat(const SkMatrix& matrix) override;
117     void didSetMatrix(const SkMatrix& matrix) override;
118     void onDrawBitmap(const SkBitmap& bm, SkScalar x, SkScalar y, const SkPaint* paint) override;
119     void onDrawBitmapLattice(const SkBitmap& bm, const SkCanvas::Lattice& lattice, const SkRect& dst,
120         const SkPaint* paint) override;
121     void onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center, const SkRect& dst, const SkPaint* paint) override;
122     void onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& dst,
123         const SkPaint* paint, SrcRectConstraint constraint) override;
124     void onDrawImage(const SkImage* img, SkScalar x, SkScalar y, const SkPaint* paint)override;
125     void onDrawImageLattice(const SkImage* img, const SkCanvas::Lattice& lattice,
126         const SkRect& dst, const SkPaint* paint) override;
127     void onDrawImageNine(const SkImage* img, const SkIRect& center, const SkRect& dst, const SkPaint* paint) override;
128     void onDrawImageRect(const SkImage* img, const SkRect* src, const SkRect& dst, const SkPaint* paint,
129         SrcRectConstraint constraint) override;
130     void onDrawVerticesObject(
131         const SkVertices*, const SkVertices::Bone bones[], int boneCount, SkBlendMode, const SkPaint&) override;
132     void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int, SkBlendMode,
133         const SkRect*, const SkPaint*) override;
onDrawEdgeAAQuad(const SkRect &,const SkPoint[4],QuadAAFlags,SkColor,SkBlendMode)134     void onDrawEdgeAAQuad(const SkRect&, const SkPoint[4], QuadAAFlags, SkColor,
135                           SkBlendMode) override {}
onDrawEdgeAAImageSet(const ImageSetEntry[],int,const SkPoint[],const SkMatrix[],const SkPaint *,SrcRectConstraint)136     void onDrawEdgeAAImageSet(const ImageSetEntry[], int, const SkPoint[],
137                               const SkMatrix[], const SkPaint*, SrcRectConstraint) override {}
138     void DrawPixelMapRect(const std::shared_ptr<Media::PixelMap>& pixelmap, const SkRect& src, const SkRect& dst,
139         const SkPaint* paint, SrcRectConstraint constraint = kStrict_SrcRectConstraint);
140     void DrawPixelMapRect(const std::shared_ptr<Media::PixelMap>& pixelmap, const SkRect& dst, const SkPaint* paint);
141     void DrawPixelMap(const std::shared_ptr<Media::PixelMap>& pixelmap, SkScalar x, SkScalar y,
142         const SkPaint* paint = nullptr);
143     void DrawImageWithParm(const sk_sp<SkImage> image, const sk_sp<SkData> data,
144         const Rosen::RsImageInfo& rsImageInfo, const SkPaint& paint);
145     void DrawPixelMapWithParm(
146         const std::shared_ptr<Media::PixelMap>& pixelmap, const Rosen::RsImageInfo& rsImageInfo, const SkPaint& paint);
147 #endif
148     sk_sp<SkSurface> onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) override;
149 
150     void willSave() override;
151     SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& rec) override;
152     void willRestore() override;
153 
154     void onFlush() override;
155 
156     void didTranslate(SkScalar dx, SkScalar dy) override;
157 
158     void onClipRect(const SkRect& rect, SkClipOp clipOp, ClipEdgeStyle style) override;
159     void onClipRRect(const SkRRect& rect, SkClipOp clipOp, ClipEdgeStyle style) override;
160     void onClipPath(const SkPath& path, SkClipOp clipOp, ClipEdgeStyle style) override;
161     void onClipRegion(const SkRegion& region, SkClipOp clipop) override;
162 
163     void onDrawPaint(const SkPaint& paint) override;
164     void onDrawBehind(const SkPaint&) override;
165     void onDrawPath(const SkPath& path, const SkPaint& paint) override;
166     void onDrawRect(const SkRect& rect, const SkPaint& paint) override;
167     void onDrawRegion(const SkRegion& region, const SkPaint& paint) override;
168     void onDrawOval(const SkRect& oval, const SkPaint& paint) override;
169     void onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
170         bool useCenter, const SkPaint& paint) override;
171     void onDrawRRect(const SkRRect& rrect, const SkPaint& paint) override;
172     void onDrawDRRect(const SkRRect& out, const SkRRect& in, const SkPaint& paint) override;
173 
174     void onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) override;
175     void onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) override;
176     void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
177 
178     void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) override;
179     void onDrawSymbol(const HMSymbolData& symbol, SkPoint locate, const SkPaint& paint) override;
180 
181     void onDrawPatch(const SkPoint[12], const SkColor[4], const SkPoint[4], SkBlendMode, const SkPaint&) override;
182     void onDrawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint) override;
183     void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
184 
185     void ClipOutsetRect(float dx, float dy);
186     void DrawAdaptiveRRect(float radius, const SkPaint& paint);
187     void DrawAdaptiveRRectScale(float radiusRatio, const SkPaint& paint);
188     void ClipAdaptiveRRect(const SkVector radius[]);
189 #ifdef ROSEN_OHOS
190     void DrawSurfaceBuffer(const RSSurfaceBufferInfo& surfaceBufferInfo);
191 #endif
192     void MultiplyAlpha(float alpha);
193     void SaveAlpha();
194     void RestoreAlpha();
195 
196     void SetIsCustomTextType(bool isCustomTextType);
197     bool IsCustomTextType() const;
198 private:
199     void DrawImageLatticeAsBitmap(
200         const SkImage* image, const SkCanvas::Lattice& lattice, const SkRect& dst, const SkPaint* paint);
201 
202     std::shared_ptr<DrawCmdList> drawCmdList_ { nullptr };
203     int saveCount_ = 0;
204     bool isCustomTextType_ = false;
205 #ifdef NEW_SKIA
206     GrRecordingContext* grContext_ = nullptr;
207 #else
208     GrContext* grContext_ = nullptr;
209 #endif
210 };
211 } // namespace Rosen
212 } // namespace OHOS
213 
214 #else
215 #include "recording/recording_canvas.h"
216 #include "pipeline/rs_draw_cmd.h"
217 
218 namespace OHOS {
219 namespace Media {
220 class PixelMap;
221 }
222 namespace Rosen {
223 class RSB_EXPORT ExtendRecordingCanvas : public Drawing::RecordingCanvas {
224 public:
225     ExtendRecordingCanvas(int width, int weight, bool addDrawOpImmediate = true);
226     ~ExtendRecordingCanvas() override = default;
227     void DrawImageWithParm(const std::shared_ptr<Drawing::Image>& image, const std::shared_ptr<Drawing::Data>& data,
228         const Drawing::AdaptiveImageInfo& rsImageInfo, const Drawing::SamplingOptions& sampling);
229     void DrawPixelMapWithParm(const std::shared_ptr<Media::PixelMap>& pixelMap,
230         const Drawing::AdaptiveImageInfo& rsImageInfo, const Drawing::SamplingOptions& sampling);
231     void DrawImageNineWithPixelMap(const std::shared_ptr<Media::PixelMap>& pixelmap, const Drawing::RectI& center,
232         const Drawing::Rect& dst, Drawing::FilterMode filter, const Drawing::Brush* brush);
233     void DrawPixelMapRect(const std::shared_ptr<Media::PixelMap>& pixelMap, const Drawing::Rect& src,
234         const Drawing::Rect& dst, const Drawing::SamplingOptions& sampling,
235         Drawing::SrcRectConstraint constraint = Drawing::SrcRectConstraint::STRICT_SRC_RECT_CONSTRAINT);
236     void DrawDrawFunc(Drawing::RecordingCanvas::DrawFunc&& drawFunc);
237 #ifdef ROSEN_OHOS
238     void DrawSurfaceBuffer(const DrawingSurfaceBufferInfo& surfaceBufferInfo);
239 #endif
240 private:
241     template<typename T, typename... Args>
242     void AddDrawOpImmediate(Args&&... args);
243     template<typename T, typename... Args>
244     void AddDrawOpDeferred(Args&&... args);
245 };
246 } // namespace Rosen
247 } // namespace OHOS
248 #endif // USE_ROSEN_DRAWING
249 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_RECORDING_CANVAS_H
250