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
25 namespace OHOS {
UISurfaceView()26 UISurfaceView::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
~UISurfaceView()38 UISurfaceView::~UISurfaceView()
39 {
40 if (surface_ != nullptr) {
41 delete surface_;
42 surface_ = nullptr;
43 }
44 }
45
SetPosition(int16_t x,int16_t y)46 void 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
SetPosition(int16_t x,int16_t y,int16_t width,int16_t height)59 void 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
Resize(int16_t width,int16_t height)74 void 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
SetWidth(int16_t width)85 void 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
SetHeight(int16_t height)95 void 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
SetX(int16_t x)105 void 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
SetY(int16_t y)116 void 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
GetSurface() const127 Surface* UISurfaceView::GetSurface() const
128 {
129 return surface_;
130 }
131
OnPreDraw(Rect & invalidatedArea) const132 bool UISurfaceView::OnPreDraw(Rect& invalidatedArea) const
133 {
134 // need fill transparent color
135 return false;
136 }
137
OnDraw(BufferInfo & gfxDstBuffer,const Rect & invalidatedArea)138 void UISurfaceView::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
139 {
140 Draw(gfxDstBuffer, invalidatedArea);
141 }
142
Draw(BufferInfo & gfxDstBuffer,const Rect & invalidatedArea)143 void 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