Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
bitmapcopy.cpp
Go to the documentation of this file.
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"
13#include "include/core/SkFont.h"
17#include "include/core/SkRect.h"
19#include "include/core/SkSize.h"
23#include "tools/ToolUtils.h"
25
26#include <string.h>
27
28namespace {
29
30static 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
62constexpr SkColorType gColorTypes[] = {
65 kN32_SkColorType,
66};
67
68#define NUM_CONFIGS std::size(gColorTypes)
69
70static void draw_checks(SkCanvas* canvas, int width, int height) {
72 paint.setColor(SK_ColorRED);
74 paint.setColor(SK_ColorGREEN);
76 paint);
77 paint.setColor(SK_ColorBLUE);
79 paint);
80 paint.setColor(SK_ColorYELLOW);
83}
84
85class BitmapCopyGM : public skiagm::GM {
87
88 void onOnceBeforeDraw() override { this->setBGColor(0xFFDDDDDD); }
89
90 SkString getName() const override { return SkString("bitmapcopy"); }
91
92 SkISize getISize() override { return {540, 330}; }
93
94 void onDraw(SkCanvas* canvas) override {
96 SkScalar horizMargin = 10;
97 SkScalar vertMargin = 10;
98
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
113
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
153DEF_GM( return new BitmapCopyGM; )
static const char * color_type_name(SkColorType colorType)
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
SkColorType
Definition SkColorType.h:19
@ kR16G16B16A16_unorm_SkColorType
pixel with a little endian uint16_t for red, green, blue
Definition SkColorType.h:50
@ kRGBA_10x6_SkColorType
pixel with 10 used bits (most significant) followed by 6 unused
Definition SkColorType.h:33
@ kR8_unorm_SkColorType
Definition SkColorType.h:54
@ kBGR_101010x_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word
Definition SkColorType.h:30
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition SkColorType.h:23
@ kR8G8_unorm_SkColorType
pixel with a uint8_t for red and green
Definition SkColorType.h:43
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition SkColorType.h:26
@ kA16_unorm_SkColorType
pixel with a little endian uint16_t for alpha
Definition SkColorType.h:48
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition SkColorType.h:38
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition SkColorType.h:21
@ kRGB_101010x_SkColorType
pixel with 10 bits each for red, green, blue; in 32-bit word
Definition SkColorType.h:29
@ kSRGBA_8888_SkColorType
Definition SkColorType.h:53
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition SkColorType.h:35
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition SkColorType.h:22
@ kBGRA_10101010_XR_SkColorType
pixel with 10 bits each for blue, green, red, alpha; in 64-bit word, extended range
Definition SkColorType.h:32
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
@ kRGB_888x_SkColorType
pixel with 8 bits each for red, green, blue; in 32-bit word
Definition SkColorType.h:25
@ kBGRA_1010102_SkColorType
10 bits for blue, green, red; 2 bits for alpha; in 32-bit word
Definition SkColorType.h:28
@ kA16_float_SkColorType
pixel with a half float for alpha
Definition SkColorType.h:45
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
Definition SkColorType.h:40
@ kRGBA_1010102_SkColorType
10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
Definition SkColorType.h:27
@ kBGR_101010x_XR_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word, extended range
Definition SkColorType.h:31
@ kR16G16_unorm_SkColorType
pixel with a little endian uint16_t for red and green
Definition SkColorType.h:49
@ kRGBA_F16Norm_SkColorType
pixel with half floats in [0,1] for red, green, blue, alpha;
Definition SkColorType.h:36
@ kUnknown_SkColorType
uninitialized
Definition SkColorType.h:20
@ kR16G16_float_SkColorType
pixel with a half float for red and green
Definition SkColorType.h:46
constexpr SkColor SK_ColorYELLOW
Definition SkColor.h:139
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
@ kUTF8
uses bytes to represent UTF-8 or ASCII
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define NUM_CONFIGS
void drawRect(const SkRect &rect, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
void clear(SkColor color)
Definition SkCanvas.h:1199
int save()
Definition SkCanvas.cpp:451
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
virtual SkISize getISize()=0
virtual void onOnceBeforeDraw()
Definition gm.cpp:167
void setBGColor(SkColor)
Definition gm.cpp:159
virtual SkString getName() const =0
virtual DrawResult onDraw(SkCanvas *, SkString *errorMsg)
Definition gm.cpp:139
const Paint & paint
float SkScalar
Definition extension.cpp:12
const char * name
Definition fuchsia.cc:50
#define DEF_GM(CODE)
Definition gm.h:40
double y
double x
SkFont DefaultPortableFont()
bool copy_to(SkBitmap *dst, SkColorType dstColorType, const SkBitmap &src)
font
Font Metadata and Metrics.
int32_t height
int32_t width
static SkRect MakeIWH(int w, int h)
Definition SkRect.h:623
constexpr SkColorType gColorTypes[]
Definition tilemodes.cpp:62