1 /*
2 * Copyright (c) 2024-2024 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 "moving_photo/moving_photo_surface_wrapper.h"
17
18 #include <memory>
19 #include <mutex>
20 #include <new>
21
22 #include "camera_log.h"
23 #include "graphic_common_c.h"
24 #include "surface_type.h"
25 #include "sync_fence.h"
26
27 namespace OHOS {
28 namespace CameraStandard {
CreateMovingPhotoSurfaceWrapper(int32_t width,int32_t height)29 sptr<MovingPhotoSurfaceWrapper> MovingPhotoSurfaceWrapper::CreateMovingPhotoSurfaceWrapper(
30 int32_t width, int32_t height)
31 {
32 CHECK_ERROR_RETURN_RET_LOG(width <= 0 || height <= 0, nullptr,
33 "MovingPhotoSurfaceWrapper::CreateMovingPhotoSurfaceWrapper size "
34 "invalid, width:%{public}d, height:%{public}d", width, height);
35 sptr<MovingPhotoSurfaceWrapper> movingPhotoSurfaceWrapper = new (std::nothrow) MovingPhotoSurfaceWrapper();
36 CHECK_ERROR_RETURN_RET_LOG(movingPhotoSurfaceWrapper == nullptr, nullptr,
37 "MovingPhotoSurfaceWrapper::CreateMovingPhotoSurfaceWrapper fail.");
38 bool initResult = movingPhotoSurfaceWrapper->Init(width, height);
39 CHECK_ERROR_RETURN_RET(initResult, movingPhotoSurfaceWrapper);
40 MEDIA_ERR_LOG("MovingPhotoSurfaceWrapper::CreateMovingPhotoSurfaceWrapper init fail.");
41 return nullptr;
42 }
43
~MovingPhotoSurfaceWrapper()44 MovingPhotoSurfaceWrapper::~MovingPhotoSurfaceWrapper()
45 {
46 MEDIA_INFO_LOG("MovingPhotoSurfaceWrapper::~MovingPhotoSurfaceWrapper");
47 }
48
GetProducer() const49 sptr<OHOS::IBufferProducer> MovingPhotoSurfaceWrapper::GetProducer() const
50 {
51 std::lock_guard<std::recursive_mutex> lock(videoSurfaceMutex_);
52 CHECK_ERROR_RETURN_RET(videoSurface_ == nullptr, nullptr);
53 return videoSurface_->GetProducer();
54 }
55
Init(int32_t width,int32_t height)56 bool MovingPhotoSurfaceWrapper::Init(int32_t width, int32_t height)
57 {
58 std::lock_guard<std::recursive_mutex> lock(videoSurfaceMutex_);
59 videoSurface_ = Surface::CreateSurfaceAsConsumer("movingPhoto");
60 CHECK_ERROR_RETURN_RET_LOG(videoSurface_ == nullptr, false, "MovingPhotoSurfaceWrapper::Init create surface fail.");
61 auto err = videoSurface_->SetDefaultUsage(BUFFER_USAGE_VIDEO_ENCODER);
62 CHECK_ERROR_RETURN_RET_LOG(err != GSERROR_OK, false, "MovingPhotoSurfaceWrapper::Init SetDefaultUsage fail.");
63 bufferConsumerListener_ = new (std::nothrow) BufferConsumerListener(this);
64 err = videoSurface_->RegisterConsumerListener(bufferConsumerListener_);
65 CHECK_ERROR_RETURN_RET_LOG(err != GSERROR_OK, false,
66 "MovingPhotoSurfaceWrapper::Init RegisterConsumerListener fail.");
67 err = videoSurface_->SetDefaultWidthAndHeight(width, height);
68 CHECK_ERROR_RETURN_RET_LOG(err != GSERROR_OK, false,
69 "MovingPhotoSurfaceWrapper::Init SetDefaultWidthAndHeight fail.");
70 return true;
71 }
72
OnBufferArrival()73 void MovingPhotoSurfaceWrapper::OnBufferArrival()
74 {
75 std::lock_guard<std::recursive_mutex> lock(videoSurfaceMutex_);
76 CHECK_ERROR_RETURN_LOG(videoSurface_ == nullptr, "MovingPhotoSurfaceWrapper::OnBufferArrival surface is nullptr");
77 auto transform = videoSurface_->GetTransform();
78 MEDIA_DEBUG_LOG("MovingPhotoSurfaceWrapper::OnBufferArrival queueSize %{public}u, transform %{public}d",
79 videoSurface_->GetQueueSize(), transform);
80
81 int64_t timestamp;
82 OHOS::Rect damage;
83 sptr<SurfaceBuffer> buffer;
84 sptr<SyncFence> syncFence = SyncFence::INVALID_FENCE;
85 GSError err = videoSurface_->AcquireBuffer(buffer, syncFence, timestamp, damage);
86 CHECK_ERROR_RETURN_LOG(err != GSERROR_OK, "Failed to acquire surface buffer");
87
88 auto surfaceBufferListener = GetSurfaceBufferListener();
89 if (surfaceBufferListener == nullptr) {
90 MEDIA_DEBUG_LOG("MovingPhotoSurfaceWrapper::OnBufferArrival surfaceBufferListener_ is nullptr.");
91 err = videoSurface_->ReleaseBuffer(buffer, SyncFence::INVALID_FENCE);
92 CHECK_ERROR_PRINT_LOG(err != GSERROR_OK, "MovingPhotoSurfaceWrapper::OnBufferArrival ReleaseBuffer fail.");
93 return;
94 }
95
96 err = videoSurface_->DetachBufferFromQueue(buffer);
97 CHECK_ERROR_RETURN_LOG(err != GSERROR_OK,
98 "MovingPhotoSurfaceWrapper::OnBufferArrival detach buffer fail. %{public}d", err);
99 MEDIA_DEBUG_LOG("MovingPhotoSurfaceWrapper::OnBufferArrival buffer %{public}d x %{public}d, stride is %{public}d",
100 buffer->GetSurfaceBufferWidth(), buffer->GetSurfaceBufferHeight(), buffer->GetStride());
101 surfaceBufferListener->OnBufferArrival(buffer, timestamp, transform);
102 }
103
BufferConsumerListener(sptr<MovingPhotoSurfaceWrapper> surfaceWrapper)104 MovingPhotoSurfaceWrapper::BufferConsumerListener::BufferConsumerListener(
105 sptr<MovingPhotoSurfaceWrapper> surfaceWrapper)
106 : movingPhotoSurfaceWrapper_(surfaceWrapper)
107 {}
108
OnBufferAvailable()109 void MovingPhotoSurfaceWrapper::BufferConsumerListener::OnBufferAvailable()
110 {
111 auto surfaceWrapper = movingPhotoSurfaceWrapper_.promote();
112 CHECK_EXECUTE(surfaceWrapper != nullptr, surfaceWrapper->OnBufferArrival());
113 }
114
RecycleBuffer(sptr<SurfaceBuffer> buffer)115 void MovingPhotoSurfaceWrapper::RecycleBuffer(sptr<SurfaceBuffer> buffer)
116 {
117 std::lock_guard<std::recursive_mutex> lock(videoSurfaceMutex_);
118
119 GSError err = videoSurface_->AttachBufferToQueue(buffer);
120 CHECK_ERROR_RETURN_LOG(err != GSERROR_OK, "Failed to attach buffer %{public}d", err);
121 err = videoSurface_->ReleaseBuffer(buffer, SyncFence::INVALID_FENCE);
122 CHECK_ERROR_RETURN_LOG(err != GSERROR_OK, "Failed to Release Buffer %{public}d", err);
123 }
124 } // namespace CameraStandard
125 } // namespace OHOS