root_view.cpp 29.6 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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 "components/root_view.h"

#include "common/screen.h"
#include "core/render_manager.h"
N
niulihua 已提交
20
#include "draw/draw_utils.h"
Z
zhangguyuan 已提交
21
#include "gfx_utils/graphic_log.h"
M
mamingshuai 已提交
22 23 24
#if ENABLE_WINDOW
#include "window/window_impl.h"
#endif
N
niulihua 已提交
25
#include "securec.h"
M
mamingshuai 已提交
26 27 28 29 30 31 32 33
namespace OHOS {
namespace {
#if LOCAL_RENDER
const constexpr uint8_t MAX_SPLIT_NUM = 32; // split at most 32 parts
// view along with its parents and siblings are at most 128
const constexpr uint8_t VIEW_STACK_DEPTH = COMPONENT_NESTING_DEPTH * 2;
#else
const constexpr uint8_t VIEW_STACK_DEPTH = COMPONENT_NESTING_DEPTH;
P
pssea 已提交
34
const constexpr uint8_t MAX_INVALIDATE_SIZE = 24;
M
mamingshuai 已提交
35 36 37 38 39 40 41 42 43
#endif
static Rect g_maskStack[COMPONENT_NESTING_DEPTH];
static UIView* g_viewStack[VIEW_STACK_DEPTH];
} // namespace
RootView::RootView()
{
#if defined __linux__ || defined __LITEOS__ || defined __APPLE__
    pthread_mutex_init(&lock_, nullptr);
#endif
N
niulihua 已提交
44
    InitDrawContext();
M
mamingshuai 已提交
45 46
}

Z
zhdengc 已提交
47 48 49 50 51 52
RootView* RootView::GetInstance()
{
    static RootView instance;
    return &instance;
}

N
niulihua 已提交
53 54 55 56 57 58 59
RootView::~RootView()
{
    DestroyDrawContext();
#if defined __linux__ || defined __LITEOS__ || defined __APPLE__
    pthread_mutex_destroy(&lock_);
#endif
}
M
mamingshuai 已提交
60 61 62 63 64 65 66
#if ENABLE_WINDOW
Window* RootView::GetBoundWindow() const
{
    return boundWindow_;
}
#endif

L
l00417912 已提交
67
Rect RootView::GetScreenRect()
M
mamingshuai 已提交
68 69 70 71 72 73
{
#if ENABLE_WINDOW
    Rect screenRect = GetRect();
#else
    Rect screenRect(0, 0, Screen::GetInstance().GetWidth() - 1, Screen::GetInstance().GetHeight() - 1);
#endif
L
l00417912 已提交
74
    return screenRect;
M
mamingshuai 已提交
75 76 77
}

#if LOCAL_RENDER
78
using namespace Graphic;
M
mamingshuai 已提交
79 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
static void DivideInvalidateRect(const Rect& originRect, Rect& leftoverRect, Vector<Rect>& splitRects)
{
    Rect mask;
    if (!mask.Intersect(originRect, leftoverRect)) {
        splitRects.PushBack(leftoverRect);
        return;
    }

    /*
     *   +---+---+---+
     *   |   | A |   |      originRect           :A+B
     *   |   +---+   |      leftoverRect         :A->0
     *   |     B     |      mask                 :A
     *   +-----------+
     */
    if (originRect.IsContains(leftoverRect)) {
        return;
    }

    int16_t reserveCnt = MAX_SPLIT_NUM - splitRects.Size();
    if (reserveCnt <= 0) {
        splitRects.PushBack(leftoverRect);
        return;
    }

    if (mask.GetWidth() == leftoverRect.GetWidth()) {
        /*
         *       +---+
         *       | A |        originRect           :B+C
         *   +-----------+    leftoverRect         :A+B->A
         *   |   | B |   |    mask                 :B
         *   |   +---+   |
         *   |     C     |
         *   +-----------+
         */
        if (mask.GetBottom() == leftoverRect.GetBottom()) {
            leftoverRect.SetBottom(mask.GetTop() - 1);
        } else if (mask.GetTop() == leftoverRect.GetTop()) {
            leftoverRect.SetTop(mask.GetBottom() + 1);
        } else {
            splitRects.PushBack(leftoverRect);
            splitRects.Back().SetBottom(mask.GetTop() - 1);
            leftoverRect.SetTop(mask.GetBottom() + 1);
        }
        splitRects.PushBack(leftoverRect);
        return;
    }
    if (mask.GetHeight() == leftoverRect.GetHeight()) {
        /*
         *      +---------+   originRect           :B+C
         *  +-------+     |   leftoverRect         :A+B->A
         *  | A | B |  C  |   mask                 :B
         *  +-------+     |
         *      +---------+
         */
        if (mask.GetLeft() == leftoverRect.GetLeft()) {
            leftoverRect.SetLeft(mask.GetRight() + 1);
        } else if (mask.GetRight() == leftoverRect.GetRight()) {
            leftoverRect.SetRight(mask.GetLeft() - 1);
        } else {
            splitRects.PushBack(leftoverRect);
            splitRects.Back().SetRight(mask.GetLeft() - 1);
            leftoverRect.SetLeft(mask.GetRight() + 1);
        }
        splitRects.PushBack(leftoverRect);
        return;
    }

    Vector<Rect> copyRect(splitRects);
    if (mask.GetLeft() != leftoverRect.GetLeft()) {
        /*
         *     |
         * +-------+
         * |   +---+   mask                 :A
         * | B | A |   leftoverRect         :A+B
         * |   +---+
         * +-------+
         *     |
         */
        if (reserveCnt-- <= 0) {
            splitRects.Swap(copyRect);
            splitRects.PushBack(leftoverRect);
            return;
        }
        splitRects.PushBack(leftoverRect);
        splitRects.Back().SetRight(mask.GetLeft() - 1);
        leftoverRect.SetLeft(mask.GetLeft());
    }

    if (mask.GetTop() != leftoverRect.GetTop()) {
        /*
         *  +-------+
         *  |   B   |   mask                 :A
         * ---+---+---  leftoverRect         :A+B
         *  | | A | |
         *  +-+---+-+
         */
        if (reserveCnt-- <= 0) {
            splitRects.Swap(copyRect);
            splitRects.PushBack(leftoverRect);
            return;
        }
        splitRects.PushBack(leftoverRect);
        splitRects.Back().SetBottom(mask.GetTop() - 1);
        leftoverRect.SetTop(mask.GetTop());
    }

    if (mask.GetRight() != leftoverRect.GetRight()) {
        /*
         *     |
         * +-------+
         * +---+   |    mask                 :A
         * | A | B |    leftoverRect         :A+B
         * +---+   |
         * +-------+
         *     |
         */
        if (reserveCnt-- <= 0) {
            splitRects.Swap(copyRect);
            splitRects.PushBack(leftoverRect);
            return;
        }
        splitRects.PushBack(leftoverRect);
        splitRects.Back().SetLeft(mask.GetRight() + 1);
        leftoverRect.SetRight(mask.GetRight());
    }

    if (mask.GetBottom() != leftoverRect.GetBottom()) {
        /*
         *  +-+---+-+
         *  | | A | |     mask                 :A
         * ---+---+---    leftoverRect         :A+B
         *  |   B   |
         *  +-------+
         */
        if (reserveCnt-- <= 0) {
            splitRects.Swap(copyRect);
            splitRects.PushBack(leftoverRect);
            return;
        }
        splitRects.PushBack(leftoverRect);
        splitRects.Back().SetTop(mask.GetBottom() + 1);
        leftoverRect.SetBottom(mask.GetBottom());
    }
    return;
}

static void AddRenderedRects(Rect& rect, List<Rect>& renderedRects, ListNode<Rect>* iter)
{
    /* Elements at front have larger area and more relevance */
    for (; iter != renderedRects.End(); iter = iter->next_) {
        Rect& curRect = iter->data_;
        if (!curRect.IsIntersect(rect)) {
            continue;
        }

        if (curRect.IsContains(rect)) {
            return;
        }

        /* Merge two rects */
        if (rect.IsContains(curRect)) {
        } else if (((curRect.GetLeft() == rect.GetLeft()) && (curRect.GetRight() == rect.GetRight())) ||
                   ((curRect.GetTop() == rect.GetTop()) && (curRect.GetBottom() == rect.GetBottom()))) {
            rect.Join(curRect, rect);
        } else {
            continue;
        }
        iter = renderedRects.Remove(iter)->prev_;
        break;
    }
    if (iter == renderedRects.End()) {     // No merge rises
        if (renderedRects.Size() == 128) { // record 128 rendered rects at most
            renderedRects.PopBack();
        }
        renderedRects.PushFront(rect);
    } else { // merge rises, go over the rest nodes
        AddRenderedRects(rect, renderedRects, iter);
    }
}

void RootView::RemoveViewFromInvalidMap(UIView* view)
{
#if defined __linux__ || defined __LITEOS__ || defined __APPLE__
    pthread_mutex_lock(&lock_);
#endif

    int16_t stackCount = 0;
    do {
        while (view != nullptr) {
            /* delete node itself */
            auto entry = invalidateMap_.find(view);
            if (entry != invalidateMap_.end()) {
                invalidateMap_.erase(entry);
            }
            /* delete node's children */
            if (view->IsViewGroup() && stackCount < COMPONENT_NESTING_DEPTH) {
                g_viewStack[stackCount++] = view;
L
Lizhiqi 已提交
277
                view = static_cast<UIViewGroup*>(view)->GetChildrenRenderHead();
M
mamingshuai 已提交
278 279
                continue;
            }
L
Lizhiqi 已提交
280 281
            /* only go to child's render sibling */
            view = view->GetNextRenderSibling();
M
mamingshuai 已提交
282 283
        }
        if (--stackCount >= 0) {
L
Lizhiqi 已提交
284
            view = g_viewStack[stackCount]->GetNextRenderSibling();
M
mamingshuai 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
        }
    } while (stackCount >= 0);

#if defined __linux__ || defined __LITEOS__ || defined __APPLE__
    pthread_mutex_unlock(&lock_);
#endif
}

void RootView::OptimizeInvalidView(UIView* curview, UIView* background, List<Rect>& renderedRects)
{
    if (curview == nullptr) {
        return;
    }
    auto mapEntry = invalidateMap_.find(curview);
    if (mapEntry == invalidateMap_.end()) {
        return;
    }

    Rect& invalidRect = mapEntry->second.Front();
    /* Transparent views should draw from background */
    if (((curview->GetStyleConst().bgOpa_ != OPA_OPAQUE) || (curview->GetOpaScale() != OPA_OPAQUE) ||
306
         (!curview->IsTransInvalid())) &&
M
mamingshuai 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
        (curview != this)) {
        AddInvalidateRect(invalidRect, background);
        invalidateMap_.erase(mapEntry);
        return;
    }

    /* Remove the rendered parts and split the origin rect into splitInvalidRects
     * For performance reason, split numbers are strictly restrained.
     */
    Vector<Rect> splitInvalidRects(MAX_SPLIT_NUM << 1);
    Rect invalidRectCopy(invalidRect);
    /* Using forward order because entries at the front are closer to the current view and have larger Size */
    for (auto iter = renderedRects.Begin(); iter != renderedRects.End(); iter = iter->next_) {
        for (int8_t i = 0; i < mapEntry->second.Size(); i++) {
            DivideInvalidateRect(iter->data_, mapEntry->second[i], splitInvalidRects);
        }
        mapEntry->second.Swap(splitInvalidRects);
        splitInvalidRects.Clear();
    }

    /* Add new opaque rects */
    Rect preDrawRect(invalidRectCopy);
    if (!curview->OnPreDraw(preDrawRect)) {
        AddInvalidateRect(invalidRectCopy, background);
    }
    AddRenderedRects(preDrawRect, renderedRects, renderedRects.Begin());
}

void RootView::OptimizeInvalidMap()
{
    UIView* curview = this;
    int16_t stackCount = 0;
    int16_t opaStackCount = 0;
    UIView* background[VIEW_STACK_DEPTH];
    bool flags[VIEW_STACK_DEPTH]; // indicate whether stack go back from child
    List<Rect> renderedRects;     // Record rendered areas to avoid rerendering

    do {
        /* push stack */
        if (curview != nullptr) {
            if (stackCount >= VIEW_STACK_DEPTH) {
                return;
            }
            g_viewStack[stackCount] = curview;
            flags[stackCount++] = false;
L
Lizhiqi 已提交
352
            curview = curview->GetNextRenderSibling();
M
mamingshuai 已提交
353 354 355 356
            continue;
        }

        curview = g_viewStack[--stackCount];
P
pssea 已提交
357
        Rect rect;
L
l00417912 已提交
358
        if (!curview->IsVisible() || !rect.Intersect(curview->GetRect(), GetScreenRect())) {
M
mamingshuai 已提交
359 360 361 362 363 364 365
            curview = nullptr;
            continue;
        }
        if (!flags[stackCount]) { // Back from sibling
            if (curview->IsViewGroup()) {
                /* Set background/topview */
                if (((curview->GetStyleConst().bgOpa_ == OPA_OPAQUE) && (curview->GetOpaScale() == OPA_OPAQUE) &&
366
                     curview->IsTransInvalid()) ||
M
mamingshuai 已提交
367 368 369 370 371 372 373 374 375 376
                    (curview == this)) {
                    background[opaStackCount] = curview;
                } else {
                    background[opaStackCount] = background[opaStackCount - 1];
                }
                ++opaStackCount;
                if (opaStackCount >= VIEW_STACK_DEPTH) {
                    return;
                }
                flags[stackCount++] = true;
L
Lizhiqi 已提交
377
                curview = static_cast<UIViewGroup*>(curview)->GetChildrenRenderHead();
M
mamingshuai 已提交
378 379 380 381 382 383 384 385
                continue;
            }
        } else { // Back from child
            opaStackCount--;
        }
        OptimizeInvalidView(curview, background[opaStackCount - 1], renderedRects);
        curview = nullptr;
    } while (stackCount > 0);
M
muyuhw 已提交
386
    renderedRects.Clear();
M
mamingshuai 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400
}

void RootView::DrawInvalidMap(const Rect& buffRect)
{
    OptimizeInvalidMap();
    Rect rect;
    for (auto& viewEntry : invalidateMap_) {
        Vector<Rect>& viewRenderRect = viewEntry.second;
        for (uint16_t i = 0; i < viewRenderRect.Size(); i++) {
            rect.Intersect(viewRenderRect[i], buffRect);
            DrawTop(viewEntry.first, rect);
        }
    }
}
P
pssea 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
#else
void RootView::OptimizeAddRect(Rect& rect)
{
    Rect joinRect;
    for (ListNode<Rect>* iter = invalidateRects_.Begin(); iter != invalidateRects_.End(); iter = iter->next_) {
        if (iter->data_.IsContains(rect)) {
            return;
        }

        if (iter->data_.IsIntersect(rect)) {
            joinRect.Join(iter->data_, rect);
            if (joinRect.GetSize() < iter->data_.GetSize() + rect.GetSize()) {
                iter->data_ = joinRect;
                return;
            }
        }
    }

    if (invalidateRects_.Size() < MAX_INVALIDATE_SIZE) {
        invalidateRects_.PushBack(rect);
    } else {
        invalidateRects_.Clear();
        invalidateRects_.PushBack(GetScreenRect());
    }
}

void RootView::OptimizeInvalidateRects()
{
    Rect joinRect;
    for (ListNode<Rect>* iter1 = invalidateRects_.Begin(); iter1 != invalidateRects_.End(); iter1 = iter1->next_) {
        for (ListNode<Rect>* iter2 = invalidateRects_.Begin(); iter2 != invalidateRects_.End(); iter2 = iter2->next_) {
            if (iter1 == iter2) {
                continue;
            }

            if (iter2->data_.IsIntersect(iter1->data_)) {
                joinRect.Join(iter1->data_, iter2->data_);
                if (joinRect.GetSize() < (iter1->data_.GetSize() + iter2->data_.GetSize())) {
                    iter2->data_ = joinRect;
                    iter1 = invalidateRects_.Remove(iter1)->prev_;
                    break;
                }
            }
        }
    }
}
M
mamingshuai 已提交
447 448 449 450
#endif

void RootView::AddInvalidateRect(Rect& rect, UIView* view)
{
L
l00417912 已提交
451 452
    Rect commonRect;
    if (commonRect.Intersect(rect, GetScreenRect())) {
M
mamingshuai 已提交
453 454 455 456 457 458 459 460
#if LOCAL_RENDER
        Vector<Rect>& invalidRects = invalidateMap_[view];
        if (invalidRects.IsEmpty()) {
            invalidRects.PushBack(commonRect);
        } else {
            invalidRects[0].Join(invalidRects[0], commonRect);
        }
#else
P
pssea 已提交
461
        OptimizeAddRect(commonRect);
M
mamingshuai 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
#endif
    }
}

void RootView::AddInvalidateRectWithLock(Rect& rect, UIView* view)
{
#if defined __linux__ || defined __LITEOS__ || defined __APPLE__
    pthread_mutex_lock(&lock_);
#endif

    AddInvalidateRect(rect, view);

#if defined __linux__ || defined __LITEOS__ || defined __APPLE__
    pthread_mutex_unlock(&lock_);
#endif
}

void RootView::Measure()
{
#if LOCAL_RENDER
    if (!invalidateMap_.empty()) {
L
Lizhiqi 已提交
483
        MeasureView(GetChildrenRenderHead());
M
mamingshuai 已提交
484 485
    }
#else
P
pssea 已提交
486
    if (invalidateRects_.Size() > 0) {
L
Lizhiqi 已提交
487
        MeasureView(GetChildrenRenderHead());
M
mamingshuai 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501
    }
#endif
}

void RootView::MeasureView(UIView* view)
{
    int16_t stackCount = 0;
    UIView* curView = view;
    while (stackCount >= 0) {
        while (curView != nullptr) {
            if (curView->IsVisible()) {
                curView->ReMeasure();
                if (curView->IsViewGroup() && stackCount < COMPONENT_NESTING_DEPTH) {
                    g_viewStack[stackCount++] = curView;
L
Lizhiqi 已提交
502
                    curView = static_cast<UIViewGroup*>(curView)->GetChildrenRenderHead();
M
mamingshuai 已提交
503 504 505
                    continue;
                }
            }
L
Lizhiqi 已提交
506
            curView = curView->GetNextRenderSibling();
M
mamingshuai 已提交
507 508
        }
        if (--stackCount >= 0) {
L
Lizhiqi 已提交
509
            curView = (g_viewStack[stackCount])->GetNextRenderSibling();
M
mamingshuai 已提交
510 511 512 513 514 515 516 517 518
        }
    }
}

void RootView::Render()
{
#if defined __linux__ || defined __LITEOS__ || defined __APPLE__
    pthread_mutex_lock(&lock_);
#endif
P
pssea 已提交
519 520 521
#if !LOCAL_RENDER
   OptimizeInvalidateRects();
#endif
M
mamingshuai 已提交
522 523 524

#if LOCAL_RENDER
    if (!invalidateMap_.empty()) {
N
niulihua 已提交
525
        RenderManager::RenderRect(GetRect(), this);
M
mamingshuai 已提交
526 527
        invalidateMap_.clear();
#else
P
pssea 已提交
528
    if ( invalidateRects_.Size() > 0) {
529 530 531 532
#if (FULLY_RENDER != 1)
        // only draw invalid rects. in this case, buffers (if there are two buffers or more to display) should keep
        // same with each others, because only delta changes write to the buffer between each frames, so it fits one
        // buffer to display.
P
pssea 已提交
533 534 535
        for (ListNode<Rect>* iter = invalidateRects_.Begin(); iter != invalidateRects_.End(); iter = iter->next_) {
            RenderManager::RenderRect(iter->data_, this);
        }
536 537 538 539 540
#else
        // fully draw whole reacts. in this case, buffers (if there are two buffers or more to display) could be
        // independent on each others, so it fits two buffers or more to display.
        RenderManager::RenderRect(GetScreenRect(), this);
#endif
P
pssea 已提交
541
        invalidateRects_.Clear();
M
mamingshuai 已提交
542 543 544 545 546 547 548 549
#endif

#if ENABLE_WINDOW
        if (boundWindow_) {
            boundWindow_->Flush();
            boundWindow_->Update();
        }
#endif
N
niulihua 已提交
550
        BaseGfxEngine::GetInstance()->Flush();
M
mamingshuai 已提交
551
    }
552 553 554
#if defined __linux__ || defined __LITEOS__ || defined __APPLE__
    pthread_mutex_unlock(&lock_);
#endif
M
mamingshuai 已提交
555 556
}

N
niulihua 已提交
557 558 559 560 561 562 563 564 565 566 567 568 569 570
void RootView::BlitMapBuffer(Rect& curViewRect, TransformMap& transMap, const Rect& invalidatedArea)
{
    Rect invalidRect = curViewRect;
    transMap.SetTransMapRect(curViewRect);
    invalidRect.Join(invalidRect, transMap.GetBoxRect());
    if (invalidRect.Intersect(invalidRect, invalidatedArea)) {
        uint8_t pxSize = DrawUtils::GetPxSizeByColorMode(dc_.mapBufferInfo->mode);
        ImageInfo imageInfo;
        imageInfo.header.colorMode = dc_.mapBufferInfo->mode;
        imageInfo.dataSize = dc_.mapBufferInfo->width * dc_.mapBufferInfo->height * (pxSize >> 3);
        imageInfo.header.width = dc_.mapBufferInfo->width;
        imageInfo.header.height = dc_.mapBufferInfo->height;
        imageInfo.header.reserved = 0;
        imageInfo.data = reinterpret_cast<uint8_t*>(dc_.mapBufferInfo->virAddr);
571 572 573
        TransformDataInfo imageTranDataInfo = {imageInfo.header, imageInfo.data, pxSize, LEVEL0, BILINEAR};
        BaseGfxEngine::GetInstance()->DrawTransform(*dc_.bufferInfo, invalidRect, {0, 0}, Color::Black(), OPA_OPAQUE,
                                                    transMap, imageTranDataInfo);
N
niulihua 已提交
574 575 576 577 578 579 580 581
    }
}

void RootView::ClearMapBuffer()
{
    uint8_t pxSize = DrawUtils::GetPxSizeByColorMode(dc_.mapBufferInfo->mode);
    uint32_t dataSize = dc_.mapBufferInfo->width * dc_.mapBufferInfo->height * (pxSize >> 3);
    if (memset_s(dc_.mapBufferInfo->virAddr, dataSize, 0, dataSize) != EOK) {
582
        GRAPHIC_LOGE("animator buffer memset failed.");
N
niulihua 已提交
583 584 585
    }
}

W
wangtiantian 已提交
586
void RootView::UpdateMapBufferInfo(Rect& invalidatedArea)
587
{
W
wangtiantian 已提交
588 589 590 591 592 593
    int16_t width = invalidatedArea.GetWidth();
    int16_t height = invalidatedArea.GetHeight();
    invalidatedArea.SetWidth(height);
    invalidatedArea.SetHeight(width);
    dc_.mapBufferInfo->width = height;
    dc_.mapBufferInfo->height = width;
594 595 596 597 598 599 600 601 602 603 604 605
    dc_.mapBufferInfo->stride = dc_.mapBufferInfo->width *
                                (DrawUtils::GetPxSizeByColorMode(dc_.mapBufferInfo->mode) >> 3); // 3: Shift 3 bits
}

void RootView::RestoreMapBufferInfo()
{
    dc_.mapBufferInfo->width = dc_.bufferInfo->width;
    dc_.mapBufferInfo->height = dc_.bufferInfo->height;
    dc_.mapBufferInfo->stride = dc_.mapBufferInfo->width *
                                (DrawUtils::GetPxSizeByColorMode(dc_.mapBufferInfo->mode) >> 3); // 3: Shift 3 bits
}

M
mamingshuai 已提交
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
void RootView::DrawTop(UIView* view, const Rect& rect)
{
    if (view == nullptr) {
        return;
    }

    int16_t stackCount = 0;
    UIView* par = view->GetParent();
    if (par == nullptr) {
        par = view;
    }
    UIView* curView = view;
    UIView* transViewGroup = nullptr;
    Rect curViewRect;
    Rect mask = rect;
621 622
    Rect origRect;
    Rect relativeRect;
L
l00417912 已提交
623
    bool enableAnimator = false;
N
niulihua 已提交
624
    TransformMap curTransMap;
625
    bool updateMapBufferInfo = false;
N
niulihua 已提交
626

N
niulihua 已提交
627 628 629 630 631 632
#if ENABLE_WINDOW
    WindowImpl* boundWin = static_cast<WindowImpl*>(GetBoundWindow());
    BufferInfo* gfxDstBuffer = boundWin->GetBufferInfo();
    UpdateBufferInfo(gfxDstBuffer);
#endif

M
mamingshuai 已提交
633 634 635
    while (par != nullptr) {
        if (curView != nullptr) {
            if (curView->IsVisible()) {
L
l00417912 已提交
636
                if (curViewRect.Intersect(curView->GetMaskedRect(), mask) || enableAnimator) {
M
mamingshuai 已提交
637
                    if ((curView->GetViewType() != UI_IMAGE_VIEW) && (curView->GetViewType() != UI_TEXTURE_MAPPER) &&
L
l00417912 已提交
638
                        !curView->IsTransInvalid() && !enableAnimator) {
639 640
                        origRect = curView->GetOrigRect();
                        relativeRect = curView->GetRelativeRect();
M
mamingshuai 已提交
641
                        curView->GetTransformMap().SetInvalid(true);
642 643 644
                        curView->SetPosition(
                            relativeRect.GetX() - origRect.GetX() - curView->GetStyle(STYLE_MARGIN_LEFT),
                            relativeRect.GetY() - origRect.GetY() - curView->GetStyle(STYLE_MARGIN_TOP));
N
niulihua 已提交
645

N
niulihua 已提交
646 647
                        ClearMapBuffer();
                        curTransMap = curView->GetTransformMap();
L
l00417912 已提交
648
                        enableAnimator = true;
M
mamingshuai 已提交
649
                    }
N
niulihua 已提交
650 651

                    if (enableAnimator) {
N
niulihua 已提交
652 653 654
                        Rect invalidatedArea;
                        invalidatedArea.SetWidth(dc_.mapBufferInfo->width);
                        invalidatedArea.SetHeight(dc_.mapBufferInfo->height);
655
                        if (invalidatedArea.GetWidth() < curView->GetWidth()) {
W
wangtiantian 已提交
656
                            UpdateMapBufferInfo(invalidatedArea);
657 658
                            updateMapBufferInfo = true;
                        }
N
niulihua 已提交
659 660
                        curView->OnDraw(*dc_.mapBufferInfo, invalidatedArea);
                        curViewRect = invalidatedArea;
N
niulihua 已提交
661 662 663
                    } else {
                        curView->OnDraw(*dc_.bufferInfo, curViewRect);
                    }
M
mamingshuai 已提交
664 665

                    if ((curView->IsViewGroup()) && (stackCount < COMPONENT_NESTING_DEPTH)) {
L
l00417912 已提交
666
                        if (enableAnimator && (transViewGroup == nullptr)) {
M
mamingshuai 已提交
667 668 669 670 671 672
                            transViewGroup = curView;
                        }
                        par = curView;
                        g_viewStack[stackCount] = curView;
                        g_maskStack[stackCount] = mask;
                        stackCount++;
L
Lizhiqi 已提交
673
                        curView = static_cast<UIViewGroup*>(curView)->GetChildrenRenderHead();
M
mamingshuai 已提交
674 675 676 677
                        mask = par->GetContentRect();
                        mask.Intersect(mask, curViewRect);
                        continue;
                    }
N
niulihua 已提交
678 679 680 681 682 683 684

                    if (enableAnimator) {
                        curView->OnPostDraw(*dc_.mapBufferInfo, curViewRect);
                    } else {
                        curView->OnPostDraw(*dc_.bufferInfo, curViewRect);
                    }

L
l00417912 已提交
685
                    if (enableAnimator && (transViewGroup == nullptr)) {
N
niulihua 已提交
686
                        BlitMapBuffer(origRect, curTransMap, mask);
687 688 689 690
                        if (updateMapBufferInfo) {
                            RestoreMapBufferInfo();
                            updateMapBufferInfo = false;
                        }
M
mamingshuai 已提交
691
                        curView->GetTransformMap().SetInvalid(false);
L
l00417912 已提交
692
                        enableAnimator = false;
693 694
                        curView->SetPosition(relativeRect.GetX() - curView->GetStyle(STYLE_MARGIN_LEFT),
                                             relativeRect.GetY() - curView->GetStyle(STYLE_MARGIN_TOP));
M
mamingshuai 已提交
695 696 697
                    }
                }
            }
L
Lizhiqi 已提交
698
            curView = curView->GetNextRenderSibling();
M
mamingshuai 已提交
699 700 701 702 703
            continue;
        }
        if (--stackCount >= 0) {
            curViewRect = par->GetMaskedRect();
            mask = g_maskStack[stackCount];
N
niulihua 已提交
704 705 706
            if (enableAnimator) {
                par->OnPostDraw(*dc_.mapBufferInfo, curViewRect);
            } else if (curViewRect.Intersect(curViewRect, mask)) {
N
niulihua 已提交
707
                par->OnPostDraw(*dc_.bufferInfo, curViewRect);
M
mamingshuai 已提交
708
            }
N
niulihua 已提交
709

L
l00417912 已提交
710
            if (enableAnimator && transViewGroup == g_viewStack[stackCount]) {
N
niulihua 已提交
711
                BlitMapBuffer(origRect, curTransMap, mask);
712 713 714 715
                if (updateMapBufferInfo) {
                    RestoreMapBufferInfo();
                    updateMapBufferInfo = false;
                }
M
mamingshuai 已提交
716
                transViewGroup->GetTransformMap().SetInvalid(false);
L
l00417912 已提交
717
                enableAnimator = false;
718 719
                transViewGroup->SetPosition(relativeRect.GetX() - transViewGroup->GetStyle(STYLE_MARGIN_LEFT),
                                            relativeRect.GetY() - transViewGroup->GetStyle(STYLE_MARGIN_TOP));
M
mamingshuai 已提交
720 721
                transViewGroup = nullptr;
            }
L
Lizhiqi 已提交
722
            curView = g_viewStack[stackCount]->GetNextRenderSibling();
M
mamingshuai 已提交
723 724 725 726
            par = par->GetParent();
            continue;
        }
        stackCount = 0;
L
Lizhiqi 已提交
727
        curView = par->GetNextRenderSibling();
Y
YueBiang 已提交
728
        if (enableAnimator) {
729
            par->OnPostDraw(*dc_.mapBufferInfo, rect);
Y
YueBiang 已提交
730
        } else {
731
            par->OnPostDraw(*dc_.bufferInfo, rect);
Y
YueBiang 已提交
732
        }
M
mamingshuai 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745
        par = par->GetParent();
    }
}

UIView* RootView::GetTopUIView(const Rect& rect)
{
    int16_t stackCount = 0;
    UIView* currentView = this;
    UIView* topView = currentView;
    Rect copyRect(rect);
    while (stackCount >= 0) {
        while (currentView != nullptr) {
            if (currentView->GetOrigRect().IsContains(rect) && currentView->IsVisible()) {
746
                if (currentView->GetStyle(STYLE_BACKGROUND_OPA) == OPA_OPAQUE && currentView->OnPreDraw(copyRect) &&
M
mamingshuai 已提交
747 748 749 750 751
                    currentView->GetOpaScale() == OPA_OPAQUE) {
                    topView = currentView;
                }
                if (currentView->IsViewGroup() && stackCount < COMPONENT_NESTING_DEPTH) {
                    g_viewStack[stackCount++] = currentView;
L
Lizhiqi 已提交
752
                    currentView = static_cast<UIViewGroup*>(currentView)->GetChildrenRenderHead();
M
mamingshuai 已提交
753 754 755
                    continue;
                }
            }
L
Lizhiqi 已提交
756
            currentView = currentView->GetNextRenderSibling();
M
mamingshuai 已提交
757 758
        }
        if (--stackCount >= 0) {
L
Lizhiqi 已提交
759
            currentView = (g_viewStack[stackCount])->GetNextRenderSibling();
M
mamingshuai 已提交
760 761
        }
    }
L
liqiang 已提交
762 763 764 765 766 767 768 769
    UIView* parentView = topView;
    while (parentView->GetParent() != nullptr) {
        UIView* tempView = parentView;
        parentView = parentView->GetParent();
        if (!tempView->IsTransInvalid()) {
            topView = parentView;
        }
    }
M
mamingshuai 已提交
770 771 772 773 774 775 776 777 778 779 780 781
    return topView;
}

bool RootView::FindSubView(const UIView& parentView, const UIView* subView)
{
    const UIView* root = &parentView;
    if (root == subView) {
        return true;
    } else if (!root->IsViewGroup() || (subView == nullptr)) {
        return false;
    }

L
Lizhiqi 已提交
782
    UIView* currentView = static_cast<const UIViewGroup*>(root)->GetChildrenRenderHead();
M
mamingshuai 已提交
783 784 785 786 787 788 789
    const UIView* parent = root;
    int8_t deep = 1;
    while (deep > 0) {
        if (currentView == subView) {
            return true;
        }
        if (currentView == nullptr) {
L
Lizhiqi 已提交
790
            currentView = parent->GetNextRenderSibling();
M
mamingshuai 已提交
791 792 793 794
            parent = parent->GetParent();
            deep--;
        } else if (currentView->IsViewGroup()) {
            parent = currentView;
L
Lizhiqi 已提交
795
            currentView = static_cast<UIViewGroup*>(currentView)->GetChildrenRenderHead();
M
mamingshuai 已提交
796 797
            deep++;
        } else {
L
Lizhiqi 已提交
798
            currentView = currentView->GetNextRenderSibling();
M
mamingshuai 已提交
799 800 801 802
        }
    }
    return false;
}
N
niulihua 已提交
803 804 805 806

void RootView::InitMapBufferInfo(BufferInfo* bufferInfo)
{
    dc_.mapBufferInfo = new BufferInfo();
N
niulihua 已提交
807 808 809
    if (dc_.mapBufferInfo == nullptr) {
        return;
    }
N
niulihua 已提交
810

N
niulihua 已提交
811 812 813 814 815
    if (memcpy_s(dc_.mapBufferInfo, sizeof(BufferInfo), bufferInfo, sizeof(BufferInfo)) != EOK) {
        delete dc_.mapBufferInfo;
        dc_.mapBufferInfo = nullptr;
        return;
    }
Y
YueBiang 已提交
816 817 818 819
    dc_.mapBufferInfo->mode = ARGB8888;
    dc_.mapBufferInfo->stride = dc_.mapBufferInfo->width *
        (DrawUtils::GetPxSizeByColorMode(dc_.mapBufferInfo->mode) >> 3); // 3: Shift right 3 bits
    uint32_t bufferSize = dc_.mapBufferInfo->stride * dc_.mapBufferInfo->height;
N
niulihua 已提交
820 821
    dc_.mapBufferInfo->virAddr = dc_.mapBufferInfo->phyAddr =
        BaseGfxEngine::GetInstance()->AllocBuffer(bufferSize, BUFFER_MAP_SURFACE);
N
niulihua 已提交
822 823 824 825 826
}

void RootView::DestroyMapBufferInfo()
{
    if (dc_.mapBufferInfo != nullptr) {
827
        BaseGfxEngine::GetInstance()->FreeBuffer(static_cast<uint8_t*>(dc_.mapBufferInfo->virAddr));
N
niulihua 已提交
828 829 830 831 832 833 834 835
        dc_.mapBufferInfo->virAddr = dc_.mapBufferInfo->phyAddr = nullptr;
        delete dc_.mapBufferInfo;
        dc_.mapBufferInfo = nullptr;
    }
}

void RootView::InitDrawContext()
{
N
niulihua 已提交
836
    dc_.bufferInfo = BaseGfxEngine::GetInstance()->GetFBBufferInfo();
N
niulihua 已提交
837 838 839
    if (dc_.bufferInfo != nullptr) {
        InitMapBufferInfo(dc_.bufferInfo);
    }
840 841 842

    bakDc_.bufferInfo = nullptr;
    bakDc_.mapBufferInfo = nullptr;
N
niulihua 已提交
843 844 845 846 847 848 849
}

void RootView::DestroyDrawContext()
{
    DestroyMapBufferInfo();
}

N
niulihua 已提交
850
void RootView::UpdateBufferInfo(BufferInfo* fbBufferInfo)
N
niulihua 已提交
851 852
{
    if (dc_.bufferInfo == nullptr) {
N
niulihua 已提交
853 854
        dc_.bufferInfo = fbBufferInfo;
        InitMapBufferInfo(fbBufferInfo);
N
niulihua 已提交
855
    } else {
856
        if (dc_.bufferInfo->width != fbBufferInfo->width || dc_.bufferInfo->height != fbBufferInfo->height ||
N
niulihua 已提交
857
            dc_.bufferInfo->mode != fbBufferInfo->mode) {
N
niulihua 已提交
858
            DestroyMapBufferInfo();
N
niulihua 已提交
859
            InitMapBufferInfo(fbBufferInfo);
N
niulihua 已提交
860
        }
N
niulihua 已提交
861
        dc_.bufferInfo = fbBufferInfo;
N
niulihua 已提交
862 863
    }
}
N
niulihua 已提交
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883

void RootView::SaveDrawContext()
{
    bakDc_.bufferInfo = dc_.bufferInfo;
    dc_.bufferInfo = nullptr;

    bakDc_.mapBufferInfo = dc_.mapBufferInfo;
    dc_.mapBufferInfo = nullptr;
}

void RootView::RestoreDrawContext()
{
    DestroyDrawContext();

    dc_.bufferInfo = bakDc_.bufferInfo;
    bakDc_.bufferInfo = nullptr;

    dc_.mapBufferInfo = bakDc_.mapBufferInfo;
    bakDc_.mapBufferInfo = nullptr;
}
N
niulihua 已提交
884
} // namespace OHOS