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