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 
8 #include "gm.h"
9 #include "sk_tool_utils.h"
10 #include "SkFont.h"
11 
12 namespace skiagm {
13 
color_type_name(SkColorType colorType)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";
27         case kRGBA_F32_SkColorType:     return "F32";
28     }
29     return "";
30 }
31 
32 constexpr SkColorType gColorTypes[] = {
33     kRGB_565_SkColorType,
34     kARGB_4444_SkColorType,
35     kN32_SkColorType,
36 };
37 
38 #define NUM_CONFIGS SK_ARRAY_COUNT(gColorTypes)
39 
draw_checks(SkCanvas * canvas,int width,int height)40 static void draw_checks(SkCanvas* canvas, int width, int height) {
41     SkPaint paint;
42     paint.setColor(SK_ColorRED);
43     canvas->drawRect(SkRect::MakeIWH(width/2, height/2), paint);
44     paint.setColor(SK_ColorGREEN);
45     canvas->drawRect({ SkIntToScalar(width/2), 0, SkIntToScalar(width), SkIntToScalar(height/2) },
46                      paint);
47     paint.setColor(SK_ColorBLUE);
48     canvas->drawRect({ 0, SkIntToScalar(height/2), SkIntToScalar(width/2), SkIntToScalar(height) },
49                      paint);
50     paint.setColor(SK_ColorYELLOW);
51     canvas->drawRect({ SkIntToScalar(width/2), SkIntToScalar(height/2), SkIntToScalar(width),
52                      SkIntToScalar(height) }, paint);
53 }
54 
55 class BitmapCopyGM : public GM {
56 public:
57     SkBitmap    fDst[NUM_CONFIGS];
58 
BitmapCopyGM()59     BitmapCopyGM() {
60         this->setBGColor(0xFFDDDDDD);
61     }
62 
63 protected:
onShortName()64     virtual SkString onShortName() {
65         return SkString("bitmapcopy");
66     }
67 
onISize()68     virtual SkISize onISize() {
69         return SkISize::Make(540, 330);
70     }
71 
onDraw(SkCanvas * canvas)72     virtual void onDraw(SkCanvas* canvas) {
73         SkPaint paint;
74         SkScalar horizMargin = 10;
75         SkScalar vertMargin = 10;
76 
77         SkBitmap src;
78         src.allocN32Pixels(40, 40, kOpaque_SkAlphaType);
79         SkCanvas canvasTmp(src);
80 
81         draw_checks(&canvasTmp, 40, 40);
82 
83         for (unsigned i = 0; i < NUM_CONFIGS; ++i) {
84             sk_tool_utils::copy_to(&fDst[i], gColorTypes[i], src);
85         }
86 
87         canvas->clear(0xFFDDDDDD);
88         paint.setAntiAlias(true);
89 
90         SkFont font(sk_tool_utils::create_portable_typeface());
91 
92         SkScalar width = SkIntToScalar(40);
93         SkScalar height = SkIntToScalar(40);
94         if (font.getSpacing() > height) {
95             height = font.getSpacing();
96         }
97         for (unsigned i = 0; i < NUM_CONFIGS; i++) {
98             const char* name = color_type_name(src.colorType());
99             SkScalar textWidth = font.measureText(name, strlen(name), kUTF8_SkTextEncoding);
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
111             const char* name = color_type_name(fDst[i].colorType());
112             SkScalar textWidth = font.measureText(name, strlen(name), kUTF8_SkTextEncoding);
113             SkScalar x = (width - textWidth) / SkScalar(2);
114             SkScalar y = font.getSpacing() / SkScalar(2);
115             canvas->drawSimpleText(name, strlen(name), kUTF8_SkTextEncoding, x, y, font, paint);
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 
127 private:
128     typedef GM INHERITED;
129 };
130 
131 //////////////////////////////////////////////////////////////////////////////
132 
133 DEF_GM( return new BitmapCopyGM; )
134 }
135