xref: /aosp_15_r20/external/skia/tests/GrOpListFlushTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2018 Google Inc.
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/SkColorType.h"
13 #include "include/core/SkImage.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/SkSamplingOptions.h"
19 #include "include/core/SkSurface.h"
20 #include "include/core/SkTypes.h"
21 #include "include/gpu/GpuTypes.h"
22 #include "include/gpu/ganesh/GrDirectContext.h"
23 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
24 #include "src/gpu/ganesh/GrDirectContextPriv.h"
25 #include "src/gpu/ganesh/GrGpu.h"
26 #include "tests/CtsEnforcement.h"
27 #include "tests/Test.h"
28 
29 #include <cstdint>
30 
31 struct GrContextOptions;
32 
check_read(skiatest::Reporter * reporter,const SkBitmap & bitmap)33 static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap) {
34     bool result = true;
35     for (int x = 0; x < 1000 && result; ++x) {
36         const uint32_t srcPixel = *bitmap.getAddr32(x, 0);
37         if (srcPixel != SK_ColorGREEN) {
38             ERRORF(reporter, "Expected color of Green, but got 0x%08x, at pixel (%d, 0).",
39                    srcPixel, x);
40             result = false;
41         }
42     }
43     return result;
44 }
45 
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(OpsTaskFlushCount,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)46 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(OpsTaskFlushCount,
47                                        reporter,
48                                        ctxInfo,
49                                        CtsEnforcement::kApiLevel_T) {
50     auto context = ctxInfo.directContext();
51     GrGpu* gpu = context->priv().getGpu();
52 
53     SkImageInfo imageInfo = SkImageInfo::Make(1000, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
54 
55     sk_sp<SkSurface> surface1 = SkSurfaces::RenderTarget(context, skgpu::Budgeted::kYes, imageInfo);
56     if (!surface1) {
57         return;
58     }
59     sk_sp<SkSurface> surface2 = SkSurfaces::RenderTarget(context, skgpu::Budgeted::kYes, imageInfo);
60     if (!surface2) {
61         return;
62     }
63 
64     SkCanvas* canvas1 = surface1->getCanvas();
65     SkCanvas* canvas2 = surface2->getCanvas();
66 
67     canvas1->clear(SK_ColorRED);
68     canvas2->clear(SK_ColorRED);
69 
70     SkRect srcRect = SkRect::MakeWH(1, 1);
71     SkRect dstRect = SkRect::MakeWH(1, 1);
72     SkPaint paint;
73     paint.setColor(SK_ColorGREEN);
74     canvas1->drawRect(dstRect, paint);
75 
76     for (int i = 0; i < 1000; ++i) {
77         srcRect.fLeft = i;
78         srcRect.fRight = srcRect.fLeft + 1;
79 
80         sk_sp<SkImage> image = surface1->makeImageSnapshot();
81         canvas2->drawImageRect(image.get(), srcRect, dstRect, SkSamplingOptions(), nullptr,
82                                SkCanvas::kStrict_SrcRectConstraint);
83         if (i != 999) {
84             dstRect.fLeft = i+1;
85             dstRect.fRight = dstRect.fLeft + 1;
86             image = surface2->makeImageSnapshot();
87             canvas1->drawImageRect(image.get(), srcRect, dstRect, SkSamplingOptions(), nullptr,
88                                    SkCanvas::kStrict_SrcRectConstraint);
89         }
90     }
91     context->flushAndSubmit();
92 
93     // In total we make 2000 oplists. Our current limit on max oplists between flushes is 100, so we
94     // should do 20 flushes while executing oplists. Additionaly we always do 1 flush at the end of
95     // executing all oplists. So in total we should see 21 flushes here.
96     REPORTER_ASSERT(reporter, gpu->stats()->numSubmitToGpus() == 21);
97 
98     SkBitmap readbackBitmap;
99     readbackBitmap.allocN32Pixels(1000, 1);
100     REPORTER_ASSERT(reporter, surface1->readPixels(readbackBitmap, 0, 0));
101     REPORTER_ASSERT(reporter, check_read(reporter, readbackBitmap));
102 
103     REPORTER_ASSERT(reporter, surface2->readPixels(readbackBitmap, 0, 0));
104     REPORTER_ASSERT(reporter, check_read(reporter, readbackBitmap));
105 }
106