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