Flutter Engine
The Flutter Engine
mipmap.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2015 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"
15#include "include/core/SkRect.h"
20
24 SkCanvas* canvas = surface->getCanvas();
25 canvas->drawColor(0xFFF8F8F8);
26
28 paint.setAntiAlias(true);
29
31 for (int i = 0; i < 20; ++i) {
32 canvas->drawCircle(-4, 25, 20, paint);
33 canvas->translate(25, 0);
34 }
35 return surface->makeImageSnapshot();
36}
37
38DEF_SIMPLE_GM(mipmap, canvas, 400, 200) {
39 sk_sp<SkImage> img(make_image());//SkImage::NewFromEncoded(data));
40
41 const SkRect dst = SkRect::MakeWH(177, 15);
42
43 SkString str;
44 str.printf("scale %g %g", dst.width() / img->width(), dst.height() / img->height());
45// canvas->drawString(str, 300, 100, SkFont(nullptr, 30), paint);
46
47 const SkSamplingOptions samplings[] = {
52 };
53
54 canvas->translate(20, 20);
55 for (size_t i = 0; i < std::size(samplings); ++i) {
56 canvas->drawImageRect(img.get(), dst, samplings[i], nullptr);
57 canvas->translate(0, 20);
58 }
59 canvas->drawImage(img.get(), 20, 20);
60}
61
62///////////////////////////////////////////////////////////////////////////////////////////////////
63
64// create a circle image computed raw, so we can wrap it as a linear or srgb image
66 const int N = 100;
67 SkImageInfo info = SkImageInfo::Make(N, N, kN32_SkColorType, kPremul_SkAlphaType, cs);
68 SkBitmap bm;
69 bm.allocPixels(info);
70
71 for (int y = 0; y < N; ++y) {
72 for (int x = 0; x < N; ++x) {
73 *bm.getAddr32(x, y) = (x ^ y) & 1 ? 0xFFFFFFFF : 0xFF000000;
74 }
75 }
76 bm.setImmutable();
77 return bm.asImage();
78}
79
80static void show_mips(SkCanvas* canvas, SkImage* img) {
83
84 // Want to ensure we never draw fractional pixels, so we use an IRect
85 SkIRect dst = SkIRect::MakeWH(img->width(), img->height());
86 while (dst.width() > 5) {
87 canvas->drawImageRect(img, SkRect::Make(dst), sampling, nullptr);
88 dst.offset(dst.width() + 10, 0);
89 dst.fRight = dst.fLeft + dst.width()/2;
90 dst.fBottom = dst.fTop + dst.height()/2;
91 }
92}
93
94/*
95 * Ensure that in L32 drawing mode, both images/mips look the same as each other, and
96 * their mips are darker than the original (since the mips should ignore the gamma in L32).
97 *
98 * Ensure that in S32 drawing mode, all images/mips look the same, and look correct (i.e.
99 * the mip levels match the original in brightness).
100 */
101DEF_SIMPLE_GM(mipmap_srgb, canvas, 260, 230) {
102 sk_sp<SkImage> limg = make(nullptr);
104
105 canvas->translate(10, 10);
106 show_mips(canvas, limg.get());
107 canvas->translate(0, limg->height() + 10.0f);
108 show_mips(canvas, simg.get());
109}
110
111///////////////////////////////////////////////////////////////////////////////////////////////////
112
113// create a gradient image computed raw, so we can wrap it as a linear or srgb image
115 const int N = 100;
117 SkBitmap bm;
118 bm.allocPixels(info);
119
120 for (int y = 0; y < N; ++y) {
121 for (int x = 0; x < N; ++x) {
122 *bm.getAddr8(x, y) = static_cast<uint8_t>(255.0f * ((x + y) / (2.0f * (N - 1))));
123 }
124 }
125 bm.setImmutable();
126 return bm.asImage();
127}
128
129static void show_mips_only(SkCanvas* canvas, SkImage* img) {
132
133 // Want to ensure we never draw fractional pixels, so we use an IRect
134 SkIRect dst = SkIRect::MakeWH(img->width() / 2, img->height() / 2);
135 while (dst.width() > 5) {
136 canvas->drawImageRect(img, SkRect::Make(dst), sampling, nullptr);
137 dst.offset(dst.width() + 10, 0);
138 dst.fRight = dst.fLeft + dst.width() / 2;
139 dst.fBottom = dst.fTop + dst.height() / 2;
140 }
141}
142
143/*
144 * Ensure that in L32 drawing mode, both images/mips look the same as each other, and
145 * their mips are darker than the original (since the mips should ignore the gamma in L32).
146 *
147 * Ensure that in S32 drawing mode, all images/mips look the same, and look correct (i.e.
148 * the mip levels match the original in brightness).
149 */
150DEF_SIMPLE_GM(mipmap_gray8_srgb, canvas, 260, 230) {
151 sk_sp<SkImage> limg = make_g8_gradient(nullptr);
153
154 canvas->translate(10, 10);
155 show_mips_only(canvas, limg.get());
156 canvas->translate(0, limg->height() + 10.0f);
157 show_mips_only(canvas, simg.get());
158}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition: SkColorType.h:35
#define N
Definition: beziers.cpp:19
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
void setImmutable()
Definition: SkBitmap.cpp:400
uint8_t * getAddr8(int x, int y) const
Definition: SkBitmap.h:1270
uint32_t * getAddr32(int x, int y) const
Definition: SkBitmap.h:1260
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition: SkCanvas.h:1182
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
Definition: SkCanvas.cpp:2333
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
Definition: SkCanvas.cpp:2707
static sk_sp< SkColorSpace > MakeSRGB()
int width() const
Definition: SkImage.h:285
int height() const
Definition: SkImage.h:291
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:534
T * get() const
Definition: SkRefCnt.h:303
const Paint & paint
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
static sk_sp< SkImage > make_g8_gradient(sk_sp< SkColorSpace > cs)
Definition: mipmap.cpp:114
static sk_sp< SkImage > make(sk_sp< SkColorSpace > cs)
Definition: mipmap.cpp:65
static sk_sp< SkImage > make_image()
Definition: mipmap.cpp:21
static void show_mips(SkCanvas *canvas, SkImage *img)
Definition: mipmap.cpp:80
DEF_SIMPLE_GM(mipmap, canvas, 400, 200)
Definition: mipmap.cpp:38
static void show_mips_only(SkCanvas *canvas, SkImage *img)
Definition: mipmap.cpp:129
double y
double x
SkSamplingOptions sampling
Definition: SkRecords.h:337
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
dst
Definition: cp.py:12
SkSamplingOptions(SkFilterMode::kLinear))
static constexpr SkCubicResampler Mitchell()
Definition: SkRect.h:32
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
static SkImageInfo MakeN32Premul(int width, int height)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609