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" |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrContextPriv.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) { |
| 32 | GrGLTextureParameters::NonsamplerState invalidNSState; |
| 33 | invalidNSState.invalidate(); |
| 34 | return 0 == memcmp(¶meters.nonsamplerState(), &invalidNSState, sizeof(invalidNSState)); |
| 35 | } |
| 36 | |
| 37 | static bool params_invalid(const GrGLTextureParameters& parameters) { |
| 38 | return sampler_params_invalid(parameters) && nonsampler_params_invalid(parameters); |
| 39 | } |
| 40 | |
| 41 | static bool params_valid(const GrGLTextureParameters& parameters, const GrGLCaps* caps) { |
| 42 | if (nonsampler_params_invalid(parameters)) { |
| 43 | return false; |
| 44 | } |
| 45 | // We should only set the sampler parameters to valid if we don't have sampler object support. |
| 46 | return caps->samplerObjectSupport() == sampler_params_invalid(parameters); |
| 47 | } |
| 48 | |
| 49 | DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GLTextureParameters, reporter, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 50 | auto context = ctxInfo.directContext(); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 51 | |
| 52 | GrBackendTexture backendTex = context->createBackendTexture( |
Emircan Uysaler | 23ca4e7 | 2019-06-24 10:53:09 -0400 | [diff] [blame] | 53 | 1, 1, kRGBA_8888_SkColorType, GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 54 | REPORTER_ASSERT(reporter, backendTex.isValid()); |
| 55 | |
| 56 | GrGLTextureInfo info; |
| 57 | REPORTER_ASSERT(reporter, backendTex.getGLTextureInfo(&info)); |
| 58 | |
| 59 | GrBackendTexture backendTexCopy = backendTex; |
| 60 | REPORTER_ASSERT(reporter, backendTexCopy.isSameTexture(backendTex)); |
| 61 | |
| 62 | sk_sp<SkImage> wrappedImage = |
| 63 | SkImage::MakeFromTexture(context, backendTex, kTopLeft_GrSurfaceOrigin, |
| 64 | kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr); |
| 65 | REPORTER_ASSERT(reporter, wrappedImage); |
| 66 | |
Greg Daniel | febdedf | 2020-02-05 17:06:27 -0500 | [diff] [blame] | 67 | const GrSurfaceProxyView* view = as_IB(wrappedImage)->view(context); |
| 68 | REPORTER_ASSERT(reporter, view); |
| 69 | REPORTER_ASSERT(reporter, view->proxy()->isInstantiated()); |
| 70 | auto texture = static_cast<GrGLTexture*>(view->proxy()->peekTexture()); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 71 | REPORTER_ASSERT(reporter, texture); |
| 72 | auto parameters = texture->parameters(); |
| 73 | REPORTER_ASSERT(reporter, parameters); |
| 74 | GrGLTextureParameters::SamplerOverriddenState invalidSState; |
| 75 | invalidSState.invalidate(); |
| 76 | GrGLTextureParameters::NonsamplerState invalidNSState; |
| 77 | invalidNSState.invalidate(); |
| 78 | |
| 79 | // After wrapping we should assume the client's texture can be in any state. |
| 80 | REPORTER_ASSERT(reporter, params_invalid(*parameters)); |
| 81 | |
| 82 | auto surf = SkSurface::MakeRenderTarget( |
| 83 | context, SkBudgeted::kYes, |
| 84 | SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1, nullptr); |
| 85 | REPORTER_ASSERT(reporter, surf); |
| 86 | surf->getCanvas()->drawImage(wrappedImage, 0, 0); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 87 | surf->flushAndSubmit(); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 88 | |
| 89 | auto caps = static_cast<const GrGLCaps*>(context->priv().caps()); |
| 90 | // Now the texture should be in a known state. |
| 91 | REPORTER_ASSERT(reporter, params_valid(*parameters, caps)); |
| 92 | |
| 93 | // Test invalidating from the GL backend texture. |
| 94 | backendTex.glTextureParametersModified(); |
| 95 | REPORTER_ASSERT(reporter, params_invalid(*parameters)); |
| 96 | |
| 97 | REPORTER_ASSERT(reporter, surf); |
| 98 | surf->getCanvas()->drawImage(wrappedImage, 0, 0); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 99 | surf->flushAndSubmit(); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 100 | REPORTER_ASSERT(reporter, params_valid(*parameters, caps)); |
| 101 | |
| 102 | // Test invalidating from the copy. |
| 103 | backendTexCopy.glTextureParametersModified(); |
| 104 | REPORTER_ASSERT(reporter, params_invalid(*parameters)); |
| 105 | |
| 106 | // Check that we can do things like assigning the backend texture to invalid one, assign an |
| 107 | // invalid one, assin a backend texture to inself etc. Success here is that we don't hit any of |
| 108 | // our ref counting asserts. |
| 109 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTexCopy)); |
| 110 | |
| 111 | GrBackendTexture invalidTexture; |
| 112 | REPORTER_ASSERT(reporter, !invalidTexture.isValid()); |
| 113 | REPORTER_ASSERT(reporter, |
| 114 | !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy)); |
| 115 | |
| 116 | backendTexCopy = invalidTexture; |
| 117 | REPORTER_ASSERT(reporter, !backendTexCopy.isValid()); |
| 118 | REPORTER_ASSERT(reporter, |
| 119 | !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy)); |
| 120 | |
| 121 | invalidTexture = backendTex; |
| 122 | REPORTER_ASSERT(reporter, invalidTexture.isValid()); |
| 123 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex)); |
| 124 | |
| 125 | invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture); |
| 126 | REPORTER_ASSERT(reporter, invalidTexture.isValid()); |
| 127 | REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture)); |
| 128 | |
| 129 | wrappedImage.reset(); |
Greg Daniel | ce9f016 | 2020-06-30 13:42:46 -0400 | [diff] [blame] | 130 | context->flush(); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 131 | context->submit(true); |
Brian Salomon | e2826ab | 2019-06-04 15:58:31 -0400 | [diff] [blame] | 132 | context->deleteBackendTexture(backendTex); |
| 133 | } |
| 134 | #endif |