1a3e0fd82Sopenharmony_ci/*
2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License.
5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at
6a3e0fd82Sopenharmony_ci *
7a3e0fd82Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a3e0fd82Sopenharmony_ci *
9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and
13a3e0fd82Sopenharmony_ci * limitations under the License.
14a3e0fd82Sopenharmony_ci */
15a3e0fd82Sopenharmony_ci
16a3e0fd82Sopenharmony_ci#include "components/ui_surface_view.h"
17a3e0fd82Sopenharmony_ci#if ENABLE_WINDOW
18a3e0fd82Sopenharmony_ci#include <string>
19a3e0fd82Sopenharmony_ci
20a3e0fd82Sopenharmony_ci#include "draw/draw_rect.h"
21a3e0fd82Sopenharmony_ci#include "draw/draw_utils.h"
22a3e0fd82Sopenharmony_ci#include "gfx_utils/graphic_log.h"
23a3e0fd82Sopenharmony_ci#include "surface_buffer.h"
24a3e0fd82Sopenharmony_ci
25a3e0fd82Sopenharmony_cinamespace OHOS {
26a3e0fd82Sopenharmony_ciUISurfaceView::UISurfaceView()
27a3e0fd82Sopenharmony_ci{
28a3e0fd82Sopenharmony_ci    surface_ = Surface::CreateSurface();
29a3e0fd82Sopenharmony_ci    if (surface_ == nullptr) {
30a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("UISurfaceView::UISurfaceView surface create failed\n");
31a3e0fd82Sopenharmony_ci        return;
32a3e0fd82Sopenharmony_ci    }
33a3e0fd82Sopenharmony_ci    surface_->SetWidthAndHeight(GetWidth(), GetHeight());
34a3e0fd82Sopenharmony_ci    surface_->SetQueueSize(DEFAULT_QUEUE_SIZE);
35a3e0fd82Sopenharmony_ci    surface_->SetFormat(IMAGE_PIXEL_FORMAT_ARGB8888);
36a3e0fd82Sopenharmony_ci}
37a3e0fd82Sopenharmony_ci
38a3e0fd82Sopenharmony_ciUISurfaceView::~UISurfaceView()
39a3e0fd82Sopenharmony_ci{
40a3e0fd82Sopenharmony_ci    if (surface_ != nullptr) {
41a3e0fd82Sopenharmony_ci        delete surface_;
42a3e0fd82Sopenharmony_ci        surface_ = nullptr;
43a3e0fd82Sopenharmony_ci    }
44a3e0fd82Sopenharmony_ci}
45a3e0fd82Sopenharmony_ci
46a3e0fd82Sopenharmony_civoid UISurfaceView::SetPosition(int16_t x, int16_t y)
47a3e0fd82Sopenharmony_ci{
48a3e0fd82Sopenharmony_ci    UIView::SetPosition(x, y);
49a3e0fd82Sopenharmony_ci    if (surface_ == nullptr) {
50a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("UISurfaceView::SetPosition surface is null\n");
51a3e0fd82Sopenharmony_ci        return;
52a3e0fd82Sopenharmony_ci    }
53a3e0fd82Sopenharmony_ci    x = GetRect().GetLeft();
54a3e0fd82Sopenharmony_ci    y = GetRect().GetTop();
55a3e0fd82Sopenharmony_ci    surface_->SetUserData(REGION_POSITION_X, std::to_string(x));
56a3e0fd82Sopenharmony_ci    surface_->SetUserData(REGION_POSITION_Y, std::to_string(y));
57a3e0fd82Sopenharmony_ci}
58a3e0fd82Sopenharmony_ci
59a3e0fd82Sopenharmony_civoid UISurfaceView::SetPosition(int16_t x, int16_t y, int16_t width, int16_t height)
60a3e0fd82Sopenharmony_ci{
61a3e0fd82Sopenharmony_ci    UIView::SetPosition(x, y, width, height);
62a3e0fd82Sopenharmony_ci    if (surface_ == nullptr) {
63a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("UISurfaceView::SetPosition surface is null\n");
64a3e0fd82Sopenharmony_ci        return;
65a3e0fd82Sopenharmony_ci    }
66a3e0fd82Sopenharmony_ci    x = GetRect().GetLeft();
67a3e0fd82Sopenharmony_ci    y = GetRect().GetTop();
68a3e0fd82Sopenharmony_ci    surface_->SetUserData(REGION_POSITION_X, std::to_string(x));
69a3e0fd82Sopenharmony_ci    surface_->SetUserData(REGION_POSITION_Y, std::to_string(y));
70a3e0fd82Sopenharmony_ci    surface_->SetUserData(REGION_WIDTH, std::to_string(width));
71a3e0fd82Sopenharmony_ci    surface_->SetUserData(REGION_HEIGHT, std::to_string(height));
72a3e0fd82Sopenharmony_ci}
73a3e0fd82Sopenharmony_ci
74a3e0fd82Sopenharmony_civoid UISurfaceView::Resize(int16_t width, int16_t height)
75a3e0fd82Sopenharmony_ci{
76a3e0fd82Sopenharmony_ci    UIView::Resize(width, height);
77a3e0fd82Sopenharmony_ci    if (surface_ == nullptr) {
78a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("UISurfaceView::Resize surface is null\n");
79a3e0fd82Sopenharmony_ci        return;
80a3e0fd82Sopenharmony_ci    }
81a3e0fd82Sopenharmony_ci    surface_->SetUserData(REGION_WIDTH, std::to_string(width));
82a3e0fd82Sopenharmony_ci    surface_->SetUserData(REGION_HEIGHT, std::to_string(height));
83a3e0fd82Sopenharmony_ci}
84a3e0fd82Sopenharmony_ci
85a3e0fd82Sopenharmony_civoid UISurfaceView::SetWidth(int16_t width)
86a3e0fd82Sopenharmony_ci{
87a3e0fd82Sopenharmony_ci    UIView::SetWidth(width);
88a3e0fd82Sopenharmony_ci    if (surface_ == nullptr) {
89a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("UISurfaceView::SetWidth surface is null\n");
90a3e0fd82Sopenharmony_ci        return;
91a3e0fd82Sopenharmony_ci    }
92a3e0fd82Sopenharmony_ci    surface_->SetUserData(REGION_WIDTH, std::to_string(width));
93a3e0fd82Sopenharmony_ci}
94a3e0fd82Sopenharmony_ci
95a3e0fd82Sopenharmony_civoid UISurfaceView::SetHeight(int16_t height)
96a3e0fd82Sopenharmony_ci{
97a3e0fd82Sopenharmony_ci    UIView::SetHeight(height);
98a3e0fd82Sopenharmony_ci    if (surface_ == nullptr) {
99a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("UISurfaceView::SetHeight surface is null\n");
100a3e0fd82Sopenharmony_ci        return;
101a3e0fd82Sopenharmony_ci    }
102a3e0fd82Sopenharmony_ci    surface_->SetUserData(REGION_HEIGHT, std::to_string(height));
103a3e0fd82Sopenharmony_ci}
104a3e0fd82Sopenharmony_ci
105a3e0fd82Sopenharmony_civoid UISurfaceView::SetX(int16_t x)
106a3e0fd82Sopenharmony_ci{
107a3e0fd82Sopenharmony_ci    UIView::SetX(x);
108a3e0fd82Sopenharmony_ci    if (surface_ == nullptr) {
109a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("UISurfaceView::SetX surface is null\n");
110a3e0fd82Sopenharmony_ci        return;
111a3e0fd82Sopenharmony_ci    }
112a3e0fd82Sopenharmony_ci    x = GetRect().GetLeft();
113a3e0fd82Sopenharmony_ci    surface_->SetUserData(REGION_POSITION_X, std::to_string(x));
114a3e0fd82Sopenharmony_ci}
115a3e0fd82Sopenharmony_ci
116a3e0fd82Sopenharmony_civoid UISurfaceView::SetY(int16_t y)
117a3e0fd82Sopenharmony_ci{
118a3e0fd82Sopenharmony_ci    UIView::SetY(y);
119a3e0fd82Sopenharmony_ci    if (surface_ == nullptr) {
120a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("UISurfaceView::SetY surface is null\n");
121a3e0fd82Sopenharmony_ci        return;
122a3e0fd82Sopenharmony_ci    }
123a3e0fd82Sopenharmony_ci    y = GetRect().GetTop();
124a3e0fd82Sopenharmony_ci    surface_->SetUserData(REGION_POSITION_Y, std::to_string(y));
125a3e0fd82Sopenharmony_ci}
126a3e0fd82Sopenharmony_ci
127a3e0fd82Sopenharmony_ciSurface* UISurfaceView::GetSurface() const
128a3e0fd82Sopenharmony_ci{
129a3e0fd82Sopenharmony_ci    return surface_;
130a3e0fd82Sopenharmony_ci}
131a3e0fd82Sopenharmony_ci
132a3e0fd82Sopenharmony_cibool UISurfaceView::OnPreDraw(Rect& invalidatedArea) const
133a3e0fd82Sopenharmony_ci{
134a3e0fd82Sopenharmony_ci    // need fill transparent color
135a3e0fd82Sopenharmony_ci    return false;
136a3e0fd82Sopenharmony_ci}
137a3e0fd82Sopenharmony_ci
138a3e0fd82Sopenharmony_civoid UISurfaceView::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
139a3e0fd82Sopenharmony_ci{
140a3e0fd82Sopenharmony_ci    Draw(gfxDstBuffer, invalidatedArea);
141a3e0fd82Sopenharmony_ci}
142a3e0fd82Sopenharmony_ci
143a3e0fd82Sopenharmony_civoid UISurfaceView::Draw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
144a3e0fd82Sopenharmony_ci{
145a3e0fd82Sopenharmony_ci    SurfaceBuffer* acquireBuffer = (surface_ != nullptr) ? surface_->AcquireBuffer() : nullptr;
146a3e0fd82Sopenharmony_ci    if (acquireBuffer != nullptr) {
147a3e0fd82Sopenharmony_ci        GRAPHIC_LOGE("UISurfaceView::Draw acquireBufferVirAddr=%p \n", acquireBuffer->GetVirAddr());
148a3e0fd82Sopenharmony_ci        // fill with buffer
149a3e0fd82Sopenharmony_ci        DrawUtils::GetInstance()->DrawWithBuffer(gfxDstBuffer, GetRect(), invalidatedArea,
150a3e0fd82Sopenharmony_ci                                                 reinterpret_cast<const ColorType*>(acquireBuffer->GetVirAddr()));
151a3e0fd82Sopenharmony_ci        surface_->ReleaseBuffer(acquireBuffer);
152a3e0fd82Sopenharmony_ci    } else {
153a3e0fd82Sopenharmony_ci        // fill with transparent color
154a3e0fd82Sopenharmony_ci        DrawUtils::GetInstance()->DrawTranspantArea(gfxDstBuffer, GetRect(), invalidatedArea);
155a3e0fd82Sopenharmony_ci    }
156a3e0fd82Sopenharmony_ci}
157a3e0fd82Sopenharmony_ci} // namespace OHOS
158a3e0fd82Sopenharmony_ci#endif // ENABLE_WINDOW
159