Flutter Engine
The Flutter Engine
overdrawcanvas.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2019 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"
12#include "include/core/SkFont.h"
18#include "include/core/SkRect.h"
24
25#define WIDTH 500
26#define HEIGHT 500
27
28static const SkColor kOverdrawColors[6] = {
29 0x00000000, 0x5fff0000, 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000,
30};
31
32
33DEF_SIMPLE_GM_BG(overdraw_canvas, canvas, WIDTH, HEIGHT, SK_ColorWHITE) {
34 // Set up the overdraw canvas.
36 sk_sp<SkSurface> offscreen = SkSurfaces::Raster(offscreenInfo);
38 auto c = offscreen->getCanvas();
39
40 SkOverdrawCanvas overdrawCanvas(c);
41
42 overdrawCanvas.drawRect(SkRect::MakeLTRB(10, 10, 200, 200), SkPaint());
43 overdrawCanvas.drawRect(SkRect::MakeLTRB(20, 20, 190, 190), SkPaint());
44 overdrawCanvas.drawRect(SkRect::MakeLTRB(30, 30, 180, 180), SkPaint());
45 overdrawCanvas.drawRect(SkRect::MakeLTRB(40, 40, 170, 170), SkPaint());
46 overdrawCanvas.drawRect(SkRect::MakeLTRB(50, 50, 160, 160), SkPaint());
47 overdrawCanvas.drawRect(SkRect::MakeLTRB(60, 60, 150, 150), SkPaint());
48
49 char text[] = "Ae_p";
50 overdrawCanvas.drawSimpleText(text, 4, SkTextEncoding::kUTF8, 300, 300, font, SkPaint());
51
53
54 // Draw overdraw colors to the canvas. The color filter will convert counts to colors.
57 canvas->drawImage(counts.get(), 0.0f, 0.0f, SkSamplingOptions(), &paint);
58 canvas->drawString("This is some text:", 180, 300, font, SkPaint());
59}
60
63 auto canvas = SkOverdrawCanvas(surface->getCanvas());
64
66 paint.setColor(SK_ColorWHITE);
67
69
70 for (int n = 1; n <= 20; n++) {
71 const float x = 10.0f;
72 const float y = n * 20.0f;
73
74 for (int i = 0; i < n * 10; i++) {
75 const char* text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
76 if (useCTM) {
77 canvas.save();
78 canvas.translate(x, y);
79 canvas.drawString(text, 0, 0, font, paint);
80 canvas.restore();
81 } else {
82 canvas.drawString(text, x, y, font, paint);
83 }
84 }
85 }
86 return surface->makeImageSnapshot();
87}
88
89// This GM tests the underlying problem from skbug.com/13732. Text drawn through an overdraw
90// canvas would have the CTM applied twice. If everything is working, both images generated should
91// look identical. When the bug was present, the second image would have the lines "double spaced",
92// because the translations were applied twice.
93DEF_SIMPLE_GM_BG(overdraw_text_xform, canvas, 512, 512, SK_ColorBLACK) {
94 SkPaint imgPaint;
95 imgPaint.setColor(SK_ColorWHITE);
96 canvas->drawImage(overdraw_text_grid(false), 0, 0, SkFilterMode::kNearest, &imgPaint);
97 canvas->drawImage(overdraw_text_grid( true), 256, 0, SkFilterMode::kNearest, &imgPaint);
98}
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
@ kUTF8
uses bytes to represent UTF-8 or ASCII
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.cpp:2413
Definition: SkFont.h:35
static sk_sp< SkColorFilter > MakeWithSkColors(const SkColor[kNumColors])
void setColor(SkColor color)
Definition: SkPaint.cpp:119
SkCanvas * getCanvas()
Definition: SkSurface.cpp:82
sk_sp< SkImage > makeImageSnapshot()
Definition: SkSurface.cpp:90
const Paint & paint
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
std::u16string text
double y
double x
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
SkFont DefaultPortableFont()
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 counts
Definition: switches.h:239
font
Font Metadata and Metrics.
#define WIDTH
DEF_SIMPLE_GM_BG(overdraw_canvas, canvas, WIDTH, HEIGHT, SK_ColorWHITE)
static const SkColor kOverdrawColors[6]
static sk_sp< SkImage > overdraw_text_grid(bool useCTM)
#define HEIGHT
static SkImageInfo MakeA8(int width, int height)
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646