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"
8 #include "sk_tool_utils.h"
9
10 namespace skiagm {
11
color_type_name(SkColorType colorType)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";
25 }
26 return "";
27 }
28
29 constexpr SkColorType gColorTypes[] = {
30 kRGB_565_SkColorType,
31 kARGB_4444_SkColorType,
32 kN32_SkColorType,
33 };
34
35 #define NUM_CONFIGS SK_ARRAY_COUNT(gColorTypes)
36
draw_checks(SkCanvas * canvas,int width,int height)37 static void draw_checks(SkCanvas* canvas, int width, int height) {
38 SkPaint paint;
39 paint.setColor(SK_ColorRED);
40 canvas->drawRect(SkRect::MakeIWH(width/2, height/2), paint);
41 paint.setColor(SK_ColorGREEN);
42 canvas->drawRect({ SkIntToScalar(width/2), 0, SkIntToScalar(width), SkIntToScalar(height/2) },
43 paint);
44 paint.setColor(SK_ColorBLUE);
45 canvas->drawRect({ 0, SkIntToScalar(height/2), SkIntToScalar(width/2), SkIntToScalar(height) },
46 paint);
47 paint.setColor(SK_ColorYELLOW);
48 canvas->drawRect({ SkIntToScalar(width/2), SkIntToScalar(height/2), SkIntToScalar(width),
49 SkIntToScalar(height) }, paint);
50 }
51
52 class BitmapCopyGM : public GM {
53 public:
54 SkBitmap fDst[NUM_CONFIGS];
55
BitmapCopyGM()56 BitmapCopyGM() {
57 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
58 }
59
60 protected:
onShortName()61 virtual SkString onShortName() {
62 return SkString("bitmapcopy");
63 }
64
onISize()65 virtual SkISize onISize() {
66 return SkISize::Make(540, 330);
67 }
68
onDraw(SkCanvas * canvas)69 virtual void onDraw(SkCanvas* canvas) {
70 SkPaint paint;
71 SkScalar horizMargin = 10;
72 SkScalar vertMargin = 10;
73
74 SkBitmap src;
75 src.allocN32Pixels(40, 40, kOpaque_SkAlphaType);
76 SkCanvas canvasTmp(src);
77
78 draw_checks(&canvasTmp, 40, 40);
79
80 for (unsigned i = 0; i < NUM_CONFIGS; ++i) {
81 sk_tool_utils::copy_to(&fDst[i], gColorTypes[i], src);
82 }
83
84 canvas->clear(sk_tool_utils::color_to_565(0xFFDDDDDD));
85 paint.setAntiAlias(true);
86 sk_tool_utils::set_portable_typeface(&paint);
87
88 SkScalar width = SkIntToScalar(40);
89 SkScalar height = SkIntToScalar(40);
90 if (paint.getFontSpacing() > height) {
91 height = paint.getFontSpacing();
92 }
93 for (unsigned i = 0; i < NUM_CONFIGS; i++) {
94 const char* name = color_type_name(src.colorType());
95 SkScalar textWidth = paint.measureText(name, strlen(name));
96 if (textWidth > width) {
97 width = textWidth;
98 }
99 }
100 SkScalar horizOffset = width + horizMargin;
101 SkScalar vertOffset = height + vertMargin;
102 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
103
104 for (unsigned i = 0; i < NUM_CONFIGS; i++) {
105 canvas->save();
106 // Draw destination config name
107 const char* name = color_type_name(fDst[i].colorType());
108 SkScalar textWidth = paint.measureText(name, strlen(name));
109 SkScalar x = (width - textWidth) / SkScalar(2);
110 SkScalar y = paint.getFontSpacing() / SkScalar(2);
111 canvas->drawString(name, x, y, paint);
112
113 // Draw destination bitmap
114 canvas->translate(0, vertOffset);
115 x = (width - 40) / SkScalar(2);
116 canvas->drawBitmap(fDst[i], x, 0, &paint);
117 canvas->restore();
118
119 canvas->translate(horizOffset, 0);
120 }
121 }
122
123 private:
124 typedef GM INHERITED;
125 };
126
127 //////////////////////////////////////////////////////////////////////////////
128
MyFactory(void *)129 static GM* MyFactory(void*) { return new BitmapCopyGM; }
130 static GMRegistry reg(MyFactory);
131 }
132