• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
RequestFrame(int32_t width,int32_t height)34 std::unique_ptr<SurfaceFrame> SurfaceOhosRaster::RequestFrame(int32_t width, int32_t height)
35 {
36     if (IsValid() == false) {
37         LOGE("SurfaceOhosRaster::RequestFrame, producer is nullptr");
38         return nullptr;
39     }
40     frame_ = std::make_unique<SurfaceFrameOhosRaster>(width, height);
41     SurfaceError err = producer_->RequestBuffer(frame_->buffer_, frame_->releaseFence_, frame_->requestConfig_);
42     if (err != SURFACE_ERROR_OK) {
43         LOGE("SurfaceOhosRaster::Requestframe Failed, error is : %{public}s", SurfaceErrorStr(err).c_str());
44         return nullptr;
45     }
46     sptr<SyncFence> tempFence = new SyncFence(frame_->releaseFence_);
47     int res = tempFence->Wait(3000);
48     if (res < 0) {
49         LOGE("RequestFrame this buffer is not available");
50     }
51 
52     LOGD("SurfaceOhosRaster RequestFrame successfully!, buffer width is %{public}d, height is %{public}d",
53         frame_->buffer_->GetWidth(), frame_->buffer_->GetHeight());
54 
55     std::unique_ptr<SurfaceFrame> ret(std::move(frame_));
56     return ret;
57 }
58 
FlushFrame(std::unique_ptr<SurfaceFrame> & frame)59 bool SurfaceOhosRaster::FlushFrame(std::unique_ptr<SurfaceFrame>& frame)
60 {
61     // SurfaceOhosRaster is the class for platform OHOS, the input pointer should be the pointer to the class
62     // SurfaceFrameOhos.
63     // We use static_cast instead of RTTI and dynamic_cast which are not permitted
64 
65     SurfaceFrameOhosRaster* oriFramePtr = static_cast<SurfaceFrameOhosRaster*>(frame.get());
66     SurfaceError err = producer_->FlushBuffer(oriFramePtr->buffer_, -1, oriFramePtr->flushConfig_);
67     if (err != SURFACE_ERROR_OK) {
68         LOGE("SurfaceOhosRaster::Flushframe Failed, error is : %s", SurfaceErrorStr(err).c_str());
69         return false;
70     }
71     LOGE("SurfaceOhosRaster::FlushFrame fence:%d", oriFramePtr->releaseFence_);
72     return true;
73 }
74 
GetCanvas(std::unique_ptr<SurfaceFrame> & frame)75 SkCanvas* SurfaceOhosRaster::GetCanvas(std::unique_ptr<SurfaceFrame>& frame)
76 {
77     return drawingProxy_->AcquireCanvas(frame);
78 }
79 } // namespace Rosen
80 } // namespace OHOS