Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Namespaces | Macros | Functions
SkShaderBase.h File Reference
#include "include/core/SkColor.h"
#include "include/core/SkFlattenable.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkShader.h"
#include "include/core/SkSurfaceProps.h"
#include "include/core/SkTypes.h"
#include "include/private/base/SkNoncopyable.h"
#include <cstddef>
#include <cstdint>
#include <optional>
#include <tuple>

Go to the source code of this file.

Classes

class  SkShaders::MatrixRec
 
class  SkShaderBase
 
struct  SkShaderBase::GradientInfo
 
struct  SkShaderBase::ContextRec
 
class  SkShaderBase::Context
 

Namespaces

namespace  SkShaders
 

Macros

#define SK_ALL_SHADERS(M)
 
#define SK_ALL_GRADIENTS(M)
 
#define M(type)   k##type,
 
#define M(type)   k##type,
 

Functions

SkShaderBaseas_SB (SkShader *shader)
 
const SkShaderBaseas_SB (const SkShader *shader)
 
const SkShaderBaseas_SB (const sk_sp< SkShader > &shader)
 
void SkRegisterBlendShaderFlattenable ()
 
void SkRegisterColor4ShaderFlattenable ()
 
void SkRegisterColorShaderFlattenable ()
 
void SkRegisterCoordClampShaderFlattenable ()
 
void SkRegisterEmptyShaderFlattenable ()
 
void SkRegisterPerlinNoiseShaderFlattenable ()
 
void SkRegisterWorkingColorSpaceShaderFlattenable ()
 

Macro Definition Documentation

◆ M [1/2]

#define M (   type)    k##type,

Definition at line 200 of file SkShaderBase.h.

◆ M [2/2]

#define M (   type)    k##type,

Definition at line 200 of file SkShaderBase.h.

◆ SK_ALL_GRADIENTS

#define SK_ALL_GRADIENTS (   M)
Value:
M(Conical) \
M(Linear) \
M(Radial) \
M(Sweep)
#define M(type)

Definition at line 180 of file SkShaderBase.h.

185 : public SkShader {
186public:
187 ~SkShaderBase() override;
188
189 sk_sp<SkShader> makeInvertAlpha() const;
190 sk_sp<SkShader> makeWithCTM(const SkMatrix&) const; // owns its own ctm
191
192 /**
193 * Returns true if the shader is guaranteed to produce only a single color.
194 * Subclasses can override this to allow loop-hoisting optimization.
195 */
196 virtual bool isConstant() const { return false; }
197
198 enum class ShaderType {
199#define M(type) k##type,
201#undef M
202 };
203
204 virtual ShaderType type() const = 0;
205
206 enum class GradientType {
207 kNone,
208#define M(type) k##type,
210#undef M
211 };
212
213 /**
214 * If the shader subclass can be represented as a gradient, asGradient
215 * returns the matching GradientType enum (or GradientType::kNone if it
216 * cannot). Also, if info is not null, asGradient populates info with
217 * the relevant (see below) parameters for the gradient. fColorCount
218 * is both an input and output parameter. On input, it indicates how
219 * many entries in fColors and fColorOffsets can be used, if they are
220 * non-NULL. After asGradient has run, fColorCount indicates how
221 * many color-offset pairs there are in the gradient. If there is
222 * insufficient space to store all of the color-offset pairs, fColors
223 * and fColorOffsets will not be altered. fColorOffsets specifies
224 * where on the range of 0 to 1 to transition to the given color.
225 * The meaning of fPoint and fRadius is dependent on the type of gradient.
226 *
227 * None:
228 * info is ignored.
229 * Color:
230 * fColorOffsets[0] is meaningless.
231 * Linear:
232 * fPoint[0] and fPoint[1] are the end-points of the gradient
233 * Radial:
234 * fPoint[0] and fRadius[0] are the center and radius
235 * Conical:
236 * fPoint[0] and fRadius[0] are the center and radius of the 1st circle
237 * fPoint[1] and fRadius[1] are the center and radius of the 2nd circle
238 * Sweep:
239 * fPoint[0] is the center of the sweep.
240 */
241 struct GradientInfo {
242 int fColorCount = 0; //!< In-out parameter, specifies passed size
243 // of fColors/fColorOffsets on input, and
244 // actual number of colors/offsets on
245 // output.
246 SkColor* fColors = nullptr; //!< The colors in the gradient.
247 SkScalar* fColorOffsets = nullptr; //!< The unit offset for color transitions.
248 SkPoint fPoint[2]; //!< Type specific, see above.
249 SkScalar fRadius[2]; //!< Type specific, see above.
250 SkTileMode fTileMode;
251 uint32_t fGradientFlags = 0; //!< see SkGradientShader::Flags
252 };
253
254 virtual GradientType asGradient(GradientInfo* info = nullptr,
255 SkMatrix* localMatrix = nullptr) const {
256 return GradientType::kNone;
257 }
258
259 enum Flags {
260 //!< set if all of the colors will be opaque
261 kOpaqueAlpha_Flag = 1 << 0,
262 };
263
264 /**
265 * ContextRec acts as a parameter bundle for creating Contexts.
266 */
267 struct ContextRec {
268 ContextRec(SkAlpha paintAlpha,
269 const SkShaders::MatrixRec& matrixRec,
270 SkColorType dstColorType,
271 SkColorSpace* dstColorSpace,
272 const SkSurfaceProps& props)
273 : fMatrixRec(matrixRec)
274 , fDstColorType(dstColorType)
275 , fDstColorSpace(dstColorSpace)
276 , fProps(props)
277 , fPaintAlpha(paintAlpha) {}
278
279 static ContextRec Concat(const ContextRec& parentRec, const SkMatrix& localM) {
280 return {parentRec.fPaintAlpha,
281 parentRec.fMatrixRec.concat(localM),
282 parentRec.fDstColorType,
283 parentRec.fDstColorSpace,
284 parentRec.fProps};
285 }
286
287 const SkShaders::MatrixRec fMatrixRec;
288 SkColorType fDstColorType; // the color type of the dest surface
289 SkColorSpace* fDstColorSpace; // the color space of the dest surface (if any)
290 SkSurfaceProps fProps; // props of the dest surface
291 SkAlpha fPaintAlpha;
292
293 bool isLegacyCompatible(SkColorSpace* shadersColorSpace) const;
294 };
295
296 class Context : public ::SkNoncopyable {
297 public:
298 Context(const SkShaderBase& shader, const ContextRec&);
299
300 virtual ~Context();
301
302 /**
303 * Called sometimes before drawing with this shader. Return the type of
304 * alpha your shader will return. The default implementation returns 0.
305 * Your subclass should override if it can (even sometimes) report a
306 * non-zero value, since that will enable various blitters to perform
307 * faster.
308 */
309 virtual uint32_t getFlags() const { return 0; }
310
311 /**
312 * Called for each span of the object being drawn. Your subclass should
313 * set the appropriate colors (with premultiplied alpha) that correspond
314 * to the specified device coordinates.
315 */
316 virtual void shadeSpan(int x, int y, SkPMColor[], int count) = 0;
317
318 protected:
319 // Reference to shader, so we don't have to dupe information.
320 const SkShaderBase& fShader;
321
322 uint8_t getPaintAlpha() const { return fPaintAlpha; }
323 const SkMatrix& getTotalInverse() const { return fTotalInverse; }
324
325 private:
326 SkMatrix fTotalInverse;
327 uint8_t fPaintAlpha;
328 };
329
330 /**
331 * Make a context using the memory provided by the arena.
332 *
333 * @return pointer to context or nullptr if can't be created
334 */
335 Context* makeContext(const ContextRec&, SkArenaAlloc*) const;
336
337 /**
338 * If the shader can represent its "average" luminance in a single color, return true and
339 * if color is not NULL, return that color. If it cannot, return false and ignore the color
340 * parameter.
341 *
342 * Note: if this returns true, the returned color will always be opaque, as only the RGB
343 * components are used to compute luminance.
344 */
345 bool asLuminanceColor(SkColor4f*) const;
346
347 /**
348 * If this returns false, then we draw nothing (do not fall back to shader context). This should
349 * only be called on a root-level effect. It assumes that the initial device coordinates have
350 * not yet been seeded.
351 */
352 [[nodiscard]] bool appendRootStages(const SkStageRec& rec, const SkMatrix& ctm) const;
353
354 /**
355 * Adds stages to implement this shader. To ensure that the correct input coords are present
356 * in r,g MatrixRec::apply() must be called (unless the shader doesn't require it's input
357 * coords). The default impl creates shadercontext and calls that (not very efficient).
358 */
359 virtual bool appendStages(const SkStageRec&, const SkShaders::MatrixRec&) const = 0;
360
361 virtual SkImage* onIsAImage(SkMatrix*, SkTileMode[2]) const {
362 return nullptr;
363 }
364
365 virtual SkRuntimeEffect* asRuntimeEffect() const { return nullptr; }
366
367 static Type GetFlattenableType() { return kSkShader_Type; }
368 Type getFlattenableType() const override { return GetFlattenableType(); }
369
370 static sk_sp<SkShaderBase> Deserialize(const void* data, size_t size,
371 const SkDeserialProcs* procs = nullptr) {
372 return sk_sp<SkShaderBase>(static_cast<SkShaderBase*>(
373 SkFlattenable::Deserialize(GetFlattenableType(), data, size, procs).release()));
374 }
375 static void RegisterFlattenables();
376
377 /** DEPRECATED. skbug.com/8941
378 * If this shader can be represented by another shader + a localMatrix, return that shader and
379 * the localMatrix. If not, return nullptr and ignore the localMatrix parameter.
380 */
381 virtual sk_sp<SkShader> makeAsALocalMatrixShader(SkMatrix* localMatrix) const;
382
383 static SkMatrix ConcatLocalMatrices(const SkMatrix& parentLM, const SkMatrix& childLM) {
384#if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) // b/256873449
385 return SkMatrix::Concat(childLM, parentLM);
386#endif
387 return SkMatrix::Concat(parentLM, childLM);
388 }
389
390protected:
391 SkShaderBase();
392
393 void flatten(SkWriteBuffer&) const override;
394
395#ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
396 /**
397 * Specialize creating a SkShader context using the supplied allocator.
398 * @return pointer to context owned by the arena allocator.
399 */
400 virtual Context* onMakeContext(const ContextRec&, SkArenaAlloc*) const {
401 return nullptr;
402 }
403#endif
404
405 virtual bool onAsLuminanceColor(SkColor4f*) const {
406 return false;
407 }
408
409 friend class SkShaders::MatrixRec;
410};
411inline SkShaderBase* as_SB(SkShader* shader) {
412 return static_cast<SkShaderBase*>(shader);
413}
414
415inline const SkShaderBase* as_SB(const SkShader* shader) {
416 return static_cast<const SkShaderBase*>(shader);
417}
418
419inline const SkShaderBase* as_SB(const sk_sp<SkShader>& shader) {
420 return static_cast<SkShaderBase*>(shader.get());
421}
422
430
431#endif // SkShaderBase_DEFINED
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
int count
SkColorType
Definition SkColorType.h:19
uint32_t SkColor
Definition SkColor.h:37
uint8_t SkAlpha
Definition SkColor.h:26
uint32_t SkPMColor
Definition SkColor.h:205
SkShaderBase * as_SB(SkShader *shader)
void SkRegisterEmptyShaderFlattenable()
void SkRegisterWorkingColorSpaceShaderFlattenable()
void SkRegisterColor4ShaderFlattenable()
#define SK_ALL_GRADIENTS(M)
void SkRegisterColorShaderFlattenable()
void SkRegisterPerlinNoiseShaderFlattenable()
void SkRegisterBlendShaderFlattenable()
void SkRegisterCoordClampShaderFlattenable()
#define SK_ALL_SHADERS(M)
SkTileMode
Definition SkTileMode.h:13
int getFlags(int index)
Definition bridge.cpp:68
static sk_sp< SkFlattenable > Deserialize(Type, const void *data, size_t length, const SkDeserialProcs *procs=nullptr)
static SkMatrix Concat(const SkMatrix &a, const SkMatrix &b)
Definition SkMatrix.h:1775
T * get() const
Definition SkRefCnt.h:303
float SkScalar
Definition extension.cpp:12
double y
double x

◆ SK_ALL_SHADERS

#define SK_ALL_SHADERS (   M)
Value:
M(Blend) \
M(CTM) \
M(Color) \
M(Color4) \
M(ColorFilter) \
M(CoordClamp) \
M(Empty) \
M(GradientBase) \
M(Image) \
M(LocalMatrix) \
M(PerlinNoise) \
M(Picture) \
M(Runtime) \
M(Transform) \
M(TriColor) \
M(WorkingColorSpace)

Definition at line 162 of file SkShaderBase.h.

Function Documentation

◆ as_SB() [1/3]

const SkShaderBase * as_SB ( const sk_sp< SkShader > &  shader)
inline

Definition at line 420 of file SkShaderBase.h.

420 {
421 return static_cast<SkShaderBase*>(shader.get());
422}

◆ as_SB() [2/3]

const SkShaderBase * as_SB ( const SkShader shader)
inline

Definition at line 416 of file SkShaderBase.h.

416 {
417 return static_cast<const SkShaderBase*>(shader);
418}

◆ as_SB() [3/3]

SkShaderBase * as_SB ( SkShader shader)
inline

Definition at line 412 of file SkShaderBase.h.

412 {
413 return static_cast<SkShaderBase*>(shader);
414}

◆ SkRegisterBlendShaderFlattenable()

void SkRegisterBlendShaderFlattenable ( )

Definition at line 139 of file SkBlendShader.cpp.

139 {
141 // Previous name
142 SkFlattenable::Register("SkShader_Blend", SkBlendShader::CreateProc);
143}
#define SK_REGISTER_FLATTENABLE(type)
static void Register(const char name[], Factory)

◆ SkRegisterColor4ShaderFlattenable()

void SkRegisterColor4ShaderFlattenable ( )

Definition at line 94 of file SkColorShader.cpp.

◆ SkRegisterColorShaderFlattenable()

void SkRegisterColorShaderFlattenable ( )

Definition at line 98 of file SkColorShader.cpp.

◆ SkRegisterCoordClampShaderFlattenable()

void SkRegisterCoordClampShaderFlattenable ( )

Definition at line 52 of file SkCoordClampShader.cpp.

52 {
54
55 // Previous name
56 SkFlattenable::Register("SkShader_CoordClamp", SkCoordClampShader::CreateProc);
57}

◆ SkRegisterEmptyShaderFlattenable()

void SkRegisterEmptyShaderFlattenable ( )

Definition at line 22 of file SkEmptyShader.cpp.

◆ SkRegisterPerlinNoiseShaderFlattenable()

void SkRegisterPerlinNoiseShaderFlattenable ( )

Definition at line 277 of file SkPerlinNoiseShaderImpl.cpp.

128 {
130 // Previous name
131 SkFlattenable::Register("SkPerlinNoiseShaderImpl", SkPerlinNoiseShader::CreateProc);
132}

◆ SkRegisterWorkingColorSpaceShaderFlattenable()

void SkRegisterWorkingColorSpaceShaderFlattenable ( )