djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 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 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 10 | |
| 11 | #include "Resources.h" |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 12 | #include "SkBlurImageFilter.h" |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 13 | #include "SkCanvas.h" |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 14 | #include "SkColorFilterImageFilter.h" |
| 15 | #include "SkColorMatrixFilter.h" |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 16 | #include "SkGradientShader.h" |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 17 | #include "SkStream.h" |
| 18 | #include "SkTypeface.h" |
| 19 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 20 | /* |
| 21 | * Spits out a dummy gradient to test blur with shader on paint |
| 22 | */ |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 23 | static sk_sp<SkShader> MakeLinear() { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 24 | constexpr SkPoint kPts[] = { { 0, 0 }, { 32, 32 } }; |
| 25 | constexpr SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 26 | constexpr SkColor kColors[] = {0x80F00080, 0xF0F08000, 0x800080F0 }; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 27 | return SkGradientShader::MakeLinear(kPts, kColors, kPos, SK_ARRAY_COUNT(kColors), |
| 28 | SkShader::kClamp_TileMode); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 29 | } |
| 30 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 31 | static sk_sp<SkImageFilter> make_grayscale(sk_sp<SkImageFilter> input) { |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 32 | SkScalar matrix[20]; |
| 33 | memset(matrix, 0, 20 * sizeof(SkScalar)); |
| 34 | matrix[0] = matrix[5] = matrix[10] = 0.2126f; |
| 35 | matrix[1] = matrix[6] = matrix[11] = 0.7152f; |
| 36 | matrix[2] = matrix[7] = matrix[12] = 0.0722f; |
| 37 | matrix[18] = 1.0f; |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 38 | sk_sp<SkColorFilter> filter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix)); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 39 | return SkColorFilterImageFilter::Make(std::move(filter), std::move(input)); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 40 | } |
| 41 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 42 | static sk_sp<SkImageFilter> make_blur(float amount, sk_sp<SkImageFilter> input) { |
| 43 | return SkBlurImageFilter::Make(amount, amount, std::move(input)); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 46 | static sk_sp<SkColorFilter> make_color_filter() { |
| 47 | return SkColorMatrixFilter::MakeLightingFilter(SkColorSetRGB(0x00, 0x80, 0xFF), |
| 48 | SkColorSetRGB(0xFF, 0x20, 0x00)); |
| 49 | } |
| 50 | |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 51 | namespace skiagm { |
| 52 | |
| 53 | class ColorEmojiGM : public GM { |
| 54 | public: |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 55 | ColorEmojiGM() { } |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 56 | |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 57 | protected: |
bungeman | 6bdc9cd | 2015-01-26 14:08:52 -0800 | [diff] [blame] | 58 | struct EmojiFont { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 59 | sk_sp<SkTypeface> typeface; |
bungeman | 6bdc9cd | 2015-01-26 14:08:52 -0800 | [diff] [blame] | 60 | const char* text; |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 61 | } emojiFont; |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 62 | virtual void onOnceBeforeDraw() override { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 63 | emojiFont.typeface = sk_tool_utils::emoji_typeface(); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 64 | emojiFont.text = sk_tool_utils::emoji_sample_text(); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 65 | } |
| 66 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 67 | SkString onShortName() override { |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 68 | SkString name("coloremoji"); |
| 69 | name.append(sk_tool_utils::platform_os_emoji()); |
| 70 | return name; |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 73 | SkISize onISize() override { return SkISize::Make(650, 1200); } |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 74 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 75 | void onDraw(SkCanvas* canvas) override { |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 76 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 77 | canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY)); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 78 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 79 | SkPaint paint; |
| 80 | paint.setTypeface(emojiFont.typeface); |
| 81 | const char* text = emojiFont.text; |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 82 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 83 | // draw text at different point sizes |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 84 | constexpr SkScalar textSizes[] = { 10, 30, 50, }; |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 85 | SkPaint::FontMetrics metrics; |
| 86 | SkScalar y = 0; |
| 87 | for (const SkScalar& textSize : textSizes) { |
| 88 | paint.setTextSize(textSize); |
| 89 | paint.getFontMetrics(&metrics); |
| 90 | y += -metrics.fAscent; |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame^] | 91 | canvas->drawString(text, 10, y, paint); |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 92 | y += metrics.fDescent + metrics.fLeading; |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 93 | } |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 94 | |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 95 | y += 20; |
| 96 | SkScalar savedY = y; |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 97 | // draw with shaders and image filters |
| 98 | for (int makeLinear = 0; makeLinear < 2; makeLinear++) { |
| 99 | for (int makeBlur = 0; makeBlur < 2; makeBlur++) { |
| 100 | for (int makeGray = 0; makeGray < 2; makeGray++) { |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 101 | for (int makeMode = 0; makeMode < 2; ++makeMode) { |
| 102 | for (int alpha = 0; alpha < 2; ++alpha) { |
| 103 | SkPaint shaderPaint; |
| 104 | shaderPaint.setTypeface(sk_ref_sp(paint.getTypeface())); |
| 105 | if (SkToBool(makeLinear)) { |
| 106 | shaderPaint.setShader(MakeLinear()); |
| 107 | } |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 108 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 109 | if (SkToBool(makeBlur) && SkToBool(makeGray)) { |
| 110 | sk_sp<SkImageFilter> grayScale(make_grayscale(nullptr)); |
| 111 | sk_sp<SkImageFilter> blur(make_blur(3.0f, std::move(grayScale))); |
| 112 | shaderPaint.setImageFilter(std::move(blur)); |
| 113 | } else if (SkToBool(makeBlur)) { |
| 114 | shaderPaint.setImageFilter(make_blur(3.0f, nullptr)); |
| 115 | } else if (SkToBool(makeGray)) { |
| 116 | shaderPaint.setImageFilter(make_grayscale(nullptr)); |
| 117 | } |
| 118 | if (makeMode) { |
| 119 | shaderPaint.setColorFilter(make_color_filter()); |
| 120 | } |
| 121 | if (alpha) { |
| 122 | shaderPaint.setAlpha(0x80); |
| 123 | } |
| 124 | shaderPaint.setTextSize(30); |
| 125 | shaderPaint.getFontMetrics(&metrics); |
| 126 | y += -metrics.fAscent; |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame^] | 127 | canvas->drawString(text, 380, y, shaderPaint); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 128 | y += metrics.fDescent + metrics.fLeading; |
| 129 | } |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 130 | } |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 131 | } |
| 132 | } |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 133 | } |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 134 | // setup work needed to draw text with different clips |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 135 | canvas->translate(10, savedY); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 136 | paint.setTextSize(40); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 137 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 138 | // compute the bounds of the text |
| 139 | SkRect bounds; |
| 140 | paint.measureText(text, strlen(text), &bounds); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 141 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 142 | const SkScalar boundsHalfWidth = bounds.width() * SK_ScalarHalf; |
| 143 | const SkScalar boundsHalfHeight = bounds.height() * SK_ScalarHalf; |
| 144 | const SkScalar boundsQuarterWidth = boundsHalfWidth * SK_ScalarHalf; |
| 145 | const SkScalar boundsQuarterHeight = boundsHalfHeight * SK_ScalarHalf; |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 146 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 147 | SkRect upperLeftClip = SkRect::MakeXYWH(bounds.left(), bounds.top(), |
| 148 | boundsHalfWidth, boundsHalfHeight); |
| 149 | SkRect lowerRightClip = SkRect::MakeXYWH(bounds.centerX(), bounds.centerY(), |
| 150 | boundsHalfWidth, boundsHalfHeight); |
| 151 | SkRect interiorClip = bounds; |
| 152 | interiorClip.inset(boundsQuarterWidth, boundsQuarterHeight); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 153 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 154 | const SkRect clipRects[] = { bounds, upperLeftClip, lowerRightClip, interiorClip }; |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 155 | |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 156 | SkPaint clipHairline; |
| 157 | clipHairline.setColor(SK_ColorWHITE); |
| 158 | clipHairline.setStyle(SkPaint::kStroke_Style); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 159 | |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 160 | for (const SkRect& clipRect : clipRects) { |
| 161 | canvas->translate(0, bounds.height()); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 162 | canvas->save(); |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 163 | canvas->drawRect(clipRect, clipHairline); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 164 | paint.setAlpha(0x20); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame^] | 165 | canvas->drawString(text, 0, 0, paint); |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 166 | canvas->clipRect(clipRect); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 167 | paint.setAlpha(0xFF); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame^] | 168 | canvas->drawString(text, 0, 0, paint); |
caryclark | c3dcb67 | 2015-07-21 12:27:36 -0700 | [diff] [blame] | 169 | canvas->restore(); |
bungeman | 5379051 | 2016-07-21 13:32:09 -0700 | [diff] [blame] | 170 | canvas->translate(0, SkIntToScalar(25)); |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 174 | typedef GM INHERITED; |
| 175 | }; |
| 176 | |
| 177 | ////////////////////////////////////////////////////////////////////////////// |
| 178 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 179 | DEF_GM(return new ColorEmojiGM;) |
djsollen@google.com | 6def2a2 | 2013-09-17 15:30:21 +0000 | [diff] [blame] | 180 | |
| 181 | } |