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