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 RENDER_SERVICE_CLIENT_CORE_RENDER_RS_IMAGE_BASE_H
17 #define RENDER_SERVICE_CLIENT_CORE_RENDER_RS_IMAGE_BASE_H
18 
19 #include <cstdint>
20 #include <mutex>
21 #include "common/rs_macros.h"
22 #include "common/rs_rect.h"
23 #include <memory>
24 #include "draw/canvas.h"
25 #include "pipeline/rs_paint_filter_canvas.h"
26 #include "transaction/rs_marshalling_helper.h"
27 
28 #if defined(ROSEN_OHOS) && defined(RS_ENABLE_VK)
29 #include "external_window.h"
30 #include "surface_buffer.h"
31 #endif
32 
33 namespace OHOS {
34 namespace Media {
35 class PixelMap;
36 }
37 namespace Rosen {
38 #if defined(ROSEN_OHOS) && defined(RS_ENABLE_VK)
39 namespace NativeBufferUtils {
40 class VulkanCleanupHelper;
41 }
42 #endif
43 class RSB_EXPORT RSImageBase {
44 public:
45     RSImageBase() = default;
46     virtual ~RSImageBase();
47 
48     virtual void DrawImage(Drawing::Canvas& canvas, const Drawing::SamplingOptions& samplingOptions,
49         Drawing::SrcRectConstraint constraint = Drawing::SrcRectConstraint::STRICT_SRC_RECT_CONSTRAINT);
50     void SetImage(const std::shared_ptr<Drawing::Image> image);
51 #if defined(ROSEN_OHOS) && (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK))
52     void SetDmaImage(const std::shared_ptr<Drawing::Image> image);
53     void MarkYUVImage();
54 #endif
55     void SetPixelMap(const std::shared_ptr<Media::PixelMap>& pixelMap);
56     void SetSrcRect(const RectF& dstRect);
57     void SetDstRect(const RectF& dstRect);
58     void SetImagePixelAddr(void* addr);
59     void UpdateNodeIdToPicture(NodeId nodeId);
60     void MarkRenderServiceImage();
61     std::shared_ptr<Media::PixelMap> GetPixelMap() const;
62     uint64_t GetUniqueId() const;
63 #ifdef ROSEN_OHOS
64     virtual bool Marshalling(Parcel& parcel) const;
65     [[nodiscard]] static RSImageBase* Unmarshalling(Parcel& parcel);
66 #endif
67 
68     void ConvertPixelMapToDrawingImage(bool parallelUpload = false);
69 
70     void Purge();
71     enum class CanPurgeFlag : int8_t {
72         UNINITED = -1,
73         DISABLED = 0,
74         ENABLED = 1,
75     };
76     CanPurgeFlag canPurgeShareMemFlag_ = CanPurgeFlag::UNINITED;
77 
78 protected:
79     void GenUniqueId(uint32_t id);
80 #if defined(ROSEN_OHOS) && (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK))
81     void ProcessYUVImage(std::shared_ptr<Drawing::GPUContext> gpuContext);
82 #if defined(RS_ENABLE_VK)
83     void BindPixelMapToDrawingImage(Drawing::Canvas& canvas);
84     std::shared_ptr<Drawing::Image> MakeFromTextureForVK(Drawing::Canvas& canvas, SurfaceBuffer* surfaceBuffer);
85 #endif
86 #endif
87     static bool UnmarshallingDrawingImageAndPixelMap(Parcel& parcel, uint64_t uniqueId, bool& useDrawingImage,
88         std::shared_ptr<Drawing::Image>& img, std::shared_ptr<Media::PixelMap>& pixelMap, void*& imagepixelAddr);
89     static void IncreaseCacheRefCount(uint64_t uniqueId,
90             bool useSkImage = true, std::shared_ptr<Media::PixelMap> pixelMap = nullptr);
91 
92     mutable std::mutex mutex_;
93     std::shared_ptr<Drawing::Image> image_;
94     void* imagePixelAddr_ = nullptr;
95     std::shared_ptr<Media::PixelMap> pixelMap_;
96 
97     RectF srcRect_;
98     RectF dstRect_;
99     Drawing::Rect src_;
100     Drawing::Rect dst_;
101     Drawing::Rect lastRect_;
102     bool isDrawn_ = false;
103     uint64_t uniqueId_ = 0;
104     bool renderServiceImage_ = false;
105     bool isYUVImage_ = false;
106 
107 #if defined(ROSEN_OHOS) && defined(RS_ENABLE_VK)
108     mutable OHNativeWindowBuffer* nativeWindowBuffer_ = nullptr;
109     mutable pid_t tid_ = 0;
110     mutable Drawing::BackendTexture backendTexture_ = {};
111     mutable NativeBufferUtils::VulkanCleanupHelper* cleanUpHelper_ = nullptr;
112 #endif
113 };
114 } // namespace Rosen
115 } // namespace OHOS
116 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_IMAGE_BASE_H
117