xref: /aosp_15_r20/external/skia/tests/DashPathEffectTestGanesh.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2023 Google LLC
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 "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkPathEffect.h"
11 #include "include/effects/SkDashPathEffect.h"
12 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
13 #include "tests/Test.h"
14 
15 // Draws a dashed circle with circumference 100, with an on-interval of 90 and an off-interval of
16 // 10, offset into the intervals by 25. This should draw a dash clockwise from 3:00 ending around
17 // 11:00, then the start of a second dash from 12:00 back to 3:00. In https://crbug.com/1495670, the
18 // math for intervals ending very near 2*pi introduced floating point error that prevented drawing
19 // the second dash.
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(DashPathEffectTest_2PiInterval,reporter,contextInfo,CtsEnforcement::kApiLevel_V)20 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(DashPathEffectTest_2PiInterval,
21                                        reporter,
22                                        contextInfo,
23                                        CtsEnforcement::kApiLevel_V) {
24     const float r = 50.0f / SK_ScalarPI;
25     const float centerX = ceilf(0.5f * r);
26     const float centerY = ceilf(r);
27     const float dashWidth = 10.0f;
28 
29     SkImageInfo ii = SkImageInfo::Make(SkISize::Make(16, 16),
30                                        SkColorType::kRGBA_8888_SkColorType,
31                                        SkAlphaType::kPremul_SkAlphaType);
32     GrDirectContext* context = contextInfo.directContext();
33     sk_sp<SkSurface> surface = SkSurfaces::RenderTarget(context, skgpu::Budgeted::kYes, ii);
34     SkCanvas* canvas = surface->getCanvas();
35 
36     SkPaint paint;
37     paint.setAntiAlias(true);
38     paint.setColor(SK_ColorRED);
39     paint.setStyle(SkPaint::kStroke_Style);
40     paint.setStrokeWidth(dashWidth);
41 
42     constexpr float intervals[2] = {90.0f, 10.0f};
43     paint.setPathEffect(SkDashPathEffect::Make(intervals, 2, 25.0f));
44     canvas->drawCircle(centerX, centerY, r, paint);
45 
46     // Check that we drew the second dash, which starts at the top of the circle.
47     SkBitmap bitmap;
48     SkPixmap pixmap;
49     bitmap.allocPixels(surface->imageInfo());
50     SkAssertResult(bitmap.peekPixels(&pixmap));
51     if (!surface->readPixels(pixmap, 0, 0)) {
52         ERRORF(reporter, "readPixels failed");
53         return;
54     }
55     SkColor topColor = pixmap.getColor(centerX + 1.0f, centerY - r);
56     REPORTER_ASSERT(reporter, topColor == SK_ColorRED);
57 }
58