blob: 28c6e7977f464c6c41dd23452b878c453c9942e2 [file] [log] [blame]
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001/*
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 Klein33d20552017-03-22 13:47:51 -04008#include "sk_tool_utils.h"
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00009
10namespace skiagm {
11
Brian Salomone41e1762018-01-25 14:07:47 -050012static 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 Klein37854712018-06-26 11:43:06 -040025 case kRGBA_F32_SkColorType: return "F32";
Brian Salomone41e1762018-01-25 14:07:47 -050026 }
27 return "";
28}
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000029
mtkleindbfd7ab2016-09-01 11:24:54 -070030constexpr SkColorType gColorTypes[] = {
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000031 kRGB_565_SkColorType,
32 kARGB_4444_SkColorType,
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000033 kN32_SkColorType,
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000034};
35
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +000036#define NUM_CONFIGS SK_ARRAY_COUNT(gColorTypes)
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000037
38static void draw_checks(SkCanvas* canvas, int width, int height) {
39 SkPaint paint;
40 paint.setColor(SK_ColorRED);
Mike Reed3661bc92017-02-22 13:21:42 -050041 canvas->drawRect(SkRect::MakeIWH(width/2, height/2), paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000042 paint.setColor(SK_ColorGREEN);
Mike Reed3661bc92017-02-22 13:21:42 -050043 canvas->drawRect({ SkIntToScalar(width/2), 0, SkIntToScalar(width), SkIntToScalar(height/2) },
44 paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000045 paint.setColor(SK_ColorBLUE);
Mike Reed3661bc92017-02-22 13:21:42 -050046 canvas->drawRect({ 0, SkIntToScalar(height/2), SkIntToScalar(width/2), SkIntToScalar(height) },
47 paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000048 paint.setColor(SK_ColorYELLOW);
Mike Reed3661bc92017-02-22 13:21:42 -050049 canvas->drawRect({ SkIntToScalar(width/2), SkIntToScalar(height/2), SkIntToScalar(width),
50 SkIntToScalar(height) }, paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000051}
52
53class BitmapCopyGM : public GM {
54public:
55 SkBitmap fDst[NUM_CONFIGS];
56
57 BitmapCopyGM() {
Mike Kleind46dce32018-08-16 10:17:03 -040058 this->setBGColor(0xFFDDDDDD);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000059 }
60
61protected:
62 virtual SkString onShortName() {
63 return SkString("bitmapcopy");
64 }
65
66 virtual SkISize onISize() {
tfarinaf5393182014-06-09 23:59:03 -070067 return SkISize::Make(540, 330);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000068 }
69
70 virtual void onDraw(SkCanvas* canvas) {
71 SkPaint paint;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000072 SkScalar horizMargin = 10;
73 SkScalar vertMargin = 10;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000074
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000075 SkBitmap src;
Matt Sarettec138b42017-04-28 16:06:34 -040076 src.allocN32Pixels(40, 40, kOpaque_SkAlphaType);
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000077 SkCanvas canvasTmp(src);
skia.committer@gmail.comd55e3572013-08-08 07:01:20 +000078
commit-bot@chromium.org488a3372013-08-07 17:03:09 +000079 draw_checks(&canvasTmp, 40, 40);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000080
81 for (unsigned i = 0; i < NUM_CONFIGS; ++i) {
Matt Sarett68b8e3d2017-04-28 11:15:22 -040082 sk_tool_utils::copy_to(&fDst[i], gColorTypes[i], src);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000083 }
84
Mike Kleind46dce32018-08-16 10:17:03 -040085 canvas->clear(0xFFDDDDDD);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000086 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070087 sk_tool_utils::set_portable_typeface(&paint);
halcanary9d524f22016-03-29 09:03:52 -070088
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000089 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 Salomone41e1762018-01-25 14:07:47 -050095 const char* name = color_type_name(src.colorType());
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +000096 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 Salomone41e1762018-01-25 14:07:47 -0500108 const char* name = color_type_name(fDst[i].colorType());
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000109 SkScalar textWidth = paint.measureText(name, strlen(name));
110 SkScalar x = (width - textWidth) / SkScalar(2);
111 SkScalar y = paint.getFontSpacing() / SkScalar(2);
Cary Clark2a475ea2017-04-28 15:35:12 -0400112 canvas->drawString(name, x, y, paint);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000113
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.orgef843cd2011-12-02 19:11:17 +0000124private:
125 typedef GM INHERITED;
126};
127
128//////////////////////////////////////////////////////////////////////////////
129
130static GM* MyFactory(void*) { return new BitmapCopyGM; }
131static GMRegistry reg(MyFactory);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000132}