1 /*
2  * Copyright 2020 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 // We want to make sure that if we collapse src-over down to src when blending, that batching still
9 // works correctly with a draw that explicitly requests src.
10 
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkShader.h"
13 #include "include/core/SkSurface.h"
14 #include "include/gpu/GrDirectContext.h"
15 #include "tests/Test.h"
16 #include "tools/gpu/GrContextFactory.h"
17 
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SrcSrcOverBatchTest,reporter,ctxInfo)18 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SrcSrcOverBatchTest, reporter, ctxInfo) {
19     auto ctx = ctxInfo.directContext();
20 
21     static const int kSize = 8;
22     const SkImageInfo ii = SkImageInfo::Make(kSize, kSize, kRGBA_8888_SkColorType,
23                                              kPremul_SkAlphaType);
24 
25     sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo,
26                                                          ii, 0, kTopLeft_GrSurfaceOrigin,
27                                                          nullptr));
28 
29     auto canvas = surface->getCanvas();
30 
31     SkPaint paint;
32     // Setting a shader so that we actually build a processor set and don't fallback to all
33     // defaults.
34     paint.setShader(SkShaders::Color(SK_ColorRED));
35 
36     SkIRect rect = SkIRect::MakeWH(2, 2);
37 
38     canvas->drawIRect(rect, paint);
39 
40     // Now draw a rect with src blend mode. If we collapsed the previous draw to src blend mode (a
41     // setting on caps plus not having any coverage), then we expect this second draw to try to
42     // batch with it. This test is a success if we don't hit any asserts, specifically making sure
43     // that both things we decided can be batched together claim to have the same value for
44     // CompatibleWithCoverageAsAlpha.
45     canvas->translate(3, 0);
46     paint.setBlendMode(SkBlendMode::kSrc);
47     canvas->drawIRect(rect, paint);
48 }
49