From 0a7b393e320ed37144719541361b412aaf3e8f62 Mon Sep 17 00:00:00 2001 From: Hal Canary Date: Thu, 2 May 2019 11:31:28 -0400 Subject: [PATCH] SkPaint: new constructor Change-Id: I5033070bf5d52571869a687387b4f4fc6dddfdd2 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/211597 Commit-Queue: Hal Canary Reviewed-by: Mike Reed --- gm/all_bitmap_configs.cpp | 11 +++-------- gm/colorwheel.cpp | 24 ++++++++---------------- gm/gm.cpp | 27 +++++++-------------------- gm/tilemodes_alpha.cpp | 3 +-- include/core/SkColor.h | 14 ++++++++++++++ include/core/SkPaint.h | 13 +++++++++++++ src/core/SkPaint.cpp | 4 ++++ 7 files changed, 50 insertions(+), 46 deletions(-) diff --git a/gm/all_bitmap_configs.cpp b/gm/all_bitmap_configs.cpp index 64b46eeada..7f3496aa8b 100644 --- a/gm/all_bitmap_configs.cpp +++ b/gm/all_bitmap_configs.cpp @@ -73,21 +73,17 @@ static SkBitmap make_bitmap(SkColorType ct) { static void draw_center_letter(char c, const SkFont& font, SkColor color, SkScalar x, SkScalar y, SkCanvas* canvas) { - SkPaint paint; - paint.setColor(color); SkRect bounds; font.measureText(&c, 1, kUTF8_SkTextEncoding, &bounds); canvas->drawSimpleText(&c, 1, kUTF8_SkTextEncoding, x - bounds.centerX(), y - bounds.centerY(), - font, paint); + font, SkPaint(SkColor4f::FromColor(color))); } static void color_wheel_native(SkCanvas* canvas) { SkAutoCanvasRestore autoCanvasRestore(canvas, true); canvas->translate(0.5f * SCALE, 0.5f * SCALE); - SkPaint p; - p.setColor(SK_ColorWHITE); - canvas->drawCircle(0.0f, 0.0f, SCALE * 0.5f, p); + canvas->drawCircle(0.0f, 0.0f, SCALE * 0.5f, SkPaint(SkColors::kWhite)); const double sqrt_3_over_2 = 0.8660254037844387; const SkScalar Z = 0.0f; @@ -131,8 +127,7 @@ static void draw(SkCanvas* canvas, DEF_SIMPLE_GM(all_bitmap_configs, canvas, SCALE, 6 * SCALE) { SkAutoCanvasRestore autoCanvasRestore(canvas, true); - SkPaint p; - p.setColor(SK_ColorBLACK); + SkPaint p(SkColors::kBlack); p.setAntiAlias(true); SkFont font(ToolUtils::create_portable_typeface()); diff --git a/gm/colorwheel.cpp b/gm/colorwheel.cpp index ffe9158120..41265f5d4e 100644 --- a/gm/colorwheel.cpp +++ b/gm/colorwheel.cpp @@ -47,23 +47,15 @@ DEF_SIMPLE_GM(colorwheel, canvas, 256, 256) { } DEF_SIMPLE_GM(colorwheelnative, canvas, 128, 28) { - SkPaint paint; - SkFont font(ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Bold()), 18); + SkFont font(ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Bold()), 18); font.setEdging(SkFont::Edging::kAlias); canvas->clear(SK_ColorLTGRAY); - paint.setColor(SK_ColorRED); - canvas->drawString("R", 8.0f, 20.0f, font, paint); - paint.setColor(SK_ColorGREEN); - canvas->drawString("G", 24.0f, 20.0f, font, paint); - paint.setColor(SK_ColorBLUE); - canvas->drawString("B", 40.0f, 20.0f, font, paint); - paint.setColor(SK_ColorCYAN); - canvas->drawString("C", 56.0f, 20.0f, font, paint); - paint.setColor(SK_ColorMAGENTA); - canvas->drawString("M", 72.0f, 20.0f, font, paint); - paint.setColor(SK_ColorYELLOW); - canvas->drawString("Y", 88.0f, 20.0f, font, paint); - paint.setColor(SK_ColorBLACK); - canvas->drawString("K", 104.0f, 20.0f, font, paint); + canvas->drawString("R", 8.0f, 20.0f, font, SkPaint(SkColors::kRed)); + canvas->drawString("G", 24.0f, 20.0f, font, SkPaint(SkColors::kGreen)); + canvas->drawString("B", 40.0f, 20.0f, font, SkPaint(SkColors::kBlue)); + canvas->drawString("C", 56.0f, 20.0f, font, SkPaint(SkColors::kCyan)); + canvas->drawString("M", 72.0f, 20.0f, font, SkPaint(SkColors::kMagenta)); + canvas->drawString("Y", 88.0f, 20.0f, font, SkPaint(SkColors::kYellow)); + canvas->drawString("K", 104.0f, 20.0f, font, SkPaint(SkColors::kBlack)); } diff --git a/gm/gm.cpp b/gm/gm.cpp index 38ab2be7a2..04a3d6667f 100644 --- a/gm/gm.cpp +++ b/gm/gm.cpp @@ -43,8 +43,7 @@ static void draw_failure_message(SkCanvas* canvas, const char format[], ...) { SkFont font; SkRect bounds; font.measureText(failureMsg.c_str(), failureMsg.size(), kUTF8_SkTextEncoding, &bounds); - SkPaint textPaint; - textPaint.setColor(SK_ColorWHITE); + SkPaint textPaint(SkColors::kWhite); canvas->drawString(failureMsg, kOffset, bounds.height() + kOffset, font, textPaint); } @@ -54,8 +53,7 @@ static void draw_gpu_only_message(SkCanvas* canvas) { SkCanvas bmpCanvas(bmp); bmpCanvas.drawColor(SK_ColorWHITE); SkFont font(ToolUtils::create_portable_typeface(), 20); - SkPaint paint; - paint.setColor(SK_ColorRED); + SkPaint paint(SkColors::kRed); bmpCanvas.drawString("GPU Only", 20, 40, font, paint); SkMatrix localM; localM.setRotate(35.f); @@ -156,12 +154,7 @@ void GM::onSetControls(const SkMetaData&) {} ///////////////////////////////////////////////////////////////////////////////////////////// void GM::drawSizeBounds(SkCanvas* canvas, SkColor color) { - SkISize size = this->getISize(); - SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()), - SkIntToScalar(size.height())); - SkPaint paint; - paint.setColor(color); - canvas->drawRect(r, paint); + canvas->drawRect(SkRect::Make(this->getISize()), SkPaint(SkColor4f::FromColor(color))); } // need to explicitly declare this, or we get some weird infinite loop llist @@ -203,15 +196,12 @@ static void mark(SkCanvas* canvas, SkScalar x, SkScalar y, Fn&& fn) { void MarkGMGood(SkCanvas* canvas, SkScalar x, SkScalar y) { mark(canvas, x,y, [&]{ - SkPaint paint; - // A green circle. - paint.setColor(SkColorSetRGB(27, 158, 119)); - canvas->drawCircle(0,0, 12, paint); + canvas->drawCircle(0, 0, 12, SkPaint(SkColor4f::FromColor(SkColorSetRGB(27, 158, 119)))); // Cut out a check mark. + SkPaint paint(SkColors::kTransparent); paint.setBlendMode(SkBlendMode::kSrc); - paint.setColor(0x00000000); paint.setStrokeWidth(2); paint.setStyle(SkPaint::kStroke_Style); canvas->drawLine(-6, 0, @@ -223,15 +213,12 @@ void MarkGMGood(SkCanvas* canvas, SkScalar x, SkScalar y) { void MarkGMBad(SkCanvas* canvas, SkScalar x, SkScalar y) { mark(canvas, x,y, [&] { - SkPaint paint; - // A red circle. - paint.setColor(SkColorSetRGB(231, 41, 138)); - canvas->drawCircle(0,0, 12, paint); + canvas->drawCircle(0,0, 12, SkPaint(SkColor4f::FromColor(SkColorSetRGB(231, 41, 138)))); // Cut out an 'X'. + SkPaint paint(SkColors::kTransparent); paint.setBlendMode(SkBlendMode::kSrc); - paint.setColor(0x00000000); paint.setStrokeWidth(2); paint.setStyle(SkPaint::kStroke_Style); canvas->drawLine(-5,-5, diff --git a/gm/tilemodes_alpha.cpp b/gm/tilemodes_alpha.cpp index e3b7b8deec..83a6d2f09c 100644 --- a/gm/tilemodes_alpha.cpp +++ b/gm/tilemodes_alpha.cpp @@ -28,8 +28,7 @@ DEF_SIMPLE_GM(tilemodes_alpha, canvas, 512, 512) { for (int x = 0; x < 4; ++x) { SkRect rect = SkRect::MakeXYWH(128 * x + 1, 128 * y + 1, 126, 126); SkMatrix matrix = SkMatrix::MakeTrans(rect.x(), rect.y()); - SkPaint paint; - paint.setAlphaf(0.5f); + SkPaint paint(SkColor4f{0, 0, 0, 0.5f}); paint.setShader(image->makeShader(kModes[x], kModes[y], &matrix)); canvas->drawRect(rect, paint); } diff --git a/include/core/SkColor.h b/include/core/SkColor.h index 3209592cca..a46c13c3e7 100644 --- a/include/core/SkColor.h +++ b/include/core/SkColor.h @@ -385,4 +385,18 @@ using SkColor4f = SkRGBA4f; template <> SK_API SkColor4f SkColor4f::FromColor(SkColor); template <> SK_API SkColor SkColor4f::toSkColor() const; +namespace SkColors { +constexpr SkColor4f kTransparent = {0, 0, 0, 0}; +constexpr SkColor4f kBlack = {1, 0, 0, 0}; +constexpr SkColor4f kDkGray = {1, 0.25f, 0.25f, 0.25f}; +constexpr SkColor4f kGray = {1, 0.50f, 0.50f, 0.50f}; +constexpr SkColor4f kLtGray = {1, 0.75f, 0.75f, 0.75f}; +constexpr SkColor4f kWhite = {1, 1, 1, 1}; +constexpr SkColor4f kRed = {1, 1, 0, 0}; +constexpr SkColor4f kGreen = {1, 0, 1, 0}; +constexpr SkColor4f kBlue = {1, 0, 0, 1}; +constexpr SkColor4f kYellow = {1, 1, 1, 0}; +constexpr SkColor4f kCyan = {1, 0, 1, 1}; +constexpr SkColor4f kMagenta = {1, 1, 0, 1}; +} // namespace SkColors #endif diff --git a/include/core/SkPaint.h b/include/core/SkPaint.h index bb5c9ef5a7..dacb909a2f 100644 --- a/include/core/SkPaint.h +++ b/include/core/SkPaint.h @@ -43,6 +43,19 @@ public: */ SkPaint(); + /** Constructs SkPaint with default values and the given color. + + Sets alpha and RGB used when stroking and filling. The color is four floating + point values, unpremultiplied. The color values are interpreted as being in + the colorSpace. If colorSpace is nullptr, then color is assumed to be in the + sRGB color space. + + @param color unpremultiplied RGBA + @param colorSpace SkColorSpace describing the encoding of color + @return SkPaint with the given color + */ + explicit SkPaint(const SkColor4f& color, SkColorSpace* colorSpace = nullptr); + /** Makes a shallow copy of SkPaint. SkPathEffect, SkShader, SkMaskFilter, SkColorFilter, SkDrawLooper, and SkImageFilter are shared between the original paint and the copy. Objects containing SkRefCnt increment diff --git a/src/core/SkPaint.cpp b/src/core/SkPaint.cpp index e6d62e5f64..2120f60747 100644 --- a/src/core/SkPaint.cpp +++ b/src/core/SkPaint.cpp @@ -56,6 +56,10 @@ SkPaint::SkPaint() static_assert(sizeof(fBitfields) == sizeof(fBitfieldsUInt), ""); } +SkPaint::SkPaint(const SkColor4f& color, SkColorSpace* colorSpace) : SkPaint() { + this->setColor4f(color, colorSpace); +} + SkPaint::SkPaint(const SkPaint& src) = default; SkPaint::SkPaint(SkPaint&& src) = default;