| 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 |
#include "src/shaders/SkColorShader.h" |
| 9 |
|
| 10 |
#include "include/core/SkAlphaType.h" |
| 11 |
#include "include/core/SkColorSpace.h" |
| 12 |
#include "include/core/SkData.h" |
| 13 |
#include "include/core/SkFlattenable.h" |
| 14 |
#include "include/core/SkRefCnt.h" |
| 15 |
#include "include/core/SkShader.h" |
| 16 |
#include "include/private/base/SkFloatingPoint.h" |
| 17 |
#include "src/core/SkColorSpacePriv.h" |
| 18 |
#include "src/core/SkColorSpaceXformSteps.h" |
| 19 |
#include "src/core/SkEffectPriv.h" |
| 20 |
#include "src/core/SkPicturePriv.h" |
| 21 |
#include "src/core/SkRasterPipeline.h" |
| 22 |
#include "src/core/SkReadBuffer.h" |
| 23 |
#include "src/core/SkWriteBuffer.h" |
| 24 |
#include "src/shaders/SkShaderBase.h" |
| 25 |
|
| 26 |
#include <utility> |
| 27 |
|
| 28 |
namespace { |
| 29 |
|
| 30 |
sk_sp<SkFlattenable> legacy_color4_create_proc(SkReadBuffer& buffer) { |
| 31 |
if (buffer.validate(buffer.isVersionLT(SkPicturePriv::Version::kCombineColorShaders))) { |
| 32 |
// Only SKPs older than this version should be referring to this create proc. |
| 33 |
SkColor4f color; |
| 34 |
sk_sp<SkColorSpace> colorSpace; |
| 35 |
buffer.readColor4f(&color); |
| 36 |
if (buffer.readBool()) { |
| 37 |
sk_sp<SkData> data = buffer.readByteArrayAsData(); |
| 38 |
colorSpace = data ? SkColorSpace::Deserialize(data->data(), data->size()) : nullptr; |
| 39 |
} |
| 40 |
return SkShaders::Color(color, std::move(colorSpace)); |
| 41 |
} |
| 42 |
|
| 43 |
return nullptr; |
| 44 |
} |
| 45 |
|
| 46 |
} // anonymous namespace |
| 47 |
|
| 48 |
sk_sp<SkFlattenable> SkColorShader::CreateProc(SkReadBuffer& buffer) { |
| 49 |
if (buffer.isVersionLT(SkPicturePriv::Version::kCombineColorShaders)) { |
| 50 |
// Stored an 8-bit color only. |
| 51 |
return SkShaders::Color(buffer.readColor()); |
| 52 |
} else { |
| 53 |
// Stores a floating-point color in sRGB. |
| 54 |
SkColor4f color; |
| 55 |
buffer.readColor4f(&color); |
| 56 |
return SkShaders::Color(color, SkColorSpace::MakeSRGB()); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
void SkColorShader::flatten(SkWriteBuffer& buffer) const { |
| 61 |
buffer.writeColor4f(fColor); |
| 62 |
} |
| 63 |
|
| 64 |
bool SkColorShader::appendStages(const SkStageRec& rec, const SkShaders::MatrixRec&) const { |
| 65 |
SkColor4f color = fColor; |
| 66 |
SkColorSpaceXformSteps(sk_srgb_singleton(), kUnpremul_SkAlphaType, |
| 67 |
rec.fDstCS, kPremul_SkAlphaType).apply(color.vec()); |
| 68 |
rec.fPipeline->appendConstantColor(rec.fAlloc, color.vec()); |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
///////////////////////////////////////////////////////////////////////////////////////////////// |
| 73 |
|
| 74 |
void SkRegisterColorShaderFlattenable() { |
| 75 |
SK_REGISTER_FLATTENABLE(SkColorShader); |
| 76 |
SkFlattenable::Register("SkColorShader4", legacy_color4_create_proc); |
| 77 |
} |
| 78 |
|
| 79 |
namespace SkShaders { |
| 80 |
sk_sp<SkShader> Color(SkColor color) { |
| 81 |
return Color(SkColor4f::FromColor(color), SkColorSpace::MakeSRGB()); |
| 82 |
} |
| 83 |
|
| 84 |
sk_sp<SkShader> Color(const SkColor4f& color, sk_sp<SkColorSpace> space) { |
| 85 |
if (!SkIsFinite(color.vec(), 4)) { |
| 86 |
return nullptr; |
| 87 |
} |
| 88 |
|
| 89 |
// Convert to sRGB to simplify what must be stored, remaining unpremul until the final dst |
| 90 |
// color space is known during actual shading. Also pin the alpha to [0,1]. |
| 91 |
SkColor4f srgb = color.pinAlpha(); |
| 92 |
SkColorSpaceXformSteps(space.get(), kUnpremul_SkAlphaType, |
| 93 |
sk_srgb_singleton(), kUnpremul_SkAlphaType).apply(srgb.vec()); |
| 94 |
|
| 95 |
return sk_make_sp<SkColorShader>(srgb); |
| 96 |
} |
| 97 |
} // namespace SkShaders |