1 /*
2 * Copyright 2012 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 "SkColorSpaceXformer.h"
9 #include "SkRadialGradient.h"
10 #include "SkRasterPipeline.h"
11 #include "SkReadBuffer.h"
12 #include "SkWriteBuffer.h"
13
14 namespace {
15
rad_to_unit_matrix(const SkPoint & center,SkScalar radius)16 SkMatrix rad_to_unit_matrix(const SkPoint& center, SkScalar radius) {
17 SkScalar inv = SkScalarInvert(radius);
18
19 SkMatrix matrix;
20 matrix.setTranslate(-center.fX, -center.fY);
21 matrix.postScale(inv, inv);
22 return matrix;
23 }
24
25 } // namespace
26
27 /////////////////////////////////////////////////////////////////////
28
SkRadialGradient(const SkPoint & center,SkScalar radius,const Descriptor & desc)29 SkRadialGradient::SkRadialGradient(const SkPoint& center, SkScalar radius, const Descriptor& desc)
30 : SkGradientShaderBase(desc, rad_to_unit_matrix(center, radius))
31 , fCenter(center)
32 , fRadius(radius) {
33 }
34
asAGradient(GradientInfo * info) const35 SkShader::GradientType SkRadialGradient::asAGradient(GradientInfo* info) const {
36 if (info) {
37 commonAsAGradient(info);
38 info->fPoint[0] = fCenter;
39 info->fRadius[0] = fRadius;
40 }
41 return kRadial_GradientType;
42 }
43
CreateProc(SkReadBuffer & buffer)44 sk_sp<SkFlattenable> SkRadialGradient::CreateProc(SkReadBuffer& buffer) {
45 DescriptorScope desc;
46 if (!desc.unflatten(buffer)) {
47 return nullptr;
48 }
49 const SkPoint center = buffer.readPoint();
50 const SkScalar radius = buffer.readScalar();
51 return SkGradientShader::MakeRadial(center, radius, desc.fColors, std::move(desc.fColorSpace),
52 desc.fPos, desc.fCount, desc.fTileMode, desc.fGradFlags,
53 desc.fLocalMatrix);
54 }
55
flatten(SkWriteBuffer & buffer) const56 void SkRadialGradient::flatten(SkWriteBuffer& buffer) const {
57 this->INHERITED::flatten(buffer);
58 buffer.writePoint(fCenter);
59 buffer.writeScalar(fRadius);
60 }
61
onMakeColorSpace(SkColorSpaceXformer * xformer) const62 sk_sp<SkShader> SkRadialGradient::onMakeColorSpace(SkColorSpaceXformer* xformer) const {
63 const AutoXformColors xformedColors(*this, xformer);
64 return SkGradientShader::MakeRadial(fCenter, fRadius, xformedColors.fColors.get(), fOrigPos,
65 fColorCount, fTileMode, fGradFlags,
66 &this->getLocalMatrix());
67 }
68
appendGradientStages(SkArenaAlloc *,SkRasterPipeline * p,SkRasterPipeline *) const69 void SkRadialGradient::appendGradientStages(SkArenaAlloc*, SkRasterPipeline* p,
70 SkRasterPipeline*) const {
71 p->append(SkRasterPipeline::xy_to_radius);
72 }
73
74 /////////////////////////////////////////////////////////////////////
75
76 #if SK_SUPPORT_GPU
77
78 #include "gradients/GrGradientShader.h"
79
asFragmentProcessor(const GrFPArgs & args) const80 std::unique_ptr<GrFragmentProcessor> SkRadialGradient::asFragmentProcessor(
81 const GrFPArgs& args) const {
82 return GrGradientShader::MakeRadial(*this, args);
83 }
84
85 #endif
86