TestContext.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2016 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 "tools/gpu/TestContext.h"
  8. #include "tools/gpu/GpuTimer.h"
  9. #include "include/gpu/GrContext.h"
  10. namespace sk_gpu_test {
  11. TestContext::TestContext()
  12. : fFenceSync(nullptr)
  13. , fGpuTimer(nullptr)
  14. , fCurrentFenceIdx(0) {
  15. memset(fFrameFences, 0, sizeof(fFrameFences));
  16. }
  17. TestContext::~TestContext() {
  18. // Subclass should call teardown.
  19. #ifdef SK_DEBUG
  20. for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) {
  21. SkASSERT(0 == fFrameFences[i]);
  22. }
  23. #endif
  24. SkASSERT(!fFenceSync);
  25. SkASSERT(!fGpuTimer);
  26. }
  27. sk_sp<GrContext> TestContext::makeGrContext(const GrContextOptions&) {
  28. return nullptr;
  29. }
  30. void TestContext::makeCurrent() const { this->onPlatformMakeCurrent(); }
  31. SkScopeExit TestContext::makeCurrentAndAutoRestore() const {
  32. auto asr = SkScopeExit(this->onPlatformGetAutoContextRestore());
  33. this->makeCurrent();
  34. return asr;
  35. }
  36. void TestContext::swapBuffers() { this->onPlatformSwapBuffers(); }
  37. void TestContext::waitOnSyncOrSwap() {
  38. if (!fFenceSync) {
  39. // Fallback on the platform SwapBuffers method for synchronization. This may have no effect.
  40. this->swapBuffers();
  41. return;
  42. }
  43. this->submit();
  44. if (fFrameFences[fCurrentFenceIdx]) {
  45. if (!fFenceSync->waitFence(fFrameFences[fCurrentFenceIdx])) {
  46. SkDebugf("WARNING: Wait failed for fence sync. Timings might not be accurate.\n");
  47. }
  48. fFenceSync->deleteFence(fFrameFences[fCurrentFenceIdx]);
  49. }
  50. fFrameFences[fCurrentFenceIdx] = fFenceSync->insertFence();
  51. fCurrentFenceIdx = (fCurrentFenceIdx + 1) % SK_ARRAY_COUNT(fFrameFences);
  52. }
  53. void TestContext::testAbandon() {
  54. if (fFenceSync) {
  55. memset(fFrameFences, 0, sizeof(fFrameFences));
  56. }
  57. }
  58. void TestContext::teardown() {
  59. if (fFenceSync) {
  60. for (size_t i = 0; i < SK_ARRAY_COUNT(fFrameFences); i++) {
  61. if (fFrameFences[i]) {
  62. fFenceSync->deleteFence(fFrameFences[i]);
  63. fFrameFences[i] = 0;
  64. }
  65. }
  66. fFenceSync.reset();
  67. }
  68. fGpuTimer.reset();
  69. }
  70. }