1 /*
2 * Copyright (c) 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 "surface_ohos_raster.h"
17
18 #include "sync_fence.h"
19 #include "surface_frame_ohos_raster.h"
20 #include "drawing_utils.h"
21
22 namespace OHOS {
23 namespace Rosen {
SurfaceOhosRaster(const sptr<Surface> & producer)24 SurfaceOhosRaster::SurfaceOhosRaster(const sptr<Surface>& producer)
25 : SurfaceOhos(producer), frame_(nullptr)
26 {
27 }
28
~SurfaceOhosRaster()29 SurfaceOhosRaster::~SurfaceOhosRaster()
30 {
31 frame_ = nullptr;
32 }
33
34
NativeRequestFrame(int32_t width,int32_t height)35 std::unique_ptr<SurfaceFrame> SurfaceOhosRaster::NativeRequestFrame(int32_t width, int32_t height)
36 {
37 return RequestFrame(width, height);
38 }
39
40
NativeFlushFrame(std::unique_ptr<SurfaceFrame> & frame)41 bool SurfaceOhosRaster::NativeFlushFrame(std::unique_ptr<SurfaceFrame>& frame)
42 {
43 return FlushFrame(frame);
44 }
45
RequestFrame(int32_t width,int32_t height)46 std::unique_ptr<SurfaceFrame> SurfaceOhosRaster::RequestFrame(int32_t width, int32_t height)
47 {
48 if (IsValid() == false) {
49 LOGE("SurfaceOhosRaster::RequestFrame, producer is nullptr");
50 return nullptr;
51 }
52 frame_ = std::make_unique<SurfaceFrameOhosRaster>(width, height);
53 SurfaceError err = producer_->RequestBuffer(frame_->buffer_, frame_->releaseFence_, frame_->requestConfig_);
54 if (err != SURFACE_ERROR_OK) {
55 LOGE("SurfaceOhosRaster::Requestframe Failed, error is : %{public}s", SurfaceErrorStr(err).c_str());
56 return nullptr;
57 }
58 sptr<SyncFence> tempFence = new SyncFence(frame_->releaseFence_);
59 int res = tempFence->Wait(3000);
60 if (res < 0) {
61 LOGE("RequestFrame this buffer is not available");
62 }
63
64 LOGD("SurfaceOhosRaster RequestFrame successfully!, buffer width is %{public}d, height is %{public}d",
65 frame_->buffer_->GetWidth(), frame_->buffer_->GetHeight());
66
67 std::unique_ptr<SurfaceFrame> ret(std::move(frame_));
68 return ret;
69 }
70
FlushFrame(std::unique_ptr<SurfaceFrame> & frame)71 bool SurfaceOhosRaster::FlushFrame(std::unique_ptr<SurfaceFrame>& frame)
72 {
73 // SurfaceOhosRaster is the class for platform OHOS, the input pointer should be the pointer to the class
74 // SurfaceFrameOhos.
75 // We use static_cast instead of RTTI and dynamic_cast which are not permitted
76
77 SurfaceFrameOhosRaster* oriFramePtr = static_cast<SurfaceFrameOhosRaster*>(frame.get());
78 SurfaceError err = producer_->FlushBuffer(oriFramePtr->buffer_, -1, oriFramePtr->flushConfig_);
79 if (err != SURFACE_ERROR_OK) {
80 LOGE("SurfaceOhosRaster::Flushframe Failed, error is : %s", SurfaceErrorStr(err).c_str());
81 return false;
82 }
83 LOGE("SurfaceOhosRaster::FlushFrame fence:%d", oriFramePtr->releaseFence_);
84 return true;
85 }
86
GetSkCanvas(std::unique_ptr<SurfaceFrame> & frame)87 SkCanvas* SurfaceOhosRaster::GetSkCanvas(std::unique_ptr<SurfaceFrame>& frame)
88 {
89 if (drawingProxy_ == nullptr) {
90 LOGE("drawingProxy_ is nullptr, can not GetCanvas");
91 return nullptr;
92 }
93 return drawingProxy_->AcquireSkCanvas(frame);
94 }
95 } // namespace Rosen
96 } // namespace OHOS