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 "recording/draw_cmd.h" 28 #include "recording/recording_canvas.h" 29 #include "render/rs_image.h" 30 31 #if defined(ROSEN_OHOS) && (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)) 32 #include "surface_buffer.h" 33 #include "sync_fence.h" 34 #include "external_window.h" 35 #endif 36 37 namespace OHOS { 38 namespace Rosen { 39 #ifdef ROSEN_OHOS 40 #ifdef RS_ENABLE_VK 41 namespace NativeBufferUtils { 42 class VulkanCleanupHelper; 43 } 44 #endif 45 struct DrawingSurfaceBufferInfo { 46 DrawingSurfaceBufferInfo() = default; 47 DrawingSurfaceBufferInfo(const sptr<SurfaceBuffer>& surfaceBuffer, int offSetX, int offSetY, int width, int height, 48 pid_t pid = {}, uint64_t uid = {}, sptr<SyncFence> acquireFence = nullptr) surfaceBuffer_DrawingSurfaceBufferInfo49 : surfaceBuffer_(surfaceBuffer), offSetX_(offSetX), offSetY_(offSetY), width_(width), height_(height), 50 pid_(pid), uid_(uid), acquireFence_(acquireFence) 51 {} 52 sptr<SurfaceBuffer> surfaceBuffer_ = nullptr; 53 int offSetX_ = 0; 54 int offSetY_ = 0; 55 int width_ = 0; 56 int height_ = 0; 57 pid_t pid_ = {}; 58 uint64_t uid_ = {}; 59 sptr<SyncFence> acquireFence_ = nullptr; 60 }; 61 #endif 62 63 class RSB_EXPORT RSExtendImageObject : public Drawing::ExtendImageObject { 64 public: 65 RSExtendImageObject() = default; 66 RSExtendImageObject(const std::shared_ptr<Drawing::Image>& image, const std::shared_ptr<Drawing::Data>& data, 67 const Drawing::AdaptiveImageInfo& imageInfo); 68 RSExtendImageObject(const std::shared_ptr<Media::PixelMap>& pixelMap, const Drawing::AdaptiveImageInfo& imageInfo); 69 ~RSExtendImageObject() override; 70 void Playback(Drawing::Canvas& canvas, const Drawing::Rect& rect, 71 const Drawing::SamplingOptions& sampling, bool isBackground = false) override; 72 bool Marshalling(Parcel &parcel) const; 73 static RSExtendImageObject *Unmarshalling(Parcel &parcel); 74 #if defined(ROSEN_OHOS) && defined(RS_ENABLE_GL) 75 bool GetDrawingImageFromSurfaceBuffer(Drawing::Canvas& canvas, SurfaceBuffer* surfaceBuffer); 76 #endif 77 #if defined(ROSEN_OHOS) && defined(RS_ENABLE_VK) 78 bool MakeFromTextureForVK(Drawing::Canvas& canvas, SurfaceBuffer *surfaceBuffer, 79 const std::shared_ptr<Drawing::ColorSpace>& colorSpace = nullptr); 80 bool GetRsImageCache(Drawing::Canvas& canvas, const std::shared_ptr<Media::PixelMap>& pixelMap, 81 SurfaceBuffer *surfaceBuffer, const std::shared_ptr<Drawing::ColorSpace>& colorSpace = nullptr); 82 #endif 83 void SetNodeId(NodeId id) override; 84 void SetPaint(Drawing::Paint paint) override; 85 void Purge() override; 86 protected: 87 std::shared_ptr<RSImage> rsImage_; 88 private: 89 #if defined(ROSEN_OHOS) && (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)) 90 void PreProcessPixelMap(Drawing::Canvas& canvas, const std::shared_ptr<Media::PixelMap>& pixelMap, 91 const Drawing::SamplingOptions& sampling); 92 #ifdef RS_ENABLE_GL 93 mutable EGLImageKHR eglImage_ = EGL_NO_IMAGE_KHR; 94 mutable GLuint texId_ = 0; 95 #endif 96 mutable OHNativeWindowBuffer* nativeWindowBuffer_ = nullptr; 97 mutable pid_t tid_ = 0; 98 #ifdef RS_ENABLE_VK 99 mutable Drawing::BackendTexture backendTexture_ = {}; 100 mutable NativeBufferUtils::VulkanCleanupHelper* cleanUpHelper_ = nullptr; 101 #endif 102 #endif 103 std::shared_ptr<Drawing::Image> image_; 104 Drawing::AdaptiveImageInfo imageInfo_; 105 }; 106 107 class RSB_EXPORT RSExtendImageBaseObj : public Drawing::ExtendImageBaseObj { 108 public: 109 RSExtendImageBaseObj() = default; 110 RSExtendImageBaseObj(const std::shared_ptr<Media::PixelMap>& pixelMap, const Drawing::Rect& src, 111 const Drawing::Rect& dst); 112 ~RSExtendImageBaseObj() override = default; 113 void Playback(Drawing::Canvas& canvas, const Drawing::Rect& rect, const Drawing::SamplingOptions& sampling, 114 Drawing::SrcRectConstraint constraint = Drawing::SrcRectConstraint::STRICT_SRC_RECT_CONSTRAINT) override; 115 bool Marshalling(Parcel &parcel) const; 116 static RSExtendImageBaseObj *Unmarshalling(Parcel &parcel); 117 void SetNodeId(NodeId id) override; 118 void Purge() override; 119 protected: 120 std::shared_ptr<RSImageBase> rsImage_; 121 }; 122 123 class RSB_EXPORT RSExtendDrawFuncObj : public Drawing::ExtendDrawFuncObj { 124 public: RSExtendDrawFuncObj(Drawing::RecordingCanvas::DrawFunc && drawFunc)125 explicit RSExtendDrawFuncObj(Drawing::RecordingCanvas::DrawFunc&& drawFunc) : drawFunc_(std::move(drawFunc)) {} 126 ~RSExtendDrawFuncObj() override = default; 127 void Playback(Drawing::Canvas* canvas, const Drawing::Rect* rect) override; 128 bool Marshalling(Parcel& parcel) const; 129 static RSExtendDrawFuncObj* Unmarshalling(Parcel& parcel); 130 131 private: 132 Drawing::RecordingCanvas::DrawFunc drawFunc_; 133 }; 134 135 namespace Drawing { 136 class DrawImageWithParmOpItem : public DrawWithPaintOpItem { 137 public: 138 struct ConstructorHandle : public OpItem { ConstructorHandleConstructorHandle139 ConstructorHandle(const OpDataHandle& objectHandle, const SamplingOptions& sampling, 140 const PaintHandle& paintHandle) 141 : OpItem(DrawOpItem::IMAGE_WITH_PARM_OPITEM), objectHandle(objectHandle), sampling(sampling), 142 paintHandle(paintHandle) {} 143 ~ConstructorHandle() override = default; 144 OpDataHandle objectHandle; 145 SamplingOptions sampling; 146 PaintHandle paintHandle; 147 }; 148 DrawImageWithParmOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle); 149 DrawImageWithParmOpItem(const std::shared_ptr<Image>& image, const std::shared_ptr<Data>& data, 150 const AdaptiveImageInfo& rsImageInfo, const SamplingOptions& sampling, const Paint& paint); 151 ~DrawImageWithParmOpItem() override = default; 152 153 static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle); 154 void Marshalling(DrawCmdList& cmdList) override; 155 void Playback(Canvas* canvas, const Rect* rect) override; 156 void SetNodeId(NodeId id) override; 157 private: 158 SamplingOptions sampling_; 159 std::shared_ptr<ExtendImageObject> objectHandle_; 160 }; 161 162 class DrawPixelMapWithParmOpItem : public DrawWithPaintOpItem { 163 public: 164 struct ConstructorHandle : public OpItem { ConstructorHandleConstructorHandle165 ConstructorHandle(const OpDataHandle& objectHandle, const SamplingOptions& sampling, 166 const PaintHandle& paintHandle) 167 : OpItem(DrawOpItem::PIXELMAP_WITH_PARM_OPITEM), objectHandle(objectHandle), sampling(sampling), 168 paintHandle(paintHandle) {} 169 ~ConstructorHandle() override = default; 170 OpDataHandle objectHandle; 171 SamplingOptions sampling; 172 PaintHandle paintHandle; 173 }; 174 DrawPixelMapWithParmOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle); 175 DrawPixelMapWithParmOpItem(const std::shared_ptr<Media::PixelMap>& pixelMap, 176 const AdaptiveImageInfo& rsImageInfo, const SamplingOptions& sampling, const Paint& paint); 177 ~DrawPixelMapWithParmOpItem() override = default; 178 179 static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle); 180 void Marshalling(DrawCmdList& cmdList) override; 181 void Playback(Canvas* canvas, const Rect* rect) override; 182 void SetNodeId(NodeId id) override; Purge()183 void Purge() override 184 { 185 if (objectHandle_) { 186 objectHandle_->Purge(); 187 } 188 } 189 private: 190 SamplingOptions sampling_; 191 std::shared_ptr<ExtendImageObject> objectHandle_; 192 }; 193 194 class DrawPixelMapRectOpItem : public DrawWithPaintOpItem { 195 public: 196 struct ConstructorHandle : public OpItem { ConstructorHandleConstructorHandle197 ConstructorHandle(const OpDataHandle& objectHandle, const SamplingOptions& sampling, 198 SrcRectConstraint constraint, const PaintHandle& paintHandle) 199 : OpItem(DrawOpItem::PIXELMAP_RECT_OPITEM), objectHandle(objectHandle), sampling(sampling), 200 constraint(constraint), paintHandle(paintHandle) {} 201 ~ConstructorHandle() override = default; 202 OpDataHandle objectHandle; 203 SamplingOptions sampling; 204 SrcRectConstraint constraint; 205 PaintHandle paintHandle; 206 }; 207 DrawPixelMapRectOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle); 208 DrawPixelMapRectOpItem(const std::shared_ptr<Media::PixelMap>& pixelMap, const Rect& src, const Rect& dst, 209 const SamplingOptions& sampling, SrcRectConstraint constraint, const Paint& paint); 210 ~DrawPixelMapRectOpItem() override = default; 211 212 static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle); 213 void Marshalling(DrawCmdList& cmdList) override; 214 void Playback(Canvas* canvas, const Rect* rect) override; 215 void SetNodeId(NodeId id) override; Purge()216 void Purge() override 217 { 218 if (objectHandle_) { 219 objectHandle_->Purge(); 220 } 221 } 222 private: 223 SamplingOptions sampling_; 224 SrcRectConstraint constraint_; 225 std::shared_ptr<ExtendImageBaseObj> objectHandle_; 226 }; 227 228 class DrawFuncOpItem : public DrawOpItem { 229 public: 230 struct ConstructorHandle : public OpItem { ConstructorHandleConstructorHandle231 ConstructorHandle(uint32_t funcObjectId) 232 : OpItem(DrawOpItem::DRAW_FUNC_OPITEM), funcObjectId(funcObjectId) {} 233 ~ConstructorHandle() override = default; 234 uint32_t funcObjectId; 235 }; 236 DrawFuncOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle); 237 explicit DrawFuncOpItem(RecordingCanvas::DrawFunc&& drawFunc); 238 ~DrawFuncOpItem() override = default; 239 240 static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle); 241 void Marshalling(DrawCmdList& cmdList) override; 242 void Playback(Canvas* canvas, const Rect* rect) override; 243 private: 244 std::shared_ptr<ExtendDrawFuncObj> objectHandle_; 245 }; 246 247 #ifdef ROSEN_OHOS 248 class DrawSurfaceBufferOpItem : public DrawWithPaintOpItem { 249 public: 250 struct ConstructorHandle : public OpItem { ConstructorHandleConstructorHandle251 ConstructorHandle(uint32_t surfaceBufferId, int offSetX, int offSetY, int width, int height, 252 pid_t pid, uint64_t uid, const PaintHandle& paintHandle) 253 : OpItem(DrawOpItem::SURFACEBUFFER_OPITEM), surfaceBufferId(surfaceBufferId), 254 surfaceBufferInfo(nullptr, offSetX, offSetY, width, height, pid, uid, nullptr), 255 paintHandle(paintHandle) {} 256 ~ConstructorHandle() override = default; 257 uint32_t surfaceBufferId; 258 DrawingSurfaceBufferInfo surfaceBufferInfo; 259 PaintHandle paintHandle; 260 }; 261 262 DrawSurfaceBufferOpItem(const DrawCmdList& cmdList, ConstructorHandle* handle); DrawSurfaceBufferOpItem(const DrawingSurfaceBufferInfo & surfaceBufferInfo,const Paint & paint)263 DrawSurfaceBufferOpItem(const DrawingSurfaceBufferInfo& surfaceBufferInfo, const Paint& paint) 264 : DrawWithPaintOpItem(paint, SURFACEBUFFER_OPITEM), surfaceBufferInfo_(surfaceBufferInfo) {} 265 ~DrawSurfaceBufferOpItem(); 266 267 static std::shared_ptr<DrawOpItem> Unmarshalling(const DrawCmdList& cmdList, void* handle); 268 void Marshalling(DrawCmdList& cmdList) override; 269 void Playback(Canvas* canvas, const Rect* rect) override; 270 RSB_EXPORT static void RegisterSurfaceBufferCallback(std::function<void(pid_t, uint64_t, uint32_t)> callback); 271 private: 272 void OnDestruct(); 273 void Clear(); 274 void Draw(Canvas* canvas); 275 void DrawWithVulkan(Canvas* canvas); 276 void DrawWithGles(Canvas* canvas); 277 bool CreateEglTextureId(); 278 Drawing::BitmapFormat CreateBitmapFormat(int32_t bufferFormat); 279 mutable DrawingSurfaceBufferInfo surfaceBufferInfo_; 280 281 #if defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK) 282 OHNativeWindowBuffer* nativeWindowBuffer_ = nullptr; 283 #endif 284 #ifdef RS_ENABLE_GL 285 mutable EGLImageKHR eglImage_ = EGL_NO_IMAGE_KHR; 286 mutable GLuint texId_ = 0; 287 #endif 288 }; 289 #endif 290 } 291 } // namespace Rosen 292 } // namespace OHOS 293 #endif // RENDER_SERVICE_CLIENT_CORE_PIPELINE_RS_DRAW_CMD_H 294