1 /*
2  * Copyright (c) 2021-2022 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 "rs_surface_frame_ohos_raster.h"
17 #include <cstdint>
18 
19 #include "platform/common/rs_log.h"
20 
21 namespace OHOS {
22 namespace Rosen {
23 
RSSurfaceFrameOhosRaster(int32_t width,int32_t height)24 RSSurfaceFrameOhosRaster::RSSurfaceFrameOhosRaster(int32_t width, int32_t height)
25 {
26     requestConfig_.width = width;
27     requestConfig_.height = height;
28     flushConfig_.damage.w = width;
29     flushConfig_.damage.h = height;
30 }
31 
SetDamageRegion(int32_t left,int32_t top,int32_t width,int32_t height)32 void RSSurfaceFrameOhosRaster::SetDamageRegion(int32_t left, int32_t top, int32_t width, int32_t height)
33 {
34     flushConfig_.damage.x = left;
35     flushConfig_.damage.y = top;
36     flushConfig_.damage.w = width;
37     flushConfig_.damage.h = height;
38 }
39 
SetDamageRegion(const std::vector<RectI> & rects)40 void RSSurfaceFrameOhosRaster::SetDamageRegion(const std::vector<RectI> &rects)
41 {
42     // currently not support SetDamageRegion
43     ROSEN_LOGE("currently not support SetDamageRegion");
44 }
45 
GetBufferAge() const46 int32_t RSSurfaceFrameOhosRaster::GetBufferAge() const
47 {
48     // currently could not get real buffer age, so return invalid age
49     // should be supported by surface
50     ROSEN_LOGD("RSSurfaceFrameOhosRaster::GetBufferAge return -1");
51     return -1;
52 }
53 
GetCanvas()54 Drawing::Canvas* RSSurfaceFrameOhosRaster::GetCanvas()
55 {
56     if (buffer_ == nullptr || buffer_->GetWidth() <= 0 || buffer_->GetHeight() <= 0) {
57         ROSEN_LOGW("buffer is invalid");
58         return nullptr;
59     }
60     if (surface_ == nullptr) {
61         CreateSurface();
62     }
63     return surface_->GetCanvas().get();
64 }
65 
GetSurface()66 std::shared_ptr<Drawing::Surface> RSSurfaceFrameOhosRaster::GetSurface()
67 {
68     if (buffer_ == nullptr || buffer_->GetWidth() <= 0 || buffer_->GetHeight() <= 0) {
69         ROSEN_LOGW("buffer is invalid");
70         return nullptr;
71     }
72     if (surface_ == nullptr) {
73         CreateSurface();
74     }
75     return surface_;
76 }
77 
CreateSurface()78 void RSSurfaceFrameOhosRaster::CreateSurface()
79 {
80     auto addr = static_cast<uint32_t*>(buffer_->GetVirAddr());
81     if (addr == nullptr) {
82         ROSEN_LOGW("buffer addr is invalid");
83         return;
84     }
85     Drawing::Bitmap bitmap;
86     Drawing::BitmapFormat format { Drawing::COLORTYPE_RGBA_8888, Drawing::ALPHATYPE_PREMUL};
87     bitmap.Build(buffer_->GetWidth(), buffer_->GetHeight(), format);
88     bitmap.SetPixels(addr);
89     surface_ = std::make_shared<Drawing::Surface>();
90     surface_->Bind(bitmap);
91 }
92 
GetReleaseFence() const93 int32_t RSSurfaceFrameOhosRaster::GetReleaseFence() const
94 {
95     return releaseFence_;
96 }
97 
SetReleaseFence(const int32_t & fence)98 void RSSurfaceFrameOhosRaster::SetReleaseFence(const int32_t& fence)
99 {
100     releaseFence_ = fence;
101 }
102 
103 } // namespace Rosen
104 } // namespace OHOS