xref: /aosp_15_r20/external/skia/src/pdf/SkPDFGradientShader.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2017 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 SkPDFGradientShader_DEFINED
8 #define SkPDFGradientShader_DEFINED
9 
10 #include "include/core/SkColor.h"
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkRect.h"
13 #include "include/core/SkScalar.h"
14 #include "include/private/base/SkAssert.h"
15 #include "include/private/base/SkPoint_impl.h"
16 #include "src/pdf/SkPDFTypes.h"
17 #include "src/pdf/SkPDFUtils.h"
18 #include "src/shaders/SkShaderBase.h"
19 
20 #include <cstdint>
21 #include <memory>
22 
23 class SkPDFDocument;
24 class SkShader;
25 
26 namespace SkPDFGradientShader {
27 
28 SkPDFIndirectReference Make(SkPDFDocument* doc,
29                             SkShader* shader,
30                             const SkMatrix& matrix,
31                             const SkIRect& surfaceBBox);
32 
33 struct Key {
34     SkShaderBase::GradientType fType;
35     SkShaderBase::GradientInfo fInfo;
36     std::unique_ptr<SkColor[]> fColors;
37     std::unique_ptr<SkScalar[]> fStops;
38     SkMatrix fCanvasTransform;
39     SkMatrix fShaderTransform;
40     SkIRect fBBox;
41     uint32_t fHash;
42 };
43 
44 struct KeyHash {
operatorKeyHash45     uint32_t operator()(const Key& k) const { return k.fHash; }
46 };
47 
48 inline bool operator==(const SkShaderBase::GradientInfo& u, const SkShaderBase::GradientInfo& v) {
49     return u.fColorCount    == v.fColorCount
50         && u.fPoint[0]      == v.fPoint[0]
51         && u.fPoint[1]      == v.fPoint[1]
52         && u.fRadius[0]     == v.fRadius[0]
53         && u.fRadius[1]     == v.fRadius[1]
54         && u.fTileMode      == v.fTileMode
55         && u.fGradientFlags == v.fGradientFlags
56         && SkPackedArrayEqual(u.fColors, v.fColors, u.fColorCount)
57         && SkPackedArrayEqual(u.fColorOffsets, v.fColorOffsets, u.fColorCount);
58 }
59 
60 inline bool operator==(const Key& u, const Key& v) {
61     SkASSERT(u.fInfo.fColors       == u.fColors.get());
62     SkASSERT(u.fInfo.fColorOffsets == u.fStops.get());
63     SkASSERT(v.fInfo.fColors       == v.fColors.get());
64     SkASSERT(v.fInfo.fColorOffsets == v.fStops.get());
65     return u.fType            == v.fType
66         && u.fInfo            == v.fInfo
67         && u.fCanvasTransform == v.fCanvasTransform
68         && u.fShaderTransform == v.fShaderTransform
69         && u.fBBox            == v.fBBox;
70 }
71 inline bool operator!=(const Key& u, const Key& v) { return !(u == v); }
72 
73 }  // namespace SkPDFGradientShader
74 #endif  // SkPDFGradientShader_DEFINED
75