• 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_DRAW_CMD_H
17 #define RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_DRAW_CMD_H
18 
19 #if (defined(ROSEN_OHOS) && defined(RS_ENABLE_GL))
20 #include <GLES/gl.h>
21 #include "EGL/egl.h"
22 #include "EGL/eglext.h"
23 #include "GLES2/gl2.h"
24 #include "GLES2/gl2ext.h"
25 #endif
26 
27 #include "common/rs_common_def.h"
28 
29 #include "recording/draw_cmd.h"
30 #include "recording/recording_canvas.h"
31 #include "render/rs_image.h"
32 
33 #if defined(ROSEN_OHOS) && (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK))
34 #include "surface_buffer.h"
35 #include "surface_type.h"
36 #include "sync_fence.h"
37 #include "external_window.h"
38 #endif
39 #ifdef RS_ENABLE_VK
40 #include "backend/native_buffer_utils.h"
41 #endif
42 
43 namespace OHOS {
44 namespace Rosen {
45 #ifdef ROSEN_OHOS
46 struct DrawingSurfaceBufferInfo {
47     DrawingSurfaceBufferInfo() = default;
48     DrawingSurfaceBufferInfo(const sptr<SurfaceBuffer>& surfaceBuffer, int offSetX, int offSetY, int width, int height,
49         pid_t pid = {}, uint64_t uid = {}, sptr<SyncFence> acquireFence = nullptr,
50         GraphicTransformType transform = GraphicTransformType::GRAPHIC_ROTATE_NONE,
51         Drawing::Rect srcRect = {})
surfaceBuffer_DrawingSurfaceBufferInfo52         : surfaceBuffer_(surfaceBuffer), srcRect_(srcRect),
53           dstRect_(Drawing::Rect { offSetX, offSetY, offSetX + width, offSetY + height }), pid_(pid), uid_(uid),
54           acquireFence_(acquireFence), transform_(transform)
55     {}
56     sptr<SurfaceBuffer> surfaceBuffer_ = nullptr;
57     Drawing::Rect srcRect_;
58     Drawing::Rect dstRect_;
59     pid_t pid_ = {};
60     uint64_t uid_ = {};
61     sptr<SyncFence> acquireFence_ = nullptr;
62     GraphicTransformType transform_ = GraphicTransformType::GRAPHIC_ROTATE_NONE;
63 };
64 #endif
65 
66 class RSB_EXPORT RSExtendImageObject : public Drawing::ExtendImageObject {
67 public:
68     RSExtendImageObject() = default;
69     RSExtendImageObject(const std::shared_ptr<Drawing::Image>& image, const std::shared_ptr<Drawing::Data>& data,
70         const Drawing::AdaptiveImageInfo& imageInfo);
71     RSExtendImageObject(const std::shared_ptr<Media::PixelMap>& pixelMap, const Drawing::AdaptiveImageInfo& imageInfo);
72     ~RSExtendImageObject() override;
73     void Playback(Drawing::Canvas& canvas, const Drawing::Rect& rect,
74         const Drawing::SamplingOptions& sampling, bool isBackground = false) override;
75     bool Marshalling(Parcel &parcel) const;
76     static RSExtendImageObject *Unmarshalling(Parcel &parcel);
77 #if defined(ROSEN_OHOS) && defined(RS_ENABLE_GL)
78     bool GetDrawingImageFromSurfaceBuffer(Drawing::Canvas& canvas, SurfaceBuffer* surfaceBuffer);
79 #endif
80 #if defined(ROSEN_OHOS) && defined(RS_ENABLE_VK)
81     bool MakeFromTextureForVK(Drawing::Canvas& canvas, SurfaceBuffer *surfaceBuffer,
82         const std::shared_ptr<Drawing::ColorSpace>& colorSpace = nullptr);
83     bool GetRsImageCache(Drawing::Canvas& canvas, const std::shared_ptr<Media::PixelMap>& pixelMap,
84         SurfaceBuffer *surfaceBuffer, const std::shared_ptr<Drawing::ColorSpace>& colorSpace = nullptr);
85 #endif
86     void SetNodeId(NodeId id) override;
87     void SetPaint(Drawing::Paint paint) override;
88     void Purge() override;
89 protected:
90     std::shared_ptr<RSImage> rsImage_ = nullptr;
91 private:
92 #if defined(ROSEN_OHOS) && (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK))
93     void PreProcessPixelMap(Drawing::Canvas& canvas, const std::shared_ptr<Media::PixelMap>& pixelMap,
94         const Drawing::SamplingOptions& sampling);
95 #ifdef RS_ENABLE_GL
96     mutable EGLImageKHR eglImage_ = EGL_NO_IMAGE_KHR;
97     mutable GLuint texId_ = 0;
98 #endif
99     mutable OHNativeWindowBuffer* nativeWindowBuffer_ = nullptr;
100     mutable pid_t tid_ = 0;
101 #endif
102 #ifdef RS_ENABLE_VK
103     mutable Drawing::BackendTexture backendTexture_ = {};
104     mutable NativeBufferUtils::VulkanCleanupHelper* cleanUpHelper_ = nullptr;
105 #endif
106     std::shared_ptr<Drawing::Image> image_;
107     Drawing::AdaptiveImageInfo imageInfo_;
108 };
109 
110 class RSB_EXPORT RSExtendImageBaseObj : public Drawing::ExtendImageBaseObj {
111 public:
112     RSExtendImageBaseObj() = default;
113     RSExtendImageBaseObj(const std::shared_ptr<Media::PixelMap>& pixelMap, const Drawing::Rect& src,
114         const Drawing::Rect& dst);
115     ~RSExtendImageBaseObj() override = default;
116     void Playback(Drawing::Canvas& canvas, const Drawing::Rect& rect, const Drawing::SamplingOptions& sampling,
117         Drawing::SrcRectConstraint constraint = Drawing::SrcRectConstraint::STRICT_SRC_RECT_CONSTRAINT) override;
118     bool Marshalling(Parcel &parcel) const;
119     static RSExtendImageBaseObj *Unmarshalling(Parcel &parcel);
120     void SetNodeId(NodeId id) override;
121     void Purge() override;
122 protected:
123     std::shared_ptr<RSImageBase> rsImage_ = nullptr;
124 };
125 
126 class RSB_EXPORT RSExtendImageNineObject : public Drawing::ExtendImageNineObject {
127 public:
128     RSExtendImageNineObject() = default;
129     RSExtendImageNineObject(const std::shared_ptr<Media::PixelMap>& pixelMap);
130     ~RSExtendImageNineObject() override = default;
131     void Playback(Drawing::Canvas& canvas, const Drawing::RectI& center, const Drawing::Rect& dst,
132         Drawing::FilterMode filterMode) override;
133     bool Marshalling(Parcel &parcel) const;
134     static RSExtendImageNineObject *Unmarshalling(Parcel &parcel);
135     void SetNodeId(NodeId id) override;
136     void Purge() override;
137 protected:
138     std::shared_ptr<RSImageBase> rsImage_ = nullptr;
139 };
140 
141 class RSB_EXPORT RSExtendImageLatticeObject : public Drawing::ExtendImageLatticeObject {
142 public:
143     RSExtendImageLatticeObject() = default;
144     RSExtendImageLatticeObject(const std::shared_ptr<Media::PixelMap>& pixelMap);
145     ~RSExtendImageLatticeObject() override = default;
146     void Playback(Drawing::Canvas& canvas, const Drawing::Lattice& lattice, const Drawing::Rect& dst,
147         Drawing::FilterMode filterMode) override;
148     bool Marshalling(Parcel &parcel) const;
149     static RSExtendImageLatticeObject *Unmarshalling(Parcel &parcel);
150     void SetNodeId(NodeId id) override;
151     void Purge() override;
152 protected:
153     std::shared_ptr<RSImageBase> rsImage_ = nullptr;
154 };
155 
156 class RSB_EXPORT RSExtendDrawFuncObj : public Drawing::ExtendDrawFuncObj {
157 public:
RSExtendDrawFuncObj(Drawing::RecordingCanvas::DrawFunc && drawFunc)158     explicit RSExtendDrawFuncObj(Drawing::RecordingCanvas::DrawFunc&& drawFunc) : drawFunc_(std::move(drawFunc)) {}
159     ~RSExtendDrawFuncObj() override = default;
160     void Playback(Drawing::Canvas* canvas, const Drawing::Rect* rect) override;
161     bool Marshalling(Parcel& parcel) const;
162     static RSExtendDrawFuncObj* Unmarshalling(Parcel& parcel);
163 
164 private:
165     Drawing::RecordingCanvas::DrawFunc drawFunc_;
166 };
167 
168 namespace Drawing {
169 class DrawImageWithParmOpItem : public DrawWithPaintOpItem {
170 public:
171     struct ConstructorHandle : public OpItem {
ConstructorHandleConstructorHandle172         ConstructorHandle(const OpDataHandle& objectHandle, const SamplingOptions& sampling,
173             const PaintHandle& paintHandle)
174             : OpItem(DrawOpItem::IMAGE_WITH_PARM_OPITEM), objectHandle(objectHandle), sampling(sampling),
175               paintHandle(paintHandle) {}
176         ~ConstructorHandle() override = default;
177         OpDataHandle objectHandle;
178         SamplingOptions sampling;
179         PaintHandle paintHandle;
180     };
181     DrawImageWithParmOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle);
182     DrawImageWithParmOpItem(const std::shared_ptr<Image>& image, const std::shared_ptr<Data>& data,
183         const AdaptiveImageInfo& rsImageInfo, const SamplingOptions& sampling, const Paint& paint);
184     ~DrawImageWithParmOpItem() override = default;
185 
186     static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle);
187     void Marshalling(DrawCmdList& cmdList) override;
188     void Playback(Canvas* canvas, const Rect* rect) override;
189     void SetNodeId(NodeId id) override;
190     void Dump(std::string& out) const override;
191 private:
192     SamplingOptions sampling_;
193     std::shared_ptr<ExtendImageObject> objectHandle_;
194 };
195 
196 class DrawPixelMapWithParmOpItem : public DrawWithPaintOpItem {
197 public:
198     struct ConstructorHandle : public OpItem {
ConstructorHandleConstructorHandle199         ConstructorHandle(const OpDataHandle& objectHandle, const SamplingOptions& sampling,
200             const PaintHandle& paintHandle)
201             : OpItem(DrawOpItem::PIXELMAP_WITH_PARM_OPITEM), objectHandle(objectHandle), sampling(sampling),
202               paintHandle(paintHandle) {}
203         ~ConstructorHandle() override = default;
204         OpDataHandle objectHandle;
205         SamplingOptions sampling;
206         PaintHandle paintHandle;
207     };
208     DrawPixelMapWithParmOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle);
209     DrawPixelMapWithParmOpItem(const std::shared_ptr<Media::PixelMap>& pixelMap,
210         const AdaptiveImageInfo& rsImageInfo, const SamplingOptions& sampling, const Paint& paint);
211     ~DrawPixelMapWithParmOpItem() override = default;
212 
213     static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle);
214     void Marshalling(DrawCmdList& cmdList) override;
215     void Playback(Canvas* canvas, const Rect* rect) override;
216     void SetNodeId(NodeId id) override;
217     virtual void DumpItems(std::string& out) const override;
Purge()218     void Purge() override
219     {
220         if (objectHandle_) {
221             objectHandle_->Purge();
222         }
223     }
224 private:
225     SamplingOptions sampling_;
226     std::shared_ptr<ExtendImageObject> objectHandle_;
227 };
228 
229 class DrawPixelMapRectOpItem : public DrawWithPaintOpItem {
230 public:
231     struct ConstructorHandle : public OpItem {
ConstructorHandleConstructorHandle232         ConstructorHandle(const OpDataHandle& objectHandle, const SamplingOptions& sampling,
233             SrcRectConstraint constraint, const PaintHandle& paintHandle)
234             : OpItem(DrawOpItem::PIXELMAP_RECT_OPITEM), objectHandle(objectHandle), sampling(sampling),
235               constraint(constraint), paintHandle(paintHandle) {}
236         ~ConstructorHandle() override = default;
237         OpDataHandle objectHandle;
238         SamplingOptions sampling;
239         SrcRectConstraint constraint;
240         PaintHandle paintHandle;
241     };
242     DrawPixelMapRectOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle);
243     DrawPixelMapRectOpItem(const std::shared_ptr<Media::PixelMap>& pixelMap, const Rect& src, const Rect& dst,
244         const SamplingOptions& sampling, SrcRectConstraint constraint, const Paint& paint);
245     ~DrawPixelMapRectOpItem() override = default;
246 
247     static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle);
248     void Marshalling(DrawCmdList& cmdList) override;
249     void Playback(Canvas* canvas, const Rect* rect) override;
250     void SetNodeId(NodeId id) override;
251     virtual void DumpItems(std::string& out) const override;
Purge()252     void Purge() override
253     {
254         if (objectHandle_) {
255             objectHandle_->Purge();
256         }
257     }
258 private:
259     SamplingOptions sampling_;
260     SrcRectConstraint constraint_;
261     std::shared_ptr<ExtendImageBaseObj> objectHandle_;
262 };
263 
264 class DrawFuncOpItem : public DrawOpItem {
265 public:
266     struct ConstructorHandle : public OpItem {
ConstructorHandleConstructorHandle267         ConstructorHandle(uint32_t funcObjectId)
268             : OpItem(DrawOpItem::DRAW_FUNC_OPITEM), funcObjectId(funcObjectId) {}
269         ~ConstructorHandle() override = default;
270         uint32_t funcObjectId;
271     };
272     DrawFuncOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle);
273     explicit DrawFuncOpItem(RecordingCanvas::DrawFunc&& drawFunc);
274     ~DrawFuncOpItem() override = default;
275 
276     static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle);
277     void Marshalling(DrawCmdList& cmdList) override;
278     void Playback(Canvas* canvas, const Rect* rect) override;
279 private:
280     std::shared_ptr<ExtendDrawFuncObj> objectHandle_;
281 };
282 
283 class DrawPixelMapNineOpItem : public DrawWithPaintOpItem {
284 public:
285     struct ConstructorHandle : public OpItem {
ConstructorHandleConstructorHandle286         ConstructorHandle(const OpDataHandle& objectHandle, const Drawing::RectI& center,
287             const Drawing::Rect& dst, Drawing::FilterMode filterMode, const PaintHandle& paintHandle)
288             : OpItem(DrawOpItem::PIXELMAP_NINE_OPITEM), objectHandle(objectHandle), center(center),
289               dst(dst), filterMode(filterMode), paintHandle(paintHandle) {}
290         ~ConstructorHandle() override = default;
291         OpDataHandle objectHandle;
292         Drawing::RectI center;
293         Drawing::Rect dst;
294         FilterMode filterMode;
295         PaintHandle paintHandle;
296     };
297     DrawPixelMapNineOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle);
298     DrawPixelMapNineOpItem(const std::shared_ptr<Media::PixelMap>& pixelMap, const Drawing::RectI& center,
299         const Drawing::Rect& dst, Drawing::FilterMode filterMode, const Paint& paint);
300     ~DrawPixelMapNineOpItem() override = default;
301 
302     static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle);
303     void Marshalling(DrawCmdList& cmdList) override;
304     void Playback(Canvas* canvas, const Rect* rect) override;
305     void SetNodeId(NodeId id) override;
306     virtual void DumpItems(std::string& out) const override;
Purge()307     void Purge() override
308     {
309         if (objectHandle_) {
310             objectHandle_->Purge();
311         }
312     }
313 private:
314     Drawing::RectI center_;
315     Drawing::Rect dst_;
316     FilterMode filterMode_;
317     std::shared_ptr<ExtendImageNineObject> objectHandle_;
318 };
319 
320 class DrawPixelMapLatticeOpItem : public DrawWithPaintOpItem {
321 public:
322     struct ConstructorHandle : public OpItem {
ConstructorHandleConstructorHandle323         ConstructorHandle(const OpDataHandle& objectHandle, const LatticeHandle& latticeHandle,
324             const Drawing::Rect& dst, Drawing::FilterMode filterMode, const PaintHandle& paintHandle)
325             : OpItem(DrawOpItem::PIXELMAP_LATTICE_OPITEM), objectHandle(objectHandle), latticeHandle(latticeHandle),
326               dst(dst), filterMode(filterMode), paintHandle(paintHandle) {}
327         ~ConstructorHandle() override = default;
328         OpDataHandle objectHandle;
329         LatticeHandle latticeHandle;
330         Drawing::Rect dst;
331         FilterMode filterMode;
332         PaintHandle paintHandle;
333     };
334     DrawPixelMapLatticeOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle);
335     DrawPixelMapLatticeOpItem(const std::shared_ptr<Media::PixelMap>& pixelMap, const Drawing::Lattice& lattice,
336         const Drawing::Rect& dst, Drawing::FilterMode filterMode, const Paint& paint);
337     ~DrawPixelMapLatticeOpItem() override = default;
338 
339     static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle);
340     void Marshalling(DrawCmdList& cmdList) override;
341     void Playback(Canvas* canvas, const Rect* rect) override;
342     void SetNodeId(NodeId id) override;
343     virtual void DumpItems(std::string& out) const override;
Purge()344     void Purge() override
345     {
346         if (objectHandle_) {
347             objectHandle_->Purge();
348         }
349     }
350 private:
351     Drawing::Lattice lattice_;
352     Drawing::Rect dst_;
353     FilterMode filterMode_;
354     std::shared_ptr<ExtendImageLatticeObject> objectHandle_;
355 };
356 
357 #ifdef ROSEN_OHOS
358 struct RSB_EXPORT DrawSurfaceBufferFinishCbData {
359     uint64_t uid;
360     pid_t pid;
361     uint32_t surfaceBufferId;
362     NodeId rootNodeId = INVALID_NODEID;
363     sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE;
364     bool isRendered = false;
365     bool isNeedTriggerCbDirectly = false;
366 };
367 
368 struct RSB_EXPORT DrawSurfaceBufferAfterAcquireCbData {
369     uint64_t uid;
370     pid_t pid;
371 };
372 
373 struct RSB_EXPORT DrawSurfaceBufferOpItemCb {
374     std::function<void(const DrawSurfaceBufferFinishCbData&)> OnFinish;
375     std::function<void(const DrawSurfaceBufferAfterAcquireCbData&)> OnAfterAcquireBuffer;
376 };
377 
378 class DrawSurfaceBufferOpItem : public DrawWithPaintOpItem {
379 public:
380     struct ConstructorHandle : public OpItem {
ConstructorHandleConstructorHandle381         ConstructorHandle(uint32_t surfaceBufferId, int offSetX, int offSetY, int width, int height, pid_t pid,
382             uint64_t uid, GraphicTransformType transform, Drawing::Rect srcRect, const PaintHandle& paintHandle)
383             : OpItem(DrawOpItem::SURFACEBUFFER_OPITEM), surfaceBufferId(surfaceBufferId),
384               surfaceBufferInfo(nullptr, offSetX, offSetY, width, height, pid, uid, nullptr, transform, srcRect),
385               paintHandle(paintHandle)
386         {}
387         ~ConstructorHandle() override = default;
388         uint32_t surfaceBufferId;
389         DrawingSurfaceBufferInfo surfaceBufferInfo;
390         PaintHandle paintHandle;
391     };
392 
393     DrawSurfaceBufferOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle);
DrawSurfaceBufferOpItem(const DrawingSurfaceBufferInfo & surfaceBufferInfo,const Paint & paint)394     DrawSurfaceBufferOpItem(const DrawingSurfaceBufferInfo& surfaceBufferInfo, const Paint& paint)
395         : DrawWithPaintOpItem(paint, SURFACEBUFFER_OPITEM), surfaceBufferInfo_(surfaceBufferInfo) {}
396     ~DrawSurfaceBufferOpItem();
397 
398     static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle);
399     void Marshalling(DrawCmdList& cmdList) override;
400     void Playback(Canvas* canvas, const Rect* rect) override;
401     virtual void DumpItems(std::string& out) const override;
402     RSB_EXPORT static void RegisterSurfaceBufferCallback(DrawSurfaceBufferOpItemCb callbacks);
403     RSB_EXPORT static void RegisterGetRootNodeIdFuncForRT(std::function<NodeId()> func);
404     RSB_EXPORT static void SetIsUniRender(bool isUniRender);
405 private:
406     void OnAfterAcquireBuffer();
407     void OnBeforeDraw();
408     void OnAfterDraw();
409     void OnDestruct();
410     void ReleaseBuffer();
411     void Clear();
412     void DealWithRotate(Canvas* canvas);
413     void Draw(Canvas* canvas);
414     void DrawWithVulkan(Canvas* canvas);
415     void DrawWithGles(Canvas* canvas);
416     bool CreateEglTextureId();
417     bool IsNeedDrawDirectly() const;
418     static Drawing::SamplingOptions CreateSamplingOptions(const Drawing::Matrix& matrix);
419     static Drawing::BitmapFormat CreateBitmapFormat(int32_t bufferFormat);
420 #ifdef RS_ENABLE_GL
421     static GLenum GetGLTextureFormatByBitmapFormat(Drawing::ColorType colorType);
422 #endif
423     static GraphicTransformType MapGraphicTransformType(GraphicTransformType origin);
424     static bool IsValidRemoteAddress(pid_t pid, uint64_t uid);
425     mutable DrawingSurfaceBufferInfo surfaceBufferInfo_;
426     NodeId rootNodeId_ = INVALID_NODEID;
427     sptr<SyncFence> releaseFence_ = SyncFence::INVALID_FENCE;
428     bool isRendered_ = false;
429     bool isNeedRotateDstRect_ = false;
430     bool isNeedDrawDirectly_ = false;
431 
432 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)
433     OHNativeWindowBuffer* nativeWindowBuffer_ = nullptr;
434 #endif
435 #ifdef RS_ENABLE_GL
436     mutable EGLImageKHR eglImage_ = EGL_NO_IMAGE_KHR;
437     mutable GLuint texId_ = 0;
438 #endif
439 };
440 #endif
441 }
442 } // namespace Rosen
443 } // namespace OHOS
444 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_DRAW_CMD_H
445