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