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