brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 GrColorSpaceXform_DEFINED |
| 9 | #define GrColorSpaceXform_DEFINED |
| 10 | |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 11 | #include "GrFragmentProcessor.h" |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 12 | #include "SkColorSpaceXformSteps.h" |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 13 | #include "SkRefCnt.h" |
| 14 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 15 | class SkColorSpace; |
| 16 | |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 17 | /** |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 18 | * Represents a color space transformation |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 19 | */ |
| 20 | class GrColorSpaceXform : public SkRefCnt { |
| 21 | public: |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 22 | GrColorSpaceXform(const SkColorSpaceXformSteps& steps) : fSteps(steps) {} |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 23 | |
Mike Klein | e03a176 | 2018-08-22 11:52:16 -0400 | [diff] [blame] | 24 | static sk_sp<GrColorSpaceXform> Make(SkColorSpace* src, SkAlphaType srcAT, |
| 25 | SkColorSpace* dst, SkAlphaType dstAT); |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 26 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 27 | const SkColorSpaceXformSteps& steps() const { return fSteps; } |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * GrGLSLFragmentProcessor::GenKey() must call this and include the returned value in its |
| 31 | * computed key. |
| 32 | */ |
Brian Osman | e411a07 | 2017-03-14 10:26:58 -0400 | [diff] [blame] | 33 | static uint32_t XformKey(const GrColorSpaceXform* xform) { |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 34 | // Code generation depends on which steps we apply |
| 35 | return xform ? xform->fSteps.flags.mask() : 0; |
brianosman | 77320db | 2016-09-07 08:09:10 -0700 | [diff] [blame] | 36 | } |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 37 | |
brianosman | b9c5137 | 2016-09-15 11:09:45 -0700 | [diff] [blame] | 38 | static bool Equals(const GrColorSpaceXform* a, const GrColorSpaceXform* b); |
| 39 | |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 40 | SkColor4f apply(const SkColor4f& srcColor); |
brianosman | 5a7ae7e | 2016-09-12 12:07:25 -0700 | [diff] [blame] | 41 | |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 42 | private: |
Brian Osman | f06ead9 | 2017-10-30 13:47:41 -0400 | [diff] [blame] | 43 | friend class GrGLSLColorSpaceXformHelper; |
| 44 | |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 45 | SkColorSpaceXformSteps fSteps; |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 48 | class GrColorSpaceXformEffect : public GrFragmentProcessor { |
| 49 | public: |
| 50 | /** |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 51 | * Returns a fragment processor that converts the input's color space from src to dst. |
| 52 | */ |
Brian Osman | 21fc5ce | 2018-08-27 20:36:19 +0000 | [diff] [blame] | 53 | static std::unique_ptr<GrFragmentProcessor> Make(SkColorSpace* src, SkAlphaType srcAT, |
Brian Osman | 15f0f29 | 2018-10-01 14:14:46 -0400 | [diff] [blame] | 54 | SkColorSpace* dst, SkAlphaType dstAT); |
Brian Osman | 3567c14 | 2018-06-18 10:20:32 -0400 | [diff] [blame] | 55 | |
| 56 | /** |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 57 | * Returns a fragment processor that calls the passed in fragment processor, and then converts |
| 58 | * the color space of the output from src to dst. |
| 59 | */ |
| 60 | static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> child, |
Brian Osman | 21fc5ce | 2018-08-27 20:36:19 +0000 | [diff] [blame] | 61 | SkColorSpace* src, SkAlphaType srcAT, |
| 62 | SkColorSpace* dst); |
Brian Osman | c4f93ca | 2017-10-17 17:15:52 -0400 | [diff] [blame] | 63 | |
| 64 | const char* name() const override { return "ColorSpaceXform"; } |
| 65 | std::unique_ptr<GrFragmentProcessor> clone() const override; |
| 66 | |
| 67 | const GrColorSpaceXform* colorXform() const { return fColorXform.get(); } |
| 68 | |
| 69 | private: |
| 70 | GrColorSpaceXformEffect(std::unique_ptr<GrFragmentProcessor> child, |
| 71 | sk_sp<GrColorSpaceXform> colorXform); |
| 72 | |
| 73 | static OptimizationFlags OptFlags(const GrFragmentProcessor* child); |
| 74 | |
| 75 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 76 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
| 77 | bool onIsEqual(const GrFragmentProcessor&) const override; |
| 78 | |
| 79 | sk_sp<GrColorSpaceXform> fColorXform; |
| 80 | |
| 81 | typedef GrFragmentProcessor INHERITED; |
| 82 | }; |
| 83 | |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 84 | #endif |