2 * Copyright 2019 Google LLC
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "include/core/SkTypes.h"
12 #include "tests/Test.h"
14 #include "include/core/SkColorSpace.h"
15 #include "include/core/SkImage.h"
16 #include "include/core/SkSurface.h"
17 #include "include/gpu/GrBackendSurface.h"
18 #include "include/gpu/GrDirectContext.h"
19 #include "include/gpu/gl/GrGLTypes.h"
20 #include "include/private/gpu/ganesh/GrGLTypesPriv.h"
21 #include "src/gpu/ganesh/GrDirectContextPriv.h"
22 #include "src/gpu/ganesh/GrTexture.h"
23 #include "src/gpu/ganesh/GrTextureProxy.h"
24 #include "src/gpu/ganesh/gl/GrGLCaps.h"
25 #include "src/gpu/ganesh/gl/GrGLTexture.h"
26 #include "src/image/SkImage_Base.h"
27 #include "tools/gpu/ProxyUtils.h"
29 static bool sampler_params_invalid(const GrGLTextureParameters& parameters) {
30 return SkScalarIsNaN(parameters.samplerOverriddenState().fMaxLOD);
33 static bool nonsampler_params_invalid(const GrGLTextureParameters& parameters) {
34 GrGLTextureParameters::NonsamplerState nsState = parameters.nonsamplerState();
35 GrGLTextureParameters::NonsamplerState invalidNSState;
36 invalidNSState.invalidate();
37 return nsState.fBaseMipMapLevel == invalidNSState.fBaseMipMapLevel &&
38 nsState.fMaxMipmapLevel == invalidNSState.fMaxMipmapLevel &&
39 nsState.fSwizzleIsRGBA == invalidNSState.fSwizzleIsRGBA;
42 static bool params_invalid(const GrGLTextureParameters& parameters) {
43 return sampler_params_invalid(parameters) && nonsampler_params_invalid(parameters);
46 static bool params_valid(const GrGLTextureParameters& parameters, const GrGLCaps* caps) {
47 if (nonsampler_params_invalid(parameters)) {
50 // We should only set the texture params that are equivalent to sampler param to valid if we're
51 // not using sampler objects.
52 return caps->useSamplerObjects() == sampler_params_invalid(parameters);
55 DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GLTextureParameters, reporter, ctxInfo) {
56 auto dContext = ctxInfo.directContext();
57 auto caps = static_cast<const GrGLCaps*>(dContext->priv().caps());
59 GrBackendTexture backendTex = dContext->createBackendTexture(
60 1, 1, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
61 REPORTER_ASSERT(reporter, backendTex.isValid());
64 REPORTER_ASSERT(reporter, backendTex.getGLTextureInfo(&info));
66 GrBackendTexture backendTexCopy = backendTex;
67 REPORTER_ASSERT(reporter, backendTexCopy.isSameTexture(backendTex));
69 sk_sp<SkImage> wrappedImage =
70 SkImage::MakeFromTexture(dContext, backendTex, kTopLeft_GrSurfaceOrigin,
71 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
72 REPORTER_ASSERT(reporter, wrappedImage);
74 GrSurfaceProxy* proxy = sk_gpu_test::GetTextureImageProxy(wrappedImage.get(), dContext);
75 REPORTER_ASSERT(reporter, proxy);
76 REPORTER_ASSERT(reporter, proxy->isInstantiated());
77 auto texture = static_cast<GrGLTexture*>(proxy->peekTexture());
78 REPORTER_ASSERT(reporter, texture);
79 auto parameters = texture->parameters();
80 REPORTER_ASSERT(reporter, parameters);
81 GrGLTextureParameters::SamplerOverriddenState invalidSState;
82 invalidSState.invalidate();
83 GrGLTextureParameters::NonsamplerState invalidNSState;
84 invalidNSState.invalidate();
86 auto surf = SkSurface::MakeRenderTarget(
87 dContext, SkBudgeted::kYes,
88 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1, nullptr);
89 REPORTER_ASSERT(reporter, surf);
91 // Test invalidating from the GL backend texture.
92 backendTex.glTextureParametersModified();
93 REPORTER_ASSERT(reporter, params_invalid(*parameters));
95 REPORTER_ASSERT(reporter, surf);
96 surf->getCanvas()->drawImage(wrappedImage, 0, 0);
97 surf->flushAndSubmit();
98 REPORTER_ASSERT(reporter, params_valid(*parameters, caps));
100 // Test invalidating from the copy.
101 backendTexCopy.glTextureParametersModified();
102 REPORTER_ASSERT(reporter, params_invalid(*parameters));
104 // Check that we can do things like assigning the backend texture to invalid one, assign an
105 // invalid one, assign a backend texture to itself etc. Success here is that we don't hit any
106 // of our ref counting asserts.
107 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTexCopy));
109 GrBackendTexture invalidTexture;
110 REPORTER_ASSERT(reporter, !invalidTexture.isValid());
111 REPORTER_ASSERT(reporter,
112 !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
114 backendTexCopy = invalidTexture;
115 REPORTER_ASSERT(reporter, !backendTexCopy.isValid());
116 REPORTER_ASSERT(reporter,
117 !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
119 invalidTexture = backendTex;
120 REPORTER_ASSERT(reporter, invalidTexture.isValid());
121 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex));
123 invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture);
124 REPORTER_ASSERT(reporter, invalidTexture.isValid());
125 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture));
127 wrappedImage.reset();
129 dContext->submit(true);
130 dContext->deleteBackendTexture(backendTex);