xref: /aosp_15_r20/external/skia/src/codec/SkSampledCodec.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 #ifndef SkSampledCodec_DEFINED
8 #define SkSampledCodec_DEFINED
9 
10 #include "include/codec/SkAndroidCodec.h"
11 #include "include/codec/SkCodec.h"
12 #include "include/core/SkSize.h"
13 
14 #include <cstddef>
15 
16 struct SkIRect;
17 struct SkImageInfo;
18 
19 /**
20  *  This class implements the functionality of SkAndroidCodec.  Scaling will
21  *  be provided by sampling if it cannot be provided by fCodec.
22  */
23 class SkSampledCodec : public SkAndroidCodec {
24 public:
25     explicit SkSampledCodec(SkCodec*);
26 
~SkSampledCodec()27     ~SkSampledCodec() override {}
28 
29 protected:
30 
31     SkISize onGetSampledDimensions(int sampleSize) const override;
32 
onGetSupportedSubset(SkIRect * desiredSubset)33     bool onGetSupportedSubset(SkIRect* desiredSubset) const override { return true; }
34 
35     SkCodec::Result onGetAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
36             const AndroidOptions& options) override;
37 
38 private:
39     /**
40      *  Find the best way to account for native scaling.
41      *
42      *  Return a size that fCodec can scale to, and adjust sampleSize to finish scaling.
43      *
44      *  @param sampleSize As an input, the requested sample size.
45      *                    As an output, sampling needed after letting fCodec
46      *                    scale to the returned dimensions.
47      *  @param nativeSampleSize Optional output parameter. Will be set to the
48      *                          effective sample size done by fCodec.
49      *  @return SkISize The size that fCodec should scale to.
50      */
51     SkISize accountForNativeScaling(int* sampleSize, int* nativeSampleSize = nullptr) const;
52 
53     /**
54      *  This fulfills the same contract as onGetAndroidPixels().
55      *
56      *  We call this function from onGetAndroidPixels() if we have determined
57      *  that fCodec does not support the requested scale, and we need to
58      *  provide the scale by sampling.
59      */
60     SkCodec::Result sampledDecode(const SkImageInfo& info, void* pixels, size_t rowBytes,
61             const AndroidOptions& options);
62 
63     using INHERITED = SkAndroidCodec;
64 };
65 #endif // SkSampledCodec_DEFINED
66