• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPath.h"
16 #include "include/core/SkPathTypes.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkSurface.h"
20 #include "include/core/SkTypes.h"
21 #include "include/gpu/GpuTypes.h"
22 #include "include/gpu/GrDirectContext.h"
23 #include "src/core/SkAutoPixmapStorage.h"
24 #include "tests/CtsEnforcement.h"
25 #include "tests/Test.h"
26 
27 #include <initializer_list>
28 
29 struct GrContextOptions;
30 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(crbug_1271431,reporter,context_info,CtsEnforcement::kApiLevel_T)31 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(crbug_1271431,
32                                        reporter,
33                                        context_info,
34                                        CtsEnforcement::kApiLevel_T) {
35     GrDirectContext* dc = context_info.directContext();
36 
37     // Make sure we don't get recycled render targets that already have stencil attachments.
38     dc->freeGpuResources();
39 
40     SkImageInfo ii = SkImageInfo::Make({100, 100},
41                                        kRGBA_8888_SkColorType,
42                                        kPremul_SkAlphaType,
43                                        nullptr);
44     sk_sp<SkSurface> surfs[2]{
45             SkSurface::MakeRenderTarget(dc, skgpu::Budgeted::kYes, ii, 1, nullptr),
46             SkSurface::MakeRenderTarget(dc, skgpu::Budgeted::kYes, ii, 1, nullptr)};
47 
48     // Make sure the surfaces' proxies are instantiated without stencil. Creating textures lazily
49     // can invalidate the current tracked FBO since FBO state must be modified to during
50     // GrGLRenderTarget creation.
51     for (int i = 0; i < 2; ++i) {
52         surfs[i]->getCanvas()->clear(SK_ColorWHITE);
53         surfs[i]->flushAndSubmit();
54     }
55 
56     auto drawWithStencilClip = [&](SkSurface& surf, SkColor color) {
57         SkPath clip;
58         clip.addCircle(50, 50, 50);
59         clip.addCircle(50, 50, 10, SkPathDirection::kCCW);
60         SkPaint paint;
61         paint.setColor(color);
62         surf.getCanvas()->clipPath(clip, false);
63         surf.getCanvas()->drawRect(SkRect::MakeLTRB(0,0, 100, 100), paint);
64     };
65 
66     // Use surfs[0] create to create a cached stencil buffer that is also sized for surfs[1].
67     drawWithStencilClip(*surfs[0], SK_ColorRED);
68     surfs[0]->flushAndSubmit();
69 
70     // Make sure surf[1]'s FBO is bound but without using draws that would attach stencil.
71     surfs[1]->getCanvas()->clear(SK_ColorGREEN);
72     surfs[1]->flushAndSubmit();
73 
74     // Now use stencil for clipping. We should now have the following properties:
75     // 1) surf[1]'s FBO is already bound
76     // 2) surf[1] doesn't have a stencil buffer
77     // 3) There is an appropriately sized stencil buffer in the cache (used with surf[0]). This is
78     //    important because creating a new stencil buffer will invalidate the bound FBO tracking.
79     // The bug was that because the correct FBO was already bound we would not rebind and would
80     // skip the lazy stencil attachment in GrGLRenderTarget. This would cause the clip to fail.
81     drawWithStencilClip(*surfs[1], SK_ColorBLUE);
82 
83     // Check that four pixels that should have been clipped out of the blue draw are green.
84     SkAutoPixmapStorage rb;
85     rb.alloc(surfs[1]->imageInfo().makeWH(1, 1));
86     for (int x : {5, 95}) {
87         for (int y : {5, 95}) {
88             if (!surfs[1]->readPixels(rb, x, y)) {
89                 ERRORF(reporter, "readback failed");
90                 return;
91             }
92             if (*rb.addr32() != SK_ColorGREEN) {
93                 ERRORF(reporter,
94                        "Expected green at (%d, %d), instead got 0x%08x.",
95                        x,
96                        y,
97                        *rb.addr32());
98                 return;
99             }
100         }
101     }
102 }
103