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