Skia
2D Graphics Library
SkPaint.h
Go to the documentation of this file.
1 /*
2  * Copyright 2006 The Android Open Source Project
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 #ifndef SkPaint_DEFINED
9 #define SkPaint_DEFINED
10 
11 #include "include/core/SkColor.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkScalar.h"
14 #include "include/core/SkTypes.h"
15 #include "include/private/base/SkCPUTypes.h"
16 #include "include/private/base/SkFloatingPoint.h"
17 #include "include/private/base/SkTo.h"
18 #include "include/private/base/SkTypeTraits.h"
19 
20 #include <cstdint>
21 #include <optional>
22 #include <type_traits>
23 
24 class SkBlender;
25 class SkColorFilter;
26 class SkColorSpace;
27 class SkImageFilter;
28 class SkMaskFilter;
29 class SkPathEffect;
30 class SkShader;
31 enum class SkBlendMode;
32 struct SkRect;
33 
44 class SK_API SkPaint {
45 public:
46 
54 
66  explicit SkPaint(const SkColor4f& color, SkColorSpace* colorSpace = nullptr);
67 
82  SkPaint(const SkPaint& paint);
83 
94  SkPaint(SkPaint&& paint);
95 
101 
114  SkPaint& operator=(const SkPaint& paint);
115 
129 
138  SK_API friend bool operator==(const SkPaint& a, const SkPaint& b);
139 
148  friend bool operator!=(const SkPaint& a, const SkPaint& b) {
149  return !(a == b);
150  }
151 
157  void reset();
158 
162  bool isAntiAlias() const {
163  return SkToBool(fBitfields.fAntiAlias);
164  }
165 
170  void setAntiAlias(bool aa) { fBitfields.fAntiAlias = static_cast<unsigned>(aa); }
171 
175  bool isDither() const {
176  return SkToBool(fBitfields.fDither);
177  }
178 
182  void setDither(bool dither) { fBitfields.fDither = static_cast<unsigned>(dither); }
183 
192  enum Style : uint8_t {
196  };
197 
200  static constexpr int kStyleCount = kStrokeAndFill_Style + 1;
201 
204  Style getStyle() const { return (Style)fBitfields.fStyle; }
205 
212  void setStyle(Style style);
213 
217  void setStroke(bool);
218 
225  SkColor getColor() const { return fColor4f.toSkColor(); }
226 
232  SkColor4f getColor4f() const { return fColor4f; }
233 
241  void setColor(SkColor color);
242 
251  void setColor(const SkColor4f& color, SkColorSpace* colorSpace = nullptr);
252 
253  void setColor4f(const SkColor4f& color, SkColorSpace* colorSpace = nullptr) {
254  this->setColor(color, colorSpace);
255  }
256 
261  float getAlphaf() const { return fColor4f.fA; }
262 
263  // Helper that scales the alpha by 255.
264  uint8_t getAlpha() const {
265  return static_cast<uint8_t>(sk_float_round2int(this->getAlphaf() * 255));
266  }
267 
276  void setAlphaf(float a);
277 
278  // Helper that accepts an int between 0 and 255, and divides it by 255.0
279  void setAlpha(U8CPU a) {
280  this->setAlphaf(a * (1.0f / 255));
281  }
282 
293  void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
294 
300  SkScalar getStrokeWidth() const { return fWidth; }
301 
313 
318  SkScalar getStrokeMiter() const { return fMiterLimit; }
319 
329 
333  enum Cap {
337  kLast_Cap = kSquare_Cap,
338  kDefault_Cap = kButt_Cap,
339  };
340 
343  static constexpr int kCapCount = kLast_Cap + 1;
344 
358  enum Join : uint8_t {
362  kLast_Join = kBevel_Join,
363  kDefault_Join = kMiter_Join,
364  };
365 
368  static constexpr int kJoinCount = kLast_Join + 1;
369 
372  Cap getStrokeCap() const { return (Cap)fBitfields.fCapType; }
373 
379  void setStrokeCap(Cap cap);
380 
383  Join getStrokeJoin() const { return (Join)fBitfields.fJoinType; }
384 
389  void setStrokeJoin(Join join);
390 
397  SkShader* getShader() const { return fShader.get(); }
398 
408 
420 
426  SkColorFilter* getColorFilter() const { return fColorFilter.get(); }
427 
436 
448 
453  std::optional<SkBlendMode> asBlendMode() const;
454 
460 
465  bool isSrcOver() const;
466 
472 
480  SkBlender* getBlender() const { return fBlender.get(); }
481 
490 
500 
506  SkPathEffect* getPathEffect() const { return fPathEffect.get(); }
507 
516 
528 
534  SkMaskFilter* getMaskFilter() const { return fMaskFilter.get(); }
535 
545 
558 
564  SkImageFilter* getImageFilter() const { return fImageFilter.get(); }
565 
574 
586 
597  bool nothingToDraw() const;
598 
606  bool canComputeFastBounds() const;
607 
632  const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const;
633 
641  SkRect* storage) const {
642  return this->doComputeFastBounds(orig, storage, kStroke_Style);
643  }
644 
655  const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage,
656  Style style) const;
657 
658  using sk_is_trivially_relocatable = std::true_type;
659 
660 private:
661  sk_sp<SkPathEffect> fPathEffect;
662  sk_sp<SkShader> fShader;
663  sk_sp<SkMaskFilter> fMaskFilter;
664  sk_sp<SkColorFilter> fColorFilter;
665  sk_sp<SkImageFilter> fImageFilter;
666  sk_sp<SkBlender> fBlender;
667 
668  SkColor4f fColor4f;
669  SkScalar fWidth;
670  SkScalar fMiterLimit;
671  union {
672  struct {
673  unsigned fAntiAlias : 1;
674  unsigned fDither : 1;
675  unsigned fCapType : 2;
676  unsigned fJoinType : 2;
677  unsigned fStyle : 2;
678  unsigned fPadding : 24; // 24 == 32 -1-1-2-2-2
679  } fBitfields;
680  uint32_t fBitfieldsUInt;
681  };
682 
683  static_assert(::sk_is_trivially_relocatable<decltype(fPathEffect)>::value);
684  static_assert(::sk_is_trivially_relocatable<decltype(fShader)>::value);
685  static_assert(::sk_is_trivially_relocatable<decltype(fMaskFilter)>::value);
686  static_assert(::sk_is_trivially_relocatable<decltype(fColorFilter)>::value);
687  static_assert(::sk_is_trivially_relocatable<decltype(fImageFilter)>::value);
688  static_assert(::sk_is_trivially_relocatable<decltype(fBlender)>::value);
689  static_assert(::sk_is_trivially_relocatable<decltype(fColor4f)>::value);
690  static_assert(::sk_is_trivially_relocatable<decltype(fBitfields)>::value);
691 
692  friend class SkPaintPriv;
693 };
694 
695 #endif
SkBlendMode
Blends are operators that take in two colors (source, destination) and return a new color.
Definition: SkBlendMode.h:38
Types, consts, functions, and macros for colors.
uint32_t SkColor
32-bit ARGB color value, unpremultiplied.
Definition: SkColor.h:37
float SkScalar
Definition: SkScalar.h:14
SkBlender represents a custom blend function in the Skia pipeline.
Definition: SkBlender.h:19
ColorFilters are optional objects in the drawing pipeline.
Definition: SkColorFilter.h:35
Definition: SkColorSpace.h:107
Base class for image filters.
Definition: SkImageFilter.h:35
SkMaskFilter is the base class for object that perform transformations on the mask before drawing it.
Definition: SkMaskFilter.h:27
SkPaint controls options applied when drawing.
Definition: SkPaint.h:44
SkPaint & operator=(const SkPaint &paint)
Makes a shallow copy of SkPaint.
SkScalar getStrokeMiter() const
Returns the limit at which a sharp corner is drawn beveled.
Definition: SkPaint.h:318
SK_API friend bool operator==(const SkPaint &a, const SkPaint &b)
Compares a and b, and returns true if a and b are equivalent.
Cap
Definition: SkPaint.h:333
@ kRound_Cap
adds circle
Definition: SkPaint.h:335
@ kButt_Cap
no stroke extension
Definition: SkPaint.h:334
@ kSquare_Cap
adds square
Definition: SkPaint.h:336
SkPaint(const SkColor4f &color, SkColorSpace *colorSpace=nullptr)
Constructs SkPaint with default values and the given color.
void setStyle(Style style)
Sets whether the geometry is filled, stroked, or filled and stroked.
Style getStyle() const
Returns whether the geometry is filled, stroked, or filled and stroked.
Definition: SkPaint.h:204
friend bool operator!=(const SkPaint &a, const SkPaint &b)
Compares a and b, and returns true if a and b are not equivalent.
Definition: SkPaint.h:148
void setColor(SkColor color)
Sets alpha and RGB used when stroking and filling.
sk_sp< SkShader > refShader() const
Returns optional colors used when filling a path, such as a gradient.
void setStrokeMiter(SkScalar miter)
Sets the limit at which a sharp corner is drawn beveled.
void setAntiAlias(bool aa)
Requests, but does not require, that edge pixels draw opaque or with partial transparency.
Definition: SkPaint.h:170
std::true_type sk_is_trivially_relocatable
Definition: SkPaint.h:658
bool isSrcOver() const
Returns true iff the current blender claims to be equivalent to SkBlendMode::kSrcOver.
unsigned fPadding
Definition: SkPaint.h:678
void setDither(bool dither)
Requests, but does not require, to distribute color error.
Definition: SkPaint.h:182
void setImageFilter(sk_sp< SkImageFilter > imageFilter)
Sets SkImageFilter to imageFilter, decreasing SkRefCnt of the previous SkImageFilter.
sk_sp< SkPathEffect > refPathEffect() const
Returns SkPathEffect if set, or nullptr.
unsigned fDither
Definition: SkPaint.h:674
void setStrokeCap(Cap cap)
Sets the geometry drawn at the beginning and end of strokes.
SkShader * getShader() const
Returns optional colors used when filling a path, such as a gradient.
Definition: SkPaint.h:397
SkBlendMode getBlendMode_or(SkBlendMode defaultMode) const
Queries the blender, and if it can be represented as a SkBlendMode, return that mode,...
sk_sp< SkMaskFilter > refMaskFilter() const
Returns SkMaskFilter if set, or nullptr.
void reset()
Sets all SkPaint contents to their initial values.
SkColor getColor() const
Retrieves alpha and RGB, unpremultiplied, packed into 32 bits.
Definition: SkPaint.h:225
Style
Definition: SkPaint.h:192
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition: SkPaint.h:193
@ kStrokeAndFill_Style
sets to stroke and fill geometry
Definition: SkPaint.h:195
void setStrokeJoin(Join join)
Sets the geometry drawn at the corners of strokes.
bool isDither() const
Returns true if color error may be distributed to smooth color transition.
Definition: SkPaint.h:175
std::optional< SkBlendMode > asBlendMode() const
If the current blender can be represented as a SkBlendMode enum, this returns that enum in the option...
const SkRect & computeFastStrokeBounds(const SkRect &orig, SkRect *storage) const
(to be made private)
Definition: SkPaint.h:640
void setAlpha(U8CPU a)
Definition: SkPaint.h:279
void setColor(const SkColor4f &color, SkColorSpace *colorSpace=nullptr)
Sets alpha and RGB used when stroking and filling.
sk_sp< SkColorFilter > refColorFilter() const
Returns SkColorFilter if set, or nullptr.
SkPaint(const SkPaint &paint)
Makes a shallow copy of SkPaint.
void setColor4f(const SkColor4f &color, SkColorSpace *colorSpace=nullptr)
Definition: SkPaint.h:253
SkMaskFilter * getMaskFilter() const
Returns SkMaskFilter if set, or nullptr.
Definition: SkPaint.h:534
void setMaskFilter(sk_sp< SkMaskFilter > maskFilter)
Sets SkMaskFilter to maskFilter, decreasing SkRefCnt of the previous SkMaskFilter.
bool nothingToDraw() const
Returns true if SkPaint prevents all drawing; otherwise, the SkPaint may or may not allow drawing.
SkColor4f getColor4f() const
Retrieves alpha and RGB, unpremultiplied, as four floating point values.
Definition: SkPaint.h:232
unsigned fCapType
Definition: SkPaint.h:675
void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Sets color used when drawing solid fills.
bool isAntiAlias() const
Returns true if pixels on the active edges of SkPath may be drawn with partial transparency.
Definition: SkPaint.h:162
void setShader(sk_sp< SkShader > shader)
Sets optional colors used when filling a path, such as a gradient.
void setPathEffect(sk_sp< SkPathEffect > pathEffect)
Sets SkPathEffect to pathEffect, decreasing SkRefCnt of the previous SkPathEffect.
float getAlphaf() const
Retrieves alpha from the color used when stroking and filling.
Definition: SkPaint.h:261
unsigned fJoinType
Definition: SkPaint.h:676
SkPaint & operator=(SkPaint &&paint)
Moves the paint to avoid increasing the reference counts of objects referenced by the paint parameter...
unsigned fAntiAlias
Definition: SkPaint.h:673
SkScalar getStrokeWidth() const
Returns the thickness of the pen used by SkPaint to outline the shape.
Definition: SkPaint.h:300
SkImageFilter * getImageFilter() const
Returns SkImageFilter if set, or nullptr.
Definition: SkPaint.h:564
uint32_t fBitfieldsUInt
Definition: SkPaint.h:680
Join
Definition: SkPaint.h:358
@ kRound_Join
adds circle
Definition: SkPaint.h:360
@ kMiter_Join
extends to miter limit
Definition: SkPaint.h:359
@ kBevel_Join
connects outside edges
Definition: SkPaint.h:361
const SkRect & computeFastBounds(const SkRect &orig, SkRect *storage) const
(to be made private) Only call this if canComputeFastBounds() returned true.
void setBlendMode(SkBlendMode mode)
Helper method for calling setBlender().
const SkRect & doComputeFastBounds(const SkRect &orig, SkRect *storage, Style style) const
(to be made private) Computes the bounds, overriding the SkPaint SkPaint::Style.
SkPathEffect * getPathEffect() const
Returns SkPathEffect if set, or nullptr.
Definition: SkPaint.h:506
SkBlender * getBlender() const
Returns the user-supplied blend function, if one has been set.
Definition: SkPaint.h:480
SkPaint()
Constructs SkPaint with default values.
uint8_t getAlpha() const
Definition: SkPaint.h:264
Cap getStrokeCap() const
Returns the geometry drawn at the beginning and end of strokes.
Definition: SkPaint.h:372
void setColorFilter(sk_sp< SkColorFilter > colorFilter)
Sets SkColorFilter to filter, decreasing SkRefCnt of the previous SkColorFilter.
~SkPaint()
Decreases SkPaint SkRefCnt of owned objects: SkPathEffect, SkShader, SkMaskFilter,...
sk_sp< SkImageFilter > refImageFilter() const
Returns SkImageFilter if set, or nullptr.
SkColorFilter * getColorFilter() const
Returns SkColorFilter if set, or nullptr.
Definition: SkPaint.h:426
void setBlender(sk_sp< SkBlender > blender)
Sets the current blender, increasing its refcnt, and if a blender is already present,...
Join getStrokeJoin() const
Returns the geometry drawn at the corners of strokes.
Definition: SkPaint.h:383
unsigned fStyle
Definition: SkPaint.h:677
void setStroke(bool)
Set paint's style to kStroke if true, or kFill if false.
bool canComputeFastBounds() const
(to be made private) Returns true if SkPaint does not include elements requiring extensive computatio...
void setStrokeWidth(SkScalar width)
Sets the thickness of the pen used by the paint to outline the shape.
sk_sp< SkBlender > refBlender() const
Returns the user-supplied blend function, if one has been set.
void setAlphaf(float a)
Replaces alpha, leaving RGB unchanged.
SkPaint(SkPaint &&paint)
Implements a move constructor to avoid increasing the reference counts of objects referenced by the p...
SkPathEffect is the base class for objects in the SkPaint that affect the geometry of a drawing primi...
Definition: SkPathEffect.h:28
Shaders specify the source color(s) for what is being drawn.
Definition: SkShader.h:35
RGBA color value, holding four floating point components.
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582