blob: 5387e8e3a18160012118594b0aaf539216b64dbe [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"
Adlai Hollera0693042020-10-14 11:23:11 -040020#include "src/gpu/GrDirectContextPriv.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"
Brian Salomone6662542021-02-23 10:45:39 -050026#include "tools/gpu/ProxyUtils.h"
Brian Salomone2826ab2019-06-04 15:58:31 -040027
28static bool sampler_params_invalid(const GrGLTextureParameters& parameters) {
29 return SkScalarIsNaN(parameters.samplerOverriddenState().fMaxLOD);
30}
31
32static bool nonsampler_params_invalid(const GrGLTextureParameters& parameters) {
Brian Salomon280fa3d2020-08-27 17:16:49 -040033 GrGLTextureParameters::NonsamplerState nsState = parameters.nonsamplerState();
Brian Salomone2826ab2019-06-04 15:58:31 -040034 GrGLTextureParameters::NonsamplerState invalidNSState;
35 invalidNSState.invalidate();
Brian Salomon280fa3d2020-08-27 17:16:49 -040036 return nsState.fBaseMipMapLevel == invalidNSState.fBaseMipMapLevel &&
37 nsState.fMaxMipmapLevel == invalidNSState.fMaxMipmapLevel &&
38 nsState.fSwizzleIsRGBA == invalidNSState.fSwizzleIsRGBA;
Brian Salomone2826ab2019-06-04 15:58:31 -040039}
40
41static bool params_invalid(const GrGLTextureParameters& parameters) {
42 return sampler_params_invalid(parameters) && nonsampler_params_invalid(parameters);
43}
44
45static bool params_valid(const GrGLTextureParameters& parameters, const GrGLCaps* caps) {
46 if (nonsampler_params_invalid(parameters)) {
47 return false;
48 }
Brian Salomona0d913b2020-09-01 12:42:26 -040049 // 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);
Brian Salomone2826ab2019-06-04 15:58:31 -040052}
53
54DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GLTextureParameters, reporter, ctxInfo) {
Adlai Holler14dc7912020-08-11 15:48:49 +000055 auto dContext = ctxInfo.directContext();
Robert Phillips37e6f532020-08-17 11:27:40 -040056 auto caps = static_cast<const GrGLCaps*>(dContext->priv().caps());
Brian Salomone2826ab2019-06-04 15:58:31 -040057
Adlai Holler14dc7912020-08-11 15:48:49 +000058 GrBackendTexture backendTex = dContext->createBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -040059 1, 1, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
Brian Salomone2826ab2019-06-04 15:58:31 -040060 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 =
Adlai Holler14dc7912020-08-11 15:48:49 +000069 SkImage::MakeFromTexture(dContext, backendTex, kTopLeft_GrSurfaceOrigin,
Brian Salomone2826ab2019-06-04 15:58:31 -040070 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
71 REPORTER_ASSERT(reporter, wrappedImage);
72
Brian Salomone6662542021-02-23 10:45:39 -050073 GrSurfaceProxy* proxy = sk_gpu_test::GetTextureImageProxy(wrappedImage.get(), dContext);
Brian Salomond0924f32021-02-03 10:15:31 -050074 REPORTER_ASSERT(reporter, proxy);
75 REPORTER_ASSERT(reporter, proxy->isInstantiated());
76 auto texture = static_cast<GrGLTexture*>(proxy->peekTexture());
Brian Salomone2826ab2019-06-04 15:58:31 -040077 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
Brian Salomone2826ab2019-06-04 15:58:31 -040085 auto surf = SkSurface::MakeRenderTarget(
Adlai Holler14dc7912020-08-11 15:48:49 +000086 dContext, SkBudgeted::kYes,
Brian Salomone2826ab2019-06-04 15:58:31 -040087 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1, nullptr);
88 REPORTER_ASSERT(reporter, surf);
Brian Salomone2826ab2019-06-04 15:58:31 -040089
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);
Greg Daniel0a2464f2020-05-14 15:45:44 -040096 surf->flushAndSubmit();
Brian Salomone2826ab2019-06-04 15:58:31 -040097 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
Robert Phillips37e6f532020-08-17 11:27:40 -0400104 // invalid one, assign a backend texture to itself etc. Success here is that we don't hit any
105 // of our ref counting asserts.
Brian Salomone2826ab2019-06-04 15:58:31 -0400106 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();
Adlai Holler14dc7912020-08-11 15:48:49 +0000127 dContext->flush();
128 dContext->submit(true);
129 dContext->deleteBackendTexture(backendTex);
Brian Salomone2826ab2019-06-04 15:58:31 -0400130}
131#endif