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