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