• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_EXT_OHOSCODEC_H
17 #define PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_EXT_OHOSCODEC_H
18 
19 #include "include/codec/SkCodec.h"
20 #ifdef USE_M133_SKIA
21 #include "include/codec/SkEncodedImageFormat.h"
22 #else
23 #include "include/core/SkEncodedImageFormat.h"
24 #endif
25 #include "include/core/SkStream.h"
26 #include "include/core/SkTypes.h"
27 
28 class SK_API SkOHOSCodec : SkNoncopyable {
29 public:
30     enum class ExifOrientationBehavior {
31         kIgnore,
32         kRespect,
33     };
34 
35     static std::unique_ptr<SkOHOSCodec> MakeFromCodec(std::unique_ptr<SkCodec>);
36 
37     static std::unique_ptr<SkOHOSCodec> MakeFromStream(std::unique_ptr<SkStream>,
38                                                        SkPngChunkReader* = nullptr);
39 
40     static std::unique_ptr<SkOHOSCodec> MakeFromData(sk_sp<SkData>, SkPngChunkReader* = nullptr);
41 
42     virtual ~SkOHOSCodec();
43 
getInfo()44     const SkImageInfo& getInfo() const { return fInfo; }
45 
getICCProfile()46     const skcms_ICCProfile* getICCProfile() const {
47         return fCodec->callGetEncodedInfo().profile();
48     }
49 
getEncodedFormat()50     SkEncodedImageFormat getEncodedFormat() const { return fCodec->getEncodedFormat(); }
51 
52     SkColorType computeOutputColorType(SkColorType requestedColorType);
53 
54     SkAlphaType computeOutputAlphaType(bool requestedUnpremul);
55 
56     sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType,
57                                                 sk_sp<SkColorSpace> prefColorSpace = nullptr);
58 
59     int computeSampleSize(SkISize* size) const;
60 
61     SkISize getSampledDimensions(int sampleSize) const;
62 
63     bool getSupportedSubset(SkIRect* desiredSubset) const;
64 
65     SkISize getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const;
66     struct OHOSOptions : public SkCodec::Options {
OHOSOptionsOHOSOptions67         OHOSOptions()
68             : SkCodec::Options(), fSampleSize(1)
69         {}
70 
71         int fSampleSize;
72     };
73 
74     SkCodec::Result getOHOSPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
75             const OHOSOptions* options);
76 
77     SkCodec::Result getOHOSPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
78 
getPixels(const SkImageInfo & info,void * pixels,size_t rowBytes)79     SkCodec::Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
80         return this->getOHOSPixels(info, pixels, rowBytes);
81     }
82 
codec()83     SkCodec* codec() const { return fCodec.get(); }
84 
85 protected:
86     SkOHOSCodec(SkCodec*);
87 
88     virtual SkISize onGetSampledDimensions(int sampleSize) const = 0;
89 
90     virtual bool onGetSupportedSubset(SkIRect* desiredSubset) const = 0;
91 
92     virtual SkCodec::Result onGetOHOSPixels(const SkImageInfo& info, void* pixels,
93         size_t rowBytes, const OHOSOptions& options) = 0;
94 
95 private:
96     const SkImageInfo               fInfo;
97     std::unique_ptr<SkCodec>        fCodec;
98 };
99 
100 class SkOHOSCodecAdapter : public SkOHOSCodec {
101     public:
102 
103         explicit SkOHOSCodecAdapter(SkCodec*);
104 
~SkOHOSCodecAdapter()105         ~SkOHOSCodecAdapter() override {}
106 
107     protected:
108 
109         SkISize onGetSampledDimensions(int sampleSize) const override;
110 
111         bool onGetSupportedSubset(SkIRect* desiredSubset) const override;
112 
113         SkCodec::Result onGetOHOSPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
114             const OHOSOptions& options) override;
115 
116     private:
117 
118         using INHERITED = SkOHOSCodec;
119 };
120 
121 class SkOHOSSampledCodec : public SkOHOSCodec {
122 public:
123     explicit SkOHOSSampledCodec(SkCodec*);
124 
~SkOHOSSampledCodec()125     ~SkOHOSSampledCodec() override {}
126 
127 protected:
128 
129     SkISize onGetSampledDimensions(int sampleSize) const override;
130 
onGetSupportedSubset(SkIRect * desiredSubset)131     bool onGetSupportedSubset(SkIRect* desiredSubset) const override { return true; }
132 
133     SkCodec::Result onGetOHOSPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
134         const OHOSOptions& options) override;
135 
136 private:
137     SkISize accountForNativeScaling(int* sampleSize, int* nativeSampleSize = nullptr) const;
138 
139     SkCodec::Result sampledDecode(const SkImageInfo& info, void* pixels, size_t rowBytes,
140         const OHOSOptions& options);
141 
142     using INHERITED = SkOHOSCodec;
143 };
144 
145 #endif // SkOHOSCodec_DEFINED