| 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 |
|
| 11 |
#include "include/core/SkFlattenable.h" |
| 12 |
#include "include/core/SkImage.h" |
| 13 |
#include "include/core/SkM44.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkRefCnt.h" |
| 16 |
#include "include/core/SkSamplingOptions.h" |
| 17 |
#include "include/core/SkTypes.h" |
| 18 |
#include "src/shaders/SkShaderBase.h" |
| 19 |
|
| 20 |
class SkArenaAlloc; |
| 21 |
class SkMatrix; |
| 22 |
class SkReadBuffer; |
| 23 |
class SkShader; |
| 24 |
class SkWriteBuffer; |
| 25 |
enum class SkTileMode; |
| 26 |
struct SkStageRec; |
| 27 |
|
| 28 |
class SkImageShader : public SkShaderBase { |
| 29 |
public: |
| 30 |
static sk_sp<SkShader> Make(sk_sp<SkImage>, |
| 31 |
SkTileMode tmx, |
| 32 |
SkTileMode tmy, |
| 33 |
const SkSamplingOptions&, |
| 34 |
const SkMatrix* localMatrix, |
| 35 |
bool clampAsIfUnpremul = false); |
| 36 |
|
| 37 |
static sk_sp<SkShader> MakeRaw(sk_sp<SkImage>, |
| 38 |
SkTileMode tmx, |
| 39 |
SkTileMode tmy, |
| 40 |
const SkSamplingOptions&, |
| 41 |
const SkMatrix* localMatrix); |
| 42 |
|
| 43 |
// TODO(skbug.com/40043877): Requires SkImage to be texture backed, and created SkShader can |
| 44 |
// only 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 |
|
| 53 |
// Given arguments for a call to SkCanvas::drawImageRect, returns a |
| 54 |
// possibly adjusted 'dst' SkRect and a shader to apply to the paint such that calling |
| 55 |
// SkCanvas::drawRect(newDst, *paint) produces |
| 56 |
// visually equivalent results to the original drawImageRect() call. |
| 57 |
static std::pair<SkRect, sk_sp<SkShader>> MakeForDrawRect(const SkImage* image, |
| 58 |
const SkPaint& paint, |
| 59 |
const SkSamplingOptions&, |
| 60 |
SkRect src, |
| 61 |
SkRect dst, |
| 62 |
bool strictSrcSubset); |
| 63 |
|
| 64 |
SkImageShader(sk_sp<SkImage>, |
| 65 |
const SkRect& subset, |
| 66 |
SkTileMode tmx, SkTileMode tmy, |
| 67 |
const SkSamplingOptions&, |
| 68 |
bool raw, |
| 69 |
bool clampAsIfUnpremul); |
| 70 |
|
| 71 |
bool isOpaque() const override; |
| 72 |
|
| 73 |
ShaderType type() const override { return ShaderType::kImage; } |
| 74 |
|
| 75 |
static SkM44 CubicResamplerMatrix(float B, float C); |
| 76 |
|
| 77 |
SkTileMode tileModeX() const { return fTileModeX; } |
| 78 |
SkTileMode tileModeY() const { return fTileModeY; } |
| 79 |
sk_sp<SkImage> image() const { return fImage; } |
| 80 |
SkSamplingOptions sampling() const { return fSampling; } |
| 81 |
SkRect subset() const { return fSubset; } |
| 82 |
bool isRaw() const { return fRaw; } |
| 83 |
|
| 84 |
private: |
| 85 |
SK_FLATTENABLE_HOOKS(SkImageShader) |
| 86 |
|
| 87 |
void flatten(SkWriteBuffer&) const override; |
| 88 |
#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT |
| 89 |
Context* onMakeContext(const ContextRec&, SkArenaAlloc* storage) const override; |
| 90 |
#endif |
| 91 |
SkImage* onIsAImage(SkMatrix*, SkTileMode*) const override; |
| 92 |
|
| 93 |
bool appendStages(const SkStageRec&, const SkShaders::MatrixRec&) const override; |
| 94 |
|
| 95 |
sk_sp<SkImage> fImage; |
| 96 |
const SkSamplingOptions fSampling; |
| 97 |
const SkTileMode fTileModeX; |
| 98 |
const SkTileMode fTileModeY; |
| 99 |
|
| 100 |
// TODO(skbug.com/40043877): This is only supported for GPU images currently. |
| 101 |
// If subset == (0,0,w,h) of the image, then no subset is applied. Subset will not be empty. |
| 102 |
const SkRect fSubset; |
| 103 |
|
| 104 |
const bool fRaw; |
| 105 |
const bool fClampAsIfUnpremul; |
| 106 |
|
| 107 |
friend class SkShaderBase; |
| 108 |
using INHERITED = SkShaderBase; |
| 109 |
}; |
| 110 |
|
| 111 |
#endif |