1 /*
2  * Copyright (c) 2021 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_CONTEXT_SAMPLE_H
17 #define RENDER_CONTEXT_SAMPLE_H
18 
19 #include <surface.h>
20 #include <sync_fence.h>
21 
22 #include "hdi_backend.h"
23 #include "hdi_layer_info.h"
24 #include "hdi_output.h"
25 #include "hdi_screen.h"
26 #include "render_context/render_context.h"
27 #include <unordered_map>
28 
29 namespace OHOS {
30 namespace Rosen {
31 
32 class RenderContextSample : public IBufferConsumerListenerClazz {
33 public:
34     RenderContextSample() = default;
35     virtual ~RenderContextSample() = default;
36 
37     void Run();
38 
39 private:
40     uint32_t freq_ = 30;
41     uint32_t display_w = 480;
42     uint32_t display_h = 960;
43     uint32_t currentModeIndex_ = 0;
44     uint32_t backGroundWidth = 0;
45     uint32_t backGroundHeight = 0;
46     uint32_t drawingWidth = 0;
47     uint32_t drawingHeight = 0;
48     std::vector<GraphicDisplayModeInfo> displayModeInfos_;
49 
50     sptr<IConsumerSurface> backGroundCSurface;
51     sptr<Surface> backGroundPSurface;
52     sptr<IConsumerSurface> drawingCSurface;
53     sptr<Surface> drawingPSurface;
54 
55     bool ready_ = false;
56     bool initDeviceFinished_ = false;
57     bool deviceConnected_ = false;
58     std::unique_ptr<HdiScreen> screen_ = nullptr;
59     HdiBackend* backend_ = nullptr;
60     std::shared_ptr<HdiOutput> output_ = nullptr;
61     struct NativeWindow* nwindow = nullptr;
62 
63     RenderContext* renderContext = nullptr;
64     EGLSurface eglSurface = EGL_NO_SURFACE;
65 
66     std::unordered_map<uint64_t, sptr<SurfaceBuffer>> prevBufferMap_;
67     std::unordered_map<uint64_t, sptr<SyncFence>> prevFenceMap_;
68     void Init();
69     void InitEGL();
70     void Draw();
71     void DrawSurface();
72     void Sync(int64_t, void *data);
73     void CreatePhysicalScreen();
74     void DoPrepareCompleted(sptr<Surface> surface, const struct PrepareCompleteParam &param);
75     void OnBufferAvailable() override;
76     SurfaceError ProduceBackGroundBuffer(uint32_t width, uint32_t height);
77     SurfaceError ProduceDrawingBuffer(uint32_t width, uint32_t height);
78     bool FillDrawingLayer(std::shared_ptr<HdiLayerInfo> &showLayer, uint32_t index,
79                           uint32_t zorder, GraphicIRect &dstRect);
80     bool FillBackGroundLayer(std::shared_ptr<HdiLayerInfo> &showLayer, uint32_t zorder, GraphicIRect &dstRect);
81     bool DrawBackgroundLayer(std::shared_ptr<HdiLayerInfo> &layer);
82     bool DrawDrawingLayer(std::shared_ptr<HdiLayerInfo> &layer);
83     void CreateBackGroundSurface();
84     void CreateDrawingSurface();
85     void OnHotPlugEvent(const std::shared_ptr<HdiOutput> &output, bool connected);
86 
87     static void OnScreenPlug(std::shared_ptr<HdiOutput> &output, bool connected, void* data);
88     static void OnPrepareCompleted(
89         sptr<Surface> &surface, const struct PrepareCompleteParam &param, void* data);
90 };
91 } // namespace Rosen
92 } // namespace OHOS
93 
94 #endif // HELLO_COMPOSER_H
95