reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 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 | |
| 8 | #ifndef SkImageShader_DEFINED |
| 9 | #define SkImageShader_DEFINED |
| 10 | |
Kevin Lubick | 8fdbbca | 2023-06-07 16:14:51 -0400 | [diff] [blame] | 11 | #include "include/core/SkFlattenable.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkImage.h" |
Lei Zhang | 7f99e8d | 2021-10-26 00:06:49 -0700 | [diff] [blame] | 13 | #include "include/core/SkM44.h" |
Kevin Lubick | 8fdbbca | 2023-06-07 16:14:51 -0400 | [diff] [blame] | 14 | #include "include/core/SkRect.h" |
| 15 | #include "include/core/SkRefCnt.h" |
| 16 | #include "include/core/SkSamplingOptions.h" |
| 17 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/shaders/SkShaderBase.h" |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 19 | |
Kevin Lubick | 8fdbbca | 2023-06-07 16:14:51 -0400 | [diff] [blame] | 20 | class SkArenaAlloc; |
| 21 | class SkMatrix; |
| 22 | class SkReadBuffer; |
| 23 | class SkShader; |
| 24 | class SkWriteBuffer; |
| 25 | enum class SkTileMode; |
| 26 | struct SkStageRec; |
Kevin Lubick | b902658 | 2022-02-23 11:50:43 -0500 | [diff] [blame] | 27 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 28 | class SkImageShader : public SkShaderBase { |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 29 | public: |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 30 | static sk_sp<SkShader> Make(sk_sp<SkImage>, |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 31 | SkTileMode tmx, |
| 32 | SkTileMode tmy, |
Mike Reed | 225cdea | 2021-03-18 22:19:44 -0400 | [diff] [blame] | 33 | const SkSamplingOptions&, |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 34 | const SkMatrix* localMatrix, |
| 35 | bool clampAsIfUnpremul = false); |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 36 | |
Brian Osman | 20c6a94 | 2021-11-29 12:27:00 -0500 | [diff] [blame] | 37 | static sk_sp<SkShader> MakeRaw(sk_sp<SkImage>, |
| 38 | SkTileMode tmx, |
| 39 | SkTileMode tmy, |
| 40 | const SkSamplingOptions&, |
| 41 | const SkMatrix* localMatrix); |
| 42 | |
Michael Ludwig | 54d9447 | 2022-01-06 20:29:39 -0500 | [diff] [blame] | 43 | // 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 Salomon | 5418c20 | 2022-10-05 15:16:44 -0400 | [diff] [blame] | 53 | SkImageShader(sk_sp<SkImage>, |
| 54 | const SkRect& subset, |
| 55 | SkTileMode tmx, SkTileMode tmy, |
| 56 | const SkSamplingOptions&, |
| 57 | bool raw, |
| 58 | bool clampAsIfUnpremul); |
| 59 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 60 | bool isOpaque() const override; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 61 | |
Kevin Lubick | 8fdbbca | 2023-06-07 16:14:51 -0400 | [diff] [blame] | 62 | ShaderType type() const override { return ShaderType::kImage; } |
| 63 | |
Mike Reed | 3d30ca6 | 2020-07-22 16:55:02 -0400 | [diff] [blame] | 64 | static SkM44 CubicResamplerMatrix(float B, float C); |
| 65 | |
Kevin Lubick | 8fdbbca | 2023-06-07 16:14:51 -0400 | [diff] [blame] | 66 | 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 Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 73 | private: |
Mike Klein | 4fee323 | 2018-10-18 17:27:16 -0400 | [diff] [blame] | 74 | SK_FLATTENABLE_HOOKS(SkImageShader) |
| 75 | |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 76 | void flatten(SkWriteBuffer&) const override; |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 77 | #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 78 | Context* onMakeContext(const ContextRec&, SkArenaAlloc* storage) const override; |
Mike Reed | e92aae6 | 2018-10-17 10:21:51 -0400 | [diff] [blame] | 79 | #endif |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 80 | SkImage* onIsAImage(SkMatrix*, SkTileMode*) const override; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 81 | |
Kevin Lubick | 8fdbbca | 2023-06-07 16:14:51 -0400 | [diff] [blame] | 82 | bool appendStages(const SkStageRec&, const SkShaders::MatrixRec&) const override; |
Herb Derby | 5992f9e | 2021-07-17 18:19:03 -0400 | [diff] [blame] | 83 | |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 84 | sk_sp<SkImage> fImage; |
| 85 | const SkSamplingOptions fSampling; |
| 86 | const SkTileMode fTileModeX; |
| 87 | const SkTileMode fTileModeY; |
Michael Ludwig | 54d9447 | 2022-01-06 20:29:39 -0500 | [diff] [blame] | 88 | |
| 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 Osman | 20c6a94 | 2021-11-29 12:27:00 -0500 | [diff] [blame] | 93 | const bool fRaw; |
Mike Reed | 74c51ac | 2020-11-20 10:23:58 -0500 | [diff] [blame] | 94 | const bool fClampAsIfUnpremul; |
Mike Reed | f8a6b5b | 2020-07-10 08:36:42 -0400 | [diff] [blame] | 95 | |
Florin Malita | 4aed138 | 2017-05-25 10:38:07 -0400 | [diff] [blame] | 96 | friend class SkShaderBase; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 97 | using INHERITED = SkShaderBase; |
reed | 856e9d9 | 2015-09-30 12:21:45 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | #endif |