Flutter Engine
The Flutter Engine
TextureBindingsResetTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2019 Google Inc.
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 "tests/Test.h"
9
10#ifdef SK_GL
20#include "include/core/SkSize.h"
26#include "include/gpu/GrTypes.h"
45
46struct GrContextOptions;
47
48DEF_GANESH_TEST_FOR_GL_CONTEXT(TextureBindingsResetTest,
50 ctxInfo,
52#define GL(F) GR_GL_CALL(ctxInfo.glContext()->gl(), F)
53
54 auto dContext = ctxInfo.directContext();
55 GrGpu* gpu = dContext->priv().getGpu();
56 GrGLGpu* glGpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
57
58 struct Target {
60 GrGLenum fQuery;
61 };
62 SkTDArray<Target> targets;
64 bool supportExternal;
65 if ((supportExternal = glGpu->glCaps().shaderCaps()->fExternalTextureSupport)) {
67 }
68 bool supportRectangle;
69 if ((supportRectangle = glGpu->glCaps().rectangleTextureSupport())) {
71 }
72 GrGLint numUnits = 0;
73 GL(GetIntegerv(GR_GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &numUnits));
74 SkTDArray<GrGLuint> claimedIDs;
75 claimedIDs.resize(numUnits * targets.size());
76 GL(GenTextures(claimedIDs.size(), claimedIDs.begin()));
77
78 auto resetBindings = [&] {
79 int i = 0;
80 for (int u = 0; u < numUnits; ++u) {
81 GL(ActiveTexture(GR_GL_TEXTURE0 + u));
82 for (auto target : targets) {
83 GL(BindTexture(target.fName, claimedIDs[i++]));
84 }
85 }
86 };
87 auto checkBindings = [&] {
88 int i = 0;
89 for (int u = 0; u < numUnits; ++u) {
90 GL(ActiveTexture(GR_GL_TEXTURE0 + u));
91 for (auto target : targets) {
92 GrGLint boundID = -1;
93 GL(GetIntegerv(target.fQuery, &boundID));
94 if (boundID != (int) claimedIDs[i] && boundID != 0) {
95 ERRORF(reporter, "Unit %d, target 0x%04x has ID %d bound. Expected %u or 0.", u,
96 target.fName, boundID, claimedIDs[i]);
97 return;
98 }
99 ++i;
100 }
101 }
102 };
103
104 // Initialize texture unit/target combo bindings to 0.
105 dContext->flushAndSubmit();
106 resetBindings();
107 dContext->resetContext();
108
109 // Test creating a texture and then resetting bindings.
110 static constexpr SkISize kDims = {10, 10};
113 auto tex = gpu->createTexture(kDims,
114 format,
117 1,
121 /*label=*/"TextureBindingsResetTest");
123 dContext->resetGLTextureBindings();
124 checkBindings();
125 resetBindings();
126 dContext->resetContext();
127
128 // Test drawing and then resetting bindings. This should force a MIP regeneration if MIP
129 // maps are supported as well.
131 auto surf = SkSurfaces::RenderTarget(dContext, skgpu::Budgeted::kYes, info, 1, nullptr);
132 surf->getCanvas()->clear(0x80FF0000);
133 auto img = surf->makeImageSnapshot();
134 surf->getCanvas()->clear(SK_ColorBLUE);
135 surf->getCanvas()->save();
136 surf->getCanvas()->scale(0.25, 0.25);
137 surf->getCanvas()->drawImage(img.get(), 0, 0, SkSamplingOptions({1.0f/3, 1.0f/3}), nullptr);
138 surf->getCanvas()->restore();
139 dContext->flushAndSubmit(surf.get(), GrSyncCpu::kNo);
140 dContext->resetGLTextureBindings();
141 checkBindings();
142 resetBindings();
143 dContext->resetContext();
144
145 if (supportExternal) {
146 GrBackendTexture texture2D = dContext->createBackendTexture(10,
147 10,
153 GrGLTextureInfo info2D;
155 GrEGLImage eglImage = ctxInfo.glContext()->texture2DToEGLImage(info2D.fID);
156 REPORTER_ASSERT(reporter, eglImage);
157 GrGLTextureInfo infoExternal;
158 infoExternal.fID = ctxInfo.glContext()->eglImageToExternalTexture(eglImage);
159 infoExternal.fTarget = GR_GL_TEXTURE_EXTERNAL;
160 infoExternal.fFormat = info2D.fFormat;
161 REPORTER_ASSERT(reporter, infoExternal.fID);
162 infoExternal.fProtected = info2D.fProtected;
163 GrBackendTexture backendTexture =
165 // Above texture creation will have messed with GL state and bindings.
166 resetBindings();
167 dContext->resetContext();
168 img = SkImages::BorrowTextureFrom(dContext,
169 backendTexture,
173 nullptr);
175 surf->getCanvas()->drawImage(img, 0, 0);
176 img.reset();
177 dContext->flushAndSubmit(surf.get(), GrSyncCpu::kNo);
178 dContext->resetGLTextureBindings();
179 checkBindings();
180 resetBindings();
181 GL(DeleteTextures(1, &infoExternal.fID));
182 ctxInfo.glContext()->destroyEGLImage(eglImage);
183 dContext->deleteBackendTexture(texture2D);
184 dContext->resetContext();
185 }
186
187 if (supportRectangle) {
189 GrBackendTexture rectangleTexture = dContext->createBackendTexture(
191 if (rectangleTexture.isValid()) {
192 img = SkImages::BorrowTextureFrom(dContext,
193 rectangleTexture,
197 nullptr);
199 surf->getCanvas()->drawImage(img, 0, 0);
200 img.reset();
201 dContext->flushAndSubmit(surf.get(), GrSyncCpu::kNo);
202 dContext->resetGLTextureBindings();
203 checkBindings();
204 resetBindings();
205 dContext->deleteBackendTexture(rectangleTexture);
206 }
207 }
208
209 GL(DeleteTextures(claimedIDs.size(), claimedIDs.begin()));
210
211#undef GL
212}
213
214#endif // SK_GL
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
const char * fName
reporter
Definition: FontMgrTest.cpp:39
#define GR_GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
Definition: GrGLDefines.h:558
#define GR_GL_RGBA8
Definition: GrGLDefines.h:524
#define GR_GL_TEXTURE_BINDING_2D
Definition: GrGLDefines.h:235
#define GR_GL_TEXTURE_2D
Definition: GrGLDefines.h:152
#define GR_GL_TEXTURE_BINDING_EXTERNAL
Definition: GrGLDefines.h:1053
#define GR_GL_TEXTURE_RECTANGLE
Definition: GrGLDefines.h:1056
#define GR_GL_TEXTURE0
Definition: GrGLDefines.h:651
#define GR_GL_TEXTURE_EXTERNAL
Definition: GrGLDefines.h:1052
#define GR_GL_TEXTURE_BINDING_RECTANGLE
Definition: GrGLDefines.h:1057
int GrGLint
Definition: GrGLTypes.h:108
unsigned int GrGLenum
Definition: GrGLTypes.h:102
void * GrEGLImage
Definition: GrGLTypes.h:165
@ kTopLeft_GrSurfaceOrigin
Definition: GrTypes.h:148
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
#define DEF_GANESH_TEST_FOR_GL_CONTEXT(name, reporter, context_info, ctsEnforcement)
Definition: Test.h:442
#define ERRORF(r,...)
Definition: Test.h:293
bool isValid() const
const GrShaderCaps * shaderCaps() const
Definition: GrCaps.h:63
GrBackendFormat getDefaultBackendFormat(GrColorType, GrRenderable) const
Definition: GrCaps.cpp:400
bool rectangleTextureSupport() const
Are textures with GL_TEXTURE_RECTANGLE type supported.
Definition: GrGLCaps.h:384
const GrGLCaps & glCaps() const
Definition: GrGLGpu.h:108
Definition: GrGpu.h:62
const GrCaps * caps() const
Definition: GrGpu.h:73
sk_sp< GrTexture > createTexture(SkISize dimensions, const GrBackendFormat &format, GrTextureType textureType, GrRenderable renderable, int renderTargetSampleCnt, skgpu::Budgeted budgeted, GrProtected isProtected, GrColorType textureColorType, GrColorType srcColorType, const GrMipLevel texels[], int texelLevelCount, std::string_view label)
Definition: GrGpu.cpp:189
int size() const
Definition: SkTDArray.h:138
void push_back(const T &v)
Definition: SkTDArray.h:219
T * begin()
Definition: SkTDArray.h:150
void resize(int count)
Definition: SkTDArray.h:183
uint32_t uint32_t * format
uint32_t * target
SK_API GrBackendFormat MakeGL(GrGLenum format, GrGLenum target)
SK_API GrBackendTexture MakeGL(int width, int height, skgpu::Mipmapped, const GrGLTextureInfo &glInfo, std::string_view label={})
SK_API bool GetGLTextureInfo(const GrBackendTexture &, GrGLTextureInfo *)
constexpr SkColor4f kTransparent
Definition: SkColor.h:434
SK_API sk_sp< SkImage > BorrowTextureFrom(GrRecordingContext *context, const GrBackendTexture &backendTexture, GrSurfaceOrigin origin, SkColorType colorType, SkAlphaType alphaType, sk_sp< SkColorSpace > colorSpace, TextureReleaseProc textureReleaseProc=nullptr, ReleaseContext releaseContext=nullptr)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
skgpu::Protected fProtected
Definition: GrGLTypes.h:184
GrGLuint fID
Definition: GrGLTypes.h:182
GrGLenum fFormat
Definition: GrGLTypes.h:183
GrGLenum fTarget
Definition: GrGLTypes.h:181
Definition: SkSize.h:16
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
bool fExternalTextureSupport
Definition: SkSLUtil.h:99