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