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
8 #include "include/core/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkColorType.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkSurface.h"
19 #include "include/gpu/GpuTypes.h"
20 #include "include/gpu/GrBackendSurface.h"
21 #include "include/gpu/GrDirectContext.h"
22 #include "include/gpu/GrTypes.h"
23 #include "include/private/gpu/ganesh/GrTypesPriv.h"
24 #include "tests/CtsEnforcement.h"
25 #include "tests/Test.h"
26
27 #include <cstdint>
28
29 struct GrContextOptions;
30
check_pixels(skiatest::Reporter * reporter,const SkBitmap & bitmap,GrSurfaceOrigin origin)31 static void check_pixels(skiatest::Reporter* reporter, const SkBitmap& bitmap,
32 GrSurfaceOrigin origin) {
33 const uint32_t* canvasPixels = static_cast<const uint32_t*>(bitmap.getPixels());
34
35 bool failureFound = false;
36 bool foundNonBlue = false;
37
38 for (int cy = 0; cy < 8 && !failureFound; ++cy) {
39 int cx = 4; // Just need to check one column;
40 SkPMColor canvasPixel = canvasPixels[cy * 8 + cx];
41 // We don't know which way the GPU will snap so the non-blue line could either be at row
42 // 3 or 4 since we drew the line at a y value of 4. We check that one of those two values
43 // is green and all the rest are blue. The key thing is that we should not get any red
44 // values since the green line in the saveLayer should snap the same way and overwrite the
45 // red line.
46 if (cy == 3) {
47 if (canvasPixel != 0xFFFF0000 && canvasPixel != 0xFF00FF00) {
48 failureFound = true;
49 ERRORF(reporter, "Wrong color at %d, %d. Got 0x%08x when we expected Blue or Green."
50 " Origin is: %s",
51 cx, cy, canvasPixel, GrSurfaceOriginToStr(origin));
52 }
53 if (canvasPixel != 0XFFFF0000) {
54 foundNonBlue = true;
55 }
56 } else {
57 SkPMColor expectedPixel;
58 if (cy == 4 && !foundNonBlue) {
59 expectedPixel = 0xFF00FF00; // Green
60 } else {
61 expectedPixel = 0xFFFF0000; // Blue
62 }
63 if (canvasPixel != expectedPixel) {
64 failureFound = true;
65 ERRORF(reporter,
66 "Wrong color at %d, %d. Got 0x%08x when we expected 0x%08x. Origin is: %s",
67 cx, cy, canvasPixel, expectedPixel, GrSurfaceOriginToStr(origin));
68 }
69 }
70 }
71 }
72
run_test(skiatest::Reporter * reporter,GrDirectContext * context,GrSurfaceOrigin origin)73 static void run_test(skiatest::Reporter* reporter,
74 GrDirectContext* context,
75 GrSurfaceOrigin origin) {
76 auto beTexture = context->createBackendTexture(8, 8, kRGBA_8888_SkColorType, GrMipmapped::kNo,
77 GrRenderable::kYes, GrProtected::kNo);
78 REPORTER_ASSERT(reporter, beTexture.isValid());
79 if (!beTexture.isValid()) {
80 return;
81 }
82
83 auto surface = SkSurface::MakeFromBackendTexture(context, beTexture, origin, 0,
84 kRGBA_8888_SkColorType, nullptr, nullptr);
85 REPORTER_ASSERT(reporter, surface);
86 if (!surface) {
87 return;
88 }
89
90 SkCanvas* canvas = surface->getCanvas();
91
92 canvas->clear(SK_ColorBLUE);
93
94 SkPaint paint;
95 paint.setColor(SK_ColorRED);
96 canvas->drawLine({ 0,4 }, { 8,4 }, paint);
97
98 SkRect bounds = SkRect::MakeWH(8, 8);
99 SkPaint layerPaint;
100 canvas->saveLayer(bounds, &paint);
101 paint.setColor(SK_ColorGREEN);
102 canvas->drawLine({ 0,4 }, { 8,4 }, paint);
103 canvas->restore();
104
105 SkBitmap bitmap;
106 bitmap.allocPixels(SkImageInfo::Make(8, 8, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
107 surface->readPixels(bitmap, 0, 0);
108
109 check_pixels(reporter, bitmap, origin);
110
111 context->deleteBackendTexture(beTexture);
112 }
113
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SaveLayerOrigin,reporter,context_info,CtsEnforcement::kApiLevel_T)114 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SaveLayerOrigin,
115 reporter,
116 context_info,
117 CtsEnforcement::kApiLevel_T) {
118 GrDirectContext* context = context_info.directContext();
119 run_test(reporter, context, kBottomLeft_GrSurfaceOrigin);
120 run_test(reporter, context, kTopLeft_GrSurfaceOrigin);
121 }
122