1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2020 Google LLC
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "include/core/SkCanvas.h"
9cb93a386Sopenharmony_ci#include "include/core/SkImageInfo.h"
10cb93a386Sopenharmony_ci#include "include/core/SkPaint.h"
11cb93a386Sopenharmony_ci#include "include/core/SkRect.h"
12cb93a386Sopenharmony_ci#include "include/core/SkSurface.h"
13cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h"
14cb93a386Sopenharmony_ci#include "tests/Test.h"
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_ciDEF_GPUTEST(GrContext_oomed, reporter, originalOptions) {
17cb93a386Sopenharmony_ci    GrContextOptions options = originalOptions;
18cb93a386Sopenharmony_ci    options.fRandomGLOOM = true;
19cb93a386Sopenharmony_ci    options.fSkipGLErrorChecks = GrContextOptions::Enable::kNo;
20cb93a386Sopenharmony_ci    sk_gpu_test::GrContextFactory factory(options);
21cb93a386Sopenharmony_ci    for (int ct = 0; ct < sk_gpu_test::GrContextFactory::kContextTypeCnt; ++ct) {
22cb93a386Sopenharmony_ci        auto contextType = static_cast<sk_gpu_test::GrContextFactory::ContextType>(ct);
23cb93a386Sopenharmony_ci        auto context = factory.get(contextType);
24cb93a386Sopenharmony_ci        if (!context) {
25cb93a386Sopenharmony_ci            continue;
26cb93a386Sopenharmony_ci        }
27cb93a386Sopenharmony_ci        if (context->backend() != GrBackendApi::kOpenGL) {
28cb93a386Sopenharmony_ci            continue;
29cb93a386Sopenharmony_ci        }
30cb93a386Sopenharmony_ci        auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
31cb93a386Sopenharmony_ci        for (int run = 0; run < 20; ++run) {
32cb93a386Sopenharmony_ci            bool oomed = false;
33cb93a386Sopenharmony_ci            for (int i = 0; i < 500; ++i) {
34cb93a386Sopenharmony_ci                // Make sure we're actually making a significant number of GL calls and not just
35cb93a386Sopenharmony_ci                // issuing a small number calls by reusing scratch resources created in a previous
36cb93a386Sopenharmony_ci                // iteration.
37cb93a386Sopenharmony_ci                context->freeGpuResources();
38cb93a386Sopenharmony_ci                auto surf =
39cb93a386Sopenharmony_ci                        SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
40cb93a386Sopenharmony_ci                SkPaint paint;
41cb93a386Sopenharmony_ci                surf->getCanvas()->drawRect(SkRect::MakeLTRB(100, 100, 2000, 2000), paint);
42cb93a386Sopenharmony_ci                surf->flushAndSubmit();
43cb93a386Sopenharmony_ci                if ((oomed = context->oomed())) {
44cb93a386Sopenharmony_ci                    REPORTER_ASSERT(reporter, !context->oomed(), "oomed() wasn't cleared");
45cb93a386Sopenharmony_ci                    break;
46cb93a386Sopenharmony_ci                }
47cb93a386Sopenharmony_ci            }
48cb93a386Sopenharmony_ci            if (!oomed) {
49cb93a386Sopenharmony_ci                ERRORF(reporter, "Did not OOM on %dth run.", run);
50cb93a386Sopenharmony_ci            }
51cb93a386Sopenharmony_ci        }
52cb93a386Sopenharmony_ci    }
53cb93a386Sopenharmony_ci}
54