• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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 
8 #ifndef SkSamplingPriv_DEFINED
9 #define SkSamplingPriv_DEFINED
10 
11 #include "include/core/SkSamplingOptions.h"
12 
13 class SkReadBuffer;
14 class SkWriteBuffer;
15 
16 // Private copy of SkFilterQuality, just for legacy deserialization
17 // Matches values in SkFilterQuality
18 enum SkLegacyFQ {
19     kNone_SkLegacyFQ   = 0,    //!< nearest-neighbor; fastest but lowest quality
20     kLow_SkLegacyFQ    = 1,    //!< bilerp
21     kMedium_SkLegacyFQ = 2,    //!< bilerp + mipmaps; good for down-scaling
22     kHigh_SkLegacyFQ   = 3,    //!< bicubic resampling; slowest but good quality
23 
24     kLast_SkLegacyFQ = kHigh_SkLegacyFQ,
25 };
26 
27 // Matches values in SkSamplingOptions::MediumBehavior
28 enum SkMediumAs {
29     kNearest_SkMediumAs,
30     kLinear_SkMediumAs,
31 };
32 
33 class SkSamplingPriv {
34 public:
FlatSize(const SkSamplingOptions & options)35     static size_t FlatSize(const SkSamplingOptions& options) {
36         size_t size = sizeof(uint32_t);  // maxAniso
37         if (!options.isAniso()) {
38             size += 3 * sizeof(uint32_t);  // bool32 + [2 floats | 2 ints]
39         }
40         return size;
41     }
42 
43     // Returns true if the sampling can be ignored when the CTM is identity.
NoChangeWithIdentityMatrix(const SkSamplingOptions & sampling)44     static bool NoChangeWithIdentityMatrix(const SkSamplingOptions& sampling) {
45         // If B == 0, the cubic resampler should have no effect for identity matrices
46         // https://entropymine.com/imageworsener/bicubic/
47         // We assume aniso has no effect with an identity transform.
48         return !sampling.useCubic || sampling.cubic.B == 0;
49     }
50 
51     // Makes a fallback SkSamplingOptions for cases where anisotropic filtering is not allowed.
52     // anisotropic filtering can access mip levels if present, but we don't add mipmaps to non-
53     // mipmapped images when the user requests anisotropic. So we shouldn't fall back to a
54     // sampling that would trigger mip map creation.
AnisoFallback(bool imageIsMipped)55     static SkSamplingOptions AnisoFallback(bool imageIsMipped) {
56         auto mm = imageIsMipped ? SkMipmapMode::kLinear : SkMipmapMode::kNone;
57         return SkSamplingOptions(SkFilterMode::kLinear, mm);
58     }
59 
60     static SkSamplingOptions FromFQ(SkLegacyFQ, SkMediumAs = kNearest_SkMediumAs);
61 };
62 
63 #endif
64