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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkFont.h"
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"
22 #include "tools/ToolUtils.h"
23 
24 #include <string.h>
25 
26 namespace {
27 
color_type_name(SkColorType colorType)28 static const char* color_type_name(SkColorType colorType) {
29     switch (colorType) {
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";
39         case kBGRA_1010102_SkColorType:       return "bgra1010102";
40         case kBGR_101010x_SkColorType:        return "bgr101010x";
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";
51     }
52     return "";
53 }
54 
55 constexpr SkColorType gColorTypes[] = {
56     kRGB_565_SkColorType,
57     kARGB_4444_SkColorType,
58     kN32_SkColorType,
59 };
60 
61 #define NUM_CONFIGS SK_ARRAY_COUNT(gColorTypes)
62 
draw_checks(SkCanvas * canvas,int width,int height)63 static void draw_checks(SkCanvas* canvas, int width, int height) {
64     SkPaint paint;
65     paint.setColor(SK_ColorRED);
66     canvas->drawRect(SkRect::MakeIWH(width/2, height/2), paint);
67     paint.setColor(SK_ColorGREEN);
68     canvas->drawRect({ SkIntToScalar(width/2), 0, SkIntToScalar(width), SkIntToScalar(height/2) },
69                      paint);
70     paint.setColor(SK_ColorBLUE);
71     canvas->drawRect({ 0, SkIntToScalar(height/2), SkIntToScalar(width/2), SkIntToScalar(height) },
72                      paint);
73     paint.setColor(SK_ColorYELLOW);
74     canvas->drawRect({ SkIntToScalar(width/2), SkIntToScalar(height/2), SkIntToScalar(width),
75                      SkIntToScalar(height) }, paint);
76 }
77 
78 class BitmapCopyGM : public skiagm::GM {
79     SkBitmap    fDst[NUM_CONFIGS];
80 
onOnceBeforeDraw()81     void onOnceBeforeDraw() override { this->setBGColor(0xFFDDDDDD); }
82 
onShortName()83     SkString onShortName() override { return SkString("bitmapcopy"); }
84 
onISize()85     SkISize onISize() override { return {540, 330}; }
86 
onDraw(SkCanvas * canvas)87     void onDraw(SkCanvas* canvas) override {
88         SkPaint paint;
89         SkScalar horizMargin = 10;
90         SkScalar vertMargin = 10;
91 
92         SkBitmap src;
93         src.allocN32Pixels(40, 40, kOpaque_SkAlphaType);
94         SkCanvas canvasTmp(src);
95 
96         draw_checks(&canvasTmp, 40, 40);
97 
98         for (unsigned i = 0; i < NUM_CONFIGS; ++i) {
99             ToolUtils::copy_to(&fDst[i], gColorTypes[i], src);
100         }
101 
102         canvas->clear(0xFFDDDDDD);
103         paint.setAntiAlias(true);
104 
105         SkFont font(ToolUtils::create_portable_typeface());
106 
107         SkScalar width = SkIntToScalar(40);
108         SkScalar height = SkIntToScalar(40);
109         if (font.getSpacing() > height) {
110             height = font.getSpacing();
111         }
112         for (unsigned i = 0; i < NUM_CONFIGS; i++) {
113             const char* name = color_type_name(src.colorType());
114             SkScalar textWidth = font.measureText(name, strlen(name), SkTextEncoding::kUTF8);
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
126             const char* name = color_type_name(fDst[i].colorType());
127             SkScalar textWidth = font.measureText(name, strlen(name), SkTextEncoding::kUTF8);
128             SkScalar x = (width - textWidth) / SkScalar(2);
129             SkScalar y = font.getSpacing() / SkScalar(2);
130             canvas->drawSimpleText(name, strlen(name), SkTextEncoding::kUTF8, x, y, font, paint);
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     }
141 };
142 }  // namespace
143 
144 //////////////////////////////////////////////////////////////////////////////
145 
146 DEF_GM( return new BitmapCopyGM; )
147