123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include "adapter/preview/entrance/ace_view_preview.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include "base/log/dump_log.h"
1923b3eb3cSopenharmony_ci#include "base/log/event_report.h"
2023b3eb3cSopenharmony_ci#include "base/log/log.h"
2123b3eb3cSopenharmony_ci#include "base/utils/macros.h"
2223b3eb3cSopenharmony_ci#include "base/utils/system_properties.h"
2323b3eb3cSopenharmony_ci#include "base/utils/utils.h"
2423b3eb3cSopenharmony_ci#include "core/common/ace_engine.h"
2523b3eb3cSopenharmony_ci#include "core/components/theme/theme_manager.h"
2623b3eb3cSopenharmony_ci#include "core/event/mouse_event.h"
2723b3eb3cSopenharmony_ci#include "core/event/touch_event.h"
2823b3eb3cSopenharmony_ci#include "core/image/image_cache.h"
2923b3eb3cSopenharmony_ci
3023b3eb3cSopenharmony_cinamespace OHOS::Ace::Platform {
3123b3eb3cSopenharmony_ciAceViewPreview* AceViewPreview::CreateView(int32_t instanceId, bool useCurrentEventRunner, bool usePlatformThread)
3223b3eb3cSopenharmony_ci{
3323b3eb3cSopenharmony_ci    auto* aceView =
3423b3eb3cSopenharmony_ci        new AceViewPreview(instanceId, ThreadModelImpl::CreateThreadModel(useCurrentEventRunner, !usePlatformThread,
3523b3eb3cSopenharmony_ci            !SystemProperties::GetRosenBackendEnabled()));
3623b3eb3cSopenharmony_ci    if (aceView != nullptr) {
3723b3eb3cSopenharmony_ci        aceView->IncRefCount();
3823b3eb3cSopenharmony_ci    }
3923b3eb3cSopenharmony_ci    return aceView;
4023b3eb3cSopenharmony_ci}
4123b3eb3cSopenharmony_ci
4223b3eb3cSopenharmony_ciAceViewPreview::AceViewPreview(int32_t instanceId, std::unique_ptr<ThreadModelImpl> threadModelImpl)
4323b3eb3cSopenharmony_ci    : instanceId_(instanceId), threadModelImpl_(std::move(threadModelImpl))
4423b3eb3cSopenharmony_ci{}
4523b3eb3cSopenharmony_ci
4623b3eb3cSopenharmony_civoid AceViewPreview::NotifySurfaceChanged(int32_t width, int32_t height,
4723b3eb3cSopenharmony_ci    WindowSizeChangeReason type, const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)
4823b3eb3cSopenharmony_ci{
4923b3eb3cSopenharmony_ci    width_ = width;
5023b3eb3cSopenharmony_ci    height_ = height;
5123b3eb3cSopenharmony_ci    CHECK_NULL_VOID(viewChangeCallback_);
5223b3eb3cSopenharmony_ci    viewChangeCallback_(width, height, type, rsTransaction);
5323b3eb3cSopenharmony_ci}
5423b3eb3cSopenharmony_ci
5523b3eb3cSopenharmony_cibool AceViewPreview::HandleMouseEvent(const MouseEvent& mouseEvent)
5623b3eb3cSopenharmony_ci{
5723b3eb3cSopenharmony_ci    int32_t eventID = mouseEvent.GetId();
5823b3eb3cSopenharmony_ci    auto markProcess = [eventID]() {};
5923b3eb3cSopenharmony_ci    mouseEventCallback_(mouseEvent, markProcess, nullptr);
6023b3eb3cSopenharmony_ci    return true;
6123b3eb3cSopenharmony_ci}
6223b3eb3cSopenharmony_ci
6323b3eb3cSopenharmony_cibool AceViewPreview::HandleAxisEvent(const AxisEvent& axisEvent)
6423b3eb3cSopenharmony_ci{
6523b3eb3cSopenharmony_ci    int32_t eventID = axisEvent.id;
6623b3eb3cSopenharmony_ci    auto markProcess = [eventID]() {};
6723b3eb3cSopenharmony_ci    axisEventCallback_(axisEvent, markProcess, nullptr);
6823b3eb3cSopenharmony_ci    return true;
6923b3eb3cSopenharmony_ci}
7023b3eb3cSopenharmony_ci
7123b3eb3cSopenharmony_cibool AceViewPreview::HandleTouchEvent(const TouchEvent& touchEvent)
7223b3eb3cSopenharmony_ci{
7323b3eb3cSopenharmony_ci    if (touchEvent.type == TouchType::UNKNOWN) {
7423b3eb3cSopenharmony_ci        LOGW("Unknown event.");
7523b3eb3cSopenharmony_ci        return false;
7623b3eb3cSopenharmony_ci    }
7723b3eb3cSopenharmony_ci    CHECK_NULL_RETURN(touchEventCallback_, true);
7823b3eb3cSopenharmony_ci    auto event = touchEvent.UpdatePointers();
7923b3eb3cSopenharmony_ci    touchEventCallback_(event, nullptr, nullptr);
8023b3eb3cSopenharmony_ci    return true;
8123b3eb3cSopenharmony_ci}
8223b3eb3cSopenharmony_ci
8323b3eb3cSopenharmony_cistd::unique_ptr<DrawDelegate> AceViewPreview::GetDrawDelegate()
8423b3eb3cSopenharmony_ci{
8523b3eb3cSopenharmony_ci    auto drawDelegate = std::make_unique<DrawDelegate>();
8623b3eb3cSopenharmony_ci
8723b3eb3cSopenharmony_ci#ifdef ENABLE_ROSEN_BACKEND
8823b3eb3cSopenharmony_ci    drawDelegate->SetDrawRSFrameCallback([](std::shared_ptr<Rosen::RSNode>& node, const Rect& rect) {});
8923b3eb3cSopenharmony_ci#else
9023b3eb3cSopenharmony_ci    drawDelegate->SetDrawFrameCallback([this](RefPtr<Flutter::Layer>& layer, const Rect& dirty) {
9123b3eb3cSopenharmony_ci        if (!layer) {
9223b3eb3cSopenharmony_ci            return;
9323b3eb3cSopenharmony_ci        }
9423b3eb3cSopenharmony_ci        RefPtr<Flutter::FlutterSceneBuilder> flutterSceneBuilder = AceType::MakeRefPtr<Flutter::FlutterSceneBuilder>();
9523b3eb3cSopenharmony_ci        layer->AddToScene(*flutterSceneBuilder, 0.0, 0.0);
9623b3eb3cSopenharmony_ci        auto scene_ = flutterSceneBuilder->Build();
9723b3eb3cSopenharmony_ci        if (!flutter::UIDartState::Current()) {
9823b3eb3cSopenharmony_ci            return;
9923b3eb3cSopenharmony_ci        }
10023b3eb3cSopenharmony_ci        auto window = flutter::UIDartState::Current()->window();
10123b3eb3cSopenharmony_ci        if (window != nullptr && window->client() != nullptr) {
10223b3eb3cSopenharmony_ci            window->client()->Render(scene_.get());
10323b3eb3cSopenharmony_ci        }
10423b3eb3cSopenharmony_ci    });
10523b3eb3cSopenharmony_ci#endif
10623b3eb3cSopenharmony_ci
10723b3eb3cSopenharmony_ci    return drawDelegate;
10823b3eb3cSopenharmony_ci}
10923b3eb3cSopenharmony_ci} // namespace OHOS::Ace::Platform
110