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