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