senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
Mike Reed | 4ca0145 | 2018-12-13 10:08:05 -0500 | [diff] [blame] | 7 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 8 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
Mike Reed | 4ca0145 | 2018-12-13 10:08:05 -0500 | [diff] [blame] | 10 | #include "SkFont.h" |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 11 | |
| 12 | namespace skiagm { |
| 13 | |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame] | 14 | static const char* color_type_name(SkColorType colorType) { |
| 15 | switch (colorType) { |
| 16 | case kUnknown_SkColorType: return "unknown"; |
| 17 | case kAlpha_8_SkColorType: return "A8"; |
| 18 | case kRGB_565_SkColorType: return "565"; |
| 19 | case kARGB_4444_SkColorType: return "4444"; |
| 20 | case kRGBA_8888_SkColorType: return "8888"; |
| 21 | case kRGB_888x_SkColorType: return "888x"; |
| 22 | case kBGRA_8888_SkColorType: return "8888"; |
| 23 | case kRGBA_1010102_SkColorType: return "1010102"; |
| 24 | case kRGB_101010x_SkColorType: return "101010x"; |
| 25 | case kGray_8_SkColorType: return "G8"; |
| 26 | case kRGBA_F16_SkColorType: return "F16"; |
Mike Klein | 3785471 | 2018-06-26 11:43:06 -0400 | [diff] [blame] | 27 | case kRGBA_F32_SkColorType: return "F32"; |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame] | 28 | } |
| 29 | return ""; |
| 30 | } |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 31 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 32 | constexpr SkColorType gColorTypes[] = { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 33 | kRGB_565_SkColorType, |
| 34 | kARGB_4444_SkColorType, |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 35 | kN32_SkColorType, |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 38 | #define NUM_CONFIGS SK_ARRAY_COUNT(gColorTypes) |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 39 | |
| 40 | static void draw_checks(SkCanvas* canvas, int width, int height) { |
| 41 | SkPaint paint; |
| 42 | paint.setColor(SK_ColorRED); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 43 | canvas->drawRect(SkRect::MakeIWH(width/2, height/2), paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 44 | paint.setColor(SK_ColorGREEN); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 45 | canvas->drawRect({ SkIntToScalar(width/2), 0, SkIntToScalar(width), SkIntToScalar(height/2) }, |
| 46 | paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 47 | paint.setColor(SK_ColorBLUE); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 48 | canvas->drawRect({ 0, SkIntToScalar(height/2), SkIntToScalar(width/2), SkIntToScalar(height) }, |
| 49 | paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 50 | paint.setColor(SK_ColorYELLOW); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 51 | canvas->drawRect({ SkIntToScalar(width/2), SkIntToScalar(height/2), SkIntToScalar(width), |
| 52 | SkIntToScalar(height) }, paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | class BitmapCopyGM : public GM { |
| 56 | public: |
| 57 | SkBitmap fDst[NUM_CONFIGS]; |
| 58 | |
| 59 | BitmapCopyGM() { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 60 | this->setBGColor(0xFFDDDDDD); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | protected: |
| 64 | virtual SkString onShortName() { |
| 65 | return SkString("bitmapcopy"); |
| 66 | } |
| 67 | |
| 68 | virtual SkISize onISize() { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 69 | return SkISize::Make(540, 330); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | virtual void onDraw(SkCanvas* canvas) { |
| 73 | SkPaint paint; |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 74 | SkScalar horizMargin = 10; |
| 75 | SkScalar vertMargin = 10; |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 76 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 77 | SkBitmap src; |
Matt Sarett | ec138b4 | 2017-04-28 16:06:34 -0400 | [diff] [blame] | 78 | src.allocN32Pixels(40, 40, kOpaque_SkAlphaType); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 79 | SkCanvas canvasTmp(src); |
skia.committer@gmail.com | d55e357 | 2013-08-08 07:01:20 +0000 | [diff] [blame] | 80 | |
commit-bot@chromium.org | 488a337 | 2013-08-07 17:03:09 +0000 | [diff] [blame] | 81 | draw_checks(&canvasTmp, 40, 40); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 82 | |
| 83 | for (unsigned i = 0; i < NUM_CONFIGS; ++i) { |
Matt Sarett | 68b8e3d | 2017-04-28 11:15:22 -0400 | [diff] [blame] | 84 | sk_tool_utils::copy_to(&fDst[i], gColorTypes[i], src); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 87 | canvas->clear(0xFFDDDDDD); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 88 | paint.setAntiAlias(true); |
Mike Reed | 4ca0145 | 2018-12-13 10:08:05 -0500 | [diff] [blame] | 89 | |
| 90 | SkFont font(sk_tool_utils::create_portable_typeface()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 91 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 92 | SkScalar width = SkIntToScalar(40); |
| 93 | SkScalar height = SkIntToScalar(40); |
Mike Reed | 4ca0145 | 2018-12-13 10:08:05 -0500 | [diff] [blame] | 94 | if (font.getSpacing() > height) { |
| 95 | height = font.getSpacing(); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 96 | } |
| 97 | for (unsigned i = 0; i < NUM_CONFIGS; i++) { |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame] | 98 | const char* name = color_type_name(src.colorType()); |
Mike Reed | 4ca0145 | 2018-12-13 10:08:05 -0500 | [diff] [blame] | 99 | SkScalar textWidth = font.measureText(name, strlen(name), kUTF8_SkTextEncoding); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 100 | if (textWidth > width) { |
| 101 | width = textWidth; |
| 102 | } |
| 103 | } |
| 104 | SkScalar horizOffset = width + horizMargin; |
| 105 | SkScalar vertOffset = height + vertMargin; |
| 106 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 107 | |
| 108 | for (unsigned i = 0; i < NUM_CONFIGS; i++) { |
| 109 | canvas->save(); |
| 110 | // Draw destination config name |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame] | 111 | const char* name = color_type_name(fDst[i].colorType()); |
Mike Reed | 4ca0145 | 2018-12-13 10:08:05 -0500 | [diff] [blame] | 112 | SkScalar textWidth = font.measureText(name, strlen(name), kUTF8_SkTextEncoding); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 113 | SkScalar x = (width - textWidth) / SkScalar(2); |
Mike Reed | 4ca0145 | 2018-12-13 10:08:05 -0500 | [diff] [blame] | 114 | SkScalar y = font.getSpacing() / SkScalar(2); |
| 115 | canvas->drawSimpleText(name, strlen(name), kUTF8_SkTextEncoding, x, y, font, paint); |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 116 | |
| 117 | // Draw destination bitmap |
| 118 | canvas->translate(0, vertOffset); |
| 119 | x = (width - 40) / SkScalar(2); |
| 120 | canvas->drawBitmap(fDst[i], x, 0, &paint); |
| 121 | canvas->restore(); |
| 122 | |
| 123 | canvas->translate(horizOffset, 0); |
| 124 | } |
| 125 | } |
| 126 | |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 127 | private: |
| 128 | typedef GM INHERITED; |
| 129 | }; |
| 130 | |
| 131 | ////////////////////////////////////////////////////////////////////////////// |
| 132 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 133 | DEF_GM( return new BitmapCopyGM; ) |
senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 134 | } |