GrContextFactoryTest.cpp 4.5 KB

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