1 /*
2  * Copyright (c) 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 #include "adapter/preview/entrance/ace_view_preview.h"
17 
18 #include "base/log/dump_log.h"
19 #include "base/log/event_report.h"
20 #include "base/log/log.h"
21 #include "base/utils/macros.h"
22 #include "base/utils/system_properties.h"
23 #include "base/utils/utils.h"
24 #include "core/common/ace_engine.h"
25 #include "core/components/theme/theme_manager.h"
26 #include "core/event/mouse_event.h"
27 #include "core/event/touch_event.h"
28 #include "core/image/image_cache.h"
29 
30 namespace OHOS::Ace::Platform {
CreateView(int32_t instanceId,bool useCurrentEventRunner,bool usePlatformThread)31 AceViewPreview* AceViewPreview::CreateView(int32_t instanceId, bool useCurrentEventRunner, bool usePlatformThread)
32 {
33     auto* aceView =
34         new AceViewPreview(instanceId, ThreadModelImpl::CreateThreadModel(useCurrentEventRunner, !usePlatformThread,
35             !SystemProperties::GetRosenBackendEnabled()));
36     if (aceView != nullptr) {
37         aceView->IncRefCount();
38     }
39     return aceView;
40 }
41 
AceViewPreview(int32_t instanceId,std::unique_ptr<ThreadModelImpl> threadModelImpl)42 AceViewPreview::AceViewPreview(int32_t instanceId, std::unique_ptr<ThreadModelImpl> threadModelImpl)
43     : instanceId_(instanceId), threadModelImpl_(std::move(threadModelImpl))
44 {}
45 
NotifySurfaceChanged(int32_t width,int32_t height,WindowSizeChangeReason type,const std::shared_ptr<Rosen::RSTransaction> & rsTransaction)46 void AceViewPreview::NotifySurfaceChanged(int32_t width, int32_t height,
47     WindowSizeChangeReason type, const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)
48 {
49     width_ = width;
50     height_ = height;
51     CHECK_NULL_VOID(viewChangeCallback_);
52     viewChangeCallback_(width, height, type, rsTransaction);
53 }
54 
HandleMouseEvent(const MouseEvent & mouseEvent)55 bool AceViewPreview::HandleMouseEvent(const MouseEvent& mouseEvent)
56 {
57     int32_t eventID = mouseEvent.GetId();
58     auto markProcess = [eventID]() {};
59     mouseEventCallback_(mouseEvent, markProcess, nullptr);
60     return true;
61 }
62 
HandleAxisEvent(const AxisEvent & axisEvent)63 bool AceViewPreview::HandleAxisEvent(const AxisEvent& axisEvent)
64 {
65     int32_t eventID = axisEvent.id;
66     auto markProcess = [eventID]() {};
67     axisEventCallback_(axisEvent, markProcess, nullptr);
68     return true;
69 }
70 
HandleTouchEvent(const TouchEvent & touchEvent)71 bool AceViewPreview::HandleTouchEvent(const TouchEvent& touchEvent)
72 {
73     if (touchEvent.type == TouchType::UNKNOWN) {
74         LOGW("Unknown event.");
75         return false;
76     }
77     CHECK_NULL_RETURN(touchEventCallback_, true);
78     touchEventCallback_(touchEvent, nullptr, nullptr);
79     return true;
80 }
81 
GetDrawDelegate()82 std::unique_ptr<DrawDelegate> AceViewPreview::GetDrawDelegate()
83 {
84     auto drawDelegate = std::make_unique<DrawDelegate>();
85 
86 #ifdef ENABLE_ROSEN_BACKEND
87     drawDelegate->SetDrawRSFrameCallback([](std::shared_ptr<Rosen::RSNode>& node, const Rect& rect) {});
88 #else
89     drawDelegate->SetDrawFrameCallback([this](RefPtr<Flutter::Layer>& layer, const Rect& dirty) {
90         if (!layer) {
91             return;
92         }
93         RefPtr<Flutter::FlutterSceneBuilder> flutterSceneBuilder = AceType::MakeRefPtr<Flutter::FlutterSceneBuilder>();
94         layer->AddToScene(*flutterSceneBuilder, 0.0, 0.0);
95         auto scene_ = flutterSceneBuilder->Build();
96         if (!flutter::UIDartState::Current()) {
97             return;
98         }
99         auto window = flutter::UIDartState::Current()->window();
100         if (window != nullptr && window->client() != nullptr) {
101             window->client()->Render(scene_.get());
102         }
103     });
104 #endif
105 
106     return drawDelegate;
107 }
108 } // namespace OHOS::Ace::Platform
109