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 
12 #include "include/core/SkColor.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/private/SkTo.h"
16 
17 class SkColorFilter;
18 class SkColorSpace;
19 struct SkRect;
20 class SkImageFilter;
21 class SkMaskFilter;
22 class SkPath;
23 class SkPathEffect;
24 class SkShader;
25 
26 // WIP to eventually remove filter-quality
27 #define SK_SUPPORT_LEGACY_SETFILTERQUALITY
28 
39 class SK_API SkPaint {
40 public:
41 
49 
61  explicit SkPaint(const SkColor4f& color, SkColorSpace* colorSpace = nullptr);
62 
77  SkPaint(const SkPaint& paint);
78 
89  SkPaint(SkPaint&& paint);
90 
96 
109  SkPaint& operator=(const SkPaint& paint);
110 
124 
133  SK_API friend bool operator==(const SkPaint& a, const SkPaint& b);
134 
143  friend bool operator!=(const SkPaint& a, const SkPaint& b) {
144  return !(a == b);
145  }
146 
161  uint32_t getHash() const;
162 
168  void reset();
169 
173  bool isAntiAlias() const {
174  return SkToBool(fBitfields.fAntiAlias);
175  }
176 
181  void setAntiAlias(bool aa) { fBitfields.fAntiAlias = static_cast<unsigned>(aa); }
182 
186  bool isDither() const {
187  return SkToBool(fBitfields.fDither);
188  }
189 
193  void setDither(bool dither) { fBitfields.fDither = static_cast<unsigned>(dither); }
194 
195 #ifndef SK_SUPPORT_LEGACY_SETFILTERQUALITY
196 private:
197 #endif
198 
202  return (SkFilterQuality)fBitfields.fFilterQuality;
203  }
204 
213 #ifndef SK_SUPPORT_LEGACY_SETFILTERQUALITY
214 public:
215 #endif
216 
225  enum Style : uint8_t {
229  };
230 
233  static constexpr int kStyleCount = kStrokeAndFill_Style + 1;
234 
237  Style getStyle() const { return (Style)fBitfields.fStyle; }
238 
245  void setStyle(Style style);
246 
250  void setStroke(bool);
251 
258  SkColor getColor() const { return fColor4f.toSkColor(); }
259 
265  SkColor4f getColor4f() const { return fColor4f; }
266 
274  void setColor(SkColor color);
275 
284  void setColor(const SkColor4f& color, SkColorSpace* colorSpace = nullptr);
285 
286  void setColor4f(const SkColor4f& color, SkColorSpace* colorSpace = nullptr) {
287  this->setColor(color, colorSpace);
288  }
289 
294  float getAlphaf() const { return fColor4f.fA; }
295 
296  // Helper that scales the alpha by 255.
297  uint8_t getAlpha() const { return sk_float_round2int(this->getAlphaf() * 255); }
298 
307  void setAlphaf(float a);
308 
309  // Helper that accepts an int between 0 and 255, and divides it by 255.0
310  void setAlpha(U8CPU a) {
311  this->setAlphaf(a * (1.0f / 255));
312  }
313 
324  void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
325 
331  SkScalar getStrokeWidth() const { return fWidth; }
332 
344 
349  SkScalar getStrokeMiter() const { return fMiterLimit; }
350 
360 
364  enum Cap {
368  kLast_Cap = kSquare_Cap,
369  kDefault_Cap = kButt_Cap,
370  };
371 
374  static constexpr int kCapCount = kLast_Cap + 1;
375 
389  enum Join : uint8_t {
393  kLast_Join = kBevel_Join,
394  kDefault_Join = kMiter_Join,
395  };
396 
399  static constexpr int kJoinCount = kLast_Join + 1;
400 
403  Cap getStrokeCap() const { return (Cap)fBitfields.fCapType; }
404 
410  void setStrokeCap(Cap cap);
411 
414  Join getStrokeJoin() const { return (Join)fBitfields.fJoinType; }
415 
420  void setStrokeJoin(Join join);
421 
431  bool getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect,
432  SkScalar resScale = 1) const;
433 
443  bool getFillPath(const SkPath& src, SkPath* dst) const {
444  return this->getFillPath(src, dst, nullptr, 1);
445  }
446 
453  SkShader* getShader() const { return fShader.get(); }
454 
464 
476 
482  SkColorFilter* getColorFilter() const { return fColorFilter.get(); }
483 
492 
504 
510  SkBlendMode getBlendMode() const { return (SkBlendMode)fBitfields.fBlendMode; }
511 
516  bool isSrcOver() const { return (SkBlendMode)fBitfields.fBlendMode == SkBlendMode::kSrcOver; }
517 
523  void setBlendMode(SkBlendMode mode) { fBitfields.fBlendMode = (unsigned)mode; }
524 
530  SkPathEffect* getPathEffect() const { return fPathEffect.get(); }
531 
540 
552 
558  SkMaskFilter* getMaskFilter() const { return fMaskFilter.get(); }
559 
569 
582 
588  SkImageFilter* getImageFilter() const { return fImageFilter.get(); }
589 
598 
610 
621  bool nothingToDraw() const;
622 
630  bool canComputeFastBounds() const;
631 
656  const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
657  // Things like stroking, etc... will do math on the bounds rect, assuming that it's sorted.
658  SkASSERT(orig.isSorted());
659  SkPaint::Style style = this->getStyle();
660  // ultra fast-case: filling with no effects that affect geometry
661  if (kFill_Style == style) {
662  uintptr_t effects = 0;
663  effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter());
664  effects |= reinterpret_cast<uintptr_t>(this->getPathEffect());
665  effects |= reinterpret_cast<uintptr_t>(this->getImageFilter());
666  if (!effects) {
667  return orig;
668  }
669  }
670 
671  return this->doComputeFastBounds(orig, storage, style);
672  }
673 
681  SkRect* storage) const {
682  return this->doComputeFastBounds(orig, storage, kStroke_Style);
683  }
684 
695  const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage,
696  Style style) const;
697 
698 private:
699  sk_sp<SkPathEffect> fPathEffect;
700  sk_sp<SkShader> fShader;
701  sk_sp<SkMaskFilter> fMaskFilter;
702  sk_sp<SkColorFilter> fColorFilter;
703  sk_sp<SkImageFilter> fImageFilter;
704 
705  SkColor4f fColor4f;
706  SkScalar fWidth;
707  SkScalar fMiterLimit;
708  union {
709  struct {
710  unsigned fAntiAlias : 1;
711  unsigned fDither : 1;
712  unsigned fCapType : 2;
713  unsigned fJoinType : 2;
714  unsigned fStyle : 2;
715  unsigned fFilterQuality : 2;
716  unsigned fBlendMode : 8; // only need 5-6?
717  unsigned fPadding : 14; // 14==32-1-1-2-2-2-2-8
718  } fBitfields;
719  uint32_t fBitfieldsUInt;
720  };
721 
722  friend class SkPaintPriv;
723 };
724 
725 #endif
SkColorFilter
ColorFilters are optional objects in the drawing pipeline.
Definition: SkColorFilter.h:25
SkPaint::SkPaint
SkPaint(const SkColor4f &color, SkColorSpace *colorSpace=nullptr)
Constructs SkPaint with default values and the given color.
SkPaint::kRound_Cap
@ kRound_Cap
adds circle
Definition: SkPaint.h:366
SkPaint::kSquare_Cap
@ kSquare_Cap
adds square
Definition: SkPaint.h:367
SkPaint::getBlendMode
SkBlendMode getBlendMode() const
Returns SkBlendMode.
Definition: SkPaint.h:510
SkPath
Definition: SkPath.h:44
SkPaint::fJoinType
unsigned fJoinType
Definition: SkPaint.h:713
SkPaint::getColorFilter
SkColorFilter * getColorFilter() const
Returns SkColorFilter if set, or nullptr.
Definition: SkPaint.h:482
SkPaint::isSrcOver
bool isSrcOver() const
Returns true if SkBlendMode is SkBlendMode::kSrcOver, the default.
Definition: SkPaint.h:516
SkRect::isSorted
bool isSorted() const
Returns true if fLeft is equal to or less than fRight, or if fTop is equal to or less than fBottom.
Definition: SkRect.h:697
SkPaint::getFillPath
bool getFillPath(const SkPath &src, SkPath *dst, const SkRect *cullRect, SkScalar resScale=1) const
Returns the filled equivalent of the stroked path.
SkPaint::computeFastBounds
const SkRect & computeFastBounds(const SkRect &orig, SkRect *storage) const
(to be made private) Only call this if canComputeFastBounds() returned true.
Definition: SkPaint.h:656
SkPaint::setStroke
void setStroke(bool)
Set paint's style to kStroke if true, or kFill if false.
SkPaint::kMiter_Join
@ kMiter_Join
extends to miter limit
Definition: SkPaint.h:390
SkColor4f
SkPaint::getFilterQuality
SkFilterQuality getFilterQuality() const
Returns SkFilterQuality, the image filtering level.
Definition: SkPaint.h:201
SkPaint::setStrokeWidth
void setStrokeWidth(SkScalar width)
Sets the thickness of the pen used by the paint to outline the shape.
SkPaint::Cap
Cap
Definition: SkPaint.h:364
SkPaint::kBevel_Join
@ kBevel_Join
connects outside edges
Definition: SkPaint.h:392
SkBlendMode
SkBlendMode
Definition: SkBlendMode.h:13
SkShader
Definition: SkShader.h:39
SkPaint::~SkPaint
~SkPaint()
Decreases SkPaint SkRefCnt of owned objects: SkPathEffect, SkShader, SkMaskFilter,...
SkPathEffect
Definition: SkPathEffect.h:27
SkPaint::SkPaint
SkPaint(SkPaint &&paint)
Implements a move constructor to avoid increasing the reference counts of objects referenced by the p...
SkPaint::canComputeFastBounds
bool canComputeFastBounds() const
(to be made private) Returns true if SkPaint does not include elements requiring extensive computatio...
SkPaint::fStyle
unsigned fStyle
Definition: SkPaint.h:714
SkPaint::setARGB
void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Sets color used when drawing solid fills.
SkPaint::operator!=
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:143
SkPaint::setAlpha
void setAlpha(U8CPU a)
Definition: SkPaint.h:310
SkPaint::refPathEffect
sk_sp< SkPathEffect > refPathEffect() const
Returns SkPathEffect if set, or nullptr.
SkPaint::setStrokeJoin
void setStrokeJoin(Join join)
Sets the geometry drawn at the corners of strokes.
SkPaint::setBlendMode
void setBlendMode(SkBlendMode mode)
Sets SkBlendMode to mode.
Definition: SkPaint.h:523
SkPaint::setDither
void setDither(bool dither)
Requests, but does not require, to distribute color error.
Definition: SkPaint.h:193
SkPaint::fDither
unsigned fDither
Definition: SkPaint.h:711
SkRefCnt.h
SkPaint::refImageFilter
sk_sp< SkImageFilter > refImageFilter() const
Returns SkImageFilter if set, or nullptr.
SkPaint::setFilterQuality
void setFilterQuality(SkFilterQuality quality)
Sets SkFilterQuality, the image filtering level.
SkPaint::getStyle
Style getStyle() const
Returns whether the geometry is filled, stroked, or filled and stroked.
Definition: SkPaint.h:237
SkPaint::getStrokeCap
Cap getStrokeCap() const
Returns the geometry drawn at the beginning and end of strokes.
Definition: SkPaint.h:403
SkPaint::operator=
SkPaint & operator=(SkPaint &&paint)
Moves the paint to avoid increasing the reference counts of objects referenced by the paint parameter...
SkPaint::SkPaint
SkPaint()
Constructs SkPaint with default values.
sk_sp< SkShader >
SkPaint::Join
Join
Definition: SkPaint.h:389
SkPaint::reset
void reset()
Sets all SkPaint contents to their initial values.
SkPaint::getMaskFilter
SkMaskFilter * getMaskFilter() const
Returns SkMaskFilter if set, or nullptr.
Definition: SkPaint.h:558
SkPaint::refShader
sk_sp< SkShader > refShader() const
Returns optional colors used when filling a path, such as a gradient.
SkToBool
static constexpr bool SkToBool(const T &x)
Definition: SkTypes.h:497
SkPaint::setStyle
void setStyle(Style style)
Sets whether the geometry is filled, stroked, or filled and stroked.
SkPaint
Definition: SkPaint.h:39
SK_API
#define SK_API
Definition: SkTypes.h:181
SkPaint::getAlpha
uint8_t getAlpha() const
Definition: SkPaint.h:297
SkASSERT
#define SkASSERT(cond)
Definition: SkTypes.h:460
SkPaint::fPadding
unsigned fPadding
Definition: SkPaint.h:717
SkPaint::setColorFilter
void setColorFilter(sk_sp< SkColorFilter > colorFilter)
Sets SkColorFilter to filter, decreasing SkRefCnt of the previous SkColorFilter.
SkPaint::setAlphaf
void setAlphaf(float a)
Replaces alpha, leaving RGB unchanged.
SkPaint::SkPaint
SkPaint(const SkPaint &paint)
Makes a shallow copy of SkPaint.
SkPaint::getHash
uint32_t getHash() const
Returns a hash generated from SkPaint values and pointers.
SkPaint::setStrokeMiter
void setStrokeMiter(SkScalar miter)
Sets the limit at which a sharp corner is drawn beveled.
SkPaint::computeFastStrokeBounds
const SkRect & computeFastStrokeBounds(const SkRect &orig, SkRect *storage) const
(to be made private)
Definition: SkPaint.h:680
SkPaint::fBlendMode
unsigned fBlendMode
Definition: SkPaint.h:716
SkPaint::kRound_Join
@ kRound_Join
adds circle
Definition: SkPaint.h:391
SkPaint::setColor
void setColor(const SkColor4f &color, SkColorSpace *colorSpace=nullptr)
Sets alpha and RGB used when stroking and filling.
SkPaint::isAntiAlias
bool isAntiAlias() const
Returns true if pixels on the active edges of SkPath may be drawn with partial transparency.
Definition: SkPaint.h:173
SkFilterQuality
SkFilterQuality
Controls how much filtering to be done when scaling/transforming complex colors e....
Definition: SkFilterQuality.h:17
SkPaint::fFilterQuality
unsigned fFilterQuality
Definition: SkPaint.h:715
SkPaint::getStrokeWidth
SkScalar getStrokeWidth() const
Returns the thickness of the pen used by SkPaint to outline the shape.
Definition: SkPaint.h:331
SkBlendMode.h
SkPaint::setAntiAlias
void setAntiAlias(bool aa)
Requests, but does not require, that edge pixels draw opaque or with partial transparency.
Definition: SkPaint.h:181
SkFilterQuality.h
SkScalar
float SkScalar
Definition: SkScalar.h:16
SkPaint::setMaskFilter
void setMaskFilter(sk_sp< SkMaskFilter > maskFilter)
Sets SkMaskFilter to maskFilter, decreasing SkRefCnt of the previous SkMaskFilter.
SkPaint::doComputeFastBounds
const SkRect & doComputeFastBounds(const SkRect &orig, SkRect *storage, Style style) const
(to be made private) Computes the bounds, overriding the SkPaint SkPaint::Style.
SkPaint::fAntiAlias
unsigned fAntiAlias
Definition: SkPaint.h:710
SkPaint::nothingToDraw
bool nothingToDraw() const
Returns true if SkPaint prevents all drawing; otherwise, the SkPaint may or may not allow drawing.
SkPaint::getAlphaf
float getAlphaf() const
Retrieves alpha from the color used when stroking and filling.
Definition: SkPaint.h:294
f
float f
Definition: skcms.cc:52
SkPaint::refMaskFilter
sk_sp< SkMaskFilter > refMaskFilter() const
Returns SkMaskFilter if set, or nullptr.
SkPaint::operator=
SkPaint & operator=(const SkPaint &paint)
Makes a shallow copy of SkPaint.
SkPaint::fBitfieldsUInt
uint32_t fBitfieldsUInt
Definition: SkPaint.h:719
SkPaint::getPathEffect
SkPathEffect * getPathEffect() const
Returns SkPathEffect if set, or nullptr.
Definition: SkPaint.h:530
SkPaint::setStrokeCap
void setStrokeCap(Cap cap)
Sets the geometry drawn at the beginning and end of strokes.
SkBlendMode::kSrcOver
@ kSrcOver
source over destination
SkPaint::refColorFilter
sk_sp< SkColorFilter > refColorFilter() const
Returns SkColorFilter if set, or nullptr.
SkPaint::kButt_Cap
@ kButt_Cap
no stroke extension
Definition: SkPaint.h:365
SkPaint::setImageFilter
void setImageFilter(sk_sp< SkImageFilter > imageFilter)
Sets SkImageFilter to imageFilter, decreasing SkRefCnt of the previous SkImageFilter.
SkColor
uint32_t SkColor
32-bit ARGB color value, unpremultiplied.
Definition: SkColor.h:35
SkPaint::Style
Style
Definition: SkPaint.h:225
SkPaint::kStroke_Style
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:227
SkMaskFilter
Definition: SkMaskFilter.h:24
SkRect
Definition: SkRect.h:572
SkPaint::setShader
void setShader(sk_sp< SkShader > shader)
Sets optional colors used when filling a path, such as a gradient.
U8CPU
unsigned U8CPU
Fast type for unsigned 8 bits.
Definition: SkTypes.h:488
SkPaint::getShader
SkShader * getShader() const
Returns optional colors used when filling a path, such as a gradient.
Definition: SkPaint.h:453
SkPaint::getFillPath
bool getFillPath(const SkPath &src, SkPath *dst) const
Returns the filled equivalent of the stroked path.
Definition: SkPaint.h:443
SkPaint::kFill_Style
@ kFill_Style
set to fill geometry
Definition: SkPaint.h:226
SkPaint::getColor
SkColor getColor() const
Retrieves alpha and RGB, unpremultiplied, packed into 32 bits.
Definition: SkPaint.h:258
SkPaint::fCapType
unsigned fCapType
Definition: SkPaint.h:712
SkColor.h
SkPaint::getStrokeMiter
SkScalar getStrokeMiter() const
Returns the limit at which a sharp corner is drawn beveled.
Definition: SkPaint.h:349
SkPaint::getColor4f
SkColor4f getColor4f() const
Retrieves alpha and RGB, unpremultiplied, as four floating point values.
Definition: SkPaint.h:265
SkPaint::setPathEffect
void setPathEffect(sk_sp< SkPathEffect > pathEffect)
Sets SkPathEffect to pathEffect, decreasing SkRefCnt of the previous SkPathEffect.
SkPaint::getImageFilter
SkImageFilter * getImageFilter() const
Returns SkImageFilter if set, or nullptr.
Definition: SkPaint.h:588
SkPaint::getStrokeJoin
Join getStrokeJoin() const
Returns the geometry drawn at the corners of strokes.
Definition: SkPaint.h:414
SkPaint::setColor4f
void setColor4f(const SkColor4f &color, SkColorSpace *colorSpace=nullptr)
Definition: SkPaint.h:286
SkColorSpace
Definition: SkColorSpace.h:104
SkPaint::isDither
bool isDither() const
Returns true if color error may be distributed to smooth color transition.
Definition: SkPaint.h:186
SkPaint::operator==
SK_API friend bool operator==(const SkPaint &a, const SkPaint &b)
Compares a and b, and returns true if a and b are equivalent.
SkPaint::setColor
void setColor(SkColor color)
Sets alpha and RGB used when stroking and filling.
SkImageFilter
Base class for image filters.
Definition: SkImageFilter.h:31
SkPaint::kStrokeAndFill_Style
@ kStrokeAndFill_Style
sets to stroke and fill geometry
Definition: SkPaint.h:228