window_impl.cpp 5.3 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "window/window_impl.h"
#include "core/render_manager.h"
Z
zhangguyuan 已提交
18
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
19 20 21 22 23 24 25 26 27
#include "iwindows_manager.h"

namespace OHOS {
WindowImpl::WindowImpl() : rootView_(nullptr), iWindow_(nullptr), isShow_(false), gfxAlloc_({}) {}

WindowImpl::~WindowImpl() {}

bool WindowImpl::Create(const WindowConfig& config)
{
28
    GRAPHIC_LOGI("Create");
M
mamingshuai 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    if (iWindow_ == nullptr) {
        config_ = config;
        LiteWinConfig liteConfig;
        liteConfig.rect = config.rect;
        liteConfig.pixelFormat = IMAGE_PIXEL_FORMAT_ARGB8888;
        liteConfig.opacity = config.opacity;
        liteConfig.isModal = config.isModal;
        liteConfig.compositeMode = static_cast<LiteWinConfig::CompositeMode>(config.compositeMode);
        iWindow_ = IWindowsManager::GetInstance()->CreateWindow(liteConfig);
        if (iWindow_ == nullptr) {
            return false;
        }
    }
    return true;
}

L
liuyuxiang-bear 已提交
45
void WindowImpl::Destroy()
M
mamingshuai 已提交
46 47 48 49 50 51 52 53 54 55
{
    Flush();
    if (iWindow_ != nullptr) {
        IWindowsManager::GetInstance()->RemoveWindow(iWindow_);
        iWindow_ = nullptr;
    }
}

void WindowImpl::AddToDisplay()
{
56
    GRAPHIC_LOGI("AddToDisplay");
M
mamingshuai 已提交
57 58 59 60 61
    RenderManager::GetInstance().AddToDisplay(this);
}

void WindowImpl::RemoveFromDisplay()
{
62
    GRAPHIC_LOGI("RemoveFromDisplay");
M
mamingshuai 已提交
63 64 65 66 67 68 69 70 71
    RenderManager::GetInstance().RemoveFromDisplay(this);
}

void WindowImpl::BindRootView(RootView* rootView)
{
    if (rootView == nullptr) {
        return;
    }
    UnbindRootView();
72
    GRAPHIC_LOGI("BindRootView");
M
mamingshuai 已提交
73 74 75 76 77 78
    rootView_ = rootView;
    rootView_->boundWindow_ = this;
}

void WindowImpl::UnbindRootView()
{
79
    GRAPHIC_LOGI("UnbindRootView");
M
mamingshuai 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    if (rootView_ != nullptr) {
        rootView_->boundWindow_ = nullptr;
        rootView_ = nullptr;
    }
}

RootView* WindowImpl::GetRootView()
{
    return rootView_;
}

Rect WindowImpl::GetRect()
{
    return config_.rect;
}

void WindowImpl::Show()
{
    if (iWindow_ == nullptr) {
        return;
    }

    if (!isShow_) {
        isShow_ = true;
        Render();
        iWindow_->Show();
    }
}

void WindowImpl::Hide()
{
    if (iWindow_ == nullptr) {
        return;
    }

    if (isShow_) {
        isShow_ = false;
        iWindow_->Hide();
    }
}

void WindowImpl::MoveTo(int16_t x, int16_t y)
{
123
    GRAPHIC_LOGI("MoveTo");
M
mamingshuai 已提交
124 125 126 127 128 129 130 131
    config_.rect.SetPosition(x, y);
    if (iWindow_ != nullptr) {
        iWindow_->MoveTo(x, y);
    }
}

void WindowImpl::Resize(int16_t width, int16_t height)
{
132
    GRAPHIC_LOGI("Resize");
M
mamingshuai 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    if ((width == config_.rect.GetWidth()) && (height == config_.rect.GetHeight())) {
        return;
    }

    config_.rect.Resize(width, height);
    Flush();
    if (iWindow_ != nullptr) {
        iWindow_->Resize(width, height);
    }

    if (rootView_ != nullptr) {
        rootView_->Invalidate();
    }
}

void WindowImpl::RaiseToTop()
{
150
    GRAPHIC_LOGI("RaiseToTop");
M
mamingshuai 已提交
151 152 153 154 155 156 157
    if (iWindow_ != nullptr) {
        iWindow_->RaiseToTop();
    }
}

void WindowImpl::LowerToBottom()
{
158
    GRAPHIC_LOGI("LowerToBottom");
M
mamingshuai 已提交
159 160 161 162 163 164 165 166 167
    if (iWindow_ != nullptr) {
        iWindow_->LowerToBottom();
    }
}

void WindowImpl::Render()
{
    UpdateHalDisplayBuffer();
    if (gfxAlloc_.virAddr == nullptr) {
168
        GRAPHIC_LOGE("window buffer is null, windId=%d", GetWindowId());
M
mamingshuai 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        return;
    }

    if (rootView_ != nullptr) {
        rootView_->Measure();
        rootView_->Render();
    }
}

void WindowImpl::Update()
{
    if (iWindow_ == nullptr) {
        return;
    }
    iWindow_->Update();
}

int32_t WindowImpl::GetWindowId()
{
    if (iWindow_ != nullptr) {
        return iWindow_->GetWindowId();
    } else {
191
        GRAPHIC_LOGE("iwindow is null!");
M
mamingshuai 已提交
192 193 194 195 196 197
        return INVALID_WINDOW_ID;
    }
}

void WindowImpl::Flush()
{
198
    GRAPHIC_LOGI("Flush");
M
mamingshuai 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    if (iWindow_ == nullptr) {
        return;
    }
    ISurface* surface = iWindow_->GetSurface();
    if (surface != nullptr) {
        surface->Unlock();
        gfxAlloc_ = {};
    }
}

void WindowImpl::UpdateHalDisplayBuffer()
{
    if ((gfxAlloc_.virAddr == nullptr) && (iWindow_ != nullptr)) {
        ISurface* surface = iWindow_->GetSurface();
        if (surface == nullptr) {
            return;
        }
        surface->Lock((void**)&gfxAlloc_.virAddr, (void**)&gfxAlloc_.phyAddr, &gfxAlloc_.stride);
    }
N
niulihua 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
}

BufferInfo* WindowImpl::GetBufferInfo()
{
    static BufferInfo bufferInfo;
    bufferInfo.virAddr = gfxAlloc_.virAddr;
    bufferInfo.phyAddr = gfxAlloc_.phyAddr;
    bufferInfo.width = config_.rect.GetWidth();
    bufferInfo.height = config_.rect.GetHeight();
    bufferInfo.mode = ARGB8888;
    bufferInfo.stride = gfxAlloc_.stride;

    bufferInfo.rect = {
        0,
        0,
        static_cast<int16_t>(bufferInfo.width - 1),
        static_cast<int16_t>(bufferInfo.height - 1)
    };
    return &bufferInfo;
M
mamingshuai 已提交
237 238
}
} // namespace OHOS