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 "thumbnail_buffer_consumer.h"
17
18 #include "camera_log.h"
19 #include "camera_surface_buffer_util.h"
20 #include "hstream_capture.h"
21 #include "task_manager.h"
22 #include "camera_server_photo_proxy.h"
23 namespace OHOS {
24 namespace CameraStandard {
25
ThumbnailBufferConsumer(wptr<HStreamCapture> streamCapture)26 ThumbnailBufferConsumer::ThumbnailBufferConsumer(wptr<HStreamCapture> streamCapture) : streamCapture_(streamCapture)
27 {
28 MEDIA_INFO_LOG("ThumbnailBufferConsumer new E");
29 }
30
~ThumbnailBufferConsumer()31 ThumbnailBufferConsumer::~ThumbnailBufferConsumer()
32 {
33 MEDIA_INFO_LOG("ThumbnailBufferConsumer ~ E");
34 }
35
OnBufferAvailable()36 void ThumbnailBufferConsumer::OnBufferAvailable()
37 {
38 MEDIA_INFO_LOG("ThumbnailBufferConsumer OnBufferAvailable E");
39 sptr<HStreamCapture> streamCapture = streamCapture_.promote();
40 CHECK_RETURN_ELOG(streamCapture == nullptr, "streamCapture is null");
41 CHECK_RETURN_ELOG(streamCapture->thumbnailTask_ == nullptr, "thumbnailTask is null");
42 wptr<ThumbnailBufferConsumer> thisPtr(this);
43 streamCapture->thumbnailTask_->SubmitTask([thisPtr]() {
44 auto listener = thisPtr.promote();
45 CHECK_EXECUTE(listener, listener->ExecuteOnBufferAvailable());
46 });
47 MEDIA_INFO_LOG("ThumbnailBufferConsumer OnBufferAvailable X");
48 }
49
ExecuteOnBufferAvailable()50 void ThumbnailBufferConsumer::ExecuteOnBufferAvailable()
51 {
52 MEDIA_INFO_LOG("T_ExecuteOnBufferAvailable E");
53 CAMERA_SYNC_TRACE;
54 sptr<HStreamCapture> streamCapture = streamCapture_.promote();
55 CHECK_RETURN_ELOG(streamCapture == nullptr, "streamCapture is null");
56 constexpr int32_t memSize = 20 * 1024;
57 streamCapture->RequireMemorySize(memSize);
58 CHECK_RETURN_ELOG(streamCapture->thumbnailSurface_ == nullptr, "surface is null");
59 sptr<SurfaceBuffer> surfaceBuffer = nullptr;
60 int32_t fence = -1;
61 int64_t timestamp;
62 OHOS::Rect damage;
63 SurfaceError surfaceRet = streamCapture->thumbnailSurface_->AcquireBuffer(surfaceBuffer, fence, timestamp, damage);
64 CHECK_RETURN_ELOG(surfaceRet != SURFACE_ERROR_OK, "ThumbnailBufferConsumer Failed to acquire surface buffer");
65 // burst captreu skip thumbnail
66 int32_t burstSeqId = CameraSurfaceBufferUtil::GetBurstSequenceId(surfaceBuffer);
67 if (burstSeqId != -1) {
68 streamCapture->thumbnailSurface_->ReleaseBuffer(surfaceBuffer, -1);
69 MEDIA_INFO_LOG("T_ExecuteOnBufferAvailable X, burstCapture skip thumbnail");
70 return;
71 }
72 sptr<SurfaceBuffer> newSurfaceBuffer = CameraSurfaceBufferUtil::DeepCopyThumbnailBuffer(surfaceBuffer);
73 MEDIA_DEBUG_LOG("ThumbnailListener ReleaseBuffer begin");
74 streamCapture->thumbnailSurface_->ReleaseBuffer(surfaceBuffer, -1);
75 CHECK_RETURN_ELOG(newSurfaceBuffer == nullptr, "newSurfaceBuffer is null");
76 MEDIA_DEBUG_LOG("ThumbnailListener ReleaseBuffer end");
77 streamCapture->OnThumbnailAvailable(newSurfaceBuffer, timestamp);
78 if (streamCapture->isYuvCapture_) {
79 sptr<CameraServerPhotoProxy> cameraPhotoProxy = new CameraServerPhotoProxy();
80 cameraPhotoProxy->GetServerPhotoProxyInfo(newSurfaceBuffer);
81 constexpr int32_t yuvFormat = 3;
82 cameraPhotoProxy->SetFormat(yuvFormat);
83 cameraPhotoProxy->SetImageFormat(yuvFormat);
84 streamCapture->UpdateMediaLibraryPhotoAssetProxy(cameraPhotoProxy);
85 }
86 MEDIA_INFO_LOG("T_ExecuteOnBufferAvailable X");
87 }
88 } // namespace CameraStandard
89 } // namespace OHOS
90