Flutter Engine
The Flutter Engine
Macros | Functions
all_bitmap_configs.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorSpace.h"
#include "include/core/SkFont.h"
#include "include/core/SkFontStyle.h"
#include "include/core/SkFontTypes.h"
#include "include/core/SkImage.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPixmap.h"
#include "include/core/SkRect.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkTypeface.h"
#include "include/core/SkTypes.h"
#include "tools/DecodeUtils.h"
#include "tools/Resources.h"
#include "tools/ToolUtils.h"
#include "tools/fonts/FontToolUtils.h"
#include <string.h>
#include <initializer_list>

Go to the source code of this file.

Macros

#define SCALE   128
 

Functions

static SkBitmap copy_bitmap (const SkBitmap &src, SkColorType colorType)
 
static SkBitmap make_bitmap (SkColorType ct)
 
static void draw_center_letter (char c, const SkFont &font, SkColor color, SkScalar x, SkScalar y, SkCanvas *canvas)
 
static void color_wheel_native (SkCanvas *canvas)
 
template<typename T >
int find (T *array, int N, T item)
 
static void draw (SkCanvas *canvas, const SkPaint &p, const SkFont &font, const SkBitmap &src, SkColorType colorType, const char text[])
 
 DEF_SIMPLE_GM (all_bitmap_configs, canvas, SCALE, 6 *SCALE)
 
sk_sp< SkImagemake_not_native32_color_wheel ()
 
 DEF_SIMPLE_GM (not_native32_bitmap_config, canvas, SCALE, SCALE)
 
static uint32_t make_pixel (int x, int y, SkAlphaType alphaType)
 
static void make_color_test_bitmap_variant (SkColorType colorType, SkAlphaType alphaType, sk_sp< SkColorSpace > colorSpace, SkBitmap *bm)
 
 DEF_SIMPLE_GM (all_variants_8888, canvas, 4 *SCALE+30, 2 *SCALE+10)
 

Macro Definition Documentation

◆ SCALE

#define SCALE   128

Definition at line 47 of file all_bitmap_configs.cpp.

Function Documentation

◆ color_wheel_native()

static void color_wheel_native ( SkCanvas canvas)
static

Definition at line 85 of file all_bitmap_configs.cpp.

85 {
86 SkAutoCanvasRestore autoCanvasRestore(canvas, true);
87 canvas->translate(0.5f * SCALE, 0.5f * SCALE);
88 canvas->drawCircle(0.0f, 0.0f, SCALE * 0.5f, SkPaint(SkColors::kWhite));
89
90 const double sqrt_3_over_2 = 0.8660254037844387;
91 const SkScalar Z = 0.0f;
92 const SkScalar D = 0.3f * SkIntToScalar(SCALE);
93 const SkScalar X = SkDoubleToScalar(D * sqrt_3_over_2);
94 const SkScalar Y = D * SK_ScalarHalf;
95
99 font.setSize(0.28125f * SCALE);
100 draw_center_letter('K', font, SK_ColorBLACK, Z, Z, canvas);
101 draw_center_letter('R', font, SK_ColorRED, Z, D, canvas);
102 draw_center_letter('G', font, SK_ColorGREEN, -X, -Y, canvas);
103 draw_center_letter('B', font, SK_ColorBLUE, X, -Y, canvas);
104 draw_center_letter('C', font, SK_ColorCYAN, Z, -D, canvas);
105 draw_center_letter('M', font, SK_ColorMAGENTA, X, Y, canvas);
106 draw_center_letter('Y', font, SK_ColorYELLOW, -X, Y, canvas);
107}
constexpr SkColor SK_ColorYELLOW
Definition: SkColor.h:139
constexpr SkColor SK_ColorMAGENTA
Definition: SkColor.h:147
constexpr SkColor SK_ColorCYAN
Definition: SkColor.h:143
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
#define SK_ScalarHalf
Definition: SkScalar.h:19
#define SkDoubleToScalar(x)
Definition: SkScalar.h:64
#define SkIntToScalar(x)
Definition: SkScalar.h:57
static const SkScalar Y
Definition: StrokeBench.cpp:55
static const SkScalar X
Definition: StrokeBench.cpp:54
#define SCALE
static void draw_center_letter(char c, const SkFont &font, SkColor color, SkScalar x, SkScalar y, SkCanvas *canvas)
#define Z
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
Definition: SkCanvas.cpp:2707
static constexpr SkFontStyle Bold()
Definition: SkFontStyle.h:69
Definition: SkFont.h:35
@ kAlias
no transparent pixels on glyph edges
float SkScalar
Definition: extension.cpp:12
constexpr SkColor4f kWhite
Definition: SkColor.h:439
sk_sp< SkTypeface > CreatePortableTypeface(const char *name, SkFontStyle style)
font
Font Metadata and Metrics.

◆ copy_bitmap()

static SkBitmap copy_bitmap ( const SkBitmap src,
SkColorType  colorType 
)
static

Definition at line 33 of file all_bitmap_configs.cpp.

33 {
34 const SkBitmap* srcPtr = &src;
35 SkBitmap tmp(src);
37 tmp.setAlphaType(kOpaque_SkAlphaType);
38 srcPtr = &tmp;
39 }
40
43 copy.setImmutable();
44 return copy;
45}
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition: SkColorType.h:22
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
static void copy(void *dst, const uint8_t *src, int width, int bpp, int deltaSrc, int offset, const SkPMColor ctable[])
Definition: SkSwizzler.cpp:31
bool copy_to(SkBitmap *dst, SkColorType dstColorType, const SkBitmap &src)
Definition: ToolUtils.cpp:394
Definition: copy.py:1

◆ DEF_SIMPLE_GM() [1/3]

DEF_SIMPLE_GM ( all_bitmap_configs  ,
canvas  ,
SCALE  ,
6 *  SCALE 
)

Definition at line 130 of file all_bitmap_configs.cpp.

130 {
131 SkAutoCanvasRestore autoCanvasRestore(canvas, true);
133 p.setAntiAlias(true);
134
136
138
140 if (ToolUtils::GetResourceAsBitmap("images/color_wheel.png", &bitmap)) {
141 bitmap.setImmutable();
142 draw(canvas, p, font, bitmap, kN32_SkColorType, "Native 32");
143
144 canvas->translate(0.0f, SkIntToScalar(SCALE));
146 p.setColor(SK_ColorRED);
147 draw(canvas, p, font, copy565, kRGB_565_SkColorType, "RGB 565");
148 p.setColor(SK_ColorBLACK);
149
150 canvas->translate(0.0f, SkIntToScalar(SCALE));
152 draw(canvas, p, font, copy4444, kARGB_4444_SkColorType, "ARGB 4444");
153
154 canvas->translate(0.0f, SkIntToScalar(SCALE));
156 draw(canvas, p, font, copyF16, kRGBA_F16_SkColorType, "RGBA F16");
157
158 } else {
159 canvas->translate(0.0f, SkIntToScalar(3 * SCALE));
160 }
161
162 canvas->translate(0.0f, SkIntToScalar(SCALE));
164 draw(canvas, p, font, bitmapA8, kAlpha_8_SkColorType, "Alpha 8");
165
166 p.setColor(SK_ColorRED);
167 canvas->translate(0.0f, SkIntToScalar(SCALE));
169 draw(canvas, p, font, bitmapG8, kGray_8_SkColorType, "Gray 8");
170}
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition: SkColorType.h:23
@ 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
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition: SkColorType.h:35
constexpr SkColor SK_ColorLTGRAY
Definition: SkColor.h:118
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
static SkBitmap copy_bitmap(const SkBitmap &src, SkColorType colorType)
static SkBitmap make_bitmap(SkColorType ct)
static void draw(SkCanvas *canvas, const SkPaint &p, const SkFont &font, const SkBitmap &src, SkColorType colorType, const char text[])
constexpr SkColor4f kBlack
Definition: SkColor.h:435
void draw_checkerboard(SkCanvas *canvas, SkColor c1, SkColor c2, int size)
Definition: ToolUtils.cpp:174
SkFont DefaultPortableFont()
bool GetResourceAsBitmap(const char *resource, SkBitmap *dst)
Definition: DecodeUtils.h:21
Definition: bitmap.py:1

◆ DEF_SIMPLE_GM() [2/3]

DEF_SIMPLE_GM ( all_variants_8888  ,
canvas  ,
4 *SCALE 30,
2 *SCALE 10 
)

Definition at line 240 of file all_bitmap_configs.cpp.

240 {
242
243 sk_sp<SkColorSpace> colorSpaces[] {
245 nullptr,
246 };
247 for (const sk_sp<SkColorSpace>& colorSpace : colorSpaces) {
248 canvas->save();
249 for (auto alphaType : {kPremul_SkAlphaType, kUnpremul_SkAlphaType}) {
250 canvas->save();
252 SkBitmap bm;
253 make_color_test_bitmap_variant(colorType, alphaType, colorSpace, &bm);
254 canvas->drawImage(bm.asImage(), 0.0f, 0.0f);
255 canvas->translate(SCALE + 10, 0.0f);
256 }
257 canvas->restore();
258 canvas->translate(0.0f, SCALE + 10);
259 }
260 canvas->restore();
261 canvas->translate(2 * (SCALE + 10), 0.0f);
262 }
263}
kUnpremul_SkAlphaType
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
static void make_color_test_bitmap_variant(SkColorType colorType, SkAlphaType alphaType, sk_sp< SkColorSpace > colorSpace, SkBitmap *bm)
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
static sk_sp< SkColorSpace > MakeSRGB()

◆ DEF_SIMPLE_GM() [3/3]

DEF_SIMPLE_GM ( not_native32_bitmap_config  ,
canvas  ,
SCALE  ,
SCALE   
)

Definition at line 189 of file all_bitmap_configs.cpp.

189 {
191 SkASSERT(notN32image);
193 canvas->drawImage(notN32image.get(), 0.0f, 0.0f);
194}
#define SkASSERT(cond)
Definition: SkAssert.h:116
sk_sp< SkImage > make_not_native32_color_wheel()

◆ draw()

static void draw ( SkCanvas canvas,
const SkPaint p,
const SkFont font,
const SkBitmap src,
SkColorType  colorType,
const char  text[] 
)
static

Definition at line 119 of file all_bitmap_configs.cpp.

124 {
125 SkASSERT(src.colorType() == colorType);
126 canvas->drawImage(src.asImage(), 0.0f, 0.0f);
127 canvas->drawSimpleText(text, strlen(text), SkTextEncoding::kUTF8, 0.0f, 12.0f, font, p);
128}
@ kUTF8
uses bytes to represent UTF-8 or ASCII
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.cpp:2413
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
std::u16string text

◆ draw_center_letter()

static void draw_center_letter ( char  c,
const SkFont font,
SkColor  color,
SkScalar  x,
SkScalar  y,
SkCanvas canvas 
)
static

Definition at line 76 of file all_bitmap_configs.cpp.

77 {
79 font.measureText(&c, 1, SkTextEncoding::kUTF8, &bounds);
81 x - bounds.centerX(), y - bounds.centerY(),
82 font, SkPaint(SkColor4f::FromColor(color)));
83}
DlColor color
double y
double x
Optional< SkRect > bounds
Definition: SkRecords.h:189

◆ find()

template<typename T >
int find ( T array,
int  N,
T  item 
)

Definition at line 110 of file all_bitmap_configs.cpp.

110 {
111 for (int i = 0; i < N; ++i) {
112 if (array[i] == item) {
113 return i;
114 }
115 }
116 return -1;
117}
#define N
Definition: beziers.cpp:19

◆ make_bitmap()

static SkBitmap make_bitmap ( SkColorType  ct)
static

Definition at line 50 of file all_bitmap_configs.cpp.

50 {
51 SkBitmap bm;
52 switch (ct) {
55 break;
57 bm.allocPixels(
59 break;
60 default:
61 SkASSERT(false);
62 return bm;
63 }
64 uint8_t spectrum[256];
65 for (int y = 0; y < 256; ++y) {
66 spectrum[y] = y;
67 }
68 for (int y = 0; y < 128; ++y) {
69 // Shift over one byte each scanline.
70 memcpy(bm.getAddr8(0, y), &spectrum[y], 128);
71 }
72 bm.setImmutable();
73 return bm;
74}
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
void setImmutable()
Definition: SkBitmap.cpp:400
uint8_t * getAddr8(int x, int y) const
Definition: SkBitmap.h:1270
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static SkImageInfo MakeA8(int width, int height)

◆ make_color_test_bitmap_variant()

static void make_color_test_bitmap_variant ( SkColorType  colorType,
SkAlphaType  alphaType,
sk_sp< SkColorSpace colorSpace,
SkBitmap bm 
)
static

Definition at line 222 of file all_bitmap_configs.cpp.

227{
229 SkASSERT(alphaType == kPremul_SkAlphaType || alphaType == kUnpremul_SkAlphaType);
230 bm->allocPixels(
231 SkImageInfo::Make(SCALE, SCALE, colorType, alphaType, colorSpace));
232 const SkPixmap& pm = bm->pixmap();
233 for (int y = 0; y < pm.height(); y++) {
234 for (int x = 0; x < pm.width(); x++) {
235 *pm.writable_addr32(x, y) = make_pixel(x, y, alphaType);
236 }
237 }
238}
static uint32_t make_pixel(int x, int y, SkAlphaType alphaType)
const SkPixmap & pixmap() const
Definition: SkBitmap.h:133
int width() const
Definition: SkPixmap.h:160
uint32_t * writable_addr32(int x, int y) const
Definition: SkPixmap.h:537
int height() const
Definition: SkPixmap.h:166

◆ make_not_native32_color_wheel()

sk_sp< SkImage > make_not_native32_color_wheel ( )

Definition at line 172 of file all_bitmap_configs.cpp.

172 {
173 SkBitmap n32bitmap, notN32bitmap;
174 n32bitmap.allocN32Pixels(SCALE, SCALE);
176 SkCanvas n32canvas(n32bitmap);
177 color_wheel_native(&n32canvas);
178 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
180 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
182 #endif
183 static_assert(ct != kN32_SkColorType, "BRGA!=RGBA");
184 SkAssertResult(ToolUtils::copy_to(&notN32bitmap, ct, n32bitmap));
185 SkASSERT(notN32bitmap.colorType() == ct);
186 return notN32bitmap.asImage();
187}
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
SkColorType
Definition: SkColorType.h:19
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
static void color_wheel_native(SkCanvas *canvas)
SkColorType colorType() const
Definition: SkBitmap.h:160
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
void eraseColor(SkColor4f) const
Definition: SkBitmap.cpp:442

◆ make_pixel()

static uint32_t make_pixel ( int  x,
int  y,
SkAlphaType  alphaType 
)
static

Definition at line 196 of file all_bitmap_configs.cpp.

196 {
197 SkASSERT(x >= 0 && x < SCALE);
198 SkASSERT(y >= 0 && y < SCALE);
199
200 SkScalar R = SCALE / 2.0f;
201
202 uint32_t alpha = 0x00;
203
204 if ((x - R) * (x - R) + (y - R) * (y - R) < R * R) {
205 alpha = 0xFF;
206 }
207
208 uint32_t component;
209 switch (alphaType) {
211 component = alpha;
212 break;
214 component = 0xFF;
215 break;
216 default:
217 SK_ABORT("Should not get here - invalid alpha type");
218 }
219 return alpha << 24 | component;
220}
#define SK_ABORT(message,...)
Definition: SkAssert.h:70
#define R(r)