Flutter Engine
The Flutter Engine
SaveLayerOriginTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2022 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
16#include "include/core/SkRect.h"
22#include "include/gpu/GrTypes.h"
27#include "tests/Test.h"
28
29#include <cstdint>
30
31struct GrContextOptions;
32
34 GrSurfaceOrigin origin) {
35 const uint32_t* canvasPixels = static_cast<const uint32_t*>(bitmap.getPixels());
36
37 bool failureFound = false;
38 bool foundNonBlue = false;
39
40 for (int cy = 0; cy < 8 && !failureFound; ++cy) {
41 int cx = 4; // Just need to check one column;
42 SkPMColor canvasPixel = canvasPixels[cy * 8 + cx];
43 // We don't know which way the GPU will snap so the non-blue line could either be at row
44 // 3 or 4 since we drew the line at a y value of 4. We check that one of those two values
45 // is green and all the rest are blue. The key thing is that we should not get any red
46 // values since the green line in the saveLayer should snap the same way and overwrite the
47 // red line.
48 if (cy == 3) {
49 if (canvasPixel != 0xFFFF0000 && canvasPixel != 0xFF00FF00) {
50 failureFound = true;
51 ERRORF(reporter, "Wrong color at %d, %d. Got 0x%08x when we expected Blue or Green."
52 " Origin is: %s",
53 cx, cy, canvasPixel, GrSurfaceOriginToStr(origin));
54 }
55 if (canvasPixel != 0XFFFF0000) {
56 foundNonBlue = true;
57 }
58 } else {
59 SkPMColor expectedPixel;
60 if (cy == 4 && !foundNonBlue) {
61 expectedPixel = 0xFF00FF00; // Green
62 } else {
63 expectedPixel = 0xFFFF0000; // Blue
64 }
65 if (canvasPixel != expectedPixel) {
66 failureFound = true;
68 "Wrong color at %d, %d. Got 0x%08x when we expected 0x%08x. Origin is: %s",
69 cx, cy, canvasPixel, expectedPixel, GrSurfaceOriginToStr(origin));
70 }
71 }
72 }
73}
74
76 GrDirectContext* context,
77 GrSurfaceOrigin origin) {
78 using namespace skgpu;
79
80 Protected isProtected = Protected(context->priv().caps()->supportsProtectedContent());
81
82 auto beTexture = context->createBackendTexture(8,
83 8,
87 isProtected);
88 REPORTER_ASSERT(reporter, beTexture.isValid());
89 if (!beTexture.isValid()) {
90 return;
91 }
92
94 context, beTexture, origin, 0, kRGBA_8888_SkColorType, nullptr, nullptr);
96 if (!surface) {
97 return;
98 }
99
100 SkCanvas* canvas = surface->getCanvas();
101
102 canvas->clear(SK_ColorBLUE);
103
105 paint.setColor(SK_ColorRED);
106 canvas->drawLine({ 0,4 }, { 8,4 }, paint);
107
109 SkPaint layerPaint;
110 canvas->saveLayer(bounds, &paint);
111 paint.setColor(SK_ColorGREEN);
112 canvas->drawLine({ 0,4 }, { 8,4 }, paint);
113 canvas->restore();
114
117 surface->readPixels(bitmap, 0, 0);
118
119 check_pixels(reporter, bitmap, origin);
120
121 context->deleteBackendTexture(beTexture);
122}
123
125 reporter,
126 context_info,
128 GrDirectContext* context = context_info.directContext();
131}
reporter
Definition: FontMgrTest.cpp:39
GrSurfaceOrigin
Definition: GrTypes.h:147
@ kBottomLeft_GrSurfaceOrigin
Definition: GrTypes.h:149
@ kTopLeft_GrSurfaceOrigin
Definition: GrTypes.h:148
skgpu::Protected Protected
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SaveLayerOrigin, reporter, context_info, CtsEnforcement::kApiLevel_T)
static void check_pixels(skiatest::Reporter *reporter, const SkBitmap &bitmap, GrSurfaceOrigin origin)
static void run_test(skiatest::Reporter *reporter, GrDirectContext *context, GrSurfaceOrigin origin)
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
uint32_t SkPMColor
Definition: SkColor.h:205
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
#define ERRORF(r,...)
Definition: Test.h:293
const GrCaps * caps() const
bool supportsProtectedContent() const
Definition: GrCaps.h:422
void deleteBackendTexture(const GrBackendTexture &)
GrBackendTexture createBackendTexture(int width, int height, const GrBackendFormat &, skgpu::Mipmapped, GrRenderable, GrProtected=GrProtected::kNo, std::string_view label={})
GrDirectContextPriv priv()
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
void restore()
Definition: SkCanvas.cpp:461
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
Definition: SkCanvas.cpp:2700
void clear(SkColor color)
Definition: SkCanvas.h:1199
const Paint & paint
Definition: color_source.cc:38
VkSurfaceKHR surface
Definition: main.cc:49
Optional< SkRect > bounds
Definition: SkRecords.h:189
SK_API sk_sp< SkSurface > WrapBackendTexture(GrRecordingContext *context, const GrBackendTexture &backendTexture, GrSurfaceOrigin origin, int sampleCnt, SkColorType colorType, sk_sp< SkColorSpace > colorSpace, const SkSurfaceProps *surfaceProps, TextureReleaseProc textureReleaseProc=nullptr, ReleaseContext releaseContext=nullptr)
Definition: bitmap.py:1
Definition: GpuTools.h:21
Protected
Definition: GpuTypes.h:61
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609