1 /*
2 * Copyright (c) 2025 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 "photo_buffer_consumer.h"
17
18 #include "camera_log.h"
19 #include "task_manager.h"
20 #include "camera_surface_buffer_util.h"
21 #include "camera_report_dfx_uitls.h"
22 #include "hstream_capture.h"
23
24 namespace OHOS {
25 namespace CameraStandard {
26
PhotoBufferConsumer(wptr<HStreamCapture> streamCapture,bool isRaw)27 PhotoBufferConsumer::PhotoBufferConsumer(wptr<HStreamCapture> streamCapture, bool isRaw)
28 : streamCapture_(streamCapture), isRaw_(isRaw)
29 {
30 MEDIA_INFO_LOG("PhotoBufferConsumer new E, isRaw:%{public}d", isRaw);
31 }
32
~PhotoBufferConsumer()33 PhotoBufferConsumer::~PhotoBufferConsumer()
34 {
35 MEDIA_INFO_LOG("PhotoBufferConsumer ~ E");
36 }
37
OnBufferAvailable()38 void PhotoBufferConsumer::OnBufferAvailable()
39 {
40 MEDIA_INFO_LOG("PhotoBufferConsumer OnBufferAvailable E");
41 sptr<HStreamCapture> streamCapture = streamCapture_.promote();
42 CHECK_RETURN_ELOG(streamCapture == nullptr, "streamCapture is null");
43 CHECK_RETURN_ELOG(streamCapture->photoTask_ == nullptr, "photoTask is null");
44 wptr<PhotoBufferConsumer> thisPtr(this);
45 streamCapture->photoTask_->SubmitTask([thisPtr]() {
46 auto listener = thisPtr.promote();
47 CHECK_EXECUTE(listener, listener->ExecuteOnBufferAvailable());
48 });
49 MEDIA_INFO_LOG("PhotoBufferConsumer OnBufferAvailable X");
50 }
51
ExecuteOnBufferAvailable()52 void PhotoBufferConsumer::ExecuteOnBufferAvailable()
53 {
54 MEDIA_INFO_LOG("P_ExecuteOnBufferAvailable E");
55 CAMERA_SYNC_TRACE;
56 sptr<HStreamCapture> streamCapture = streamCapture_.promote();
57 CHECK_RETURN_ELOG(streamCapture == nullptr, "streamCapture is null");
58 sptr<Surface> surface;
59 if (isRaw_) {
60 surface = streamCapture->rawSurface_;
61 } else {
62 surface = streamCapture->surface_;
63 }
64 CHECK_RETURN_ELOG(surface == nullptr, "surface is null");
65 sptr<SurfaceBuffer> surfaceBuffer = nullptr;
66 int32_t fence = -1;
67 int64_t timestamp;
68 OHOS::Rect damage;
69 SurfaceError surfaceRet = surface->AcquireBuffer(surfaceBuffer, fence, timestamp, damage);
70 CHECK_RETURN_ELOG(surfaceRet != SURFACE_ERROR_OK, "PhotoBufferConsumer Failed to acquire surface buffer");
71 int32_t isDegradedImage = CameraSurfaceBufferUtil::GetIsDegradedImage(surfaceBuffer);
72 MEDIA_INFO_LOG("PhotoBufferConsumer ts isDegradedImage:%{public}d", isDegradedImage);
73 MEDIA_INFO_LOG("PhotoBufferConsumer ts is:%{public}" PRId64, timestamp);
74 // deep copy surfaceBuffer
75 sptr<SurfaceBuffer> newSurfaceBuffer = CameraSurfaceBufferUtil::DeepCopyBuffer(surfaceBuffer);
76 // release surfaceBuffer to bufferQueue
77 surface->ReleaseBuffer(surfaceBuffer, -1);
78 CHECK_RETURN_ELOG(newSurfaceBuffer == nullptr, "newSurfaceBuffer is null");
79 int32_t captureId = CameraSurfaceBufferUtil::GetCaptureId(newSurfaceBuffer);
80 CameraReportDfxUtils::GetInstance()->SetFirstBufferEndInfo(captureId);
81 CameraReportDfxUtils::GetInstance()->SetPrepareProxyStartInfo(captureId);
82 streamCapture->OnPhotoAvailable(newSurfaceBuffer, timestamp, isRaw_);
83 MEDIA_INFO_LOG("P_ExecuteOnBufferAvailable X");
84 }
85 } // namespace CameraStandard
86 } // namespace OHOS
87