1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2011 Google Inc.
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/SkTypes.h"
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "include/core/SkExecutor.h"
11cb93a386Sopenharmony_ci#include "include/gpu/GrDirectContext.h"
12cb93a386Sopenharmony_ci#include "src/gpu/GrCaps.h"
13cb93a386Sopenharmony_ci#include "src/gpu/GrDirectContextPriv.h"
14cb93a386Sopenharmony_ci#include "tests/Test.h"
15cb93a386Sopenharmony_ci#include "tools/gpu/GrContextFactory.h"
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_ciusing namespace sk_gpu_test;
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_ciDEF_GPUTEST(GrContextFactory_abandon, reporter, options) {
20cb93a386Sopenharmony_ci    for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
21cb93a386Sopenharmony_ci        GrContextFactory testFactory(options);
22cb93a386Sopenharmony_ci        GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i;
23cb93a386Sopenharmony_ci        ContextInfo info1 = testFactory.getContextInfo(ctxType);
24cb93a386Sopenharmony_ci        if (!info1.directContext()) {
25cb93a386Sopenharmony_ci            continue;
26cb93a386Sopenharmony_ci        }
27cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, info1.testContext());
28cb93a386Sopenharmony_ci         // Ref for comparison. The API does not explicitly say that this stays alive.
29cb93a386Sopenharmony_ci        info1.directContext()->ref();
30cb93a386Sopenharmony_ci        testFactory.abandonContexts();
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_ci        // Test that we get different context after abandon.
33cb93a386Sopenharmony_ci        ContextInfo info2 = testFactory.getContextInfo(ctxType);
34cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, info2.directContext());
35cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, info2.testContext());
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, info1.directContext() != info2.directContext());
38cb93a386Sopenharmony_ci        // The GL context should also change, but it also could get the same address.
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_ci        info1.directContext()->unref();
41cb93a386Sopenharmony_ci    }
42cb93a386Sopenharmony_ci}
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ciDEF_GPUTEST(GrContextFactory_sharedContexts, reporter, options) {
45cb93a386Sopenharmony_ci    for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
46cb93a386Sopenharmony_ci        GrContextFactory testFactory(options);
47cb93a386Sopenharmony_ci        GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
48cb93a386Sopenharmony_ci        ContextInfo info1 = testFactory.getContextInfo(ctxType);
49cb93a386Sopenharmony_ci        if (!info1.directContext()) {
50cb93a386Sopenharmony_ci            continue;
51cb93a386Sopenharmony_ci        }
52cb93a386Sopenharmony_ci
53cb93a386Sopenharmony_ci        // Ref for passing in. The API does not explicitly say that this stays alive.
54cb93a386Sopenharmony_ci        info1.directContext()->ref();
55cb93a386Sopenharmony_ci        testFactory.abandonContexts();
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ci        // Test that creating a context in a share group with an abandoned context fails.
58cb93a386Sopenharmony_ci        ContextInfo info2 = testFactory.getSharedContextInfo(info1.directContext());
59cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, !info2.directContext());
60cb93a386Sopenharmony_ci        info1.directContext()->unref();
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_ci        // Create a new base context
63cb93a386Sopenharmony_ci        ContextInfo info3 = testFactory.getContextInfo(ctxType);
64cb93a386Sopenharmony_ci
65cb93a386Sopenharmony_ci        // Creating a context in a share group may fail, but should never crash.
66cb93a386Sopenharmony_ci        ContextInfo info4 = testFactory.getSharedContextInfo(info3.directContext());
67cb93a386Sopenharmony_ci        if (!info4.directContext()) {
68cb93a386Sopenharmony_ci            continue;
69cb93a386Sopenharmony_ci        }
70cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, info3.directContext() != info4.directContext());
71cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, info3.testContext() != info4.testContext());
72cb93a386Sopenharmony_ci
73cb93a386Sopenharmony_ci        // Passing a different index should create a new (unique) context.
74cb93a386Sopenharmony_ci        ContextInfo info5 = testFactory.getSharedContextInfo(info3.directContext(), 1);
75cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, info5.directContext());
76cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, info5.testContext());
77cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, info5.directContext() != info4.directContext());
78cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, info5.testContext() != info4.testContext());
79cb93a386Sopenharmony_ci    }
80cb93a386Sopenharmony_ci}
81cb93a386Sopenharmony_ci
82cb93a386Sopenharmony_ciDEF_GPUTEST(GrContextFactory_executorAndTaskGroup, reporter, options) {
83cb93a386Sopenharmony_ci    for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
84cb93a386Sopenharmony_ci        // Verify that contexts have a task group iff we supply an executor with context options
85cb93a386Sopenharmony_ci        GrContextOptions contextOptions = options;
86cb93a386Sopenharmony_ci        contextOptions.fExecutor = nullptr;
87cb93a386Sopenharmony_ci        GrContextFactory serialFactory(contextOptions);
88cb93a386Sopenharmony_ci
89cb93a386Sopenharmony_ci        std::unique_ptr<SkExecutor> threadPool = SkExecutor::MakeFIFOThreadPool(1);
90cb93a386Sopenharmony_ci        contextOptions.fExecutor = threadPool.get();
91cb93a386Sopenharmony_ci        GrContextFactory threadedFactory(contextOptions);
92cb93a386Sopenharmony_ci
93cb93a386Sopenharmony_ci        GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
94cb93a386Sopenharmony_ci        ContextInfo serialInfo = serialFactory.getContextInfo(ctxType);
95cb93a386Sopenharmony_ci        if (auto serialContext = serialInfo.directContext()) {
96cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, nullptr == serialContext->priv().getTaskGroup());
97cb93a386Sopenharmony_ci        }
98cb93a386Sopenharmony_ci
99cb93a386Sopenharmony_ci        ContextInfo threadedInfo = threadedFactory.getContextInfo(ctxType);
100cb93a386Sopenharmony_ci        if (auto threadedContext = threadedInfo.directContext()) {
101cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, nullptr != threadedContext->priv().getTaskGroup());
102cb93a386Sopenharmony_ci        }
103cb93a386Sopenharmony_ci    }
104cb93a386Sopenharmony_ci}
105cb93a386Sopenharmony_ci
106cb93a386Sopenharmony_ci#ifdef SK_ENABLE_DUMP_GPU
107cb93a386Sopenharmony_ciDEF_GPUTEST_FOR_ALL_CONTEXTS(GrContextDump, reporter, ctxInfo) {
108cb93a386Sopenharmony_ci    // Ensure that GrDirectContext::dump doesn't assert (which is possible, if the JSON code
109cb93a386Sopenharmony_ci    // is wrong)
110cb93a386Sopenharmony_ci    SkString result = ctxInfo.directContext()->dump();
111cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, !result.isEmpty());
112cb93a386Sopenharmony_ci}
113cb93a386Sopenharmony_ci#endif
114