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_BASE_RENDER_RENDER_RS_IMAGE_BASE_H 17 #define RENDER_SERVICE_BASE_RENDER_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 void SetCompressData(const std::shared_ptr<Drawing::Data> CompressData); 68 #if defined(ROSEN_OHOS) && (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)) 69 void SetDmaImage(const std::shared_ptr<Drawing::Image> image); 70 void MarkYUVImage(); 71 #endif 72 void SetPixelMap(const std::shared_ptr<Media::PixelMap>& pixelMap); 73 void SetSrcRect(const RectF& dstRect); 74 void SetDstRect(const RectF& dstRect); 75 void SetImagePixelAddr(void* addr); 76 void UpdateNodeIdToPicture(NodeId nodeId); 77 void MarkRenderServiceImage(); 78 void MarkPurgeable(); 79 bool IsPurgeable() const; 80 std::shared_ptr<Media::PixelMap> GetPixelMap() const; 81 void DumpPicture(DfxString& info) const; 82 uint64_t GetUniqueId() const; 83 #ifdef ROSEN_OHOS 84 virtual bool Marshalling(Parcel& parcel) const; 85 [[nodiscard]] static RSImageBase* Unmarshalling(Parcel& parcel); 86 #endif 87 88 /* 89 * This function is used to reduce memory usage by unmap the memory of the pixelMap_. 90 * Only the pixelMap_ with one RefCount and one UseCount can be purged. 91 */ 92 void Purge(); 93 void DePurge(); 94 95 protected: 96 void ConvertPixelMapToDrawingImage(); 97 void GenUniqueId(uint32_t id); 98 void UploadGpu(Drawing::Canvas& canvas); 99 #if defined(ROSEN_OHOS) && (defined(RS_ENABLE_GL) || defined(RS_ENABLE_VK)) 100 void ProcessYUVImage(std::shared_ptr<Drawing::GPUContext> gpuContext, Drawing::Canvas& canvas); 101 bool SetCompressedDataForASTC(); 102 #if defined(RS_ENABLE_VK) 103 void BindPixelMapToDrawingImage(Drawing::Canvas& canvas); 104 std::shared_ptr<Drawing::Image> MakeFromTextureForVK(Drawing::Canvas& canvas, SurfaceBuffer* surfaceBuffer); 105 #endif 106 #endif 107 static bool UnmarshallingDrawingImageAndPixelMap(Parcel& parcel, uint64_t uniqueId, bool& useDrawingImage, 108 std::shared_ptr<Drawing::Image>& img, std::shared_ptr<Media::PixelMap>& pixelMap, void*& imagepixelAddr); 109 static void IncreaseCacheRefCount(uint64_t uniqueId, 110 bool useSkImage = true, std::shared_ptr<Media::PixelMap> pixelMap = nullptr); 111 112 mutable std::mutex mutex_; 113 std::shared_ptr<Drawing::Image> image_; 114 void* imagePixelAddr_ = nullptr; 115 std::shared_ptr<Media::PixelMap> pixelMap_; 116 // used for astc render 117 std::shared_ptr<Drawing::Data> compressData_; 118 119 RectF srcRect_; 120 RectF dstRect_; 121 Drawing::Rect src_; 122 Drawing::Rect dst_; 123 Drawing::Rect lastRect_; 124 bool isDrawn_ = false; 125 uint64_t uniqueId_ = 0; 126 bool renderServiceImage_ = false; 127 bool isYUVImage_ = false; 128 129 #if defined(ROSEN_OHOS) && defined(RS_ENABLE_VK) 130 mutable OHNativeWindowBuffer* nativeWindowBuffer_ = nullptr; 131 mutable pid_t tid_ = 0; 132 mutable Drawing::BackendTexture backendTexture_ = {}; 133 mutable NativeBufferUtils::VulkanCleanupHelper* cleanUpHelper_ = nullptr; 134 #endif 135 136 enum class CanPurgeFlag : int8_t { 137 UNINITED = -1, 138 DISABLED = 0, 139 ENABLED = 1, 140 }; 141 CanPurgeFlag canPurgeShareMemFlag_ = CanPurgeFlag::UNINITED; 142 }; 143 } // namespace Rosen 144 } // namespace OHOS 145 #endif // RENDER_SERVICE_BASE_RENDER_RENDER_RS_IMAGE_BASE_H 146