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 #ifndef USE_ROSEN_DRAWING 24 #include "include/core/SkCanvas.h" 25 #else 26 #include <memory> 27 #include "draw/canvas.h" 28 #endif 29 #include "transaction/rs_marshalling_helper.h" 30 31 namespace OHOS { 32 namespace Media { 33 class PixelMap; 34 } 35 namespace Rosen { 36 class RSB_EXPORT RSImageBase { 37 public: 38 RSImageBase() = default; 39 virtual ~RSImageBase(); 40 41 #ifndef USE_ROSEN_DRAWING 42 #ifdef NEW_SKIA 43 virtual void DrawImage(SkCanvas& canvas, const SkSamplingOptions& samplingOptions, const SkPaint& paint); 44 #else 45 virtual void DrawImage(SkCanvas& canvas, const SkPaint& paint); 46 #endif 47 void SetImage(const sk_sp<SkImage> image); 48 #else 49 virtual void DrawImage(Drawing::Canvas& canvas, const Drawing::Brush& brush); 50 void SetImage(const std::shared_ptr<Drawing::Image> image); 51 #endif 52 void SetPixelMap(const std::shared_ptr<Media::PixelMap>& pixelMap); 53 void SetSrcRect(const RectF& dstRect); 54 void SetDstRect(const RectF& dstRect); 55 void SetImagePixelAddr(void* addr); 56 void UpdateNodeIdToPicture(NodeId nodeId); 57 void MarkRenderServiceImage(); 58 std::shared_ptr<Media::PixelMap> GetPixelMap() const; 59 #ifdef ROSEN_OHOS 60 virtual bool Marshalling(Parcel& parcel) const; 61 [[nodiscard]] static RSImageBase* Unmarshalling(Parcel& parcel); 62 #endif 63 64 protected: 65 #ifndef USE_ROSEN_DRAWING 66 void ConvertPixelMapToSkImage(); 67 #else 68 void ConvertPixelMapToDrawingImage(); 69 #endif 70 void GenUniqueId(uint32_t id); 71 #ifndef USE_ROSEN_DRAWING 72 static bool UnmarshallingSkImageAndPixelMap(Parcel& parcel, uint64_t uniqueId, bool& useSkImage, 73 sk_sp<SkImage>& img, std::shared_ptr<Media::PixelMap>& pixelMap, void*& imagepixelAddr); 74 #else 75 static bool UnmarshallingDrawingImageAndPixelMap(Parcel& parcel, uint64_t uniqueId, bool& useDrawingImage, 76 std::shared_ptr<Drawing::Image>& img, std::shared_ptr<Media::PixelMap>& pixelMap, void*& imagepixelAddr); 77 #endif 78 static void IncreaseCacheRefCount(uint64_t uniqueId, 79 bool useSkImage = true, std::shared_ptr<Media::PixelMap> pixelMap = nullptr); 80 81 mutable std::mutex mutex_; 82 #ifndef USE_ROSEN_DRAWING 83 sk_sp<SkImage> image_; 84 #else 85 std::shared_ptr<Drawing::Image> image_; 86 #endif 87 void* imagePixelAddr_ = nullptr; 88 std::shared_ptr<Media::PixelMap> pixelMap_; 89 90 RectF srcRect_; 91 RectF dstRect_; 92 uint64_t uniqueId_ = 0; 93 bool renderServiceImage_ = false; 94 }; 95 } // namespace Rosen 96 } // namespace OHOS 97 #endif // RENDER_SERVICE_CLIENT_CORE_RENDER_RS_IMAGE_BASE_H 98