Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
LazyStencilAttachmentTest.cpp
Go to the documentation of this file.
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
15#include "include/core/SkPath.h"
17#include "include/core/SkRect.h"
23#include "include/gpu/GrTypes.h"
27#include "tests/Test.h"
28
29#include <initializer_list>
30
31struct GrContextOptions;
32
35 context_info,
37 GrDirectContext* dc = context_info.directContext();
38
39 // Make sure we don't get recycled render targets that already have stencil attachments.
40 dc->freeGpuResources();
41
42 SkImageInfo ii = SkImageInfo::Make({100, 100},
45 nullptr);
48
49 // Make sure the surfaces' proxies are instantiated without stencil. Creating textures lazily
50 // can invalidate the current tracked FBO since FBO state must be modified to during
51 // GrGLRenderTarget creation.
52 for (int i = 0; i < 2; ++i) {
53 surfs[i]->getCanvas()->clear(SK_ColorWHITE);
54 dc->flushAndSubmit(surfs[i].get(), GrSyncCpu::kNo);
55 }
56
57 auto drawWithStencilClip = [&](SkSurface& surf, SkColor color) {
59 clip.addCircle(50, 50, 50);
62 paint.setColor(color);
63 surf.getCanvas()->clipPath(clip, false);
64 surf.getCanvas()->drawRect(SkRect::MakeLTRB(0,0, 100, 100), paint);
65 };
66
67 // Use surfs[0] create to create a cached stencil buffer that is also sized for surfs[1].
68 drawWithStencilClip(*surfs[0], SK_ColorRED);
69 dc->flushAndSubmit(surfs[0].get(), GrSyncCpu::kNo);
70
71 // Make sure surf[1]'s FBO is bound but without using draws that would attach stencil.
72 surfs[1]->getCanvas()->clear(SK_ColorGREEN);
73 dc->flushAndSubmit(surfs[1].get(), GrSyncCpu::kNo);
74
75 // Now use stencil for clipping. We should now have the following properties:
76 // 1) surf[1]'s FBO is already bound
77 // 2) surf[1] doesn't have a stencil buffer
78 // 3) There is an appropriately sized stencil buffer in the cache (used with surf[0]). This is
79 // important because creating a new stencil buffer will invalidate the bound FBO tracking.
80 // The bug was that because the correct FBO was already bound we would not rebind and would
81 // skip the lazy stencil attachment in GrGLRenderTarget. This would cause the clip to fail.
82 drawWithStencilClip(*surfs[1], SK_ColorBLUE);
83
84 // Check that four pixels that should have been clipped out of the blue draw are green.
86 rb.alloc(surfs[1]->imageInfo().makeWH(1, 1));
87 for (int x : {5, 95}) {
88 for (int y : {5, 95}) {
89 if (!surfs[1]->readPixels(rb, x, y)) {
90 ERRORF(reporter, "readback failed");
91 return;
92 }
93 if (*rb.addr32() != SK_ColorGREEN) {
95 "Expected green at (%d, %d), instead got 0x%08x.",
96 x,
97 y,
98 *rb.addr32());
99 return;
100 }
101 }
102 }
103}
reporter
SkColor4f color
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition SkPath.cpp:3824
#define ERRORF(r,...)
Definition Test.h:293
#define DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info, ctsEnforcement)
Definition Test.h:434
void flushAndSubmit(GrSyncCpu sync=GrSyncCpu::kNo)
void alloc(const SkImageInfo &)
void drawRect(const SkRect &rect, const SkPaint &paint)
void clear(SkColor color)
Definition SkCanvas.h:1199
void clipPath(const SkPath &path, SkClipOp op, bool doAntiAlias)
SkPath & addCircle(SkScalar x, SkScalar y, SkScalar radius, SkPathDirection dir=SkPathDirection::kCW)
Definition SkPath.cpp:1149
const uint32_t * addr32() const
Definition SkPixmap.h:352
SkCanvas * getCanvas()
Definition SkSurface.cpp:82
const Paint & paint
double y
double x
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646