Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 1 | /* |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 2 | * Copyright 2018 Google Inc. |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 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 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 8 | #ifndef GrYUVtoRGBEffect_DEFINED |
| 9 | #define GrYUVtoRGBEffect_DEFINED |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 10 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 11 | #include "SkTypes.h" |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 12 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 13 | #include "GrFragmentProcessor.h" |
| 14 | #include "GrCoordTransform.h" |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame^] | 15 | #include "GrTextureDomain.h" |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 16 | |
| 17 | #include "SkYUVAIndex.h" |
| 18 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 19 | class GrYUVtoRGBEffect : public GrFragmentProcessor { |
| 20 | public: |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame^] | 21 | // The domain supported by this effect is more limited than the general GrTextureDomain due |
| 22 | // to the multi-planar, varying resolution images that it has to sample. If 'domain' is provided |
| 23 | // it is the Y plane's domain. This will automatically inset for bilinear filtering, and only |
| 24 | // the clamp wrap mode is supported. |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 25 | static std::unique_ptr<GrFragmentProcessor> Make(const sk_sp<GrTextureProxy> proxies[], |
| 26 | const SkYUVAIndex indices[4], |
Jim Van Verth | 30e0d7f | 2018-11-02 13:36:42 -0400 | [diff] [blame] | 27 | SkYUVColorSpace yuvColorSpace, |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame^] | 28 | GrSamplerState::Filter filterMode, |
| 29 | const SkMatrix& localMatrix = SkMatrix::I(), |
| 30 | const SkRect* domain = nullptr); |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 31 | #ifdef SK_DEBUG |
Robert Phillips | ba5c439 | 2018-07-25 12:37:14 -0400 | [diff] [blame] | 32 | SkString dumpInfo() const override; |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 33 | #endif |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 34 | |
Robert Phillips | b651aac | 2019-03-15 12:18:49 -0400 | [diff] [blame] | 35 | SkYUVColorSpace yuvColorSpace() const { return fYUVColorSpace; } |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 36 | const SkYUVAIndex& yuvaIndex(int i) const { return fYUVAIndices[i]; } |
| 37 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 38 | GrYUVtoRGBEffect(const GrYUVtoRGBEffect& src); |
| 39 | std::unique_ptr<GrFragmentProcessor> clone() const override; |
| 40 | const char* name() const override { return "YUVtoRGBEffect"; } |
| 41 | |
| 42 | private: |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 43 | GrYUVtoRGBEffect(const sk_sp<GrTextureProxy> proxies[], const SkSize scales[], |
| 44 | const GrSamplerState::Filter filterModes[], int numPlanes, |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame^] | 45 | const SkYUVAIndex yuvaIndices[4], SkYUVColorSpace yuvColorSpace, |
| 46 | const SkMatrix& localMatrix, const SkRect* domain) |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 47 | : INHERITED(kGrYUVtoRGBEffect_ClassID, kNone_OptimizationFlags) |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame^] | 48 | , fDomains{GrTextureDomain::IgnoredDomain(), GrTextureDomain::IgnoredDomain(), |
| 49 | GrTextureDomain::IgnoredDomain(), GrTextureDomain::IgnoredDomain()} |
Robert Phillips | b651aac | 2019-03-15 12:18:49 -0400 | [diff] [blame] | 50 | , fYUVColorSpace(yuvColorSpace) { |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 51 | for (int i = 0; i < numPlanes; ++i) { |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame^] | 52 | SkMatrix planeMatrix = SkMatrix::MakeScale(scales[i].width(), scales[i].height()); |
| 53 | if (domain) { |
| 54 | SkASSERT(filterModes[i] != GrSamplerState::Filter::kMipMap); |
| 55 | |
| 56 | SkRect scaledDomain = planeMatrix.mapRect(*domain); |
| 57 | if (filterModes[i] != GrSamplerState::Filter::kNearest) { |
| 58 | // Inset by half a pixel for bilerp, after scaling to the size of the plane |
| 59 | scaledDomain.inset(0.5f, 0.5f); |
| 60 | } |
| 61 | |
| 62 | fDomains[i] = GrTextureDomain(proxies[i].get(), scaledDomain, |
| 63 | GrTextureDomain::kClamp_Mode, GrTextureDomain::kClamp_Mode, i); |
| 64 | } |
| 65 | |
| 66 | planeMatrix.postConcat(localMatrix); |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 67 | fSamplers[i].reset(std::move(proxies[i]), |
| 68 | GrSamplerState(GrSamplerState::WrapMode::kClamp, filterModes[i])); |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame^] | 69 | fSamplerTransforms[i] = planeMatrix; |
Brian Salomon | 246bc3d | 2018-12-06 15:33:02 -0500 | [diff] [blame] | 70 | fSamplerCoordTransforms[i] = |
| 71 | GrCoordTransform(fSamplerTransforms[i], fSamplers[i].proxy()); |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | this->setTextureSamplerCnt(numPlanes); |
| 75 | for (int i = 0; i < numPlanes; ++i) { |
| 76 | this->addCoordTransform(&fSamplerCoordTransforms[i]); |
| 77 | } |
| 78 | |
| 79 | memcpy(fYUVAIndices, yuvaIndices, sizeof(fYUVAIndices)); |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 80 | } |
| 81 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 82 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
| 83 | bool onIsEqual(const GrFragmentProcessor&) const override; |
Brian Salomon | f7dcd76 | 2018-07-30 14:48:15 -0400 | [diff] [blame] | 84 | const TextureSampler& onTextureSampler(int) const override; |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 85 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 86 | |
| 87 | TextureSampler fSamplers[4]; |
| 88 | SkMatrix44 fSamplerTransforms[4]; |
| 89 | GrCoordTransform fSamplerCoordTransforms[4]; |
Michael Ludwig | a6a8400 | 2019-04-12 15:03:02 -0400 | [diff] [blame^] | 90 | GrTextureDomain fDomains[4]; |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 91 | SkYUVAIndex fYUVAIndices[4]; |
Robert Phillips | b651aac | 2019-03-15 12:18:49 -0400 | [diff] [blame] | 92 | SkYUVColorSpace fYUVColorSpace; |
Robert Phillips | 94ade75 | 2018-10-09 12:32:31 -0400 | [diff] [blame] | 93 | |
Ethan Nicholas | 7461a4a | 2017-12-21 14:18:01 -0500 | [diff] [blame] | 94 | typedef GrFragmentProcessor INHERITED; |
| 95 | }; |
| 96 | #endif |