skia2/include/effects/SkGradientShader.h

256 lines
16 KiB
C
Raw Normal View History

/*
Automatic update of all copyright notices to reflect new license terms. I have manually examined all of these diffs and restored a few files that seem to require manual adjustment. The following files still need to be modified manually, in a separate CL: android_sample/SampleApp/AndroidManifest.xml android_sample/SampleApp/res/layout/layout.xml android_sample/SampleApp/res/menu/sample.xml android_sample/SampleApp/res/values/strings.xml android_sample/SampleApp/src/com/skia/sampleapp/SampleApp.java android_sample/SampleApp/src/com/skia/sampleapp/SampleView.java experimental/CiCarbonSampleMain.c experimental/CocoaDebugger/main.m experimental/FileReaderApp/main.m experimental/SimpleCocoaApp/main.m experimental/iOSSampleApp/Shared/SkAlertPrompt.h experimental/iOSSampleApp/Shared/SkAlertPrompt.m experimental/iOSSampleApp/SkiOSSampleApp-Base.xcconfig experimental/iOSSampleApp/SkiOSSampleApp-Debug.xcconfig experimental/iOSSampleApp/SkiOSSampleApp-Release.xcconfig gpu/src/android/GrGLDefaultInterface_android.cpp gyp/common.gypi gyp_skia include/ports/SkHarfBuzzFont.h include/views/SkOSWindow_wxwidgets.h make.bat make.py src/opts/memset.arm.S src/opts/memset16_neon.S src/opts/memset32_neon.S src/opts/opts_check_arm.cpp src/ports/SkDebug_brew.cpp src/ports/SkMemory_brew.cpp src/ports/SkOSFile_brew.cpp src/ports/SkXMLParser_empty.cpp src/utils/ios/SkImageDecoder_iOS.mm src/utils/ios/SkOSFile_iOS.mm src/utils/ios/SkStream_NSData.mm tests/FillPathTest.cpp Review URL: http://codereview.appspot.com/4816058 git-svn-id: http://skia.googlecode.com/svn/trunk@1982 2bbb7eff-a529-9590-31e7-b0007b416f81
2011-07-28 14:26:00 +00:00
* Copyright 2006 The Android Open Source Project
*
Automatic update of all copyright notices to reflect new license terms. I have manually examined all of these diffs and restored a few files that seem to require manual adjustment. The following files still need to be modified manually, in a separate CL: android_sample/SampleApp/AndroidManifest.xml android_sample/SampleApp/res/layout/layout.xml android_sample/SampleApp/res/menu/sample.xml android_sample/SampleApp/res/values/strings.xml android_sample/SampleApp/src/com/skia/sampleapp/SampleApp.java android_sample/SampleApp/src/com/skia/sampleapp/SampleView.java experimental/CiCarbonSampleMain.c experimental/CocoaDebugger/main.m experimental/FileReaderApp/main.m experimental/SimpleCocoaApp/main.m experimental/iOSSampleApp/Shared/SkAlertPrompt.h experimental/iOSSampleApp/Shared/SkAlertPrompt.m experimental/iOSSampleApp/SkiOSSampleApp-Base.xcconfig experimental/iOSSampleApp/SkiOSSampleApp-Debug.xcconfig experimental/iOSSampleApp/SkiOSSampleApp-Release.xcconfig gpu/src/android/GrGLDefaultInterface_android.cpp gyp/common.gypi gyp_skia include/ports/SkHarfBuzzFont.h include/views/SkOSWindow_wxwidgets.h make.bat make.py src/opts/memset.arm.S src/opts/memset16_neon.S src/opts/memset32_neon.S src/opts/opts_check_arm.cpp src/ports/SkDebug_brew.cpp src/ports/SkMemory_brew.cpp src/ports/SkOSFile_brew.cpp src/ports/SkXMLParser_empty.cpp src/utils/ios/SkImageDecoder_iOS.mm src/utils/ios/SkOSFile_iOS.mm src/utils/ios/SkStream_NSData.mm tests/FillPathTest.cpp Review URL: http://codereview.appspot.com/4816058 git-svn-id: http://skia.googlecode.com/svn/trunk@1982 2bbb7eff-a529-9590-31e7-b0007b416f81
2011-07-28 14:26:00 +00:00
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkGradientShader_DEFINED
#define SkGradientShader_DEFINED
#include "include/core/SkRefCnt.h"
#include "include/core/SkShader.h"
#include "include/core/SkTileMode.h"
class SkColorSpace;
/** \class SkGradientShader
SkGradientShader hosts factories for creating subclasses of SkShader that
Reland "Improve degenerate 2pt conical gradient cases" This reverts commit 95af4726bf91669e51250ebd3baa2925b6975e8e. Reason for revert: I think this may not have been the reason the Android roll was failing. We've rolled, so it's a good time to try again. Original change's description: > Revert "Improve degenerate 2pt conical gradient cases" > > This reverts commit 879dab87ab78613458f9d36f843bc3c6ffdafc74. > > Reason for revert: Android roll failed. > https://sponge.corp.google.com/target?id=93bc6b8d-9b42-4805-b204-46ae62f1b005&target=x86+CtsGraphicsTestCases&searchFor=&show=FAILED&sortBy=STATUS > A test VectorDrawableTest.testVectorDrawableGradient fails. > > Original change's description: > > Improve degenerate 2pt conical gradient cases > > > > This was originally a reland of "Fix div-by-zero loophole in gradient factory func", c34dd6c5263490b94ef9af7a14dee1b4bc872b58, but: > > > > The change caused blink layout tests when encountering very small or zero radii. The original patch switched the order of checking if the radii are equal and if the start radius was 0. In the case where both radii are 0, the original code created an actual radial gradient of radius 0 and the new code rejected the shader. A radial gradient with radius of 0 properly renders the last border color as a fill. > > > > This made me realize that the case when the center positions and the radii are the same can be handled more correctly than just always returning an empty shader, so the fix now applies simplifications to the gradient definition depending on the tile mode and should not trigger any blink tests. I added a row to the gradient edge cases GM to make sure it degrades gracefully. > > > > Original change's description: > > > Fix div-by-zero loophole in gradient factory func > > > > > > Bug: oss-fuzz:10373 > > > Change-Id: I4277fb63e3186ee34feaf09ecf6aeddeb532f9c1 > > > Reviewed-on: https://skia-review.googlesource.com/c/168269 > > > Reviewed-by: Kevin Lubick <kjlubick@google.com> > > > Commit-Queue: Michael Ludwig <michaelludwig@google.com> > > > > Docs-Preview: https://skia.org/?cl=168487 > > Bug: oss-fuzz:10373 > > Change-Id: Ib0a6e7f807560a5dcf24d1c8e0146817af2d9606 > > Reviewed-on: https://skia-review.googlesource.com/c/168487 > > Reviewed-by: Mike Reed <reed@google.com> > > Reviewed-by: Florin Malita <fmalita@chromium.org> > > Commit-Queue: Michael Ludwig <michaelludwig@google.com> > > TBR=caryclark@google.com,fmalita@chromium.org,fmalita@google.com,reed@google.com,michaelludwig@google.com > > Change-Id: I91b896c4a438c02206679b327a01b47f40993965 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: oss-fuzz:10373 > Reviewed-on: https://skia-review.googlesource.com/c/170272 > Reviewed-by: Stan Iliev <stani@google.com> > Commit-Queue: Stan Iliev <stani@google.com> TBR=caryclark@google.com,fmalita@chromium.org,fmalita@google.com,reed@google.com,stani@google.com,michaelludwig@google.com # Not skipping CQ checks because original CL landed > 1 day ago. Bug: oss-fuzz:10373 Change-Id: I7577fcea9eb8a875e94723ab2cca2fcc990b82b2 Reviewed-on: https://skia-review.googlesource.com/c/170279 Reviewed-by: Mike Klein <mtklein@google.com> Commit-Queue: Mike Klein <mtklein@google.com>
2018-11-11 00:26:30 +00:00
render linear and radial gradients. In general, degenerate cases should not
produce surprising results, but there are several types of degeneracies:
* A linear gradient made from the same two points.
* A radial gradient with a radius of zero.
* A sweep gradient where the start and end angle are the same.
* A two point conical gradient where the two centers and the two radii are
the same.
For any degenerate gradient with a decal tile mode, it will draw empty since the interpolating
region is zero area and the outer region is discarded by the decal mode.
For any degenerate gradient with a repeat or mirror tile mode, it will draw a solid color that
is the average gradient color, since infinitely many repetitions of the gradients will fill the
shape.
For a clamped gradient, every type is well-defined at the limit except for linear gradients. The
radial gradient with zero radius becomes the last color. The sweep gradient draws the sector
from 0 to the provided angle with the first color, with a hardstop switching to the last color.
When the provided angle is 0, this is just the solid last color again. Similarly, the two point
conical gradient becomes a circle filled with the first color, sized to the provided radius,
with a hardstop switching to the last color. When the two radii are both zero, this is just the
solid last color.
As a linear gradient approaches the degenerate case, its shader will approach the appearance of
two half planes, each filled by the first and last colors of the gradient. The planes will be
oriented perpendicular to the vector between the two defining points of the gradient. However,
once they become the same point, Skia cannot reconstruct what that expected orientation is. To
provide a stable and predictable color in this case, Skia just uses the last color as a solid
fill to be similar to many of the other degenerate gradients' behaviors in clamp mode.
*/
class SK_API SkGradientShader {
public:
enum Flags {
/** By default gradients will interpolate their colors in unpremul space
* and then premultiply each of the results. By setting this flag, the
* gradients will premultiply their colors first, and then interpolate
* between them.
* example: https://fiddle.skia.org/c/@GradientShader_MakeLinear
*/
kInterpolateColorsInPremul_Flag = 1 << 0,
};
/** Returns a shader that generates a linear gradient between the two specified points.
<p />
@param pts The start and end points for the gradient.
@param colors The array[count] of colors, to be distributed between the two points
@param pos May be NULL. array[count] of SkScalars, or NULL, of the relative position of
each corresponding color in the colors array. If this is NULL,
the the colors are distributed evenly between the start and end point.
If this is not null, the values must begin with 0, end with 1.0, and
intermediate values must be strictly increasing.
@param count Must be >=2. The number of colors (and pos if not NULL) entries.
@param mode The tiling mode
example: https://fiddle.skia.org/c/@GradientShader_MakeLinear
*/
static sk_sp<SkShader> MakeLinear(const SkPoint pts[2],
const SkColor colors[], const SkScalar pos[], int count,
SkTileMode mode,
uint32_t flags, const SkMatrix* localMatrix);
static sk_sp<SkShader> MakeLinear(const SkPoint pts[2],
const SkColor colors[], const SkScalar pos[], int count,
SkTileMode mode) {
return MakeLinear(pts, colors, pos, count, mode, 0, nullptr);
}
/** Returns a shader that generates a linear gradient between the two specified points.
<p />
@param pts The start and end points for the gradient.
@param colors The array[count] of colors, to be distributed between the two points
@param pos May be NULL. array[count] of SkScalars, or NULL, of the relative position of
each corresponding color in the colors array. If this is NULL,
the the colors are distributed evenly between the start and end point.
If this is not null, the values must begin with 0, end with 1.0, and
intermediate values must be strictly increasing.
@param count Must be >=2. The number of colors (and pos if not NULL) entries.
@param mode The tiling mode
example: https://fiddle.skia.org/c/@GradientShader_MakeLinear
*/
static sk_sp<SkShader> MakeLinear(const SkPoint pts[2],
const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
const SkScalar pos[], int count, SkTileMode mode,
uint32_t flags, const SkMatrix* localMatrix);
static sk_sp<SkShader> MakeLinear(const SkPoint pts[2],
const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
const SkScalar pos[], int count, SkTileMode mode);
/** Returns a shader that generates a radial gradient given the center and radius.
<p />
@param center The center of the circle for this gradient
@param radius Must be positive. The radius of the circle for this gradient
@param colors The array[count] of colors, to be distributed between the center and edge of the circle
@param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
each corresponding color in the colors array. If this is NULL,
the the colors are distributed evenly between the center and edge of the circle.
If this is not null, the values must begin with 0, end with 1.0, and
intermediate values must be strictly increasing.
@param count Must be >= 2. The number of colors (and pos if not NULL) entries
@param mode The tiling mode
*/
static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius,
const SkColor colors[], const SkScalar pos[], int count,
SkTileMode mode,
uint32_t flags, const SkMatrix* localMatrix);
static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius,
const SkColor colors[], const SkScalar pos[], int count,
SkTileMode mode) {
return MakeRadial(center, radius, colors, pos, count, mode, 0, nullptr);
}
/** Returns a shader that generates a radial gradient given the center and radius.
<p />
@param center The center of the circle for this gradient
@param radius Must be positive. The radius of the circle for this gradient
@param colors The array[count] of colors, to be distributed between the center and edge of the circle
@param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative position of
each corresponding color in the colors array. If this is NULL,
the the colors are distributed evenly between the center and edge of the circle.
If this is not null, the values must begin with 0, end with 1.0, and
intermediate values must be strictly increasing.
@param count Must be >= 2. The number of colors (and pos if not NULL) entries
@param mode The tiling mode
*/
static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius,
const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
const SkScalar pos[], int count, SkTileMode mode,
uint32_t flags, const SkMatrix* localMatrix);
static sk_sp<SkShader> MakeRadial(const SkPoint& center, SkScalar radius,
const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
const SkScalar pos[], int count, SkTileMode mode);
/**
* Returns a shader that generates a conical gradient given two circles, or
* returns NULL if the inputs are invalid. The gradient interprets the
* two circles according to the following HTML spec.
* http://dev.w3.org/html5/2dcontext/#dom-context-2d-createradialgradient
*/
static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius,
const SkPoint& end, SkScalar endRadius,
const SkColor colors[], const SkScalar pos[],
int count, SkTileMode mode,
uint32_t flags, const SkMatrix* localMatrix);
static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius,
const SkPoint& end, SkScalar endRadius,
const SkColor colors[], const SkScalar pos[],
int count, SkTileMode mode) {
return MakeTwoPointConical(start, startRadius, end, endRadius, colors, pos, count, mode,
0, nullptr);
}
/**
* Returns a shader that generates a conical gradient given two circles, or
* returns NULL if the inputs are invalid. The gradient interprets the
* two circles according to the following HTML spec.
* http://dev.w3.org/html5/2dcontext/#dom-context-2d-createradialgradient
*/
static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius,
const SkPoint& end, SkScalar endRadius,
const SkColor4f colors[],
sk_sp<SkColorSpace> colorSpace, const SkScalar pos[],
int count, SkTileMode mode,
uint32_t flags, const SkMatrix* localMatrix);
static sk_sp<SkShader> MakeTwoPointConical(const SkPoint& start, SkScalar startRadius,
const SkPoint& end, SkScalar endRadius,
const SkColor4f colors[],
sk_sp<SkColorSpace> colorSpace, const SkScalar pos[],
int count, SkTileMode mode);
/** Returns a shader that generates a sweep gradient given a center.
<p />
@param cx The X coordinate of the center of the sweep
@param cx The Y coordinate of the center of the sweep
@param colors The array[count] of colors, to be distributed around the center, within
the gradient angle range.
@param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative
position of each corresponding color in the colors array. If this is
NULL, then the colors are distributed evenly within the angular range.
If this is not null, the values must begin with 0, end with 1.0, and
intermediate values must be strictly increasing.
@param count Must be >= 2. The number of colors (and pos if not NULL) entries
@param mode Tiling mode: controls drawing outside of the gradient angular range.
@param startAngle Start of the angular range, corresponding to pos == 0.
@param endAngle End of the angular range, corresponding to pos == 1.
*/
static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy,
const SkColor colors[], const SkScalar pos[], int count,
SkTileMode mode,
SkScalar startAngle, SkScalar endAngle,
uint32_t flags, const SkMatrix* localMatrix);
static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy,
const SkColor colors[], const SkScalar pos[], int count,
uint32_t flags, const SkMatrix* localMatrix) {
return MakeSweep(cx, cy, colors, pos, count, SkTileMode::kClamp, 0, 360, flags,
localMatrix);
}
static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy,
const SkColor colors[], const SkScalar pos[], int count) {
return MakeSweep(cx, cy, colors, pos, count, 0, nullptr);
}
/** Returns a shader that generates a sweep gradient given a center.
<p />
@param cx The X coordinate of the center of the sweep
@param cx The Y coordinate of the center of the sweep
@param colors The array[count] of colors, to be distributed around the center, within
the gradient angle range.
@param pos May be NULL. The array[count] of SkScalars, or NULL, of the relative
position of each corresponding color in the colors array. If this is
NULL, then the colors are distributed evenly within the angular range.
If this is not null, the values must begin with 0, end with 1.0, and
intermediate values must be strictly increasing.
@param count Must be >= 2. The number of colors (and pos if not NULL) entries
@param mode Tiling mode: controls drawing outside of the gradient angular range.
@param startAngle Start of the angular range, corresponding to pos == 0.
@param endAngle End of the angular range, corresponding to pos == 1.
*/
static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy,
const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
const SkScalar pos[], int count,
SkTileMode mode,
SkScalar startAngle, SkScalar endAngle,
uint32_t flags, const SkMatrix* localMatrix);
static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy,
const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
const SkScalar pos[], int count,
uint32_t flags, const SkMatrix* localMatrix);
static sk_sp<SkShader> MakeSweep(SkScalar cx, SkScalar cy,
const SkColor4f colors[], sk_sp<SkColorSpace> colorSpace,
const SkScalar pos[], int count);
static void RegisterFlattenables();
};
#endif