162b8cbc9Sopenharmony_ci/*
262b8cbc9Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
362b8cbc9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
462b8cbc9Sopenharmony_ci * you may not use this file except in compliance with the License.
562b8cbc9Sopenharmony_ci * You may obtain a copy of the License at
662b8cbc9Sopenharmony_ci *
762b8cbc9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
862b8cbc9Sopenharmony_ci *
962b8cbc9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1062b8cbc9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1162b8cbc9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1262b8cbc9Sopenharmony_ci * See the License for the specific language governing permissions and
1362b8cbc9Sopenharmony_ci * limitations under the License.
1462b8cbc9Sopenharmony_ci */
1562b8cbc9Sopenharmony_ci
1662b8cbc9Sopenharmony_ci#include "lite_wm.h"
1762b8cbc9Sopenharmony_ci
1862b8cbc9Sopenharmony_ci#include "gfx_utils/color.h"
1962b8cbc9Sopenharmony_ci#include "gfx_utils/graphic_log.h"
2062b8cbc9Sopenharmony_ci#include "gfx_utils/pixel_format_utils.h"
2162b8cbc9Sopenharmony_ci
2262b8cbc9Sopenharmony_cinamespace OHOS {
2362b8cbc9Sopenharmony_cinamespace {
2462b8cbc9Sopenharmony_ci#define EXPAND_RECT(x) x.GetLeft(), x.GetTop(), x.GetRight(), x.GetBottom()
2562b8cbc9Sopenharmony_ciconst uint16_t CURSOR_WIDTH = 24;
2662b8cbc9Sopenharmony_ciconst uint16_t CURSOR_HEIGHT = 25;
2762b8cbc9Sopenharmony_ciconst uint8_t CURSOR_MAP[] = {
2862b8cbc9Sopenharmony_ci    /* Pixel format: ARGB1555 */
2962b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3062b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3162b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
3262b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3362b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3462b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3562b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3662b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80,
3762b8cbc9Sopenharmony_ci    0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3862b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3962b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00,
4062b8cbc9Sopenharmony_ci    0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4162b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4262b8cbc9Sopenharmony_ci    0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80,
4362b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4462b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4562b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00,
4662b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4762b8cbc9Sopenharmony_ci    0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4862b8cbc9Sopenharmony_ci    0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4962b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00,
5062b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80,
5162b8cbc9Sopenharmony_ci    0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5262b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5362b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
5462b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00,
5562b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5662b8cbc9Sopenharmony_ci    0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5762b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5862b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
5962b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
6062b8cbc9Sopenharmony_ci    0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80,
6162b8cbc9Sopenharmony_ci    0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6262b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6362b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6462b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6562b8cbc9Sopenharmony_ci    0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
6662b8cbc9Sopenharmony_ci    0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6762b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
6862b8cbc9Sopenharmony_ci    0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00,
6962b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7062b8cbc9Sopenharmony_ci    0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80,
7162b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7262b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80,
7362b8cbc9Sopenharmony_ci    0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
7462b8cbc9Sopenharmony_ci    0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7562b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7662b8cbc9Sopenharmony_ci    0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
7762b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00,
7862b8cbc9Sopenharmony_ci    0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
7962b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8062b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8162b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
8262b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8362b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00,
8462b8cbc9Sopenharmony_ci    0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8562b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8662b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
8762b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8862b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8962b8cbc9Sopenharmony_ci    0x00, 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9062b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9162b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9262b8cbc9Sopenharmony_ci    0x00, 0x00, 0x00,
9362b8cbc9Sopenharmony_ci};
9462b8cbc9Sopenharmony_ci
9562b8cbc9Sopenharmony_ciconst uint8_t MAX_WINDOW_NUMBLE = 32;
9662b8cbc9Sopenharmony_ciconst uint32_t WINDOW_ID_FULL_STORAGE = 0xFFFFFFFF;
9762b8cbc9Sopenharmony_ci}
9862b8cbc9Sopenharmony_ci
9962b8cbc9Sopenharmony_ciLiteWM::LiteWM()
10062b8cbc9Sopenharmony_ci    : updates_({}), layerData_(nullptr), cursorInfo_({}), mousePosition_({}), deviceData_({}),
10162b8cbc9Sopenharmony_ci      needScreenshot_(false), screenshotSurface_(nullptr), winIdStorage(0)
10262b8cbc9Sopenharmony_ci{
10362b8cbc9Sopenharmony_ci    InitMutex(stackLock_, PTHREAD_MUTEX_RECURSIVE);
10462b8cbc9Sopenharmony_ci    pthread_mutex_init(&mouseLock_, nullptr);
10562b8cbc9Sopenharmony_ci    pthread_mutex_init(&eventLock_, nullptr);
10662b8cbc9Sopenharmony_ci    pthread_mutex_init(&screenshotMutex_, nullptr);
10762b8cbc9Sopenharmony_ci
10862b8cbc9Sopenharmony_ci    InputManagerService::GetInstance()->GetDistributer()->AddRawEventListener(this);
10962b8cbc9Sopenharmony_ci    InitMouseCursor();
11062b8cbc9Sopenharmony_ci
11162b8cbc9Sopenharmony_ci    layerData_ = GetDevSurfaceData();
11262b8cbc9Sopenharmony_ci    if (layerData_ != nullptr) {
11362b8cbc9Sopenharmony_ci        if (layerData_->virAddr == nullptr) {
11462b8cbc9Sopenharmony_ci            GRAPHIC_LOGE("LayerInfo addr is null!");
11562b8cbc9Sopenharmony_ci        }
11662b8cbc9Sopenharmony_ci        GRAPHIC_LOGI("LayerInfo, width=%d, height=%d, stride=%d",
11762b8cbc9Sopenharmony_ci            layerData_->width, layerData_->height, layerData_->stride);
11862b8cbc9Sopenharmony_ci    } else {
11962b8cbc9Sopenharmony_ci        GRAPHIC_LOGE("LayerInfo is null!");
12062b8cbc9Sopenharmony_ci    }
12162b8cbc9Sopenharmony_ci}
12262b8cbc9Sopenharmony_ci
12362b8cbc9Sopenharmony_ciLiteWM::~LiteWM()
12462b8cbc9Sopenharmony_ci{
12562b8cbc9Sopenharmony_ci}
12662b8cbc9Sopenharmony_ci
12762b8cbc9Sopenharmony_ciLiteWM* LiteWM::GetInstance()
12862b8cbc9Sopenharmony_ci{
12962b8cbc9Sopenharmony_ci    static LiteWM liteWm;
13062b8cbc9Sopenharmony_ci    return &liteWm;
13162b8cbc9Sopenharmony_ci}
13262b8cbc9Sopenharmony_ci
13362b8cbc9Sopenharmony_civoid LiteWM::MainTaskHandler()
13462b8cbc9Sopenharmony_ci{
13562b8cbc9Sopenharmony_ci    if (layerData_ == nullptr || layerData_->virAddr == nullptr) {
13662b8cbc9Sopenharmony_ci        return;
13762b8cbc9Sopenharmony_ci    }
13862b8cbc9Sopenharmony_ci
13962b8cbc9Sopenharmony_ci    if (needScreenshot_) {
14062b8cbc9Sopenharmony_ci        Screenshot();
14162b8cbc9Sopenharmony_ci        needScreenshot_ = false;
14262b8cbc9Sopenharmony_ci    }
14362b8cbc9Sopenharmony_ci
14462b8cbc9Sopenharmony_ci    if (cursorInfo_.enableCursor) {
14562b8cbc9Sopenharmony_ci        UpdateMouseCursor();
14662b8cbc9Sopenharmony_ci    }
14762b8cbc9Sopenharmony_ci
14862b8cbc9Sopenharmony_ci    ProcessUpdates();
14962b8cbc9Sopenharmony_ci}
15062b8cbc9Sopenharmony_ci
15162b8cbc9Sopenharmony_civoid LiteWM::UpdateMouseCursor()
15262b8cbc9Sopenharmony_ci{
15362b8cbc9Sopenharmony_ci    Rect& rect = cursorInfo_.rect;
15462b8cbc9Sopenharmony_ci    Point point = GetMousePosition();
15562b8cbc9Sopenharmony_ci    if (rect.GetLeft() == point.x && rect.GetTop() == point.y) {
15662b8cbc9Sopenharmony_ci        return;
15762b8cbc9Sopenharmony_ci    }
15862b8cbc9Sopenharmony_ci    cursorInfo_.needRedraw = true;
15962b8cbc9Sopenharmony_ci    AddUpdateRegion(rect);
16062b8cbc9Sopenharmony_ci    rect.SetPosition(point.x, point.y);
16162b8cbc9Sopenharmony_ci}
16262b8cbc9Sopenharmony_ci
16362b8cbc9Sopenharmony_civoid LiteWM::GetLayerInfo(LiteLayerInfo& layerInfo)
16462b8cbc9Sopenharmony_ci{
16562b8cbc9Sopenharmony_ci    if (layerData_ == nullptr) {
16662b8cbc9Sopenharmony_ci        return;
16762b8cbc9Sopenharmony_ci    }
16862b8cbc9Sopenharmony_ci    layerInfo.pixelFormat = layerData_->pixelFormat;
16962b8cbc9Sopenharmony_ci    layerInfo.width = layerData_->width;
17062b8cbc9Sopenharmony_ci    layerInfo.height = layerData_->height;
17162b8cbc9Sopenharmony_ci}
17262b8cbc9Sopenharmony_ci
17362b8cbc9Sopenharmony_ciSurface* LiteWM::GetSurface(int32_t id)
17462b8cbc9Sopenharmony_ci{
17562b8cbc9Sopenharmony_ci    LiteWindow* window = GetWindowById(id);
17662b8cbc9Sopenharmony_ci    if (window != nullptr) {
17762b8cbc9Sopenharmony_ci        return window->GetSurface();
17862b8cbc9Sopenharmony_ci    }
17962b8cbc9Sopenharmony_ci    return nullptr;
18062b8cbc9Sopenharmony_ci}
18162b8cbc9Sopenharmony_ci
18262b8cbc9Sopenharmony_civoid LiteWM::Show(int32_t id)
18362b8cbc9Sopenharmony_ci{
18462b8cbc9Sopenharmony_ci    GraphicLocker lock(stackLock_);
18562b8cbc9Sopenharmony_ci    LiteWindow* window = GetWindowById(id);
18662b8cbc9Sopenharmony_ci    if (window != nullptr) {
18762b8cbc9Sopenharmony_ci        window->SetIsShow(true);
18862b8cbc9Sopenharmony_ci        UpdateWindowRegion(window, window->config_.rect);
18962b8cbc9Sopenharmony_ci    }
19062b8cbc9Sopenharmony_ci}
19162b8cbc9Sopenharmony_ci
19262b8cbc9Sopenharmony_civoid LiteWM::Hide(int32_t id)
19362b8cbc9Sopenharmony_ci{
19462b8cbc9Sopenharmony_ci    GraphicLocker lock(stackLock_);
19562b8cbc9Sopenharmony_ci    LiteWindow* window = GetWindowById(id);
19662b8cbc9Sopenharmony_ci    if (window != nullptr) {
19762b8cbc9Sopenharmony_ci        window->SetIsShow(false);
19862b8cbc9Sopenharmony_ci        UpdateWindowRegion(window, window->config_.rect);
19962b8cbc9Sopenharmony_ci    }
20062b8cbc9Sopenharmony_ci}
20162b8cbc9Sopenharmony_ci
20262b8cbc9Sopenharmony_civoid LiteWM::RaiseToTop(int32_t id)
20362b8cbc9Sopenharmony_ci{
20462b8cbc9Sopenharmony_ci    GraphicLocker lock(stackLock_);
20562b8cbc9Sopenharmony_ci    auto node = GetWindowNodeById(id);
20662b8cbc9Sopenharmony_ci    if (node != nullptr) {
20762b8cbc9Sopenharmony_ci        node->prev_->next_ = node->next_;
20862b8cbc9Sopenharmony_ci        node->next_->prev_ = node->prev_;
20962b8cbc9Sopenharmony_ci        auto head = winList_.Begin()->prev_;
21062b8cbc9Sopenharmony_ci        node->next_ = head->next_;
21162b8cbc9Sopenharmony_ci        node->prev_ = head;
21262b8cbc9Sopenharmony_ci        head->next_->prev_ = node;
21362b8cbc9Sopenharmony_ci        head->next_ = node;
21462b8cbc9Sopenharmony_ci
21562b8cbc9Sopenharmony_ci        if (node->data_ != nullptr) {
21662b8cbc9Sopenharmony_ci            UpdateWindowRegion(winList_.Begin()->data_, node->data_->config_.rect);
21762b8cbc9Sopenharmony_ci        }
21862b8cbc9Sopenharmony_ci    }
21962b8cbc9Sopenharmony_ci}
22062b8cbc9Sopenharmony_ci
22162b8cbc9Sopenharmony_civoid LiteWM::LowerToBottom(int32_t id)
22262b8cbc9Sopenharmony_ci{
22362b8cbc9Sopenharmony_ci    GraphicLocker lock(stackLock_);
22462b8cbc9Sopenharmony_ci    auto node = GetWindowNodeById(id);
22562b8cbc9Sopenharmony_ci    if (node != nullptr) {
22662b8cbc9Sopenharmony_ci        node->prev_->next_ = node->next_;
22762b8cbc9Sopenharmony_ci        node->next_->prev_ = node->prev_;
22862b8cbc9Sopenharmony_ci        auto head = winList_.Begin()->prev_;
22962b8cbc9Sopenharmony_ci        node->prev_ = head->prev_;
23062b8cbc9Sopenharmony_ci        node->next_ = head;
23162b8cbc9Sopenharmony_ci        head->prev_->next_ = node;
23262b8cbc9Sopenharmony_ci        head->prev_ = node;
23362b8cbc9Sopenharmony_ci
23462b8cbc9Sopenharmony_ci        if (node->data_ != nullptr) {
23562b8cbc9Sopenharmony_ci            UpdateWindowRegion(winList_.Begin()->data_, node->data_->config_.rect);
23662b8cbc9Sopenharmony_ci        }
23762b8cbc9Sopenharmony_ci    }
23862b8cbc9Sopenharmony_ci}
23962b8cbc9Sopenharmony_ci
24062b8cbc9Sopenharmony_civoid LiteWM::MoveTo(int32_t id, int16_t x, int16_t y)
24162b8cbc9Sopenharmony_ci{
24262b8cbc9Sopenharmony_ci    GraphicLocker lock(stackLock_);
24362b8cbc9Sopenharmony_ci    LiteWindow* window = GetWindowById(id);
24462b8cbc9Sopenharmony_ci    if (window != nullptr) {
24562b8cbc9Sopenharmony_ci        window->MoveTo(x, y);
24662b8cbc9Sopenharmony_ci    }
24762b8cbc9Sopenharmony_ci}
24862b8cbc9Sopenharmony_ci
24962b8cbc9Sopenharmony_civoid LiteWM::Resize(int32_t id, int16_t width, int16_t height)
25062b8cbc9Sopenharmony_ci{
25162b8cbc9Sopenharmony_ci    GraphicLocker lock(stackLock_);
25262b8cbc9Sopenharmony_ci    LiteWindow* window = GetWindowById(id);
25362b8cbc9Sopenharmony_ci    if (window == nullptr) {
25462b8cbc9Sopenharmony_ci        return;
25562b8cbc9Sopenharmony_ci    }
25662b8cbc9Sopenharmony_ci    Rect rectBefore = window->config_.rect;
25762b8cbc9Sopenharmony_ci    window->Resize(width, height);
25862b8cbc9Sopenharmony_ci    Rect rectAfter = window->config_.rect;
25962b8cbc9Sopenharmony_ci    Rect mask;
26062b8cbc9Sopenharmony_ci    if (mask.Intersect(rectBefore, rectAfter)) {
26162b8cbc9Sopenharmony_ci        int16_t x1 = mask.GetLeft();
26262b8cbc9Sopenharmony_ci        int16_t x2 = mask.GetRight();
26362b8cbc9Sopenharmony_ci        int16_t y1 = mask.GetTop();
26462b8cbc9Sopenharmony_ci        int16_t y2 = mask.GetBottom();
26562b8cbc9Sopenharmony_ci        if (x2 != rectBefore.GetRight()) {
26662b8cbc9Sopenharmony_ci            UpdateWindowRegion(window, {static_cast<int16_t>(x2 + 1), y1, rectBefore.GetRight(),
26762b8cbc9Sopenharmony_ci                rectBefore.GetBottom()});
26862b8cbc9Sopenharmony_ci        }
26962b8cbc9Sopenharmony_ci        if (y2 != rectBefore.GetBottom()) {
27062b8cbc9Sopenharmony_ci            UpdateWindowRegion(window, {x1, static_cast<int16_t>(y2 + 1), x2, rectBefore.GetBottom()});
27162b8cbc9Sopenharmony_ci        }
27262b8cbc9Sopenharmony_ci    }
27362b8cbc9Sopenharmony_ci}
27462b8cbc9Sopenharmony_ci
27562b8cbc9Sopenharmony_civoid LiteWM::UpdateWindow(int32_t id)
27662b8cbc9Sopenharmony_ci{
27762b8cbc9Sopenharmony_ci    GRAPHIC_LOGI("UpdateWindow, id=%d", id);
27862b8cbc9Sopenharmony_ci    LiteWindow* window = GetWindowById(id);
27962b8cbc9Sopenharmony_ci    if (window != nullptr) {
28062b8cbc9Sopenharmony_ci        UpdateWindowRegion(window, window->config_.rect);
28162b8cbc9Sopenharmony_ci    }
28262b8cbc9Sopenharmony_ci}
28362b8cbc9Sopenharmony_ci
28462b8cbc9Sopenharmony_ciLiteWindow* LiteWM::GetWindowById(int32_t id)
28562b8cbc9Sopenharmony_ci{
28662b8cbc9Sopenharmony_ci    if (id == INVALID_WINDOW_ID) {
28762b8cbc9Sopenharmony_ci        return nullptr;
28862b8cbc9Sopenharmony_ci    }
28962b8cbc9Sopenharmony_ci
29062b8cbc9Sopenharmony_ci    LiteWindow* ret = nullptr;
29162b8cbc9Sopenharmony_ci    auto node = winList_.Begin();
29262b8cbc9Sopenharmony_ci    while (node != winList_.End()) {
29362b8cbc9Sopenharmony_ci        if (node->data_->id_ == id) {
29462b8cbc9Sopenharmony_ci            ret = node->data_;
29562b8cbc9Sopenharmony_ci            break;
29662b8cbc9Sopenharmony_ci        }
29762b8cbc9Sopenharmony_ci        node = node->next_;
29862b8cbc9Sopenharmony_ci    }
29962b8cbc9Sopenharmony_ci    return ret;
30062b8cbc9Sopenharmony_ci}
30162b8cbc9Sopenharmony_ci
30262b8cbc9Sopenharmony_ciListNode<LiteWindow*>* LiteWM::GetWindowNodeById(int32_t id)
30362b8cbc9Sopenharmony_ci{
30462b8cbc9Sopenharmony_ci    if (id == INVALID_WINDOW_ID) {
30562b8cbc9Sopenharmony_ci        return nullptr;
30662b8cbc9Sopenharmony_ci    }
30762b8cbc9Sopenharmony_ci
30862b8cbc9Sopenharmony_ci    ListNode<LiteWindow*>* ret = nullptr;
30962b8cbc9Sopenharmony_ci    auto node = winList_.Begin();
31062b8cbc9Sopenharmony_ci    while (node != winList_.End()) {
31162b8cbc9Sopenharmony_ci        if (node->data_->id_ == id) {
31262b8cbc9Sopenharmony_ci            ret = node;
31362b8cbc9Sopenharmony_ci            break;
31462b8cbc9Sopenharmony_ci        }
31562b8cbc9Sopenharmony_ci        node = node->next_;
31662b8cbc9Sopenharmony_ci    }
31762b8cbc9Sopenharmony_ci    return ret;
31862b8cbc9Sopenharmony_ci}
31962b8cbc9Sopenharmony_ci
32062b8cbc9Sopenharmony_civoid LiteWM::InitMouseCursor()
32162b8cbc9Sopenharmony_ci{
32262b8cbc9Sopenharmony_ci    GRAPHIC_LOGI("InitMouseCursor");
32362b8cbc9Sopenharmony_ci    cursorInfo_.rect.SetRect(0, 0, CURSOR_WIDTH - 1, CURSOR_HEIGHT - 1);
32462b8cbc9Sopenharmony_ci    cursorInfo_.needRedraw = false;
32562b8cbc9Sopenharmony_ci    cursorInfo_.enableCursor = false;
32662b8cbc9Sopenharmony_ci}
32762b8cbc9Sopenharmony_ci
32862b8cbc9Sopenharmony_ciLiteWindow* LiteWM::CreateWindow(const LiteWinConfig& config, pid_t pid)
32962b8cbc9Sopenharmony_ci{
33062b8cbc9Sopenharmony_ci    GraphicLocker lock(stackLock_);
33162b8cbc9Sopenharmony_ci    if (!CheckWinIdIsAvailable()) {
33262b8cbc9Sopenharmony_ci        return nullptr;
33362b8cbc9Sopenharmony_ci    }
33462b8cbc9Sopenharmony_ci    LiteWindow* window = new LiteWindow(config);
33562b8cbc9Sopenharmony_ci    if (window == nullptr) {
33662b8cbc9Sopenharmony_ci        return nullptr;
33762b8cbc9Sopenharmony_ci    }
33862b8cbc9Sopenharmony_ci    if (!window->CreateSurface()) {
33962b8cbc9Sopenharmony_ci        delete window;
34062b8cbc9Sopenharmony_ci        return nullptr;
34162b8cbc9Sopenharmony_ci    }
34262b8cbc9Sopenharmony_ci    window->SetPid(pid);
34362b8cbc9Sopenharmony_ci    winList_.PushFront(window);
34462b8cbc9Sopenharmony_ci    return window;
34562b8cbc9Sopenharmony_ci}
34662b8cbc9Sopenharmony_ci
34762b8cbc9Sopenharmony_civoid LiteWM::RemoveWindow(int32_t id)
34862b8cbc9Sopenharmony_ci{
34962b8cbc9Sopenharmony_ci    GraphicLocker lock(stackLock_);
35062b8cbc9Sopenharmony_ci    auto node = GetWindowNodeById(id);
35162b8cbc9Sopenharmony_ci    if (node == nullptr) {
35262b8cbc9Sopenharmony_ci        return;
35362b8cbc9Sopenharmony_ci    }
35462b8cbc9Sopenharmony_ci    LiteWindow* window = node->data_;
35562b8cbc9Sopenharmony_ci    winList_.Remove(node);
35662b8cbc9Sopenharmony_ci    if (window != nullptr) {
35762b8cbc9Sopenharmony_ci        AddUpdateRegion(window->config_.rect);
35862b8cbc9Sopenharmony_ci        delete window;
35962b8cbc9Sopenharmony_ci    }
36062b8cbc9Sopenharmony_ci}
36162b8cbc9Sopenharmony_ci
36262b8cbc9Sopenharmony_cibool LiteWM::CheckWinIdIsAvailable()
36362b8cbc9Sopenharmony_ci{
36462b8cbc9Sopenharmony_ci    if (winIdStorage == WINDOW_ID_FULL_STORAGE) {
36562b8cbc9Sopenharmony_ci        GRAPHIC_LOGE("reach max window num!");
36662b8cbc9Sopenharmony_ci        return false;
36762b8cbc9Sopenharmony_ci    }
36862b8cbc9Sopenharmony_ci    return true;
36962b8cbc9Sopenharmony_ci}
37062b8cbc9Sopenharmony_ci
37162b8cbc9Sopenharmony_ciint32_t LiteWM::GetUniqueWinId()
37262b8cbc9Sopenharmony_ci{
37362b8cbc9Sopenharmony_ci    static uint8_t winId = 0;
37462b8cbc9Sopenharmony_ci    if (!CheckWinIdIsAvailable()) {
37562b8cbc9Sopenharmony_ci        return INVALID_WINDOW_ID;
37662b8cbc9Sopenharmony_ci    }
37762b8cbc9Sopenharmony_ci    while (winIdStorage & (1 << winId)) {
37862b8cbc9Sopenharmony_ci        winId++;
37962b8cbc9Sopenharmony_ci        winId %= MAX_WINDOW_NUMBLE;
38062b8cbc9Sopenharmony_ci    }
38162b8cbc9Sopenharmony_ci    winIdStorage |= (1 << winId);
38262b8cbc9Sopenharmony_ci    return winId;
38362b8cbc9Sopenharmony_ci}
38462b8cbc9Sopenharmony_ci
38562b8cbc9Sopenharmony_civoid LiteWM::RecycleWinId(int32_t id)
38662b8cbc9Sopenharmony_ci{
38762b8cbc9Sopenharmony_ci    if (id == INVALID_WINDOW_ID) {
38862b8cbc9Sopenharmony_ci        return;
38962b8cbc9Sopenharmony_ci    }
39062b8cbc9Sopenharmony_ci    winIdStorage &= (~(1 << static_cast<uint32_t>(id)));
39162b8cbc9Sopenharmony_ci}
39262b8cbc9Sopenharmony_ci
39362b8cbc9Sopenharmony_civoid LiteWM::InitMutex(pthread_mutex_t& mutex, int type)
39462b8cbc9Sopenharmony_ci{
39562b8cbc9Sopenharmony_ci    pthread_mutexattr_t attr;
39662b8cbc9Sopenharmony_ci    pthread_mutexattr_init(&attr);
39762b8cbc9Sopenharmony_ci    pthread_mutexattr_settype(&attr, type);
39862b8cbc9Sopenharmony_ci    pthread_mutex_init(&mutex, &attr);
39962b8cbc9Sopenharmony_ci    pthread_mutexattr_destroy(&attr);
40062b8cbc9Sopenharmony_ci}
40162b8cbc9Sopenharmony_ci
40262b8cbc9Sopenharmony_civoid LiteWM::UpdateWindowRegion(const LiteWindow* window, const Rect& rect)
40362b8cbc9Sopenharmony_ci{
40462b8cbc9Sopenharmony_ci    ListNode<LiteWindow *>* winNode = winList_.Tail();
40562b8cbc9Sopenharmony_ci    while (winNode != winList_.End()) {
40662b8cbc9Sopenharmony_ci        if (winNode->data_ == window) {
40762b8cbc9Sopenharmony_ci            CalculateUpdateRegion(winNode->prev_, EXPAND_RECT(rect));
40862b8cbc9Sopenharmony_ci            return;
40962b8cbc9Sopenharmony_ci        }
41062b8cbc9Sopenharmony_ci        winNode = winNode->prev_;
41162b8cbc9Sopenharmony_ci    }
41262b8cbc9Sopenharmony_ci    AddUpdateRegion(rect);
41362b8cbc9Sopenharmony_ci}
41462b8cbc9Sopenharmony_ci
41562b8cbc9Sopenharmony_civoid LiteWM::CalculateUpdateRegion(const ListNode<LiteWindow*>* winNode, int16_t x1, int16_t y1, int16_t x2, int16_t y2)
41662b8cbc9Sopenharmony_ci{
41762b8cbc9Sopenharmony_ci    Rect rect(x1, y1, x2, y2);
41862b8cbc9Sopenharmony_ci    if (winNode == winList_.End()) {
41962b8cbc9Sopenharmony_ci        AddUpdateRegion(rect);
42062b8cbc9Sopenharmony_ci        return;
42162b8cbc9Sopenharmony_ci    }
42262b8cbc9Sopenharmony_ci    if (winNode == nullptr) {
42362b8cbc9Sopenharmony_ci        return;
42462b8cbc9Sopenharmony_ci    }
42562b8cbc9Sopenharmony_ci    LiteWindow* window = winNode->data_;
42662b8cbc9Sopenharmony_ci    if ((window != nullptr) && window->isShow_ && window->IsCoverMode()) {
42762b8cbc9Sopenharmony_ci        Rect& winRect = window->config_.rect;
42862b8cbc9Sopenharmony_ci        Rect mask;
42962b8cbc9Sopenharmony_ci        GRAPHIC_LOGD("winRect={%d,%d,%d,%d}, rect={%d,%d,%d,%d}", EXPAND_RECT(winRect), EXPAND_RECT(rect));
43062b8cbc9Sopenharmony_ci        if (mask.Intersect(winRect, rect)) {
43162b8cbc9Sopenharmony_ci            if (x1 != mask.GetLeft()) {
43262b8cbc9Sopenharmony_ci                CalculateUpdateRegion(winNode->prev_, x1, y1, mask.GetLeft() - 1, y2);
43362b8cbc9Sopenharmony_ci            }
43462b8cbc9Sopenharmony_ci
43562b8cbc9Sopenharmony_ci            if (y1 != mask.GetTop()) {
43662b8cbc9Sopenharmony_ci                CalculateUpdateRegion(winNode->prev_, mask.GetLeft(), y1, x2, mask.GetTop() - 1);
43762b8cbc9Sopenharmony_ci            }
43862b8cbc9Sopenharmony_ci
43962b8cbc9Sopenharmony_ci            if (x2 != mask.GetRight()) {
44062b8cbc9Sopenharmony_ci                CalculateUpdateRegion(winNode->prev_, mask.GetRight() + 1, mask.GetTop(), x2, y2);
44162b8cbc9Sopenharmony_ci            }
44262b8cbc9Sopenharmony_ci
44362b8cbc9Sopenharmony_ci            if (y2 != mask.GetBottom()) {
44462b8cbc9Sopenharmony_ci                CalculateUpdateRegion(winNode->prev_, mask.GetLeft(), mask.GetBottom() + 1, mask.GetRight(), y2);
44562b8cbc9Sopenharmony_ci            }
44662b8cbc9Sopenharmony_ci            return;
44762b8cbc9Sopenharmony_ci        }
44862b8cbc9Sopenharmony_ci    }
44962b8cbc9Sopenharmony_ci    CalculateUpdateRegion(winNode->prev_, x1, y1, x2, y2);
45062b8cbc9Sopenharmony_ci}
45162b8cbc9Sopenharmony_ci
45262b8cbc9Sopenharmony_civoid LiteWM::AddUpdateRegion(const Rect& rect)
45362b8cbc9Sopenharmony_ci{
45462b8cbc9Sopenharmony_ci    GraphicLocker lock(stackLock_);
45562b8cbc9Sopenharmony_ci    GRAPHIC_LOGD("AddUpdateRegion, rect={%d,%d,%d,%d}", EXPAND_RECT(rect));
45662b8cbc9Sopenharmony_ci    if (updates_.num == 0) {
45762b8cbc9Sopenharmony_ci        updates_.updates[updates_.num++] = rect;
45862b8cbc9Sopenharmony_ci        updates_.bound = rect;
45962b8cbc9Sopenharmony_ci    } else {
46062b8cbc9Sopenharmony_ci        for (int i = 0; i < updates_.num; i++) {
46162b8cbc9Sopenharmony_ci            Rect& updateRect = updates_.updates[i];
46262b8cbc9Sopenharmony_ci            if (updateRect.IsIntersect(rect) || updateRect.IsExtends(rect)) {
46362b8cbc9Sopenharmony_ci                updateRect.Join(updateRect, rect);
46462b8cbc9Sopenharmony_ci                updates_.bound.Join(updates_.bound, rect);
46562b8cbc9Sopenharmony_ci                return;
46662b8cbc9Sopenharmony_ci            }
46762b8cbc9Sopenharmony_ci        }
46862b8cbc9Sopenharmony_ci        if (updates_.num == MAX_UPDATE_SIZE) {
46962b8cbc9Sopenharmony_ci            updates_.bound.Join(updates_.bound, rect);
47062b8cbc9Sopenharmony_ci            updates_.num = 0;
47162b8cbc9Sopenharmony_ci            updates_.updates[updates_.num++] = updates_.bound;
47262b8cbc9Sopenharmony_ci        } else {
47362b8cbc9Sopenharmony_ci            updates_.updates[updates_.num++] = rect;
47462b8cbc9Sopenharmony_ci            updates_.bound.Join(updates_.bound, rect);
47562b8cbc9Sopenharmony_ci        }
47662b8cbc9Sopenharmony_ci    }
47762b8cbc9Sopenharmony_ci}
47862b8cbc9Sopenharmony_ci
47962b8cbc9Sopenharmony_civoid LiteWM::ProcessUpdates()
48062b8cbc9Sopenharmony_ci{
48162b8cbc9Sopenharmony_ci    bool needFlush = false;
48262b8cbc9Sopenharmony_ci    {
48362b8cbc9Sopenharmony_ci        GraphicLocker lock(stackLock_);
48462b8cbc9Sopenharmony_ci        for (int i = 0; i < updates_.num; i++) {
48562b8cbc9Sopenharmony_ci            ListNode<LiteWindow *>* winNode = winList_.Begin();
48662b8cbc9Sopenharmony_ci            DrawRegion(winNode, EXPAND_RECT(updates_.updates[i]));
48762b8cbc9Sopenharmony_ci
48862b8cbc9Sopenharmony_ci            if (cursorInfo_.enableCursor && cursorInfo_.rect.IsIntersect(updates_.updates[i])) {
48962b8cbc9Sopenharmony_ci                cursorInfo_.needRedraw = true;
49062b8cbc9Sopenharmony_ci            }
49162b8cbc9Sopenharmony_ci        }
49262b8cbc9Sopenharmony_ci        if (updates_.num != 0 || (cursorInfo_.enableCursor && cursorInfo_.needRedraw)) {
49362b8cbc9Sopenharmony_ci            needFlush = true;
49462b8cbc9Sopenharmony_ci        }
49562b8cbc9Sopenharmony_ci        updates_ = {};
49662b8cbc9Sopenharmony_ci    }
49762b8cbc9Sopenharmony_ci
49862b8cbc9Sopenharmony_ci    if (cursorInfo_.enableCursor && cursorInfo_.needRedraw) {
49962b8cbc9Sopenharmony_ci        DrawMouseCursor();
50062b8cbc9Sopenharmony_ci        cursorInfo_.needRedraw = false;
50162b8cbc9Sopenharmony_ci    }
50262b8cbc9Sopenharmony_ci
50362b8cbc9Sopenharmony_ci    if (needFlush) {
50462b8cbc9Sopenharmony_ci        LcdFlush();
50562b8cbc9Sopenharmony_ci    }
50662b8cbc9Sopenharmony_ci}
50762b8cbc9Sopenharmony_ci
50862b8cbc9Sopenharmony_civoid LiteWM::DrawRegion(const ListNode<LiteWindow*>* winNode, int16_t x1, int16_t y1, int16_t x2, int16_t y2)
50962b8cbc9Sopenharmony_ci{
51062b8cbc9Sopenharmony_ci    if (winNode == nullptr) {
51162b8cbc9Sopenharmony_ci        return;
51262b8cbc9Sopenharmony_ci    }
51362b8cbc9Sopenharmony_ci
51462b8cbc9Sopenharmony_ci    if (winNode == winList_.End()) {
51562b8cbc9Sopenharmony_ci        DrawBackground(x1, y1, x2, y2);
51662b8cbc9Sopenharmony_ci        return;
51762b8cbc9Sopenharmony_ci    }
51862b8cbc9Sopenharmony_ci
51962b8cbc9Sopenharmony_ci    LiteWindow* window = winNode->data_;
52062b8cbc9Sopenharmony_ci    if (window == nullptr) {
52162b8cbc9Sopenharmony_ci        return;
52262b8cbc9Sopenharmony_ci    }
52362b8cbc9Sopenharmony_ci    window->UpdateBackBuf();
52462b8cbc9Sopenharmony_ci
52562b8cbc9Sopenharmony_ci    Rect rect(x1, y1, x2, y2);
52662b8cbc9Sopenharmony_ci    Rect& winRect = window->config_.rect;
52762b8cbc9Sopenharmony_ci    Rect mask;
52862b8cbc9Sopenharmony_ci    if (!window->isShow_ || window->NoNeedToDraw() ||
52962b8cbc9Sopenharmony_ci        window->backBuf_ == nullptr || !mask.Intersect(winRect, rect)) {
53062b8cbc9Sopenharmony_ci        GRAPHIC_LOGI("winRect={%d,%d,%d,%d}, rect={%d,%d,%d,%d}", EXPAND_RECT(winRect), EXPAND_RECT(rect));
53162b8cbc9Sopenharmony_ci        DrawRegion(winNode->next_, x1, y1, x2, y2);
53262b8cbc9Sopenharmony_ci        return;
53362b8cbc9Sopenharmony_ci    }
53462b8cbc9Sopenharmony_ci
53562b8cbc9Sopenharmony_ci    int x = mask.GetLeft();
53662b8cbc9Sopenharmony_ci    int y = mask.GetTop();
53762b8cbc9Sopenharmony_ci
53862b8cbc9Sopenharmony_ci    if (!window->IsCoverMode()) {
53962b8cbc9Sopenharmony_ci        DrawRegion(winNode->next_, EXPAND_RECT(mask));
54062b8cbc9Sopenharmony_ci    }
54162b8cbc9Sopenharmony_ci
54262b8cbc9Sopenharmony_ci    Rect srcRect = mask;
54362b8cbc9Sopenharmony_ci    srcRect.SetPosition(mask.GetLeft() - winRect.GetLeft(), mask.GetTop() - winRect.GetTop());
54462b8cbc9Sopenharmony_ci    GRAPHIC_LOGD("Blit, id=%d, srcRect={%d,%d,%d,%d}, x=%d, y=%d", window->id_, EXPAND_RECT(srcRect), x, y);
54562b8cbc9Sopenharmony_ci    window->Flush(srcRect, layerData_, x, y);
54662b8cbc9Sopenharmony_ci    GRAPHIC_LOGD("Blit finish");
54762b8cbc9Sopenharmony_ci
54862b8cbc9Sopenharmony_ci    if (x1 != mask.GetLeft()) {
54962b8cbc9Sopenharmony_ci        DrawRegion(winNode->next_, x1, y1, mask.GetLeft() - 1, y2);
55062b8cbc9Sopenharmony_ci    }
55162b8cbc9Sopenharmony_ci
55262b8cbc9Sopenharmony_ci    if (y1 != mask.GetTop()) {
55362b8cbc9Sopenharmony_ci        DrawRegion(winNode->next_, mask.GetLeft(), y1, x2, mask.GetTop() - 1);
55462b8cbc9Sopenharmony_ci    }
55562b8cbc9Sopenharmony_ci
55662b8cbc9Sopenharmony_ci    if (x2 != mask.GetRight()) {
55762b8cbc9Sopenharmony_ci        DrawRegion(winNode->next_, mask.GetRight() + 1, mask.GetTop(), x2, y2);
55862b8cbc9Sopenharmony_ci    }
55962b8cbc9Sopenharmony_ci
56062b8cbc9Sopenharmony_ci    if (y2 != mask.GetBottom()) {
56162b8cbc9Sopenharmony_ci        DrawRegion(winNode->next_, mask.GetLeft(), mask.GetBottom() + 1, mask.GetRight(), y2);
56262b8cbc9Sopenharmony_ci    }
56362b8cbc9Sopenharmony_ci}
56462b8cbc9Sopenharmony_ci
56562b8cbc9Sopenharmony_civoid LiteWM::DrawBackground(int16_t x1, int16_t y1, int16_t x2, int16_t y2)
56662b8cbc9Sopenharmony_ci{
56762b8cbc9Sopenharmony_ci    Rect rect(0, 0, layerData_->width - 1, layerData_->height - 1);
56862b8cbc9Sopenharmony_ci    Rect rectBg(x1, y1, x2, y2);
56962b8cbc9Sopenharmony_ci    if (!rect.Intersect(rect, rectBg)) {
57062b8cbc9Sopenharmony_ci        return;
57162b8cbc9Sopenharmony_ci    }
57262b8cbc9Sopenharmony_ci
57362b8cbc9Sopenharmony_ci    x1 = rect.GetLeft();
57462b8cbc9Sopenharmony_ci    x2 = rect.GetRight();
57562b8cbc9Sopenharmony_ci    y1 = rect.GetTop();
57662b8cbc9Sopenharmony_ci    y2 = rect.GetBottom();
57762b8cbc9Sopenharmony_ci
57862b8cbc9Sopenharmony_ci    GRAPHIC_LOGD("DrawBackground, {%d,%d,%d,%d}", x1, y1, x2, y2);
57962b8cbc9Sopenharmony_ci    int32_t len = static_cast<int32_t>(x2 - x1 + 1) * layerData_->bytePerPixel;
58062b8cbc9Sopenharmony_ci    for (int16_t y = y1; y <= y2; y++) {
58162b8cbc9Sopenharmony_ci        LayerColorType* buf1 = reinterpret_cast<LayerColorType*>(layerData_->virAddr + y * layerData_->stride);
58262b8cbc9Sopenharmony_ci        if (memset_s(buf1 + x1, len, 0, len) != EOK) {
58362b8cbc9Sopenharmony_ci            GRAPHIC_LOGE("memset_s error!");
58462b8cbc9Sopenharmony_ci        }
58562b8cbc9Sopenharmony_ci    }
58662b8cbc9Sopenharmony_ci}
58762b8cbc9Sopenharmony_ci
58862b8cbc9Sopenharmony_civoid LiteWM::DrawMouseCursor()
58962b8cbc9Sopenharmony_ci{
59062b8cbc9Sopenharmony_ci    Rect rect(0, 0, layerData_->width - 1, layerData_->height - 1);
59162b8cbc9Sopenharmony_ci    if (!rect.Intersect(rect, cursorInfo_.rect)) {
59262b8cbc9Sopenharmony_ci        return;
59362b8cbc9Sopenharmony_ci    }
59462b8cbc9Sopenharmony_ci
59562b8cbc9Sopenharmony_ci    int16_t x1 = rect.GetLeft();
59662b8cbc9Sopenharmony_ci    int16_t x2 = rect.GetRight();
59762b8cbc9Sopenharmony_ci    int16_t y1 = rect.GetTop();
59862b8cbc9Sopenharmony_ci    int16_t y2 = rect.GetBottom();
59962b8cbc9Sopenharmony_ci
60062b8cbc9Sopenharmony_ci    const uint16_t* srcbuf = reinterpret_cast<const uint16_t*>(CURSOR_MAP);
60162b8cbc9Sopenharmony_ci    uint8_t* dstBuf = layerData_->virAddr + y1 * layerData_->stride + x1 * sizeof(LayerColorType);
60262b8cbc9Sopenharmony_ci    for (int16_t y = y1; y <= y2; y++) {
60362b8cbc9Sopenharmony_ci        const uint16_t* tmpSrc = srcbuf;
60462b8cbc9Sopenharmony_ci        LayerColorType* tmpDst = reinterpret_cast<LayerColorType*>(dstBuf);
60562b8cbc9Sopenharmony_ci        for (int16_t x = x1; x <= x2; x++) {
60662b8cbc9Sopenharmony_ci            if ((*tmpSrc) & 0x8000) {
60762b8cbc9Sopenharmony_ci#ifdef LAYER_PF_ARGB1555
60862b8cbc9Sopenharmony_ci                *tmpDst = *tmpSrc;
60962b8cbc9Sopenharmony_ci#elif defined LAYER_PF_ARGB8888
61062b8cbc9Sopenharmony_ci                *tmpDst = PixelFormatUtils::ARGB1555ToARGB8888(*tmpSrc);
61162b8cbc9Sopenharmony_ci#endif
61262b8cbc9Sopenharmony_ci            }
61362b8cbc9Sopenharmony_ci            tmpSrc++;
61462b8cbc9Sopenharmony_ci            tmpDst++;
61562b8cbc9Sopenharmony_ci        }
61662b8cbc9Sopenharmony_ci        dstBuf += layerData_->stride;
61762b8cbc9Sopenharmony_ci        srcbuf += CURSOR_WIDTH;
61862b8cbc9Sopenharmony_ci    }
61962b8cbc9Sopenharmony_ci}
62062b8cbc9Sopenharmony_ci
62162b8cbc9Sopenharmony_ciLiteWindow* LiteWM::FindTargetWindow(const RawEvent& event)
62262b8cbc9Sopenharmony_ci{
62362b8cbc9Sopenharmony_ci    if (winList_.IsEmpty()) {
62462b8cbc9Sopenharmony_ci        return nullptr;
62562b8cbc9Sopenharmony_ci    }
62662b8cbc9Sopenharmony_ci
62762b8cbc9Sopenharmony_ci    LiteWindow* targetWindow = nullptr;
62862b8cbc9Sopenharmony_ci    auto node = winList_.Begin();
62962b8cbc9Sopenharmony_ci    while (node != winList_.End()) {
63062b8cbc9Sopenharmony_ci        if (node->data_->GetConfig().isModal) {
63162b8cbc9Sopenharmony_ci            return node->data_;
63262b8cbc9Sopenharmony_ci        }
63362b8cbc9Sopenharmony_ci        node = node->next_;
63462b8cbc9Sopenharmony_ci    }
63562b8cbc9Sopenharmony_ci
63662b8cbc9Sopenharmony_ci    switch (event.type) {
63762b8cbc9Sopenharmony_ci        case InputDevType::INDEV_TYPE_MOUSE:
63862b8cbc9Sopenharmony_ci            // fall-through
63962b8cbc9Sopenharmony_ci        case InputDevType::INDEV_TYPE_TOUCH: {
64062b8cbc9Sopenharmony_ci            auto win = winList_.Begin();
64162b8cbc9Sopenharmony_ci            while (win != winList_.End()) {
64262b8cbc9Sopenharmony_ci                Point p = { event.x, event.y };
64362b8cbc9Sopenharmony_ci                if (win->data_->isShow_ && win->data_->GetConfig().rect.IsContains(p)) {
64462b8cbc9Sopenharmony_ci                    targetWindow = win->data_;
64562b8cbc9Sopenharmony_ci                    break;
64662b8cbc9Sopenharmony_ci                }
64762b8cbc9Sopenharmony_ci                win = win->next_;
64862b8cbc9Sopenharmony_ci            }
64962b8cbc9Sopenharmony_ci            break;
65062b8cbc9Sopenharmony_ci        }
65162b8cbc9Sopenharmony_ci        case InputDevType::INDEV_TYPE_KEY:
65262b8cbc9Sopenharmony_ci            // fall-through
65362b8cbc9Sopenharmony_ci        case InputDevType::INDEV_TYPE_BUTTON: {
65462b8cbc9Sopenharmony_ci            targetWindow = winList_.Front();
65562b8cbc9Sopenharmony_ci            break;
65662b8cbc9Sopenharmony_ci        }
65762b8cbc9Sopenharmony_ci        default:
65862b8cbc9Sopenharmony_ci            break;
65962b8cbc9Sopenharmony_ci    }
66062b8cbc9Sopenharmony_ci    return targetWindow;
66162b8cbc9Sopenharmony_ci}
66262b8cbc9Sopenharmony_ci
66362b8cbc9Sopenharmony_civoid LiteWM::OnRawEvent(const RawEvent& rawEvent)
66462b8cbc9Sopenharmony_ci{
66562b8cbc9Sopenharmony_ci    static bool firstTime = true;
66662b8cbc9Sopenharmony_ci    if (layerData_ == nullptr) {
66762b8cbc9Sopenharmony_ci        return;
66862b8cbc9Sopenharmony_ci    }
66962b8cbc9Sopenharmony_ci
67062b8cbc9Sopenharmony_ci    RawEvent event = rawEvent;
67162b8cbc9Sopenharmony_ci    if (GetLayerRotateType() == LAYER_ROTATE_90) {
67262b8cbc9Sopenharmony_ci        int16_t tmp = layerData_->height - event.x;
67362b8cbc9Sopenharmony_ci        event.x = event.y;
67462b8cbc9Sopenharmony_ci        event.y = tmp;
67562b8cbc9Sopenharmony_ci    }
67662b8cbc9Sopenharmony_ci
67762b8cbc9Sopenharmony_ci    if (firstTime) {
67862b8cbc9Sopenharmony_ci        if (event.type == InputDevType::INDEV_TYPE_MOUSE) {
67962b8cbc9Sopenharmony_ci            cursorInfo_.enableCursor = true;
68062b8cbc9Sopenharmony_ci            cursorInfo_.needRedraw = true;
68162b8cbc9Sopenharmony_ci        } else {
68262b8cbc9Sopenharmony_ci            cursorInfo_.enableCursor = false;
68362b8cbc9Sopenharmony_ci        }
68462b8cbc9Sopenharmony_ci        firstTime = false;
68562b8cbc9Sopenharmony_ci    }
68662b8cbc9Sopenharmony_ci
68762b8cbc9Sopenharmony_ci    if (cursorInfo_.enableCursor) {
68862b8cbc9Sopenharmony_ci        SetMousePosition(event.x, event.y);
68962b8cbc9Sopenharmony_ci    }
69062b8cbc9Sopenharmony_ci
69162b8cbc9Sopenharmony_ci    LiteWindow* targetWindow = FindTargetWindow(event);
69262b8cbc9Sopenharmony_ci    if (targetWindow == nullptr) {
69362b8cbc9Sopenharmony_ci        return;
69462b8cbc9Sopenharmony_ci    }
69562b8cbc9Sopenharmony_ci
69662b8cbc9Sopenharmony_ci    SetEventData(targetWindow, event);
69762b8cbc9Sopenharmony_ci}
69862b8cbc9Sopenharmony_ci
69962b8cbc9Sopenharmony_cibool LiteWM::OnScreenshot(Surface* surface)
70062b8cbc9Sopenharmony_ci{
70162b8cbc9Sopenharmony_ci    GraphicLocker lock(screenshotMutex_);
70262b8cbc9Sopenharmony_ci    if (!needScreenshot_) {
70362b8cbc9Sopenharmony_ci        screenshotSurface_ = surface;
70462b8cbc9Sopenharmony_ci        needScreenshot_ = true;
70562b8cbc9Sopenharmony_ci        return true;
70662b8cbc9Sopenharmony_ci    }
70762b8cbc9Sopenharmony_ci    return false;
70862b8cbc9Sopenharmony_ci}
70962b8cbc9Sopenharmony_ci
71062b8cbc9Sopenharmony_civoid LiteWM::Screenshot()
71162b8cbc9Sopenharmony_ci{
71262b8cbc9Sopenharmony_ci    int32_t lineSize = 0;
71362b8cbc9Sopenharmony_ci    int16_t bpp = 0;
71462b8cbc9Sopenharmony_ci    uint8_t* dstAddr = nullptr;
71562b8cbc9Sopenharmony_ci    uint8_t* srcAddr = nullptr;
71662b8cbc9Sopenharmony_ci    uint32_t width = 0;
71762b8cbc9Sopenharmony_ci    uint32_t height = 0;
71862b8cbc9Sopenharmony_ci    if (screenshotSurface_ == nullptr) {
71962b8cbc9Sopenharmony_ci        return;
72062b8cbc9Sopenharmony_ci    }
72162b8cbc9Sopenharmony_ci
72262b8cbc9Sopenharmony_ci    SurfaceBuffer* buffer = screenshotSurface_->RequestBuffer();
72362b8cbc9Sopenharmony_ci    if (buffer == nullptr) {
72462b8cbc9Sopenharmony_ci        goto end2;
72562b8cbc9Sopenharmony_ci    }
72662b8cbc9Sopenharmony_ci
72762b8cbc9Sopenharmony_ci    width = screenshotSurface_->GetWidth();
72862b8cbc9Sopenharmony_ci    height = screenshotSurface_->GetHeight();
72962b8cbc9Sopenharmony_ci    if (width > layerData_->width || height > layerData_->height) {
73062b8cbc9Sopenharmony_ci        goto end1;
73162b8cbc9Sopenharmony_ci    }
73262b8cbc9Sopenharmony_ci
73362b8cbc9Sopenharmony_ci    if (!PixelFormatUtils::BppOfPixelFormat(static_cast<ImagePixelFormat>(screenshotSurface_->GetFormat()), bpp)) {
73462b8cbc9Sopenharmony_ci        goto end1;
73562b8cbc9Sopenharmony_ci    }
73662b8cbc9Sopenharmony_ci
73762b8cbc9Sopenharmony_ci    lineSize = width * bpp;
73862b8cbc9Sopenharmony_ci    dstAddr = static_cast<uint8_t*>(buffer->GetVirAddr());
73962b8cbc9Sopenharmony_ci    srcAddr = layerData_->virAddr;
74062b8cbc9Sopenharmony_ci    if (dstAddr != nullptr && srcAddr != nullptr) {
74162b8cbc9Sopenharmony_ci        for (uint32_t i = 0; i < height; i++) {
74262b8cbc9Sopenharmony_ci            if (memcpy_s(dstAddr, lineSize, srcAddr, lineSize) != EOK) {
74362b8cbc9Sopenharmony_ci                GRAPHIC_LOGE("memcpy_s error!");
74462b8cbc9Sopenharmony_ci            }
74562b8cbc9Sopenharmony_ci            dstAddr += lineSize;
74662b8cbc9Sopenharmony_ci            srcAddr += layerData_->stride;
74762b8cbc9Sopenharmony_ci        }
74862b8cbc9Sopenharmony_ci    }
74962b8cbc9Sopenharmony_ciend1:
75062b8cbc9Sopenharmony_ci    screenshotSurface_->FlushBuffer(buffer);
75162b8cbc9Sopenharmony_ciend2:
75262b8cbc9Sopenharmony_ci    delete screenshotSurface_;
75362b8cbc9Sopenharmony_ci    screenshotSurface_ = nullptr;
75462b8cbc9Sopenharmony_ci}
75562b8cbc9Sopenharmony_ci
75662b8cbc9Sopenharmony_civoid LiteWM::OnClientDeathNotify(pid_t pid)
75762b8cbc9Sopenharmony_ci{
75862b8cbc9Sopenharmony_ci    GRAPHIC_LOGI("OnClientDeathNotify");
75962b8cbc9Sopenharmony_ci    GraphicLocker lock(stackLock_);
76062b8cbc9Sopenharmony_ci    auto node = winList_.Begin();
76162b8cbc9Sopenharmony_ci    while (node != winList_.End()) {
76262b8cbc9Sopenharmony_ci        auto tmp = node;
76362b8cbc9Sopenharmony_ci        node = node->next_;
76462b8cbc9Sopenharmony_ci        LiteWindow* window = tmp->data_;
76562b8cbc9Sopenharmony_ci        GRAPHIC_LOGI("window->GetPid() = %d,pid = %d", window->GetPid(), pid);
76662b8cbc9Sopenharmony_ci        if (window->GetPid() == pid) {
76762b8cbc9Sopenharmony_ci            winList_.Remove(tmp);
76862b8cbc9Sopenharmony_ci            AddUpdateRegion(window->config_.rect);
76962b8cbc9Sopenharmony_ci            delete window;
77062b8cbc9Sopenharmony_ci        }
77162b8cbc9Sopenharmony_ci    }
77262b8cbc9Sopenharmony_ci}
77362b8cbc9Sopenharmony_ci}
774