Brian Osman | 10b6ad1 | 2017-07-25 09:59:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Brian Osman | 10b6ad1 | 2017-07-25 09:59:24 -0400 | [diff] [blame] | 8 | #include "SkColorSpace_Base.h" |
Mike Klein | 92db5a3 | 2017-07-25 10:51:41 -0400 | [diff] [blame] | 9 | #include "SkPM4fPriv.h" |
Brian Osman | 10b6ad1 | 2017-07-25 09:59:24 -0400 | [diff] [blame] | 10 | #include "SkRasterPipeline.h" |
| 11 | #include "SkString.h" |
Mike Klein | 92db5a3 | 2017-07-25 10:51:41 -0400 | [diff] [blame] | 12 | #include "SkToSRGBColorFilter.h" |
Brian Osman | 10b6ad1 | 2017-07-25 09:59:24 -0400 | [diff] [blame] | 13 | |
| 14 | #if SK_SUPPORT_GPU |
Mike Klein | 92db5a3 | 2017-07-25 10:51:41 -0400 | [diff] [blame] | 15 | #include "effects/GrNonlinearColorSpaceXformEffect.h" |
Brian Osman | 10b6ad1 | 2017-07-25 09:59:24 -0400 | [diff] [blame] | 16 | #endif |
| 17 | |
| 18 | void SkToSRGBColorFilter::onAppendStages(SkRasterPipeline* p, |
Mike Klein | 92db5a3 | 2017-07-25 10:51:41 -0400 | [diff] [blame] | 19 | SkColorSpace* /*dst color space*/, |
| 20 | SkArenaAlloc* alloc, |
Brian Osman | 10b6ad1 | 2017-07-25 09:59:24 -0400 | [diff] [blame] | 21 | bool shaderIsOpaque) const { |
Mike Klein | 92db5a3 | 2017-07-25 10:51:41 -0400 | [diff] [blame] | 22 | if (fSrcColorSpace->isSRGB()) { |
| 23 | // That was easy. |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | // Step 1: Linearize by undoing the src transfer function. |
| 28 | // Linear and sRGB will return true to isNumericalTransferFn(), so we check them first. |
| 29 | SkColorSpaceTransferFn srcFn; |
| 30 | if (fSrcColorSpace->gammaIsLinear()) { |
| 31 | // Nothing to do. |
| 32 | } else if (fSrcColorSpace->gammaCloseToSRGB()) { |
| 33 | p->append_from_srgb(shaderIsOpaque ? kOpaque_SkAlphaType |
| 34 | : kPremul_SkAlphaType); |
| 35 | } else if (fSrcColorSpace->isNumericalTransferFn(&srcFn)) { |
| 36 | p->append(SkRasterPipeline::parametric_r, &srcFn); |
| 37 | p->append(SkRasterPipeline::parametric_g, &srcFn); |
| 38 | p->append(SkRasterPipeline::parametric_b, &srcFn); |
| 39 | } else { |
| 40 | SkDEBUGFAIL("Looks like we got a table transfer function here, quite unexpectedly."); |
| 41 | // TODO: If we really need to handle this, we can, but I don't think Ganesh does. |
| 42 | } |
| 43 | |
| 44 | // Step 2: Transform to sRGB gamut, without clamping. |
| 45 | (void)append_gamut_transform_noclamp(p, |
| 46 | alloc->makeArrayDefault<float>(12), |
| 47 | fSrcColorSpace.get(), |
| 48 | SkColorSpace::MakeSRGB().get()); |
| 49 | |
| 50 | // Step 3: Back to sRGB encoding. |
| 51 | p->append(SkRasterPipeline::to_srgb); |
Brian Osman | 10b6ad1 | 2017-07-25 09:59:24 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | sk_sp<SkColorFilter> SkToSRGBColorFilter::Make(sk_sp<SkColorSpace> srcColorSpace) { |
Brian Osman | fa3ed03 | 2017-07-25 10:48:19 -0400 | [diff] [blame^] | 55 | if (srcColorSpace->isSRGB()) { |
| 56 | return nullptr; |
| 57 | } else { |
| 58 | return sk_sp<SkColorFilter>(new SkToSRGBColorFilter(std::move(srcColorSpace))); |
| 59 | } |
Brian Osman | 10b6ad1 | 2017-07-25 09:59:24 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | SkToSRGBColorFilter::SkToSRGBColorFilter(sk_sp<SkColorSpace> srcColorSpace) |
| 63 | : fSrcColorSpace(std::move(srcColorSpace)) {} |
| 64 | |
| 65 | sk_sp<SkFlattenable> SkToSRGBColorFilter::CreateProc(SkReadBuffer&) { |
| 66 | // TODO |
| 67 | return nullptr; |
| 68 | } |
| 69 | |
| 70 | void SkToSRGBColorFilter::flatten(SkWriteBuffer&) const { |
| 71 | // TODO |
| 72 | } |
| 73 | |
| 74 | #ifndef SK_IGNORE_TO_STRING |
| 75 | void SkToSRGBColorFilter::toString(SkString* str) const { |
| 76 | // TODO |
| 77 | str->append("SkToSRGBColorFilter "); |
| 78 | } |
| 79 | #endif |
| 80 | |
| 81 | #if SK_SUPPORT_GPU |
| 82 | sk_sp<GrFragmentProcessor> SkToSRGBColorFilter::asFragmentProcessor(GrContext*, |
| 83 | SkColorSpace*) const { |
| 84 | return GrNonlinearColorSpaceXformEffect::Make(fSrcColorSpace.get(), |
| 85 | SkColorSpace::MakeSRGB().get()); |
| 86 | } |
| 87 | #endif |