• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 kRGBA_F16_SkColorType:           return "F16";
49         case kRGBA_F32_SkColorType:           return "F32";
50         case kR8G8_unorm_SkColorType:         return "R8G8_unorm";
51         case kA16_unorm_SkColorType:          return "A16_unorm";
52         case kR16G16_unorm_SkColorType:       return "R16G16_unorm";
53         case kA16_float_SkColorType:          return "A16_float";
54         case kR16G16_float_SkColorType:       return "R16G16_float";
55         case kR16G16B16A16_unorm_SkColorType: return "R16G16B16A16_unorm";
56         case kSRGBA_8888_SkColorType:         return "SRGBA_8888";
57         case kR8_unorm_SkColorType:           return "R8_unorm";
58     }
59     return "";
60 }
61 
62 constexpr SkColorType gColorTypes[] = {
63     kRGB_565_SkColorType,
64     kARGB_4444_SkColorType,
65     kN32_SkColorType,
66 };
67 
68 #define NUM_CONFIGS std::size(gColorTypes)
69 
draw_checks(SkCanvas * canvas,int width,int height)70 static void draw_checks(SkCanvas* canvas, int width, int height) {
71     SkPaint paint;
72     paint.setColor(SK_ColorRED);
73     canvas->drawRect(SkRect::MakeIWH(width/2, height/2), paint);
74     paint.setColor(SK_ColorGREEN);
75     canvas->drawRect({ SkIntToScalar(width/2), 0, SkIntToScalar(width), SkIntToScalar(height/2) },
76                      paint);
77     paint.setColor(SK_ColorBLUE);
78     canvas->drawRect({ 0, SkIntToScalar(height/2), SkIntToScalar(width/2), SkIntToScalar(height) },
79                      paint);
80     paint.setColor(SK_ColorYELLOW);
81     canvas->drawRect({ SkIntToScalar(width/2), SkIntToScalar(height/2), SkIntToScalar(width),
82                      SkIntToScalar(height) }, paint);
83 }
84 
85 class BitmapCopyGM : public skiagm::GM {
86     SkBitmap    fDst[NUM_CONFIGS];
87 
onOnceBeforeDraw()88     void onOnceBeforeDraw() override { this->setBGColor(0xFFDDDDDD); }
89 
getName() const90     SkString getName() const override { return SkString("bitmapcopy"); }
91 
getISize()92     SkISize getISize() override { return {540, 330}; }
93 
onDraw(SkCanvas * canvas)94     void onDraw(SkCanvas* canvas) override {
95         SkPaint paint;
96         SkScalar horizMargin = 10;
97         SkScalar vertMargin = 10;
98 
99         SkBitmap src;
100         src.allocN32Pixels(40, 40, kOpaque_SkAlphaType);
101         SkCanvas canvasTmp(src);
102 
103         draw_checks(&canvasTmp, 40, 40);
104 
105         for (unsigned i = 0; i < NUM_CONFIGS; ++i) {
106             ToolUtils::copy_to(&fDst[i], gColorTypes[i], src);
107         }
108 
109         canvas->clear(0xFFDDDDDD);
110         paint.setAntiAlias(true);
111 
112         SkFont font = ToolUtils::DefaultPortableFont();
113 
114         SkScalar width = SkIntToScalar(40);
115         SkScalar height = SkIntToScalar(40);
116         if (font.getSpacing() > height) {
117             height = font.getSpacing();
118         }
119         for (unsigned i = 0; i < NUM_CONFIGS; i++) {
120             const char* name = color_type_name(src.colorType());
121             SkScalar textWidth = font.measureText(name, strlen(name), SkTextEncoding::kUTF8);
122             if (textWidth > width) {
123                 width = textWidth;
124             }
125         }
126         SkScalar horizOffset = width + horizMargin;
127         SkScalar vertOffset = height + vertMargin;
128         canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
129 
130         for (unsigned i = 0; i < NUM_CONFIGS; i++) {
131             canvas->save();
132             // Draw destination config name
133             const char* name = color_type_name(fDst[i].colorType());
134             SkScalar textWidth = font.measureText(name, strlen(name), SkTextEncoding::kUTF8);
135             SkScalar x = (width - textWidth) / SkScalar(2);
136             SkScalar y = font.getSpacing() / SkScalar(2);
137             canvas->drawSimpleText(name, strlen(name), SkTextEncoding::kUTF8, x, y, font, paint);
138 
139             // Draw destination bitmap
140             canvas->translate(0, vertOffset);
141             x = (width - 40) / SkScalar(2);
142             canvas->drawImage(fDst[i].asImage(), x, 0, SkSamplingOptions(), &paint);
143             canvas->restore();
144 
145             canvas->translate(horizOffset, 0);
146         }
147     }
148 };
149 }  // namespace
150 
151 //////////////////////////////////////////////////////////////////////////////
152 
153 DEF_GM( return new BitmapCopyGM; )
154