xref: /aosp_15_r20/external/skia/tests/GrContextOOM.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #include "include/core/SkAlphaType.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColorType.h"
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkSurface.h"
16 #include "include/core/SkTypes.h"
17 #include "include/gpu/GpuTypes.h"
18 #include "include/gpu/ganesh/GrContextOptions.h"
19 #include "include/gpu/ganesh/GrDirectContext.h"
20 #include "include/gpu/ganesh/GrTypes.h"
21 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
22 #include "tests/CtsEnforcement.h"
23 #include "tests/Test.h"
24 #include "tools/gpu/ContextType.h"
25 
DEF_GANESH_TEST(GrContext_oomed,reporter,originalOptions,CtsEnforcement::kApiLevel_T)26 DEF_GANESH_TEST(GrContext_oomed, reporter, originalOptions, CtsEnforcement::kApiLevel_T) {
27     GrContextOptions options = originalOptions;
28     options.fRandomGLOOM = true;
29     options.fSkipGLErrorChecks = GrContextOptions::Enable::kNo;
30     sk_gpu_test::GrContextFactory factory(options);
31     for (int ct = 0; ct < skgpu::kContextTypeCount; ++ct) {
32         auto contextType = static_cast<skgpu::ContextType>(ct);
33         auto context = factory.get(contextType);
34         if (!context) {
35             continue;
36         }
37         if (context->backend() != GrBackendApi::kOpenGL) {
38             continue;
39         }
40         auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
41         for (int run = 0; run < 20; ++run) {
42             bool oomed = false;
43             for (int i = 0; i < 500; ++i) {
44                 // Make sure we're actually making a significant number of GL calls and not just
45                 // issuing a small number calls by reusing scratch resources created in a previous
46                 // iteration.
47                 context->freeGpuResources();
48                 auto surf =
49                         SkSurfaces::RenderTarget(context, skgpu::Budgeted::kYes, info, 1, nullptr);
50                 SkPaint paint;
51                 surf->getCanvas()->drawRect(SkRect::MakeLTRB(100, 100, 2000, 2000), paint);
52                 context->flushAndSubmit(surf.get(), GrSyncCpu::kNo);
53                 if ((oomed = context->oomed())) {
54                     REPORTER_ASSERT(reporter, !context->oomed(), "oomed() wasn't cleared");
55                     break;
56                 }
57             }
58             if (!oomed) {
59                 ERRORF(reporter, "Did not OOM on %dth run.", run);
60             }
61         }
62     }
63 }
64