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