blob: 70d4fcc76f4541bf30f623ab33712be750a0c1dd [file] [log] [blame]
reed856e9d92015-09-30 12:21:45 -07001/*
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
8#ifndef SkImageShader_DEFINED
9#define SkImageShader_DEFINED
10
Kevin Lubick8fdbbca2023-06-07 16:14:51 -040011#include "include/core/SkFlattenable.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkImage.h"
Lei Zhang7f99e8d2021-10-26 00:06:49 -070013#include "include/core/SkM44.h"
Kevin Lubick8fdbbca2023-06-07 16:14:51 -040014#include "include/core/SkRect.h"
15#include "include/core/SkRefCnt.h"
16#include "include/core/SkSamplingOptions.h"
17#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/shaders/SkShaderBase.h"
reed856e9d92015-09-30 12:21:45 -070019
Kevin Lubick8fdbbca2023-06-07 16:14:51 -040020class SkArenaAlloc;
21class SkMatrix;
22class SkReadBuffer;
23class SkShader;
24class SkWriteBuffer;
25enum class SkTileMode;
26struct SkStageRec;
Kevin Lubickb9026582022-02-23 11:50:43 -050027
Florin Malita4aed1382017-05-25 10:38:07 -040028class SkImageShader : public SkShaderBase {
reed856e9d92015-09-30 12:21:45 -070029public:
Mike Klein1f313092018-01-03 10:30:21 -050030 static sk_sp<SkShader> Make(sk_sp<SkImage>,
Mike Reede25b4472019-04-02 17:49:12 -040031 SkTileMode tmx,
32 SkTileMode tmy,
Mike Reed225cdea2021-03-18 22:19:44 -040033 const SkSamplingOptions&,
Mike Klein1f313092018-01-03 10:30:21 -050034 const SkMatrix* localMatrix,
35 bool clampAsIfUnpremul = false);
reed856e9d92015-09-30 12:21:45 -070036
Brian Osman20c6a942021-11-29 12:27:00 -050037 static sk_sp<SkShader> MakeRaw(sk_sp<SkImage>,
38 SkTileMode tmx,
39 SkTileMode tmy,
40 const SkSamplingOptions&,
41 const SkMatrix* localMatrix);
42
Michael Ludwig54d94472022-01-06 20:29:39 -050043 // TODO(skbug.com/12784): Requires SkImage to be texture backed, and created SkShader can only
44 // be used on GPU-backed surfaces.
45 static sk_sp<SkShader> MakeSubset(sk_sp<SkImage>,
46 const SkRect& subset,
47 SkTileMode tmx,
48 SkTileMode tmy,
49 const SkSamplingOptions&,
50 const SkMatrix* localMatrix,
51 bool clampAsIfUnpremul = false);
52
Brian Salomon5418c202022-10-05 15:16:44 -040053 SkImageShader(sk_sp<SkImage>,
54 const SkRect& subset,
55 SkTileMode tmx, SkTileMode tmy,
56 const SkSamplingOptions&,
57 bool raw,
58 bool clampAsIfUnpremul);
59
reed856e9d92015-09-30 12:21:45 -070060 bool isOpaque() const override;
reed856e9d92015-09-30 12:21:45 -070061
Kevin Lubick8fdbbca2023-06-07 16:14:51 -040062 ShaderType type() const override { return ShaderType::kImage; }
63
Mike Reed3d30ca62020-07-22 16:55:02 -040064 static SkM44 CubicResamplerMatrix(float B, float C);
65
Kevin Lubick8fdbbca2023-06-07 16:14:51 -040066 SkTileMode tileModeX() const { return fTileModeX; }
67 SkTileMode tileModeY() const { return fTileModeY; }
68 sk_sp<SkImage> image() const { return fImage; }
69 SkSamplingOptions sampling() const { return fSampling; }
70 SkRect subset() const { return fSubset; }
71 bool isRaw() const { return fRaw; }
72
Mike Klein1f313092018-01-03 10:30:21 -050073private:
Mike Klein4fee3232018-10-18 17:27:16 -040074 SK_FLATTENABLE_HOOKS(SkImageShader)
75
reed856e9d92015-09-30 12:21:45 -070076 void flatten(SkWriteBuffer&) const override;
Mike Reede92aae62018-10-17 10:21:51 -040077#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
Herb Derby83e939b2017-02-07 14:25:11 -050078 Context* onMakeContext(const ContextRec&, SkArenaAlloc* storage) const override;
Mike Reede92aae62018-10-17 10:21:51 -040079#endif
Mike Reedfae8fce2019-04-03 10:27:45 -040080 SkImage* onIsAImage(SkMatrix*, SkTileMode*) const override;
reed856e9d92015-09-30 12:21:45 -070081
Kevin Lubick8fdbbca2023-06-07 16:14:51 -040082 bool appendStages(const SkStageRec&, const SkShaders::MatrixRec&) const override;
Herb Derby5992f9e2021-07-17 18:19:03 -040083
Mike Reed74c51ac2020-11-20 10:23:58 -050084 sk_sp<SkImage> fImage;
85 const SkSamplingOptions fSampling;
86 const SkTileMode fTileModeX;
87 const SkTileMode fTileModeY;
Michael Ludwig54d94472022-01-06 20:29:39 -050088
89 // TODO(skbug.com/12784): This is only supported for GPU images currently.
90 // If subset == (0,0,w,h) of the image, then no subset is applied. Subset will not be empty.
91 const SkRect fSubset;
92
Brian Osman20c6a942021-11-29 12:27:00 -050093 const bool fRaw;
Mike Reed74c51ac2020-11-20 10:23:58 -050094 const bool fClampAsIfUnpremul;
Mike Reedf8a6b5b2020-07-10 08:36:42 -040095
Florin Malita4aed1382017-05-25 10:38:07 -040096 friend class SkShaderBase;
John Stiles7571f9e2020-09-02 22:42:33 -040097 using INHERITED = SkShaderBase;
reed856e9d92015-09-30 12:21:45 -070098};
99
100#endif