blob: f5a61e2e6753d6c15caaa3dd885f4150b268b173 [file] [log] [blame]
Brian Salomone2826ab2019-06-04 15:58:31 -04001/*
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 Phillips6d344c32020-07-06 10:56:46 -040017#include "include/gpu/GrDirectContext.h"
Brian Salomone2826ab2019-06-04 15:58:31 -040018#include "include/gpu/gl/GrGLTypes.h"
19#include "include/private/GrGLTypesPriv.h"
Brian Salomone2826ab2019-06-04 15:58:31 -040020#include "src/gpu/GrContextPriv.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000021#include "src/gpu/GrTexture.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040022#include "src/gpu/GrTextureProxy.h"
Brian Salomone2826ab2019-06-04 15:58:31 -040023#include "src/gpu/gl/GrGLCaps.h"
24#include "src/gpu/gl/GrGLTexture.h"
25#include "src/image/SkImage_Base.h"
26
27static bool sampler_params_invalid(const GrGLTextureParameters& parameters) {
28 return SkScalarIsNaN(parameters.samplerOverriddenState().fMaxLOD);
29}
30
31static bool nonsampler_params_invalid(const GrGLTextureParameters& parameters) {
32 GrGLTextureParameters::NonsamplerState invalidNSState;
33 invalidNSState.invalidate();
34 return 0 == memcmp(&parameters.nonsamplerState(), &invalidNSState, sizeof(invalidNSState));
35}
36
37static bool params_invalid(const GrGLTextureParameters& parameters) {
38 return sampler_params_invalid(parameters) && nonsampler_params_invalid(parameters);
39}
40
41static 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
49DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GLTextureParameters, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -040050 auto context = ctxInfo.directContext();
Brian Salomone2826ab2019-06-04 15:58:31 -040051
52 GrBackendTexture backendTex = context->createBackendTexture(
Emircan Uysaler23ca4e72019-06-24 10:53:09 -040053 1, 1, kRGBA_8888_SkColorType, GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
Brian Salomone2826ab2019-06-04 15:58:31 -040054 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 Danielfebdedf2020-02-05 17:06:27 -050067 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 Salomone2826ab2019-06-04 15:58:31 -040071 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 Daniel0a2464f2020-05-14 15:45:44 -040087 surf->flushAndSubmit();
Brian Salomone2826ab2019-06-04 15:58:31 -040088
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 Daniel0a2464f2020-05-14 15:45:44 -040099 surf->flushAndSubmit();
Brian Salomone2826ab2019-06-04 15:58:31 -0400100 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 Danielce9f0162020-06-30 13:42:46 -0400130 context->flush();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400131 context->submit(true);
Brian Salomone2826ab2019-06-04 15:58:31 -0400132 context->deleteBackendTexture(backendTex);
133}
134#endif