GLBackendSurfaceTest.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2019 Google LLC
  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. #ifdef SK_GL
  9. #include "tests/Test.h"
  10. #include "include/core/SkImage.h"
  11. #include "include/core/SkSurface.h"
  12. #include "include/gpu/GrBackendSurface.h"
  13. #include "include/gpu/GrTexture.h"
  14. #include "include/gpu/gl/GrGLTypes.h"
  15. #include "include/private/GrGLTypesPriv.h"
  16. #include "src/gpu/GrContextPriv.h"
  17. #include "src/gpu/GrTextureProxy.h"
  18. #include "src/gpu/gl/GrGLCaps.h"
  19. #include "src/gpu/gl/GrGLTexture.h"
  20. #include "src/image/SkImage_Base.h"
  21. static bool sampler_params_invalid(const GrGLTextureParameters& parameters) {
  22. return SkScalarIsNaN(parameters.samplerOverriddenState().fMaxLOD);
  23. }
  24. static bool nonsampler_params_invalid(const GrGLTextureParameters& parameters) {
  25. GrGLTextureParameters::NonsamplerState invalidNSState;
  26. invalidNSState.invalidate();
  27. return 0 == memcmp(&parameters.nonsamplerState(), &invalidNSState, sizeof(invalidNSState));
  28. }
  29. static bool params_invalid(const GrGLTextureParameters& parameters) {
  30. return sampler_params_invalid(parameters) && nonsampler_params_invalid(parameters);
  31. }
  32. static bool params_valid(const GrGLTextureParameters& parameters, const GrGLCaps* caps) {
  33. if (nonsampler_params_invalid(parameters)) {
  34. return false;
  35. }
  36. // We should only set the sampler parameters to valid if we don't have sampler object support.
  37. return caps->samplerObjectSupport() == sampler_params_invalid(parameters);
  38. }
  39. DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GLTextureParameters, reporter, ctxInfo) {
  40. GrContext* context = ctxInfo.grContext();
  41. GrBackendTexture backendTex = context->createBackendTexture(
  42. 1, 1, kRGBA_8888_SkColorType, GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
  43. REPORTER_ASSERT(reporter, backendTex.isValid());
  44. GrGLTextureInfo info;
  45. REPORTER_ASSERT(reporter, backendTex.getGLTextureInfo(&info));
  46. GrBackendTexture backendTexCopy = backendTex;
  47. REPORTER_ASSERT(reporter, backendTexCopy.isSameTexture(backendTex));
  48. sk_sp<SkImage> wrappedImage =
  49. SkImage::MakeFromTexture(context, backendTex, kTopLeft_GrSurfaceOrigin,
  50. kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
  51. REPORTER_ASSERT(reporter, wrappedImage);
  52. sk_sp<GrTextureProxy> texProxy = as_IB(wrappedImage)->asTextureProxyRef(context);
  53. REPORTER_ASSERT(reporter, texProxy.get());
  54. REPORTER_ASSERT(reporter, texProxy->isInstantiated());
  55. auto texture = static_cast<GrGLTexture*>(texProxy->peekTexture());
  56. REPORTER_ASSERT(reporter, texture);
  57. auto parameters = texture->parameters();
  58. REPORTER_ASSERT(reporter, parameters);
  59. GrGLTextureParameters::SamplerOverriddenState invalidSState;
  60. invalidSState.invalidate();
  61. GrGLTextureParameters::NonsamplerState invalidNSState;
  62. invalidNSState.invalidate();
  63. // After wrapping we should assume the client's texture can be in any state.
  64. REPORTER_ASSERT(reporter, params_invalid(*parameters));
  65. auto surf = SkSurface::MakeRenderTarget(
  66. context, SkBudgeted::kYes,
  67. SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1, nullptr);
  68. REPORTER_ASSERT(reporter, surf);
  69. surf->getCanvas()->drawImage(wrappedImage, 0, 0);
  70. surf->flush();
  71. auto caps = static_cast<const GrGLCaps*>(context->priv().caps());
  72. // Now the texture should be in a known state.
  73. REPORTER_ASSERT(reporter, params_valid(*parameters, caps));
  74. // Test invalidating from the GL backend texture.
  75. backendTex.glTextureParametersModified();
  76. REPORTER_ASSERT(reporter, params_invalid(*parameters));
  77. REPORTER_ASSERT(reporter, surf);
  78. surf->getCanvas()->drawImage(wrappedImage, 0, 0);
  79. surf->flush();
  80. REPORTER_ASSERT(reporter, params_valid(*parameters, caps));
  81. // Test invalidating from the copy.
  82. backendTexCopy.glTextureParametersModified();
  83. REPORTER_ASSERT(reporter, params_invalid(*parameters));
  84. // Check that we can do things like assigning the backend texture to invalid one, assign an
  85. // invalid one, assin a backend texture to inself etc. Success here is that we don't hit any of
  86. // our ref counting asserts.
  87. REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTexCopy));
  88. GrBackendTexture invalidTexture;
  89. REPORTER_ASSERT(reporter, !invalidTexture.isValid());
  90. REPORTER_ASSERT(reporter,
  91. !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
  92. backendTexCopy = invalidTexture;
  93. REPORTER_ASSERT(reporter, !backendTexCopy.isValid());
  94. REPORTER_ASSERT(reporter,
  95. !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
  96. invalidTexture = backendTex;
  97. REPORTER_ASSERT(reporter, invalidTexture.isValid());
  98. REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex));
  99. invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture);
  100. REPORTER_ASSERT(reporter, invalidTexture.isValid());
  101. REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture));
  102. wrappedImage.reset();
  103. GrFlushInfo flushInfo;
  104. flushInfo.fFlags = kSyncCpu_GrFlushFlag;
  105. context->flush(flushInfo);
  106. context->deleteBackendTexture(backendTex);
  107. }
  108. #endif