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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_SKIA_IMAGE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_SKIA_IMAGE_H
18 
19 #include <stdint.h>
20 
21 #include "include/core/SkImage.h"
22 
23 #include "core/components_ng/render/canvas_image.h"
24 
25 namespace OHOS::Ace::NG {
26 
27 class SkiaImage : public virtual CanvasImage {
DECLARE_ACE_TYPE(SkiaImage,CanvasImage)28     DECLARE_ACE_TYPE(SkiaImage, CanvasImage)
29 public:
30     explicit SkiaImage(const sk_sp<SkImage>& image) : image_(image) {}
31     SkiaImage() = default;
32     ~SkiaImage() override = default;
33 
GetImage()34     virtual sk_sp<SkImage> GetImage() const
35     {
36         return image_;
37     }
38 
GetCompressData()39     virtual sk_sp<SkData> GetCompressData() const
40     {
41         return compressData_;
42     }
43 
HasData()44     bool HasData() const override
45     {
46         return GetCompressData() || GetImage();
47     }
48 
SetCompressData(sk_sp<SkData> data,int32_t w,int32_t h)49     void SetCompressData(sk_sp<SkData> data, int32_t w, int32_t h)
50     {
51         compressData_ = data;
52         compressWidth_ = w;
53         compressHeight_ = h;
54         uniqueId_ = image_->uniqueID();
55     }
56 
SetRawCompressData(void * dataPtr,int32_t w,int32_t h)57     void SetRawCompressData(void* dataPtr, int32_t w, int32_t h) override
58     {
59         auto* skData = reinterpret_cast<sk_sp<SkData>*>(dataPtr);
60         SetCompressData(*skData, w, h);
61     }
62 
GetCompressWidth()63     virtual int32_t GetCompressWidth() const
64     {
65         return compressWidth_;
66     }
67 
GetCompressHeight()68     virtual int32_t GetCompressHeight() const
69     {
70         return compressHeight_;
71     }
72 
GetUniqueID()73     virtual uint32_t GetUniqueID() const
74     {
75         return uniqueId_;
76     }
77 
SetUniqueID(uint32_t id)78     virtual void SetUniqueID(uint32_t id)
79     {
80         uniqueId_ = id;
81     }
82 
83     RefPtr<CanvasImage> Clone() override;
84 
85     void Cache(const std::string& key) override;
86 
87     RefPtr<PixelMap> GetPixelMap() const override;
88 
89     void ReplaceSkImage(sk_sp<SkImage> newImage);
90     int32_t GetWidth() const override;
91     int32_t GetHeight() const override;
92 
93     void DrawToRSCanvas(
94         RSCanvas& canvas, const RSRect& srcRect, const RSRect& dstRect, const BorderRadiusArray& radiusXY) override;
95 
96     static RefPtr<CanvasImage> QueryFromCache(const std::string& key);
97 
98     static sk_sp<SkImage> MakeSkImageFromPixmap(const RefPtr<PixelMap>& pixmap);
99     static sk_sp<SkColorSpace> ColorSpaceToSkColorSpace(const RefPtr<PixelMap>& pixmap);
100     static SkAlphaType AlphaTypeToSkAlphaType(const RefPtr<PixelMap>& pixmap);
101     static SkColorType PixelFormatToSkColorType(const RefPtr<PixelMap>& pixmap);
102 
103 private:
DrawRect(RSCanvas & canvas,const RSRect & srcRect,const RSRect & dstRect)104     void DrawRect(RSCanvas& canvas, const RSRect& srcRect, const RSRect& dstRect) override {}
105     void ClipRRect(RSCanvas& canvas, const RSRect& dstRect, const BorderRadiusArray& radiusXY);
106     bool DrawWithRecordingCanvas(RSCanvas& canvas, const BorderRadiusArray& radiusXY);
107 
108     uint32_t uniqueId_ = 0;
109     sk_sp<SkImage> image_;
110     sk_sp<SkData> compressData_;
111     int32_t compressWidth_ = 0;
112     int32_t compressHeight_ = 0;
113 };
114 } // namespace OHOS::Ace::NG
115 
116 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_SKIA_IMAGE_H
117