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 #include "include/core/SkAlphaType.h"
12 #include "include/core/SkCanvas.h"
13 #include "include/core/SkColorSpace.h"
14 #include "include/core/SkColorType.h"
15 #include "include/core/SkImage.h"
16 #include "include/core/SkImageInfo.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkSurface.h"
20 #include "include/core/SkTypes.h"
21 #include "include/gpu/GpuTypes.h"
22 #include "include/gpu/ganesh/GrBackendSurface.h"
23 #include "include/gpu/ganesh/GrDirectContext.h"
24 #include "include/gpu/ganesh/GrTypes.h"
25 #include "include/gpu/ganesh/SkImageGanesh.h"
26 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
27 #include "include/gpu/ganesh/gl/GrGLBackendSurface.h"
28 #include "include/gpu/ganesh/gl/GrGLTypes.h"
29 #include "src/gpu/ganesh/GrDirectContextPriv.h"
30 #include "src/gpu/ganesh/GrSurfaceProxy.h"
31 #include "src/gpu/ganesh/GrTextureProxy.h"
32 #include "src/gpu/ganesh/gl/GrGLCaps.h"
33 #include "src/gpu/ganesh/gl/GrGLTexture.h"
34 #include "src/gpu/ganesh/gl/GrGLTypesPriv.h"
35 #include "tests/CtsEnforcement.h"
36 #include "tests/Test.h"
37 #include "tools/gpu/ProxyUtils.h"
38 struct GrContextOptions;
39
sampler_params_invalid(const GrGLTextureParameters & parameters)40 static bool sampler_params_invalid(const GrGLTextureParameters& parameters) {
41 return SkIsNaN(parameters.samplerOverriddenState().fMaxLOD);
42 }
43
nonsampler_params_invalid(const GrGLTextureParameters & parameters)44 static bool nonsampler_params_invalid(const GrGLTextureParameters& parameters) {
45 GrGLTextureParameters::NonsamplerState nsState = parameters.nonsamplerState();
46 GrGLTextureParameters::NonsamplerState invalidNSState;
47 invalidNSState.invalidate();
48 return nsState.fBaseMipMapLevel == invalidNSState.fBaseMipMapLevel &&
49 nsState.fMaxMipmapLevel == invalidNSState.fMaxMipmapLevel &&
50 nsState.fSwizzleIsRGBA == invalidNSState.fSwizzleIsRGBA;
51 }
52
params_invalid(const GrGLTextureParameters & parameters)53 static bool params_invalid(const GrGLTextureParameters& parameters) {
54 return sampler_params_invalid(parameters) && nonsampler_params_invalid(parameters);
55 }
56
params_valid(const GrGLTextureParameters & parameters,const GrGLCaps * caps)57 static bool params_valid(const GrGLTextureParameters& parameters, const GrGLCaps* caps) {
58 if (nonsampler_params_invalid(parameters)) {
59 return false;
60 }
61 // We should only set the texture params that are equivalent to sampler param to valid if we're
62 // not using sampler objects.
63 return caps->useSamplerObjects() == sampler_params_invalid(parameters);
64 }
65
DEF_GANESH_TEST_FOR_GL_CONTEXT(GLTextureParameters,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)66 DEF_GANESH_TEST_FOR_GL_CONTEXT(GLTextureParameters,
67 reporter,
68 ctxInfo,
69 CtsEnforcement::kApiLevel_T) {
70 auto dContext = ctxInfo.directContext();
71 auto caps = static_cast<const GrGLCaps*>(dContext->priv().caps());
72
73 GrBackendTexture backendTex = dContext->createBackendTexture(1,
74 1,
75 kRGBA_8888_SkColorType,
76 skgpu::Mipmapped::kNo,
77 GrRenderable::kNo,
78 GrProtected::kNo);
79 REPORTER_ASSERT(reporter, backendTex.isValid());
80
81 GrGLTextureInfo info;
82 REPORTER_ASSERT(reporter, GrBackendTextures::GetGLTextureInfo(backendTex, &info));
83
84 GrBackendTexture backendTexCopy = backendTex;
85 REPORTER_ASSERT(reporter, backendTexCopy.isSameTexture(backendTex));
86
87 sk_sp<SkImage> wrappedImage = SkImages::BorrowTextureFrom(dContext,
88 backendTex,
89 kTopLeft_GrSurfaceOrigin,
90 kRGBA_8888_SkColorType,
91 kPremul_SkAlphaType,
92 nullptr);
93 REPORTER_ASSERT(reporter, wrappedImage);
94
95 GrSurfaceProxy* proxy = sk_gpu_test::GetTextureImageProxy(wrappedImage.get(), dContext);
96 REPORTER_ASSERT(reporter, proxy);
97 REPORTER_ASSERT(reporter, proxy->isInstantiated());
98 auto texture = static_cast<GrGLTexture*>(proxy->peekTexture());
99 REPORTER_ASSERT(reporter, texture);
100 auto parameters = texture->parameters();
101 REPORTER_ASSERT(reporter, parameters);
102 GrGLTextureParameters::SamplerOverriddenState invalidSState;
103 invalidSState.invalidate();
104 GrGLTextureParameters::NonsamplerState invalidNSState;
105 invalidNSState.invalidate();
106
107 auto surf = SkSurfaces::RenderTarget(
108 dContext,
109 skgpu::Budgeted::kYes,
110 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
111 1,
112 nullptr);
113 REPORTER_ASSERT(reporter, surf);
114
115 // Test invalidating from the GL backend texture.
116 GrBackendTextures::GLTextureParametersModified(&backendTex);
117 REPORTER_ASSERT(reporter, params_invalid(*parameters));
118
119 REPORTER_ASSERT(reporter, surf);
120 surf->getCanvas()->drawImage(wrappedImage, 0, 0);
121 dContext->flushAndSubmit(surf.get(), GrSyncCpu::kNo);
122 REPORTER_ASSERT(reporter, params_valid(*parameters, caps));
123
124 // Test invalidating from the copy.
125 GrBackendTextures::GLTextureParametersModified(&backendTexCopy);
126 REPORTER_ASSERT(reporter, params_invalid(*parameters));
127
128 // Check that we can do things like assigning the backend texture to invalid one, assign an
129 // invalid one, assign a backend texture to itself etc. Success here is that we don't hit any
130 // of our ref counting asserts.
131 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTexCopy));
132
133 GrBackendTexture invalidTexture;
134 REPORTER_ASSERT(reporter, !invalidTexture.isValid());
135 REPORTER_ASSERT(reporter,
136 !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
137
138 backendTexCopy = invalidTexture;
139 REPORTER_ASSERT(reporter, !backendTexCopy.isValid());
140 REPORTER_ASSERT(reporter,
141 !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
142
143 invalidTexture = backendTex;
144 REPORTER_ASSERT(reporter, invalidTexture.isValid());
145 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex));
146
147 invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture);
148 REPORTER_ASSERT(reporter, invalidTexture.isValid());
149 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture));
150
151 wrappedImage.reset();
152 dContext->flush();
153 dContext->submit(GrSyncCpu::kYes);
154 dContext->deleteBackendTexture(backendTex);
155 }
156 #endif
157