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