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 DRAWING_ENGINE_SAMPLE_H
17 #define DRAWING_ENGINE_SAMPLE_H
18 
19 #include "include/core/SkCanvas.h"
20 #include "include/core/SkPaint.h"
21 
22 #include <surface.h>
23 #include <event_handler.h>
24 #include "hdi_backend.h"
25 #include "hdi_layer_info.h"
26 #include "hdi_output.h"
27 #include "hdi_screen.h"
28 #include "drawing_utils.h"
29 #include "drawing_proxy.h"
30 #include "surface_ohos.h"
31 #include "benchmark.h"
32 
33 namespace OHOS {
34 namespace Rosen {
35 class DrawingEngineSample : public IBufferConsumerListenerClazz {
36 public:
37     DrawingEngineSample() = default;
38     ~DrawingEngineSample();
39     void Run();
40     void SetBenchMark(OHOS::Rosen::BenchMark* benchMark);
41 private:
42     OHOS::Rosen::BenchMark* benchMark_ = nullptr;
43     uint32_t freq_ = 30;
44     int32_t display_w = 0;
45     int32_t display_h = 0;
46     uint32_t currentModeIndex_ = 0;
47     int32_t drawingWidth = 0;
48     int32_t drawingHeight = 0;
49     std::vector<GraphicDisplayModeInfo> displayModeInfos_;
50     std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler;
51     sptr<IConsumerSurface> drawingCSurface;
52     sptr<Surface> drawingPSurface;
53 
54     bool ready_ = false;
55     bool initDeviceFinished_ = false;
56     bool deviceConnected_ = false;
57     bool Initilized = false;
58     std::unique_ptr<HdiScreen> screen_ = nullptr;
59     HdiBackend* backend_ = nullptr;
60     std::shared_ptr<HdiOutput> output_ = nullptr;
61 
62     DrawingProxy* drawingProxy = nullptr;
63     std::shared_ptr<SurfaceBase> surface_ = nullptr;
64 
65     std::unordered_map<uint64_t, sptr<SurfaceBuffer>> prevBufferMap_;
66     std::unordered_map<uint64_t, sptr<SyncFence>> prevFenceMap_;
67     OHOS::sptr<SurfaceBuffer> prevBuffer_;
68 
69     void Init();
70     void InitContext();
71     void OutPutDisplay();
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 DoDraw();
77     void ExcuteBenchMark(Drawing::Canvas* canvas);
78     bool DrawDrawingLayer(std::shared_ptr<HdiLayerInfo> &layer);
79     void CreateDrawingSurface();
80     void OnHotPlugEvent(const std::shared_ptr<HdiOutput> &output, bool connected);
81     static void OnScreenPlug(std::shared_ptr<HdiOutput> &output, bool connected, void* data);
82     static void OnPrepareCompleted(
83         sptr<Surface> &surface, const struct PrepareCompleteParam &param, void* data);
84 };
85 } // namespace Rosen
86 } // namespace OHOS
87 
88 #endif // HELLO_COMPOSER_H