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 #ifdef ROSEN_OHOS 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 "sync_fence.h" 36 #include "external_window.h" 37 #endif 38 39 namespace OHOS { 40 namespace Rosen { 41 #ifdef ROSEN_OHOS 42 #ifdef RS_ENABLE_VK 43 namespace NativeBufferUtils { 44 class VulkanCleanupHelper; 45 } 46 #endif 47 struct DrawingSurfaceBufferInfo { 48 DrawingSurfaceBufferInfo() = default; 49 DrawingSurfaceBufferInfo(const sptr<SurfaceBuffer>& surfaceBuffer, int offSetX, int offSetY, int width, int height, 50 pid_t pid = {}, uint64_t uid = {}, sptr<SyncFence> acquireFence = nullptr, Drawing::Rect srcRect = {}) 51 : surfaceBuffer_(surfaceBuffer), srcRect_(srcRect), 52 dstRect_(Drawing::Rect { offSetX, offSetY, offSetX + width, offSetY + height }), pid_(pid), uid_(uid), 53 acquireFence_(acquireFence) 54 {} 55 sptr<SurfaceBuffer> surfaceBuffer_ = nullptr; 56 Drawing::Rect srcRect_; 57 Drawing::Rect dstRect_; 58 pid_t pid_ = {}; 59 uint64_t uid_ = {}; 60 sptr<SyncFence> acquireFence_ = nullptr; 61 }; 62 #endif 63 64 class RSB_EXPORT RSExtendImageObject : public Drawing::ExtendImageObject { 65 public: 66 RSExtendImageObject() = default; 67 RSExtendImageObject(const std::shared_ptr<Drawing::Image>& image, const std::shared_ptr<Drawing::Data>& data, 68 const Drawing::AdaptiveImageInfo& imageInfo); 69 RSExtendImageObject(const std::shared_ptr<Media::PixelMap>& pixelMap, const Drawing::AdaptiveImageInfo& imageInfo); 70 ~RSExtendImageObject() override; 71 void Playback(Drawing::Canvas& canvas, const Drawing::Rect& rect, 72 const Drawing::SamplingOptions& sampling, bool isBackground = false) override; 73 bool Marshalling(Parcel &parcel) const; 74 static RSExtendImageObject *Unmarshalling(Parcel &parcel); 75 #if defined(ROSEN_OHOS) && defined(RS_ENABLE_GL) 76 bool GetDrawingImageFromSurfaceBuffer(Drawing::Canvas& canvas, SurfaceBuffer* surfaceBuffer); 77 #endif 78 #if defined(ROSEN_OHOS) && defined(RS_ENABLE_VK) 79 bool MakeFromTextureForVK(Drawing::Canvas& canvas, SurfaceBuffer *surfaceBuffer, 80 const std::shared_ptr<Drawing::ColorSpace>& colorSpace = nullptr); 81 bool GetRsImageCache(Drawing::Canvas& canvas, const std::shared_ptr<Media::PixelMap>& pixelMap, 82 SurfaceBuffer *surfaceBuffer, const std::shared_ptr<Drawing::ColorSpace>& colorSpace = nullptr); 83 #endif 84 void SetNodeId(NodeId id) override; 85 void SetPaint(Drawing::Paint paint) override; 86 void Purge() override; 87 protected: 88 std::shared_ptr<RSImage> rsImage_; 89 private: 90 #if defined(ROSEN_OHOS) && (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)) 91 void PreProcessPixelMap(Drawing::Canvas& canvas, const std::shared_ptr<Media::PixelMap>& pixelMap, 92 const Drawing::SamplingOptions& sampling); 93 #ifdef RS_ENABLE_GL 94 mutable EGLImageKHR eglImage_ = EGL_NO_IMAGE_KHR; 95 mutable GLuint texId_ = 0; 96 #endif 97 mutable OHNativeWindowBuffer* nativeWindowBuffer_ = nullptr; 98 mutable pid_t tid_ = 0; 99 #ifdef RS_ENABLE_VK 100 mutable Drawing::BackendTexture backendTexture_ = {}; 101 mutable NativeBufferUtils::VulkanCleanupHelper* cleanUpHelper_ = nullptr; 102 #endif 103 #endif 104 std::shared_ptr<Drawing::Image> image_; 105 Drawing::AdaptiveImageInfo imageInfo_; 106 }; 107 108 class RSB_EXPORT RSExtendImageBaseObj : public Drawing::ExtendImageBaseObj { 109 public: 110 RSExtendImageBaseObj() = default; 111 RSExtendImageBaseObj(const std::shared_ptr<Media::PixelMap>& pixelMap, const Drawing::Rect& src, 112 const Drawing::Rect& dst); 113 ~RSExtendImageBaseObj() override = default; 114 void Playback(Drawing::Canvas& canvas, const Drawing::Rect& rect, const Drawing::SamplingOptions& sampling, 115 Drawing::SrcRectConstraint constraint = Drawing::SrcRectConstraint::STRICT_SRC_RECT_CONSTRAINT) override; 116 bool Marshalling(Parcel &parcel) const; 117 static RSExtendImageBaseObj *Unmarshalling(Parcel &parcel); 118 void SetNodeId(NodeId id) override; 119 void Purge() override; 120 protected: 121 std::shared_ptr<RSImageBase> rsImage_; 122 }; 123 124 class RSB_EXPORT RSExtendDrawFuncObj : public Drawing::ExtendDrawFuncObj { 125 public: RSExtendDrawFuncObj(Drawing::RecordingCanvas::DrawFunc && drawFunc)126 explicit RSExtendDrawFuncObj(Drawing::RecordingCanvas::DrawFunc&& drawFunc) : drawFunc_(std::move(drawFunc)) {} 127 ~RSExtendDrawFuncObj() override = default; 128 void Playback(Drawing::Canvas* canvas, const Drawing::Rect* rect) override; 129 bool Marshalling(Parcel& parcel) const; 130 static RSExtendDrawFuncObj* Unmarshalling(Parcel& parcel); 131 132 private: 133 Drawing::RecordingCanvas::DrawFunc drawFunc_; 134 }; 135 136 namespace Drawing { 137 class DrawImageWithParmOpItem : public DrawWithPaintOpItem { 138 public: 139 struct ConstructorHandle : public OpItem { ConstructorHandleConstructorHandle140 ConstructorHandle(const OpDataHandle& objectHandle, const SamplingOptions& sampling, 141 const PaintHandle& paintHandle) 142 : OpItem(DrawOpItem::IMAGE_WITH_PARM_OPITEM), objectHandle(objectHandle), sampling(sampling), 143 paintHandle(paintHandle) {} 144 ~ConstructorHandle() override = default; 145 OpDataHandle objectHandle; 146 SamplingOptions sampling; 147 PaintHandle paintHandle; 148 }; 149 DrawImageWithParmOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle); 150 DrawImageWithParmOpItem(const std::shared_ptr<Image>& image, const std::shared_ptr<Data>& data, 151 const AdaptiveImageInfo& rsImageInfo, const SamplingOptions& sampling, const Paint& paint); 152 ~DrawImageWithParmOpItem() override = default; 153 154 static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle); 155 void Marshalling(DrawCmdList& cmdList) override; 156 void Playback(Canvas* canvas, const Rect* rect) override; 157 void SetNodeId(NodeId id) override; 158 private: 159 SamplingOptions sampling_; 160 std::shared_ptr<ExtendImageObject> objectHandle_; 161 }; 162 163 class DrawPixelMapWithParmOpItem : public DrawWithPaintOpItem { 164 public: 165 struct ConstructorHandle : public OpItem { ConstructorHandleConstructorHandle166 ConstructorHandle(const OpDataHandle& objectHandle, const SamplingOptions& sampling, 167 const PaintHandle& paintHandle) 168 : OpItem(DrawOpItem::PIXELMAP_WITH_PARM_OPITEM), objectHandle(objectHandle), sampling(sampling), 169 paintHandle(paintHandle) {} 170 ~ConstructorHandle() override = default; 171 OpDataHandle objectHandle; 172 SamplingOptions sampling; 173 PaintHandle paintHandle; 174 }; 175 DrawPixelMapWithParmOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle); 176 DrawPixelMapWithParmOpItem(const std::shared_ptr<Media::PixelMap>& pixelMap, 177 const AdaptiveImageInfo& rsImageInfo, const SamplingOptions& sampling, const Paint& paint); 178 ~DrawPixelMapWithParmOpItem() override = default; 179 180 static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle); 181 void Marshalling(DrawCmdList& cmdList) override; 182 void Playback(Canvas* canvas, const Rect* rect) override; 183 void SetNodeId(NodeId id) override; Purge()184 void Purge() override 185 { 186 if (objectHandle_) { 187 objectHandle_->Purge(); 188 } 189 } 190 private: 191 SamplingOptions sampling_; 192 std::shared_ptr<ExtendImageObject> objectHandle_; 193 }; 194 195 class DrawPixelMapRectOpItem : public DrawWithPaintOpItem { 196 public: 197 struct ConstructorHandle : public OpItem { ConstructorHandleConstructorHandle198 ConstructorHandle(const OpDataHandle& objectHandle, const SamplingOptions& sampling, 199 SrcRectConstraint constraint, const PaintHandle& paintHandle) 200 : OpItem(DrawOpItem::PIXELMAP_RECT_OPITEM), objectHandle(objectHandle), sampling(sampling), 201 constraint(constraint), paintHandle(paintHandle) {} 202 ~ConstructorHandle() override = default; 203 OpDataHandle objectHandle; 204 SamplingOptions sampling; 205 SrcRectConstraint constraint; 206 PaintHandle paintHandle; 207 }; 208 DrawPixelMapRectOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle); 209 DrawPixelMapRectOpItem(const std::shared_ptr<Media::PixelMap>& pixelMap, const Rect& src, const Rect& dst, 210 const SamplingOptions& sampling, SrcRectConstraint constraint, const Paint& paint); 211 ~DrawPixelMapRectOpItem() 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; Purge()217 void Purge() override 218 { 219 if (objectHandle_) { 220 objectHandle_->Purge(); 221 } 222 } 223 private: 224 SamplingOptions sampling_; 225 SrcRectConstraint constraint_; 226 std::shared_ptr<ExtendImageBaseObj> objectHandle_; 227 }; 228 229 class DrawFuncOpItem : public DrawOpItem { 230 public: 231 struct ConstructorHandle : public OpItem { ConstructorHandleConstructorHandle232 ConstructorHandle(uint32_t funcObjectId) 233 : OpItem(DrawOpItem::DRAW_FUNC_OPITEM), funcObjectId(funcObjectId) {} 234 ~ConstructorHandle() override = default; 235 uint32_t funcObjectId; 236 }; 237 DrawFuncOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle); 238 explicit DrawFuncOpItem(RecordingCanvas::DrawFunc&& drawFunc); 239 ~DrawFuncOpItem() override = default; 240 241 static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle); 242 void Marshalling(DrawCmdList& cmdList) override; 243 void Playback(Canvas* canvas, const Rect* rect) override; 244 private: 245 std::shared_ptr<ExtendDrawFuncObj> objectHandle_; 246 }; 247 248 #ifdef ROSEN_OHOS 249 struct RSB_EXPORT DrawSurfaceBufferFinishCbData { 250 uint64_t uid; 251 pid_t pid; 252 uint32_t surfaceBufferId; 253 NodeId rootNodeId = INVALID_NODEID; 254 sptr<SyncFence> releaseFence = SyncFence::INVALID_FENCE; 255 bool isRendered = false; 256 bool isNeedTriggerCbDirectly = false; 257 }; 258 259 struct RSB_EXPORT DrawSurfaceBufferAfterAcquireCbData { 260 uint64_t uid; 261 pid_t pid; 262 }; 263 264 struct RSB_EXPORT DrawSurfaceBufferOpItemCb { 265 std::function<void(const DrawSurfaceBufferFinishCbData&)> OnFinish; 266 std::function<void(const DrawSurfaceBufferAfterAcquireCbData&)> OnAfterAcquireBuffer; 267 }; 268 269 class DrawSurfaceBufferOpItem : public DrawWithPaintOpItem { 270 public: 271 struct ConstructorHandle : public OpItem { ConstructorHandleConstructorHandle272 ConstructorHandle(uint32_t surfaceBufferId, int offSetX, int offSetY, int width, int height, pid_t pid, 273 uint64_t uid, Drawing::Rect srcRect, const PaintHandle& paintHandle) 274 : OpItem(DrawOpItem::SURFACEBUFFER_OPITEM), surfaceBufferId(surfaceBufferId), 275 surfaceBufferInfo(nullptr, offSetX, offSetY, width, height, pid, uid, nullptr, srcRect), 276 paintHandle(paintHandle) 277 {} 278 ~ConstructorHandle() override = default; 279 uint32_t surfaceBufferId; 280 DrawingSurfaceBufferInfo surfaceBufferInfo; 281 PaintHandle paintHandle; 282 }; 283 284 DrawSurfaceBufferOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle); DrawSurfaceBufferOpItem(const DrawingSurfaceBufferInfo & surfaceBufferInfo,const Paint & paint)285 DrawSurfaceBufferOpItem(const DrawingSurfaceBufferInfo& surfaceBufferInfo, const Paint& paint) 286 : DrawWithPaintOpItem(paint, SURFACEBUFFER_OPITEM), surfaceBufferInfo_(surfaceBufferInfo) {} 287 ~DrawSurfaceBufferOpItem(); 288 289 static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle); 290 void Marshalling(DrawCmdList& cmdList) override; 291 void Playback(Canvas* canvas, const Rect* rect) override; 292 RSB_EXPORT static void RegisterSurfaceBufferCallback(DrawSurfaceBufferOpItemCb callbacks); 293 RSB_EXPORT static void RegisterGetRootNodeIdFuncForRT(std::function<NodeId()> func); 294 RSB_EXPORT static void SetIsUniRender(bool isUniRender); 295 private: 296 void OnDestruct(); 297 void OnAfterAcquireBuffer(); 298 void OnAfterDraw(); 299 void ReleaseBuffer(); 300 void Clear(); 301 void Draw(Canvas* canvas); 302 void DrawWithVulkan(Canvas* canvas); 303 void DrawWithGles(Canvas* canvas); 304 bool CreateEglTextureId(); 305 static Drawing::BitmapFormat CreateBitmapFormat(int32_t bufferFormat); 306 #ifdef RS_ENABLE_GL 307 static GLenum GetGLTextureFormatByBitmapFormat(Drawing::ColorType colorType); 308 #endif 309 mutable DrawingSurfaceBufferInfo surfaceBufferInfo_; 310 NodeId rootNodeId_ = INVALID_NODEID; 311 sptr<SyncFence> releaseFence_ = SyncFence::INVALID_FENCE; 312 bool isRendered_ = false; 313 314 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK) 315 OHNativeWindowBuffer* nativeWindowBuffer_ = nullptr; 316 #endif 317 #ifdef RS_ENABLE_GL 318 mutable EGLImageKHR eglImage_ = EGL_NO_IMAGE_KHR; 319 mutable GLuint texId_ = 0; 320 #endif 321 }; 322 #endif 323 } 324 } // namespace Rosen 325 } // namespace OHOS 326 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_DRAW_CMD_H 327