1 /*
2  * Copyright 2016 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 SkColorSpaceXform_Base_DEFINED
9 #define SkColorSpaceXform_Base_DEFINED
10 
11 #include "SkColorSpace.h"
12 #include "SkColorSpaceXform.h"
13 #include "SkResourceCache.h"
14 
15 class SkColorSpace_XYZ;
16 
17 class SkColorSpaceXform_Base : public SkColorSpaceXform {
18 public:
19     // A somewhat more powerful SkColorSpaceXform::New() that allows tweaking premulBehavior.
20     static std::unique_ptr<SkColorSpaceXform> New(SkColorSpace* srcSpace,
21                                                   SkColorSpace* dstSpace,
22                                                   SkTransferFunctionBehavior premulBehavior);
23 
24     static constexpr int kDstGammaTableSize = 1024;
25     static void BuildDstGammaTables(const uint8_t* outGammaTables[3],
26                                     uint8_t* gammaTableStorage,
27                                     const SkColorSpace_XYZ* space,
28                                     bool gammasAreMatching);
29 
30     virtual bool onApply(ColorFormat dstFormat, void* dst, ColorFormat srcFormat, const void* src,
31                          int count, SkAlphaType alphaType) const = 0;
32 };
33 
34 enum SrcGamma {
35     kLinear_SrcGamma,
36     kTable_SrcGamma,
37     kSRGB_SrcGamma,
38 };
39 
40 enum DstGamma {
41     kLinear_DstGamma,
42     kSRGB_DstGamma,
43     k2Dot2_DstGamma,
44     kTable_DstGamma,
45 };
46 
47 class SkColorSpaceXform_XYZ : public SkColorSpaceXform_Base {
48 public:
49     SkColorSpaceXform_XYZ(SkColorSpace_XYZ* src, SkColorSpace_XYZ* dst, SkTransferFunctionBehavior);
50 
51     bool onApply(ColorFormat dstFormat, void* dst,
52                  ColorFormat srcFormat, const void* src,
53                  int count, SkAlphaType alphaType) const override;
54 
pretendNotToBeIdentityForTesting()55     void pretendNotToBeIdentityForTesting() {
56         fSrcToDstIsIdentity = false;
57     }
58 
59 private:
60     // These tables pointers may point into fSrcStorage/fDstStorage or into pre-baked tables.
61     const float*               fSrcGammaTables[3];
62     const uint8_t*             fDstGammaTables[3];
63     SkAutoTMalloc<float>       fSrcStorage;
64     sk_sp<SkData>              fDstStorage;
65 
66     float                      fSrcToDst[12];
67     bool                       fSrcToDstIsIdentity;
68     bool                       fColorSpacesAreIdentical;
69     SrcGamma                   fSrcGamma;
70     DstGamma                   fDstGamma;
71     SkTransferFunctionBehavior fPremulBehavior;
72 };
73 
74 struct LoadTablesContext {
75     const void*  fSrc;
76     const float* fR;
77     const float* fG;
78     const float* fB;
79 };
80 
81 // Must be kept in sync with "Tables" struct in RasterPipeline_opts byte_tables_rgb.
82 struct TablesContext {
83     const uint8_t* fR;
84     const uint8_t* fG;
85     const uint8_t* fB;
86     int            fCount;
87 };
88 
89 // For testing.  Bypasses opts for when src and dst color spaces are equal.
90 std::unique_ptr<SkColorSpaceXform> SlowIdentityXform(SkColorSpace_XYZ* space);
91 
92 #endif
93