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/SkColorType.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkFontTypes.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkSize.h"
20 #include "include/core/SkString.h"
21 #include "include/core/SkTypeface.h"
22 #include "include/core/SkTypes.h"
23 #include "tools/ToolUtils.h"
24 #include "tools/fonts/FontToolUtils.h"
25
26 #include <string.h>
27
28 namespace {
29
color_type_name(SkColorType colorType)30 static const char* color_type_name(SkColorType colorType) {
31 switch (colorType) {
32 case kUnknown_SkColorType: return "unknown";
33 case kAlpha_8_SkColorType: return "A8";
34 case kRGB_565_SkColorType: return "565";
35 case kARGB_4444_SkColorType: return "4444";
36 case kRGBA_8888_SkColorType: return "8888";
37 case kRGB_888x_SkColorType: return "888x";
38 case kBGRA_8888_SkColorType: return "8888";
39 case kRGBA_1010102_SkColorType: return "1010102";
40 case kRGB_101010x_SkColorType: return "101010x";
41 case kBGRA_1010102_SkColorType: return "bgra1010102";
42 case kBGR_101010x_SkColorType: return "bgr101010x";
43 case kBGR_101010x_XR_SkColorType: return "bgr101010x_xr";
44 case kBGRA_10101010_XR_SkColorType: return "bgra10101010_xr";
45 case kRGBA_10x6_SkColorType: return "10101010";
46 case kGray_8_SkColorType: return "G8";
47 case kRGBA_F16Norm_SkColorType: return "F16Norm";
48 case kRGB_F16F16F16x_SkColorType: return "F16F16F16x";
49 case kRGBA_F16_SkColorType: return "F16";
50 case kRGBA_F32_SkColorType: return "F32";
51 case kR8G8_unorm_SkColorType: return "R8G8_unorm";
52 case kA16_unorm_SkColorType: return "A16_unorm";
53 case kR16G16_unorm_SkColorType: return "R16G16_unorm";
54 case kA16_float_SkColorType: return "A16_float";
55 case kR16G16_float_SkColorType: return "R16G16_float";
56 case kR16G16B16A16_unorm_SkColorType: return "R16G16B16A16_unorm";
57 case kSRGBA_8888_SkColorType: return "SRGBA_8888";
58 case kR8_unorm_SkColorType: return "R8_unorm";
59 }
60 return "";
61 }
62
63 constexpr SkColorType gColorTypes[] = {
64 kRGB_565_SkColorType,
65 kARGB_4444_SkColorType,
66 kN32_SkColorType,
67 };
68
69 #define NUM_CONFIGS std::size(gColorTypes)
70
draw_checks(SkCanvas * canvas,int width,int height)71 static void draw_checks(SkCanvas* canvas, int width, int height) {
72 SkPaint paint;
73 paint.setColor(SK_ColorRED);
74 canvas->drawRect(SkRect::MakeIWH(width/2, height/2), paint);
75 paint.setColor(SK_ColorGREEN);
76 canvas->drawRect({ SkIntToScalar(width/2), 0, SkIntToScalar(width), SkIntToScalar(height/2) },
77 paint);
78 paint.setColor(SK_ColorBLUE);
79 canvas->drawRect({ 0, SkIntToScalar(height/2), SkIntToScalar(width/2), SkIntToScalar(height) },
80 paint);
81 paint.setColor(SK_ColorYELLOW);
82 canvas->drawRect({ SkIntToScalar(width/2), SkIntToScalar(height/2), SkIntToScalar(width),
83 SkIntToScalar(height) }, paint);
84 }
85
86 class BitmapCopyGM : public skiagm::GM {
87 SkBitmap fDst[NUM_CONFIGS];
88
onOnceBeforeDraw()89 void onOnceBeforeDraw() override { this->setBGColor(0xFFDDDDDD); }
90
getName() const91 SkString getName() const override { return SkString("bitmapcopy"); }
92
getISize()93 SkISize getISize() override { return {540, 330}; }
94
onDraw(SkCanvas * canvas)95 void onDraw(SkCanvas* canvas) override {
96 SkPaint paint;
97 SkScalar horizMargin = 10;
98 SkScalar vertMargin = 10;
99
100 SkBitmap src;
101 src.allocN32Pixels(40, 40, kOpaque_SkAlphaType);
102 SkCanvas canvasTmp(src);
103
104 draw_checks(&canvasTmp, 40, 40);
105
106 for (unsigned i = 0; i < NUM_CONFIGS; ++i) {
107 ToolUtils::copy_to(&fDst[i], gColorTypes[i], src);
108 }
109
110 canvas->clear(0xFFDDDDDD);
111 paint.setAntiAlias(true);
112
113 SkFont font = ToolUtils::DefaultPortableFont();
114
115 SkScalar width = SkIntToScalar(40);
116 SkScalar height = SkIntToScalar(40);
117 if (font.getSpacing() > height) {
118 height = font.getSpacing();
119 }
120 for (unsigned i = 0; i < NUM_CONFIGS; i++) {
121 const char* name = color_type_name(src.colorType());
122 SkScalar textWidth = font.measureText(name, strlen(name), SkTextEncoding::kUTF8);
123 if (textWidth > width) {
124 width = textWidth;
125 }
126 }
127 SkScalar horizOffset = width + horizMargin;
128 SkScalar vertOffset = height + vertMargin;
129 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
130
131 for (unsigned i = 0; i < NUM_CONFIGS; i++) {
132 canvas->save();
133 // Draw destination config name
134 const char* name = color_type_name(fDst[i].colorType());
135 SkScalar textWidth = font.measureText(name, strlen(name), SkTextEncoding::kUTF8);
136 SkScalar x = (width - textWidth) / SkScalar(2);
137 SkScalar y = font.getSpacing() / SkScalar(2);
138 canvas->drawSimpleText(name, strlen(name), SkTextEncoding::kUTF8, x, y, font, paint);
139
140 // Draw destination bitmap
141 canvas->translate(0, vertOffset);
142 x = (width - 40) / SkScalar(2);
143 canvas->drawImage(fDst[i].asImage(), x, 0, SkSamplingOptions(), &paint);
144 canvas->restore();
145
146 canvas->translate(horizOffset, 0);
147 }
148 }
149 };
150 } // namespace
151
152 //////////////////////////////////////////////////////////////////////////////
153
154 DEF_GM( return new BitmapCopyGM; )
155