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 #ifndef IMAGE_CREATOR_H
17 #define IMAGE_CREATOR_H
18 
19 #include <surface.h>
20 #include <cstdint>
21 #include <mutex>
22 #include <string>
23 #include <list>
24 #include <securec.h>
25 #include "image_format.h"
26 #include "image_type.h"
27 #include "media_errors.h"
28 #include "pixel_map.h"
29 #include "display_type.h"
30 #include "image_creator_context.h"
31 #include "image_receiver.h"
32 #include "native_image.h"
33 
34 namespace OHOS {
35 namespace Media {
36 class IBufferProcessor;
37 class NativeImage;
38 class SurfaceBufferReleaseListener {
39 public:
40     SurfaceBufferReleaseListener()= default;
41     virtual ~SurfaceBufferReleaseListener()= default;
42     virtual void OnSurfaceBufferRelease() = 0;
43 };
44 class ImageCreator {
45 public:
46     sptr<IConsumerSurface> creatorConsumerSurface_ = nullptr;
47     sptr<Surface> creatorProducerSurface_ = nullptr;
48     std::shared_ptr<ImageCreatorContext> iraContext_ = nullptr;
49     std::shared_ptr<SurfaceBufferReleaseListener> surfaceBufferReleaseListener_ = nullptr;
50     std::shared_ptr<SurfaceBufferAvaliableListener> surfaceBufferAvaliableListener_ = nullptr;
ImageCreator()51     ImageCreator() {};
52     ~ImageCreator();
RegisterBufferAvaliableListener(std::shared_ptr<SurfaceBufferAvaliableListener> surfaceBufferAvaliableListener)53     void RegisterBufferAvaliableListener(
54         std::shared_ptr<SurfaceBufferAvaliableListener> surfaceBufferAvaliableListener)
55     {
56         surfaceBufferAvaliableListener_ = surfaceBufferAvaliableListener;
57     }
58     static std::shared_ptr<ImageCreator> CreateImageCreator(int32_t width,
59                                                             int32_t height,
60                                                             int32_t format,
61                                                             int32_t capicity);
62     static int32_t SaveSTP(uint32_t *buffer,
63                            uint8_t *tempBuffer,
64                            uint32_t bufferSize,
65                            InitializationOptions initializationOpts);
66 
67     int32_t SaveSenderBufferAsImage(
68                               OHOS::sptr<OHOS::SurfaceBuffer> buffer,
69                               InitializationOptions initializationOpts);
70     int32_t SaveSenderBufferAsImage(InitializationOptions initializationOpts);
71     OHOS::sptr<OHOS::SurfaceBuffer> DequeueImage();
72     void QueueImage(OHOS::sptr<OHOS::SurfaceBuffer> &buffer);
RegisterBufferReleaseListener(std::shared_ptr<SurfaceBufferReleaseListener> release)73     void RegisterBufferReleaseListener(std::shared_ptr<SurfaceBufferReleaseListener> release)
74     {
75         surfaceBufferReleaseListener_ = release;
76     };
UnRegisterBufferReleaseListener()77     void UnRegisterBufferReleaseListener()
78     {
79         surfaceBufferReleaseListener_.reset();
80     };
81     sptr<IConsumerSurface> GetCreatorSurface();
82     static sptr<IConsumerSurface> getSurfaceById(std::string id);
83     void ReleaseCreator();
84     static GSError OnBufferRelease(sptr<SurfaceBuffer> &buffer);
85     static std::map<uint8_t*, ImageCreator*> bufferCreatorMap_;
86     static std::mutex creatorMutex_;
87 
88     std::shared_ptr<IBufferProcessor> GetBufferProcessor();
89     std::shared_ptr<NativeImage> DequeueNativeImage();
90     void QueueNativeImage(std::shared_ptr<NativeImage> image);
91 private:
92     std::shared_ptr<IBufferProcessor> bufferProcessor_;
93 };
94 class ImageCreatorSurfaceListener : public IBufferConsumerListener {
95 public:
96     std::shared_ptr<ImageCreator> ic_;
97     void OnBufferAvailable() override;
98 };
99 } // namespace Media
100 } // namespace OHOS
101 #endif
102