1 /*
2  * Copyright (c) 2024 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 RS_SURFACE_CAPTURE_TASK_PARALLEL
17 #define RS_SURFACE_CAPTURE_TASK_PARALLEL
18 #define EGL_EGLEXT_PROTOTYPES
19 #define GL_GLEXT_PROTOTYPES
20 
21 #include "drawable/rs_render_node_drawable.h"
22 #include "pipeline/rs_surface_capture_task.h"
23 #include "pixel_map.h"
24 #include "system/rs_system_parameters.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 #if defined(ROSEN_OHOS) && defined(RS_ENABLE_VK)
29 class DmaMem {
30 public:
31     DmaMem() = default;
32     ~DmaMem();
33     sptr<SurfaceBuffer> DmaMemAlloc(Drawing::ImageInfo &dstInfo, const std::unique_ptr<Media::PixelMap>& pixelMap);
34     std::shared_ptr<Drawing::Surface> GetSurfaceFromSurfaceBuffer(sptr<SurfaceBuffer> surfaceBuffer,
35         std::shared_ptr<Drawing::GPUContext> gpuContext);
36     void ReleaseDmaMemory();
37 private:
38     OHNativeWindowBuffer* nativeWindowBuffer_ = nullptr;
39 };
40 #endif
41 
42 class RSSurfaceCaptureTaskParallel {
43 public:
RSSurfaceCaptureTaskParallel(NodeId nodeId,const RSSurfaceCaptureConfig & captureConfig)44     explicit RSSurfaceCaptureTaskParallel(NodeId nodeId, const RSSurfaceCaptureConfig& captureConfig)
45         : nodeId_(nodeId), captureConfig_(captureConfig) {}
46     ~RSSurfaceCaptureTaskParallel() = default;
47 
48     // Confirm whether the node is occlusive which should apply modifiers
49     static void CheckModifiers(NodeId id, bool useCurWindow);
50     // Do capture pipeline task
51     static void Capture(NodeId id, sptr<RSISurfaceCaptureCallback> callback,
52         const RSSurfaceCaptureConfig& captureConfig, bool isSystemCalling, bool isFreeze = false);
53 
54 #ifdef RS_ENABLE_UNI_RENDER
55     static std::function<void()> CreateSurfaceSyncCopyTask(std::shared_ptr<Drawing::Surface> surface,
56         std::unique_ptr<Media::PixelMap> pixelMap, NodeId id, sptr<RSISurfaceCaptureCallback> callback,
57         int32_t rotation = 0, bool useDma = false);
58 #endif
59 
60     bool CreateResources();
61 
62     bool Run(sptr<RSISurfaceCaptureCallback> callback, bool isSystemCalling, bool isFreeze = false);
63 
64     static void ClearCacheImageByFreeze(NodeId id);
65 
66 private:
67     std::shared_ptr<Drawing::Surface> CreateSurface(const std::unique_ptr<Media::PixelMap>& pixelmap);
68 
69     std::unique_ptr<Media::PixelMap> CreatePixelMapBySurfaceNode(std::shared_ptr<RSSurfaceRenderNode> node);
70 
71     std::unique_ptr<Media::PixelMap> CreatePixelMapByDisplayNode(std::shared_ptr<RSDisplayRenderNode> node);
72 
73     void SetupGpuContext();
74 
75     int32_t CalPixelMapRotation();
76 
77     std::unique_ptr<Media::PixelMap> pixelMap_ = nullptr;
78     std::shared_ptr<DrawableV2::RSRenderNodeDrawable> surfaceNodeDrawable_ = nullptr;
79     std::shared_ptr<DrawableV2::RSRenderNodeDrawable> displayNodeDrawable_ = nullptr;
80     NodeId nodeId_;
81     RSSurfaceCaptureConfig captureConfig_;
82     ScreenRotation screenCorrection_ = ScreenRotation::ROTATION_0;
83     ScreenRotation screenRotation_ = ScreenRotation::ROTATION_0;
84     int32_t finalRotationAngle_ = RS_ROTATION_0;
85     std::shared_ptr<Drawing::ColorSpace> colorSpace_ = nullptr;
86 
87     // only used for RSUniRenderThread
88     std::shared_ptr<Drawing::GPUContext> gpuContext_ = nullptr;
89 };
90 
91 } // namespace Rosen
92 } // namespace OHOS
93 
94 #endif // RS_SURFACE_CAPTURE_TASK_PARALLEL
95