1 /*
2  * Copyright (c) 2022 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 DRAWING_SAMPLE_H
17 #define DRAWING_SAMPLE_H
18 
19 #include <iostream>
20 #include <string>
21 
22 #include <event_handler.h>
23 #include <surface.h>
24 
25 #include "hdi_backend.h"
26 #include "hdi_layer_info.h"
27 #include "hdi_output.h"
28 #include "hdi_screen.h"
29 #include "layer_context.h"
30 
31 namespace OHOS {
32 namespace Rosen {
33 namespace Drawing {
34 class DrawingSample {
35 public:
36     using TestFunc = std::function<void(Canvas&, uint32_t, uint32_t)>;
37     DrawingSample() = default;
38     virtual ~DrawingSample() = default;
39 
40     void Init();
41     void Run();
42 
SetDraw(std::map<std::string,std::vector<TestFunc>> testFuncMap)43     inline DrawingSample& SetDraw(std::map<std::string, std::vector<TestFunc>> testFuncMap)
44     {
45         drawFuncMap_.swap(testFuncMap);
46         return *this;
47     }
48 private:
49     uint32_t freq_ = 30;
50     uint32_t currScreenId_ = 0;
51     // key is screenId
52     std::unordered_map<uint32_t, uint32_t> displayWidthsMap_;
53     std::unordered_map<uint32_t, uint32_t> displayHeightsMap_;
54     uint32_t currentModeIndex_ = 0;
55     std::vector<GraphicDisplayModeInfo> displayModeInfos_;
56     // key is screenId
57     std::unordered_map<uint32_t, std::shared_ptr<HdiOutput>> outputMap_;
58     bool ready_ = false;
59     bool initDeviceFinished_ = false;
60     bool deviceConnected_ = false;
61     bool postHotPlugEvent_ = false;
62     HdiBackend* backend_ = nullptr;
63     std::vector<std::unique_ptr<HdiScreen>> screens_;
64     std::shared_ptr<HdiOutput> curOutput_;
65     // key is screenId
66     std::unordered_map<uint32_t, std::unique_ptr<LayerContext>> drawLayersMap_;
67     std::map<std::string, std::vector<TestFunc>> drawFuncMap_;
68     std::shared_ptr<OHOS::AppExecFwk::EventHandler> mainThreadHandler_;
69 
70     void InitLayers(uint32_t screenId);
71     void RequestSync();
72     void CreateShowLayers();
73     void Draw();
74     void Sync(int64_t, void* data);
75     void DoPrepareCompleted(sptr<Surface> surface, const struct PrepareCompleteParam& param);
76     void OnHotPlug(std::shared_ptr<HdiOutput>& output, bool connected);
77     void OnHotPlugEvent(std::shared_ptr<HdiOutput>& output, bool connected);
78     uint32_t CreatePhysicalScreen();
79 
80     static void OnScreenPlug(std::shared_ptr<HdiOutput>& output, bool connected, void* data);
81     static void OnPrepareCompleted(
82         sptr<Surface> &surface, const struct PrepareCompleteParam& param, void* data);
83 };
84 } // namespace Drawing
85 } // namespace Rosen
86 } // namespace OHOS
87 #endif // DRAWING_SAMPLE_H