| 1 | /* |
| 2 | * Copyright 2014 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 SkPictureShader_DEFINED |
| 9 | #define SkPictureShader_DEFINED |
| 10 | |
| 11 | #include "include/core/SkTileMode.h" |
| 12 | #include "src/shaders/SkShaderBase.h" |
| 13 | #include <atomic> |
| 14 | |
| 15 | class SkArenaAlloc; |
| 16 | class SkBitmap; |
| 17 | class SkPicture; |
| 18 | |
| 19 | /* |
| 20 | * An SkPictureShader can be used to draw SkPicture-based patterns. |
| 21 | * |
| 22 | * The SkPicture is first rendered into a tile, which is then used to shade the area according |
| 23 | * to specified tiling rules. |
| 24 | */ |
| 25 | class SkPictureShader : public SkShaderBase { |
| 26 | public: |
| 27 | ~SkPictureShader() override; |
| 28 | |
| 29 | static sk_sp<SkShader> Make(sk_sp<SkPicture>, SkTileMode, SkTileMode, const SkMatrix*, |
| 30 | const SkRect*); |
| 31 | |
| 32 | #if SK_SUPPORT_GPU |
| 33 | std::unique_ptr<GrFragmentProcessor> asFragmentProcessor(const GrFPArgs&) const override; |
| 34 | #endif |
| 35 | |
| 36 | protected: |
| 37 | SkPictureShader(SkReadBuffer&); |
| 38 | void flatten(SkWriteBuffer&) const override; |
| 39 | bool onAppendStages(const SkStageRec&) const override; |
| 40 | skvm::Color onProgram(skvm::Builder*, skvm::Coord device, skvm::Coord local, skvm::Color paint, |
| 41 | const SkMatrixProvider&, const SkMatrix* localM, |
| 42 | SkFilterQuality quality, const SkColorInfo& dst, |
| 43 | skvm::Uniforms* uniforms, SkArenaAlloc* alloc) const override; |
| 44 | |
| 45 | #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT |
| 46 | Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const override; |
| 47 | #endif |
| 48 | |
| 49 | private: |
| 50 | SK_FLATTENABLE_HOOKS(SkPictureShader) |
| 51 | |
| 52 | SkPictureShader(sk_sp<SkPicture>, SkTileMode, SkTileMode, const SkMatrix*, const SkRect*); |
| 53 | |
| 54 | sk_sp<SkShader> refBitmapShader(const SkMatrix&, SkTCopyOnFirstWrite<SkMatrix>* localMatrix, |
| 55 | SkColorType dstColorType, SkColorSpace* dstColorSpace, |
| 56 | const int maxTextureSize = 0) const; |
| 57 | |
| 58 | class PictureShaderContext : public Context { |
| 59 | public: |
| 60 | PictureShaderContext( |
| 61 | const SkPictureShader&, const ContextRec&, sk_sp<SkShader> bitmapShader, SkArenaAlloc*); |
| 62 | |
| 63 | uint32_t getFlags() const override; |
| 64 | |
| 65 | void shadeSpan(int x, int y, SkPMColor dstC[], int count) override; |
| 66 | |
| 67 | sk_sp<SkShader> fBitmapShader; |
| 68 | SkShaderBase::Context* fBitmapShaderContext; |
| 69 | void* fBitmapShaderContextStorage; |
| 70 | |
| 71 | typedef Context INHERITED; |
| 72 | }; |
| 73 | |
| 74 | sk_sp<SkPicture> fPicture; |
| 75 | SkRect fTile; |
| 76 | SkTileMode fTmx, fTmy; |
| 77 | |
| 78 | const uint32_t fUniqueID; |
| 79 | mutable std::atomic<bool> fAddedToCache; |
| 80 | |
| 81 | typedef SkShaderBase INHERITED; |
| 82 | }; |
| 83 | |
| 84 | #endif // SkPictureShader_DEFINED |
| 85 | |