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