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 #ifndef OHOS_CAMERA_MOVING_PHOTO_SURFACE_WRAPPER_H
17 #define OHOS_CAMERA_MOVING_PHOTO_SURFACE_WRAPPER_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <mutex>
22 #include <ratio>
23 
24 #include "refbase.h"
25 #include "surface.h"
26 
27 namespace OHOS {
28 namespace CameraStandard {
29 class MovingPhotoSurfaceWrapper : public RefBase {
30 public:
31     class SurfaceBufferListener : public RefBase {
32     public:
33         virtual void OnBufferArrival(sptr<SurfaceBuffer> buffer, int64_t timestamp, GraphicTransformType transform) = 0;
34     };
35 
36     static sptr<MovingPhotoSurfaceWrapper> CreateMovingPhotoSurfaceWrapper(int32_t width, int32_t height);
37     sptr<OHOS::IBufferProducer> GetProducer() const;
38 
39     void RecycleBuffer(sptr<SurfaceBuffer> buffer);
40     void OnBufferArrival();
41 
SetSurfaceBufferListener(sptr<SurfaceBufferListener> listener)42     inline void SetSurfaceBufferListener(sptr<SurfaceBufferListener> listener)
43     {
44         std::lock_guard<std::mutex> lock(surfaceBufferListenerMutex_);
45         surfaceBufferListener_ = listener;
46     }
47 
GetSurfaceBufferListener()48     inline sptr<SurfaceBufferListener> GetSurfaceBufferListener()
49     {
50         std::lock_guard<std::mutex> lock(surfaceBufferListenerMutex_);
51         return surfaceBufferListener_.promote();
52     }
53 
54 private:
55     class BufferConsumerListener : public IBufferConsumerListener {
56     public:
57         explicit BufferConsumerListener(sptr<MovingPhotoSurfaceWrapper> surfaceWrapper);
58         void OnBufferAvailable() override;
59 
60     private:
61         wptr<MovingPhotoSurfaceWrapper> movingPhotoSurfaceWrapper_ = nullptr;
62     };
63 
64     explicit MovingPhotoSurfaceWrapper() = default;
65     ~MovingPhotoSurfaceWrapper() override;
66     bool Init(int32_t width, int32_t height);
67 
68     mutable std::recursive_mutex videoSurfaceMutex_;
69     sptr<Surface> videoSurface_ = nullptr;
70 
71     sptr<IBufferConsumerListener> bufferConsumerListener_ = nullptr;
72 
73     std::mutex surfaceBufferListenerMutex_;
74     wptr<SurfaceBufferListener> surfaceBufferListener_ = nullptr;
75 };
76 } // namespace CameraStandard
77 } // namespace OHOS
78 #endif