• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
17 #include "include/gpu/GrDirectContext.h"
18 #include "include/gpu/gl/GrGLTypes.h"
19 #include "include/private/GrGLTypesPriv.h"
20 #include "src/gpu/GrDirectContextPriv.h"
21 #include "src/gpu/GrTexture.h"
22 #include "src/gpu/GrTextureProxy.h"
23 #include "src/gpu/gl/GrGLCaps.h"
24 #include "src/gpu/gl/GrGLTexture.h"
25 #include "src/image/SkImage_Base.h"
26 #include "tools/gpu/ProxyUtils.h"
27 
sampler_params_invalid(const GrGLTextureParameters & parameters)28 static bool sampler_params_invalid(const GrGLTextureParameters& parameters) {
29     return SkScalarIsNaN(parameters.samplerOverriddenState().fMaxLOD);
30 }
31 
nonsampler_params_invalid(const GrGLTextureParameters & parameters)32 static bool nonsampler_params_invalid(const GrGLTextureParameters& parameters) {
33     GrGLTextureParameters::NonsamplerState nsState = parameters.nonsamplerState();
34     GrGLTextureParameters::NonsamplerState invalidNSState;
35     invalidNSState.invalidate();
36     return nsState.fBaseMipMapLevel == invalidNSState.fBaseMipMapLevel &&
37            nsState.fMaxMipmapLevel  == invalidNSState.fMaxMipmapLevel  &&
38            nsState.fSwizzleIsRGBA   == invalidNSState.fSwizzleIsRGBA;
39 }
40 
params_invalid(const GrGLTextureParameters & parameters)41 static bool params_invalid(const GrGLTextureParameters& parameters) {
42     return sampler_params_invalid(parameters) && nonsampler_params_invalid(parameters);
43 }
44 
params_valid(const GrGLTextureParameters & parameters,const GrGLCaps * caps)45 static bool params_valid(const GrGLTextureParameters& parameters, const GrGLCaps* caps) {
46     if (nonsampler_params_invalid(parameters)) {
47         return false;
48     }
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);
52 }
53 
DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GLTextureParameters,reporter,ctxInfo)54 DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GLTextureParameters, reporter, ctxInfo) {
55     auto dContext = ctxInfo.directContext();
56     auto caps = static_cast<const GrGLCaps*>(dContext->priv().caps());
57 
58     GrBackendTexture backendTex = dContext->createBackendTexture(
59             1, 1, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
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 =
69             SkImage::MakeFromTexture(dContext, backendTex, kTopLeft_GrSurfaceOrigin,
70                                      kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
71     REPORTER_ASSERT(reporter, wrappedImage);
72 
73     GrSurfaceProxy* proxy = sk_gpu_test::GetTextureImageProxy(wrappedImage.get(), dContext);
74     REPORTER_ASSERT(reporter, proxy);
75     REPORTER_ASSERT(reporter, proxy->isInstantiated());
76     auto texture = static_cast<GrGLTexture*>(proxy->peekTexture());
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 
85     auto surf = SkSurface::MakeRenderTarget(
86             dContext, SkBudgeted::kYes,
87             SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1, nullptr);
88     REPORTER_ASSERT(reporter, surf);
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);
96     surf->flushAndSubmit();
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
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.
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();
127     dContext->flush();
128     dContext->submit(true);
129     dContext->deleteBackendTexture(backendTex);
130 }
131 #endif
132