Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 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 | |
| 8 | #include "include/core/SkTypes.h" |
| 9 | |
| 10 | #ifdef SK_GL |
| 11 | |
| 12 | #include "tests/Test.h" |
| 13 | |
| 14 | #include "include/core/SkImage.h" |
| 15 | #include "include/core/SkSurface.h" |
| 16 | #include "include/gpu/GrBackendSurface.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 17 | #include "include/gpu/GrDirectContext.h" |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 18 | #include "include/gpu/gl/GrGLTypes.h" |
| 19 | #include "include/private/GrGLTypesPriv.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrDirectContextPriv.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 21 | #include "src/gpu/GrTexture.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrTextureProxy.h" |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 23 | #include "src/gpu/gl/GrGLCaps.h" |
| 24 | #include "src/gpu/gl/GrGLTexture.h" |
| 25 | #include "src/image/SkImage_Base.h" |
Brian Salomon | e666254 | 2021-02-23 10:45:39 -0500 | [diff] [blame] | 26 | #include "tools/gpu/ProxyUtils.h" |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 27 | |
| 28 | static bool sampler_params_invalid(const GrGLTextureParameters& parameters) { |
| 29 | return SkScalarIsNaN(parameters.samplerOverriddenState().fMaxLOD); |
| 30 | } |
| 31 | |
| 32 | static bool nonsampler_params_invalid(const GrGLTextureParameters& parameters) { |
Brian Salomon | 280fa3d | 2020-08-27 17:16:49 -0400 | [diff] [blame] | 33 | GrGLTextureParameters::NonsamplerState nsState = parameters.nonsamplerState(); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 34 | GrGLTextureParameters::NonsamplerState invalidNSState; |
| 35 | invalidNSState.invalidate(); |
Brian Salomon | 280fa3d | 2020-08-27 17:16:49 -0400 | [diff] [blame] | 36 | return nsState.fBaseMipMapLevel == invalidNSState.fBaseMipMapLevel && |
| 37 | nsState.fMaxMipmapLevel == invalidNSState.fMaxMipmapLevel && |
| 38 | nsState.fSwizzleIsRGBA == invalidNSState.fSwizzleIsRGBA; |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static bool params_invalid(const GrGLTextureParameters& parameters) { |
| 42 | return sampler_params_invalid(parameters) && nonsampler_params_invalid(parameters); |
| 43 | } |
| 44 | |
| 45 | static bool params_valid(const GrGLTextureParameters& parameters, const GrGLCaps* caps) { |
| 46 | if (nonsampler_params_invalid(parameters)) { |
| 47 | return false; |
| 48 | } |
Brian Salomon | a0d913b | 2020-09-01 12:42:26 -0400 | [diff] [blame] | 49 | // We should only set the texture params that are equivalent to sampler param to valid if we're |
| 50 | // not using sampler objects. |
| 51 | return caps->useSamplerObjects() == sampler_params_invalid(parameters); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GLTextureParameters, reporter, ctxInfo) { |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 55 | auto dContext = ctxInfo.directContext(); |
Robert Phillips | 37e6f53 | 2020-08-17 11:27:40 -0400 | [diff] [blame] | 56 | auto caps = static_cast<const GrGLCaps*>(dContext->priv().caps()); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 57 | |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 58 | GrBackendTexture backendTex = dContext->createBackendTexture( |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 59 | 1, 1, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 60 | REPORTER_ASSERT(reporter, backendTex.isValid()); |
| 61 | |
| 62 | GrGLTextureInfo info; |
| 63 | REPORTER_ASSERT(reporter, backendTex.getGLTextureInfo(&info)); |
| 64 | |
| 65 | GrBackendTexture backendTexCopy = backendTex; |
| 66 | REPORTER_ASSERT(reporter, backendTexCopy.isSameTexture(backendTex)); |
| 67 | |
| 68 | sk_sp<SkImage> wrappedImage = |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 69 | SkImage::MakeFromTexture(dContext, backendTex, kTopLeft_GrSurfaceOrigin, |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 70 | kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr); |
| 71 | REPORTER_ASSERT(reporter, wrappedImage); |
| 72 | |
Brian Salomon | e666254 | 2021-02-23 10:45:39 -0500 | [diff] [blame] | 73 | GrSurfaceProxy* proxy = sk_gpu_test::GetTextureImageProxy(wrappedImage.get(), dContext); |
Brian Salomon | d0924f3 | 2021-02-03 10:15:31 -0500 | [diff] [blame] | 74 | REPORTER_ASSERT(reporter, proxy); |
| 75 | REPORTER_ASSERT(reporter, proxy->isInstantiated()); |
| 76 | auto texture = static_cast<GrGLTexture*>(proxy->peekTexture()); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 77 | REPORTER_ASSERT(reporter, texture); |
| 78 | auto parameters = texture->parameters(); |
| 79 | REPORTER_ASSERT(reporter, parameters); |
| 80 | GrGLTextureParameters::SamplerOverriddenState invalidSState; |
| 81 | invalidSState.invalidate(); |
| 82 | GrGLTextureParameters::NonsamplerState invalidNSState; |
| 83 | invalidNSState.invalidate(); |
| 84 | |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 85 | auto surf = SkSurface::MakeRenderTarget( |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 86 | dContext, SkBudgeted::kYes, |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 87 | SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1, nullptr); |
| 88 | REPORTER_ASSERT(reporter, surf); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 89 | |
| 90 | // Test invalidating from the GL backend texture. |
| 91 | backendTex.glTextureParametersModified(); |
| 92 | REPORTER_ASSERT(reporter, params_invalid(*parameters)); |
| 93 | |
| 94 | REPORTER_ASSERT(reporter, surf); |
| 95 | surf->getCanvas()->drawImage(wrappedImage, 0, 0); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 96 | surf->flushAndSubmit(); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 97 | REPORTER_ASSERT(reporter, params_valid(*parameters, caps)); |
| 98 | |
| 99 | // Test invalidating from the copy. |
| 100 | backendTexCopy.glTextureParametersModified(); |
| 101 | REPORTER_ASSERT(reporter, params_invalid(*parameters)); |
| 102 | |
| 103 | // Check that we can do things like assigning the backend texture to invalid one, assign an |
Robert Phillips | 37e6f53 | 2020-08-17 11:27:40 -0400 | [diff] [blame] | 104 | // invalid one, assign a backend texture to itself etc. Success here is that we don't hit any |
| 105 | // of our ref counting asserts. |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 106 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTexCopy)); |
| 107 | |
| 108 | GrBackendTexture invalidTexture; |
| 109 | REPORTER_ASSERT(reporter, !invalidTexture.isValid()); |
| 110 | REPORTER_ASSERT(reporter, |
| 111 | !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy)); |
| 112 | |
| 113 | backendTexCopy = invalidTexture; |
| 114 | REPORTER_ASSERT(reporter, !backendTexCopy.isValid()); |
| 115 | REPORTER_ASSERT(reporter, |
| 116 | !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy)); |
| 117 | |
| 118 | invalidTexture = backendTex; |
| 119 | REPORTER_ASSERT(reporter, invalidTexture.isValid()); |
| 120 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex)); |
| 121 | |
| 122 | invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture); |
| 123 | REPORTER_ASSERT(reporter, invalidTexture.isValid()); |
| 124 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture)); |
| 125 | |
| 126 | wrappedImage.reset(); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 127 | dContext->flush(); |
| 128 | dContext->submit(true); |
| 129 | dContext->deleteBackendTexture(backendTex); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 130 | } |
| 131 | #endif |