1 /*
2  * Copyright (c) 2021-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 SKIAIMAGE_H
17 #define SKIAIMAGE_H
18 
19 #include "include/core/SkBitmap.h"
20 #include "include/core/SkImage.h"
21 #include "include/core/SkPaint.h"
22 #include "include/core/SkPicture.h"
23 #ifdef RS_ENABLE_GPU
24 #include "include/gpu/GrDirectContext.h"
25 #endif
26 #include "skia_bitmap.h"
27 #include "skia_color_space.h"
28 #include "skia_matrix.h"
29 #include "skia_paint.h"
30 #include "skia_picture.h"
31 #include "skia_yuv_info.h"
32 #include "include/gpu/GrBackendSurface.h"
33 
34 #include "impl_interface/image_impl.h"
35 
36 namespace OHOS {
37 namespace Rosen {
38 namespace Drawing {
39 class DRAWING_API SkiaImage : public ImageImpl {
40 public:
41     static inline constexpr AdapterType TYPE = AdapterType::SKIA_ADAPTER;
42 
43     SkiaImage() noexcept;
44     explicit SkiaImage(sk_sp<SkImage> skImg) noexcept;
45     ~SkiaImage() override;
46 
GetType()47     AdapterType GetType() const override
48     {
49         return AdapterType::SKIA_ADAPTER;
50     }
51 
52     static std::shared_ptr<Image> MakeFromRaster(const Pixmap& pixmap,
53         RasterReleaseProc rasterReleaseProc, ReleaseContext releaseContext);
54     static std::shared_ptr<Image> MakeRasterData(const ImageInfo& info, std::shared_ptr<Data> pixels,
55         size_t rowBytes);
56     bool BuildFromBitmap(const Bitmap& bitmap) override;
57 #ifdef RS_ENABLE_GPU
58     static std::shared_ptr<Image> MakeFromYUVAPixmaps(GPUContext& gpuContext, const YUVInfo& info, void* memory);
59     bool BuildFromSurface(GPUContext& gpuContext, Surface& surface, TextureOrigin origin,
60         BitmapFormat bitmapFormat, const std::shared_ptr<ColorSpace>& colorSpace) override;
61     bool BuildFromBitmap(GPUContext& gpuContext, const Bitmap& bitmap) override;
62     bool MakeFromEncoded(const std::shared_ptr<Data>& data) override;
63     bool BuildSubset(const std::shared_ptr<Image> image, const RectI& rect, GPUContext& gpuContext) override;
64     bool BuildFromCompressed(GPUContext& gpuContext, const std::shared_ptr<Data>& data, int width, int height,
65         CompressedType type, const std::shared_ptr<ColorSpace>& colorSpace = nullptr) override;
66     bool BuildFromTexture(GPUContext& gpuContext, const TextureInfo& info, TextureOrigin origin,
67         BitmapFormat bitmapFormat, const std::shared_ptr<ColorSpace>& colorSpace,
68         void (*deleteFunc)(void*) = nullptr, void* cleanupHelper = nullptr) override;
69     void DeleteCleanupHelper(void (*deleteFunc)(void*), void* cleanupHelper);
70     BackendTexture GetBackendTexture(bool flushPendingGrContextIO, TextureOrigin* origin) override;
71     void SetGrBackendTexture(const GrBackendTexture& grBackendTexture);
72     bool IsValid(GPUContext* context) const override;
73 #endif
74     bool AsLegacyBitmap(Bitmap& bitmap) const override;
75     int GetWidth() const override;
76     int GetHeight() const override;
77     ColorType GetColorType() const override;
78     AlphaType GetAlphaType() const override;
79     std::shared_ptr<ColorSpace> GetColorSpace() const override;
80     uint32_t GetUniqueID() const override;
81     ImageInfo GetImageInfo() override;
82     bool ReadPixels(Bitmap& bitmap, int x, int y) override;
83     bool ReadPixels(Pixmap& pixmap, int x, int y) override;
84     bool ReadPixels(const ImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
85                     int32_t srcX, int32_t srcY) const override;
86     bool IsTextureBacked() const override;
87 
88     bool ScalePixels(const Bitmap& bitmap, const SamplingOptions& sampling,
89         bool allowCachingHint = true) const override;
90     std::shared_ptr<Data> EncodeToData(EncodedImageFormat encodedImageFormat, int quality) const override;
91     bool IsLazyGenerated() const override;
92     bool GetROPixels(Bitmap& bitmap) const override;
93     std::shared_ptr<Image> MakeRasterImage() const override;
94     bool CanPeekPixels() const override;
95 
96     bool IsOpaque() const override;
97     void HintCacheGpuResource() const override;
98 
99     const sk_sp<SkImage> GetImage() const;
100 
101     /*
102      * @brief  Update the member variable to skImage, adaptation layer calls.
103      */
104     void SetSkImage(const sk_sp<SkImage>& skImage);
105 #ifdef RS_ENABLE_GPU
106     /*
107      * @brief  Export Skia member variables for use by the adaptation layer.
108      */
109     sk_sp<GrDirectContext> GetGrContext() const;
110 #endif
111 
112     std::shared_ptr<Data> Serialize() const override;
113     bool Deserialize(std::shared_ptr<Data> data) override;
114 
115     void PostSkImgToTargetThread();
116 private:
117 #ifdef RS_ENABLE_GPU
118     sk_sp<GrDirectContext> grContext_ = nullptr;
119 #endif
120     sk_sp<SkImage> skiaImage_;
121     GrBackendTexture grBackendTexture_;
122 };
123 } // namespace Drawing
124 } // namespace Rosen
125 } // namespace OHOS
126 #endif
127