1 /*
2  * Copyright (c) 2023 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_JPEG_IMAGE_PROCESS_H
17 #define OHOS_JPEG_IMAGE_PROCESS_H
18 
19 #include <thread>
20 #include <mutex>
21 #include <condition_variable>
22 
23 #include "data_buffer.h"
24 #include "dscreen_constants.h"
25 #include "iimage_source_processor_listener.h"
26 
27 #include "surface.h"
28 #include "video_param.h"
29 namespace OHOS {
30 namespace DistributedHardware {
31 class JpegImageProcessor : public std::enable_shared_from_this<JpegImageProcessor> {
32 public:
JpegImageProcessor(const VideoParam & configParam)33     explicit JpegImageProcessor(const VideoParam &configParam) : configParam_(configParam){};
34     ~JpegImageProcessor() = default;
35     int32_t SetOutputSurface(sptr<Surface> surface);
36     int32_t FillDirtyImages2Surface(const std::shared_ptr<DataBuffer> &data, uint8_t *lastFrame);
37     int32_t ProcessDamageSurface(sptr<SurfaceBuffer> &surfaceBuffer, const std::vector<OHOS::Rect> &damages);
38     int32_t SetImageProcessListener(std::shared_ptr<IImageSourceProcessorListener> &listener);
39     void EncodeDamageData(sptr<SurfaceBuffer> &surfaceBuffer, const OHOS::Rect &damage,
40         std::shared_ptr<DataBuffer> &data);
41     int32_t DecodeDamageData(const std::shared_ptr<DataBuffer> &data, uint8_t *lastFrame);
42     int32_t ReplaceDamage2LastFrame(uint8_t *lastFrame, uint8_t *dirtyImageData, const DirtyRect rect);
43 private:
44     uint32_t CompressRgbaToJpeg(const OHOS::Rect &damage, uint8_t *inputData,
45         uint32_t inputDataSize, std::shared_ptr<DataBuffer> &data);
46     void DecompressJpegToNV12(size_t jpegSize, uint8_t *inputData, uint8_t *outputData);
47 
48     static const constexpr char *DSCREEN_LOG_TAG = "JpegImageProcessor";
49     sptr<Surface> imageSurface_;
50     VideoParam configParam_;
51     std::weak_ptr<IImageSourceProcessorListener> imageProcessorListener_;
52     std::mutex dataMutex_;
53     std::condition_variable dataCond_;
54 };
55 } // namespace DistributedHardware
56 } // namespace OHOS
57 #endif