• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gpu/ganesh/SkSurfaceGanesh.h"
24 #include "include/private/gpu/ganesh/GrTypesPriv.h"
25 #include "src/gpu/ganesh/GrDirectContextPriv.h"
26 #include "tests/CtsEnforcement.h"
27 #include "tests/Test.h"
28 
29 #include <cstdint>
30 
31 struct GrContextOptions;
32 
check_pixels(skiatest::Reporter * reporter,const SkBitmap & bitmap,GrSurfaceOrigin origin)33 static void check_pixels(skiatest::Reporter* reporter, const SkBitmap& bitmap,
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;
67                 ERRORF(reporter,
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 
run_test(skiatest::Reporter * reporter,GrDirectContext * context,GrSurfaceOrigin origin)75 static void run_test(skiatest::Reporter* reporter,
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,
84                                                    kRGBA_8888_SkColorType,
85                                                    Mipmapped::kNo,
86                                                    GrRenderable::kYes,
87                                                    isProtected);
88     REPORTER_ASSERT(reporter, beTexture.isValid());
89     if (!beTexture.isValid()) {
90         return;
91     }
92 
93     auto surface = SkSurfaces::WrapBackendTexture(
94             context, beTexture, origin, 0, kRGBA_8888_SkColorType, nullptr, nullptr);
95     REPORTER_ASSERT(reporter, surface);
96     if (!surface) {
97         return;
98     }
99 
100     SkCanvas* canvas = surface->getCanvas();
101 
102     canvas->clear(SK_ColorBLUE);
103 
104     SkPaint paint;
105     paint.setColor(SK_ColorRED);
106     canvas->drawLine({ 0,4 }, { 8,4 }, paint);
107 
108     SkRect bounds = SkRect::MakeWH(8, 8);
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 
115     SkBitmap bitmap;
116     bitmap.allocPixels(SkImageInfo::Make(8, 8, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
117     surface->readPixels(bitmap, 0, 0);
118 
119     check_pixels(reporter, bitmap, origin);
120 
121     context->deleteBackendTexture(beTexture);
122 }
123 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SaveLayerOrigin,reporter,context_info,CtsEnforcement::kApiLevel_T)124 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(SaveLayerOrigin,
125                                        reporter,
126                                        context_info,
127                                        CtsEnforcement::kApiLevel_T) {
128     GrDirectContext* context = context_info.directContext();
129     run_test(reporter, context, kBottomLeft_GrSurfaceOrigin);
130     run_test(reporter, context, kTopLeft_GrSurfaceOrigin);
131 }
132