• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "memory/rs_dfx_string.h"
26 #include "pipeline/rs_paint_filter_canvas.h"
27 #include "transaction/rs_marshalling_helper.h"
28 
29 #if defined(ROSEN_OHOS) && defined(RS_ENABLE_VK)
30 #include "external_window.h"
31 #include "surface_buffer.h"
32 #endif
33 
34 namespace OHOS {
35 namespace Media {
36 class PixelMap;
37 }
38 namespace Rosen {
39 #if defined(ROSEN_OHOS) && defined(RS_ENABLE_VK)
40 namespace NativeBufferUtils {
41 class VulkanCleanupHelper;
42 }
43 #endif
44 class RSB_EXPORT RSImageBase {
45 public:
46 #ifdef ROSEN_OHOS
47     /* This class is used to avoid Unmap is being called after ReMap and before pixelMap is used. */
48     class PixelMapUseCountGuard {
49     public:
50         PixelMapUseCountGuard(std::shared_ptr<Media::PixelMap> pixelMap, bool purgeable);
51         ~PixelMapUseCountGuard();
52     private:
53         std::shared_ptr<Media::PixelMap> pixelMap_ = nullptr;
54         bool purgeable_ = false;
55     };
56 #endif
57     RSImageBase() = default;
58     virtual ~RSImageBase();
59 
60     virtual void DrawImage(Drawing::Canvas& canvas, const Drawing::SamplingOptions& samplingOptions,
61         Drawing::SrcRectConstraint constraint = Drawing::SrcRectConstraint::STRICT_SRC_RECT_CONSTRAINT);
62     virtual void DrawImageNine(Drawing::Canvas& canvas, const Drawing::RectI& center, const Drawing::Rect& dst,
63         Drawing::FilterMode filterMode = Drawing::FilterMode::NEAREST);
64     virtual void DrawImageLattice(Drawing::Canvas& canvas, const Drawing::Lattice& lattice, const Drawing::Rect& dst,
65         Drawing::FilterMode filterMode = Drawing::FilterMode::NEAREST);
66     void SetImage(const std::shared_ptr<Drawing::Image> image);
67 #if defined(ROSEN_OHOS) && (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK))
68     void SetDmaImage(const std::shared_ptr<Drawing::Image> image);
69     void MarkYUVImage();
70 #endif
71     void SetPixelMap(const std::shared_ptr<Media::PixelMap>& pixelMap);
72     void SetSrcRect(const RectF& dstRect);
73     void SetDstRect(const RectF& dstRect);
74     void SetImagePixelAddr(void* addr);
75     void UpdateNodeIdToPicture(NodeId nodeId);
76     void MarkRenderServiceImage();
77     void MarkPurgeable();
78     bool IsPurgeable() const;
79     std::shared_ptr<Media::PixelMap> GetPixelMap() const;
80     void DumpPicture(DfxString& info) const;
81     uint64_t GetUniqueId() const;
82 #ifdef ROSEN_OHOS
83     virtual bool Marshalling(Parcel& parcel) const;
84     [[nodiscard]] static RSImageBase* Unmarshalling(Parcel& parcel);
85 #endif
86 
87     void ConvertPixelMapToDrawingImage(bool parallelUpload = false);
88 
89     /*
90      * This function is used to reduce memory usage by unmap the memory of the pixelMap_.
91      * Only the pixelMap_ with one RefCount and one UseCount can be purged.
92     */
93     void Purge();
94     void DePurge();
95 
96 protected:
97     void GenUniqueId(uint32_t id);
98 #if defined(ROSEN_OHOS) && (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK))
99     void ProcessYUVImage(std::shared_ptr<Drawing::GPUContext> gpuContext);
100 #if defined(RS_ENABLE_VK)
101     void BindPixelMapToDrawingImage(Drawing::Canvas& canvas);
102     std::shared_ptr<Drawing::Image> MakeFromTextureForVK(Drawing::Canvas& canvas, SurfaceBuffer* surfaceBuffer);
103 #endif
104 #endif
105     static bool UnmarshallingDrawingImageAndPixelMap(Parcel& parcel, uint64_t uniqueId, bool& useDrawingImage,
106         std::shared_ptr<Drawing::Image>& img, std::shared_ptr<Media::PixelMap>& pixelMap, void*& imagepixelAddr);
107     static void IncreaseCacheRefCount(uint64_t uniqueId,
108             bool useSkImage = true, std::shared_ptr<Media::PixelMap> pixelMap = nullptr);
109 
110     mutable std::mutex mutex_;
111     std::shared_ptr<Drawing::Image> image_;
112     void* imagePixelAddr_ = nullptr;
113     std::shared_ptr<Media::PixelMap> pixelMap_;
114 
115     RectF srcRect_;
116     RectF dstRect_;
117     Drawing::Rect src_;
118     Drawing::Rect dst_;
119     Drawing::Rect lastRect_;
120     bool isDrawn_ = false;
121     uint64_t uniqueId_ = 0;
122     bool renderServiceImage_ = false;
123     bool isYUVImage_ = false;
124 
125 #if defined(ROSEN_OHOS) && defined(RS_ENABLE_VK)
126     mutable OHNativeWindowBuffer* nativeWindowBuffer_ = nullptr;
127     mutable pid_t tid_ = 0;
128     mutable Drawing::BackendTexture backendTexture_ = {};
129     mutable NativeBufferUtils::VulkanCleanupHelper* cleanUpHelper_ = nullptr;
130 #endif
131 
132     enum class CanPurgeFlag : int8_t {
133         UNINITED = -1,
134         DISABLED = 0,
135         ENABLED = 1,
136     };
137     CanPurgeFlag canPurgeShareMemFlag_ = CanPurgeFlag::UNINITED;
138 };
139 } // namespace Rosen
140 } // namespace OHOS
141 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_IMAGE_BASE_H
142