blob: 1514d67717bf121f508a167237f58016bac89204 [file] [log] [blame]
keyar@chromium.org5bdef292012-08-14 22:02:48 +00001/*
2 * Copyright 2011 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
halcanary96fcdcc2015-08-27 07:41:13 -07008#include "SkTypes.h"
9
keyar@chromium.org5bdef292012-08-14 22:02:48 +000010#if SK_SUPPORT_GPU
keyar@chromium.org5bdef292012-08-14 22:02:48 +000011
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000012#include "GrContextFactory.h"
Brian Osman51279982017-08-23 10:12:00 -040013#include "GrContextPriv.h"
kkinnunena18a8bc2015-12-03 23:04:50 -080014#include "GrCaps.h"
Brian Osman51279982017-08-23 10:12:00 -040015#include "SkExecutor.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000016#include "Test.h"
tfarina@chromium.org4ee16bf2014-01-10 22:08:27 +000017
bsalomonf2f1c172016-04-05 12:59:06 -070018using namespace sk_gpu_test;
bsalomon3724e572016-03-30 18:56:19 -070019
kkinnunen5219fd92015-12-10 06:28:13 -080020DEF_GPUTEST(GrContextFactory_NVPRContextOptionHasPathRenderingSupport, reporter, /*factory*/) {
kkinnunena18a8bc2015-12-03 23:04:50 -080021 // Test that if NVPR is requested, the context always has path rendering
22 // or the context creation fails.
23 GrContextFactory testFactory;
kkinnunen3e980c32015-12-23 01:33:00 -080024 // Test that if NVPR is possible, caps are in sync.
bsalomon85b4b532016-04-05 11:06:27 -070025 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
26 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
27 GrContext* context = testFactory.get(ctxType,
csmartdaltone812d492017-02-21 12:36:05 -070028 GrContextFactory::ContextOverrides::kRequireNVPRSupport);
kkinnunen3e980c32015-12-23 01:33:00 -080029 if (!context) {
30 continue;
31 }
kkinnunena18a8bc2015-12-03 23:04:50 -080032 REPORTER_ASSERT(
33 reporter,
34 context->caps()->shaderCaps()->pathRenderingSupport());
35 }
36}
37
csmartdaltone812d492017-02-21 12:36:05 -070038DEF_GPUTEST(GrContextFactory_NoPathRenderingIfNVPRDisabled, reporter, /*factory*/) {
39 // Test that if NVPR is explicitly disabled, the context has no path rendering support.
kkinnunena18a8bc2015-12-03 23:04:50 -080040
41 GrContextFactory testFactory;
bsalomon85b4b532016-04-05 11:06:27 -070042 for (int i = 0; i <= GrContextFactory::kLastContextType; ++i) {
43 GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType)i;
csmartdaltone812d492017-02-21 12:36:05 -070044 GrContext* context =
45 testFactory.get(ctxType, GrContextFactory::ContextOverrides::kDisableNVPR);
kkinnunena18a8bc2015-12-03 23:04:50 -080046 if (context) {
47 REPORTER_ASSERT(
48 reporter,
49 !context->caps()->shaderCaps()->pathRenderingSupport());
50 }
51 }
52}
keyar@chromium.org5bdef292012-08-14 22:02:48 +000053
brianosman61d3b082016-03-30 11:19:36 -070054DEF_GPUTEST(GrContextFactory_RequiredSRGBSupport, reporter, /*factory*/) {
55 // Test that if sRGB support is requested, the context always has that capability
56 // or the context creation fails. Also test that if the creation fails, a context
57 // created without that flag would not have had sRGB support.
58 GrContextFactory testFactory;
59 // Test that if sRGB is requested, caps are in sync.
bsalomon85b4b532016-04-05 11:06:27 -070060 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
61 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
brianosman61d3b082016-03-30 11:19:36 -070062 GrContext* context =
csmartdaltone812d492017-02-21 12:36:05 -070063 testFactory.get(ctxType, GrContextFactory::ContextOverrides::kRequireSRGBSupport);
brianosman61d3b082016-03-30 11:19:36 -070064
65 if (context) {
66 REPORTER_ASSERT(reporter, context->caps()->srgbSupport());
67 } else {
bsalomon85b4b532016-04-05 11:06:27 -070068 context = testFactory.get(ctxType);
brianosman61d3b082016-03-30 11:19:36 -070069 if (context) {
70 REPORTER_ASSERT(reporter, !context->caps()->srgbSupport());
71 }
72 }
73 }
74}
75
kkinnunen34058002016-01-06 23:49:30 -080076DEF_GPUTEST(GrContextFactory_abandon, reporter, /*factory*/) {
77 GrContextFactory testFactory;
bsalomon85b4b532016-04-05 11:06:27 -070078 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
79 GrContextFactory::ContextType ctxType = (GrContextFactory::ContextType) i;
bsalomonf2f1c172016-04-05 12:59:06 -070080 ContextInfo info1 = testFactory.getContextInfo(ctxType);
bsalomon8b7451a2016-05-11 06:33:06 -070081 if (!info1.grContext()) {
kkinnunen34058002016-01-06 23:49:30 -080082 continue;
83 }
bsalomon0fd3c812016-05-11 10:38:05 -070084 REPORTER_ASSERT(reporter, info1.testContext());
kkinnunen34058002016-01-06 23:49:30 -080085 // Ref for comparison. The API does not explicitly say that this stays alive.
bsalomon8b7451a2016-05-11 06:33:06 -070086 info1.grContext()->ref();
kkinnunen34058002016-01-06 23:49:30 -080087 testFactory.abandonContexts();
88
89 // Test that we get different context after abandon.
bsalomonf2f1c172016-04-05 12:59:06 -070090 ContextInfo info2 = testFactory.getContextInfo(ctxType);
bsalomon8b7451a2016-05-11 06:33:06 -070091 REPORTER_ASSERT(reporter, info2.grContext());
bsalomon0fd3c812016-05-11 10:38:05 -070092 REPORTER_ASSERT(reporter, info2.testContext());
93
bsalomon8b7451a2016-05-11 06:33:06 -070094 REPORTER_ASSERT(reporter, info1.grContext() != info2.grContext());
95 // The GL context should also change, but it also could get the same address.
kkinnunen34058002016-01-06 23:49:30 -080096
bsalomon8b7451a2016-05-11 06:33:06 -070097 info1.grContext()->unref();
kkinnunen34058002016-01-06 23:49:30 -080098 }
99}
100
Brian Osman60c774d2017-02-21 16:58:08 -0500101DEF_GPUTEST(GrContextFactory_sharedContexts, reporter, /*factory*/) {
102 GrContextFactory testFactory;
Brian Osman60c774d2017-02-21 16:58:08 -0500103
104 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
105 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
106 ContextInfo info1 = testFactory.getContextInfo(ctxType);
107 if (!info1.grContext()) {
108 continue;
109 }
110
111 // Ref for passing in. The API does not explicitly say that this stays alive.
112 info1.grContext()->ref();
113 testFactory.abandonContexts();
114
115 // Test that creating a context in a share group with an abandoned context fails.
Brian Osman9eac2ea2017-02-24 14:51:44 -0500116 ContextInfo info2 = testFactory.getSharedContextInfo(info1.grContext());
Brian Osman60c774d2017-02-21 16:58:08 -0500117 REPORTER_ASSERT(reporter, !info2.grContext());
118 info1.grContext()->unref();
119
120 // Create a new base context
121 ContextInfo info3 = testFactory.getContextInfo(ctxType);
Brian Osmanc746bc12017-02-28 10:05:43 -0500122 if (!info3.grContext()) {
123 // Vulkan NexusPlayer bot fails here. Sigh.
124 continue;
125 }
Brian Osman60c774d2017-02-21 16:58:08 -0500126
127 // Creating a context in a share group may fail, but should never crash.
Brian Osman9eac2ea2017-02-24 14:51:44 -0500128 ContextInfo info4 = testFactory.getSharedContextInfo(info3.grContext());
Brian Osman60c774d2017-02-21 16:58:08 -0500129 if (!info4.grContext()) {
130 continue;
131 }
132 REPORTER_ASSERT(reporter, info3.grContext() != info4.grContext());
133 REPORTER_ASSERT(reporter, info3.testContext() != info4.testContext());
134
135 // Passing a different index should create a new (unique) context.
Brian Osman9eac2ea2017-02-24 14:51:44 -0500136 ContextInfo info5 = testFactory.getSharedContextInfo(info3.grContext(), 1);
Brian Osman60c774d2017-02-21 16:58:08 -0500137 REPORTER_ASSERT(reporter, info5.grContext());
138 REPORTER_ASSERT(reporter, info5.testContext());
139 REPORTER_ASSERT(reporter, info5.grContext() != info4.grContext());
140 REPORTER_ASSERT(reporter, info5.testContext() != info4.testContext());
Brian Osman60c774d2017-02-21 16:58:08 -0500141 }
142}
143
Brian Osman51279982017-08-23 10:12:00 -0400144DEF_GPUTEST(GrContextFactory_executorAndTaskGroup, reporter, /*factory*/) {
145 // Verify that contexts have a task group iff we supply an executor with context options
146 GrContextOptions contextOptions;
147 contextOptions.fExecutor = nullptr;
148 GrContextFactory serialFactory(contextOptions);
149
Mike Klein022cfa22017-09-01 11:53:16 -0400150 std::unique_ptr<SkExecutor> threadPool = SkExecutor::MakeFIFOThreadPool(1);
Brian Osman51279982017-08-23 10:12:00 -0400151 contextOptions.fExecutor = threadPool.get();
152 GrContextFactory threadedFactory(contextOptions);
153
154 for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
155 GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
156 ContextInfo serialInfo = serialFactory.getContextInfo(ctxType);
157 if (GrContext* serialContext = serialInfo.grContext()) {
158 REPORTER_ASSERT(reporter, nullptr == serialContext->contextPriv().getTaskGroup());
159 }
160
161 ContextInfo threadedInfo = threadedFactory.getContextInfo(ctxType);
162 if (GrContext* threadedContext = threadedInfo.grContext()) {
163 REPORTER_ASSERT(reporter, nullptr != threadedContext->contextPriv().getTaskGroup());
164 }
165 }
166}
167
Brian Osman71a18892017-08-10 10:23:25 -0400168DEF_GPUTEST_FOR_ALL_CONTEXTS(GrContextDump, reporter, ctxInfo) {
169 // Ensure that GrContext::dump doesn't assert (which is possible, if the JSON code is wrong)
170 SkString result = ctxInfo.grContext()->dump();
171 REPORTER_ASSERT(reporter, !result.isEmpty());
172}
173
keyar@chromium.org5bdef292012-08-14 22:02:48 +0000174#endif