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