blob: b6fde94f310e7c165d7388521825aa820e9e7c31 [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"
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) {
Brian Salomon280fa3d2020-08-27 17:16:49 -040032 GrGLTextureParameters::NonsamplerState nsState = parameters.nonsamplerState();
Brian Salomone2826ab2019-06-04 15:58:31 -040033 GrGLTextureParameters::NonsamplerState invalidNSState;
34 invalidNSState.invalidate();
Brian Salomon280fa3d2020-08-27 17:16:49 -040035 return nsState.fBaseMipMapLevel == invalidNSState.fBaseMipMapLevel &&
36 nsState.fMaxMipmapLevel == invalidNSState.fMaxMipmapLevel &&
37 nsState.fSwizzleIsRGBA == invalidNSState.fSwizzleIsRGBA;
Brian Salomone2826ab2019-06-04 15:58:31 -040038}
39
40static bool params_invalid(const GrGLTextureParameters& parameters) {
41 return sampler_params_invalid(parameters) && nonsampler_params_invalid(parameters);
42}
43
44static bool params_valid(const GrGLTextureParameters& parameters, const GrGLCaps* caps) {
45 if (nonsampler_params_invalid(parameters)) {
46 return false;
47 }
Brian Salomona0d913b2020-09-01 12:42:26 -040048 // We should only set the texture params that are equivalent to sampler param to valid if we're
49 // not using sampler objects.
50 return caps->useSamplerObjects() == sampler_params_invalid(parameters);
Brian Salomone2826ab2019-06-04 15:58:31 -040051}
52
53DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GLTextureParameters, reporter, ctxInfo) {
Adlai Holler14dc7912020-08-11 15:48:49 +000054 auto dContext = ctxInfo.directContext();
Robert Phillips37e6f532020-08-17 11:27:40 -040055 auto caps = static_cast<const GrGLCaps*>(dContext->priv().caps());
Brian Salomone2826ab2019-06-04 15:58:31 -040056
Adlai Holler14dc7912020-08-11 15:48:49 +000057 GrBackendTexture backendTex = dContext->createBackendTexture(
Brian Salomon7e67dca2020-07-21 09:27:25 -040058 1, 1, kRGBA_8888_SkColorType, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
Brian Salomone2826ab2019-06-04 15:58:31 -040059 REPORTER_ASSERT(reporter, backendTex.isValid());
60
61 GrGLTextureInfo info;
62 REPORTER_ASSERT(reporter, backendTex.getGLTextureInfo(&info));
63
64 GrBackendTexture backendTexCopy = backendTex;
65 REPORTER_ASSERT(reporter, backendTexCopy.isSameTexture(backendTex));
66
67 sk_sp<SkImage> wrappedImage =
Adlai Holler14dc7912020-08-11 15:48:49 +000068 SkImage::MakeFromTexture(dContext, backendTex, kTopLeft_GrSurfaceOrigin,
Brian Salomone2826ab2019-06-04 15:58:31 -040069 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
70 REPORTER_ASSERT(reporter, wrappedImage);
71
Adlai Holler14dc7912020-08-11 15:48:49 +000072 const GrSurfaceProxyView* view = as_IB(wrappedImage)->view(dContext);
Greg Danielfebdedf2020-02-05 17:06:27 -050073 REPORTER_ASSERT(reporter, view);
74 REPORTER_ASSERT(reporter, view->proxy()->isInstantiated());
75 auto texture = static_cast<GrGLTexture*>(view->proxy()->peekTexture());
Brian Salomone2826ab2019-06-04 15:58:31 -040076 REPORTER_ASSERT(reporter, texture);
77 auto parameters = texture->parameters();
78 REPORTER_ASSERT(reporter, parameters);
79 GrGLTextureParameters::SamplerOverriddenState invalidSState;
80 invalidSState.invalidate();
81 GrGLTextureParameters::NonsamplerState invalidNSState;
82 invalidNSState.invalidate();
83
Brian Salomone2826ab2019-06-04 15:58:31 -040084 auto surf = SkSurface::MakeRenderTarget(
Adlai Holler14dc7912020-08-11 15:48:49 +000085 dContext, SkBudgeted::kYes,
Brian Salomone2826ab2019-06-04 15:58:31 -040086 SkImageInfo::Make(1, 1, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1, nullptr);
87 REPORTER_ASSERT(reporter, surf);
Brian Salomone2826ab2019-06-04 15:58:31 -040088
89 // Test invalidating from the GL backend texture.
90 backendTex.glTextureParametersModified();
91 REPORTER_ASSERT(reporter, params_invalid(*parameters));
92
93 REPORTER_ASSERT(reporter, surf);
94 surf->getCanvas()->drawImage(wrappedImage, 0, 0);
Greg Daniel0a2464f2020-05-14 15:45:44 -040095 surf->flushAndSubmit();
Brian Salomone2826ab2019-06-04 15:58:31 -040096 REPORTER_ASSERT(reporter, params_valid(*parameters, caps));
97
98 // Test invalidating from the copy.
99 backendTexCopy.glTextureParametersModified();
100 REPORTER_ASSERT(reporter, params_invalid(*parameters));
101
102 // Check that we can do things like assigning the backend texture to invalid one, assign an
Robert Phillips37e6f532020-08-17 11:27:40 -0400103 // invalid one, assign a backend texture to itself etc. Success here is that we don't hit any
104 // of our ref counting asserts.
Brian Salomone2826ab2019-06-04 15:58:31 -0400105 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(backendTex, backendTexCopy));
106
107 GrBackendTexture invalidTexture;
108 REPORTER_ASSERT(reporter, !invalidTexture.isValid());
109 REPORTER_ASSERT(reporter,
110 !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
111
112 backendTexCopy = invalidTexture;
113 REPORTER_ASSERT(reporter, !backendTexCopy.isValid());
114 REPORTER_ASSERT(reporter,
115 !GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTexCopy));
116
117 invalidTexture = backendTex;
118 REPORTER_ASSERT(reporter, invalidTexture.isValid());
119 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, backendTex));
120
121 invalidTexture = static_cast<decltype(invalidTexture)&>(invalidTexture);
122 REPORTER_ASSERT(reporter, invalidTexture.isValid());
123 REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(invalidTexture, invalidTexture));
124
125 wrappedImage.reset();
Adlai Holler14dc7912020-08-11 15:48:49 +0000126 dContext->flush();
127 dContext->submit(true);
128 dContext->deleteBackendTexture(backendTex);
Brian Salomone2826ab2019-06-04 15:58:31 -0400129}
130#endif