blob: acfbae659234ad091b82cad8ac4b0a9f28569b81 [file] [log] [blame]
Brian Salomon1f05d452019-02-08 12:33:08 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkSurface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -04009#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/gpu/GrContextPriv.h"
11#include "src/gpu/gl/GrGLDefines.h"
12#include "src/gpu/gl/GrGLGpu.h"
13#include "src/gpu/gl/GrGLUtil.h"
14#include "tests/Test.h"
Brian Salomon1f05d452019-02-08 12:33:08 -050015
John Rosascoa9b348f2019-11-08 13:18:15 -080016#ifdef SK_GL
17
Brian Salomon1f05d452019-02-08 12:33:08 -050018DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(TextureBindingsResetTest, reporter, ctxInfo) {
19#define GL(F) GR_GL_CALL(ctxInfo.glContext()->gl(), F)
20
Robert Phillips6d344c32020-07-06 10:56:46 -040021 auto context = ctxInfo.directContext();
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040022 GrGpu* gpu = context->priv().getGpu();
23 GrGLGpu* glGpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Brian Salomon1f05d452019-02-08 12:33:08 -050024
25 struct Target {
26 GrGLenum fName;
27 GrGLenum fQuery;
28 };
29 SkTDArray<Target> targets;
30 targets.push_back({GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BINDING_2D});
31 bool supportExternal;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040032 if ((supportExternal = glGpu->glCaps().shaderCaps()->externalTextureSupport())) {
Brian Salomon1f05d452019-02-08 12:33:08 -050033 targets.push_back({GR_GL_TEXTURE_EXTERNAL, GR_GL_TEXTURE_BINDING_EXTERNAL});
34 }
35 bool supportRectangle;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040036 if ((supportRectangle = glGpu->glCaps().rectangleTextureSupport())) {
Brian Salomon1f05d452019-02-08 12:33:08 -050037 targets.push_back({GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_BINDING_RECTANGLE});
38 }
39 GrGLint numUnits;
40 GL(GetIntegerv(GR_GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &numUnits));
41 SkTDArray<GrGLuint> claimedIDs;
42 claimedIDs.setCount(numUnits * targets.count());
43 GL(GenTextures(claimedIDs.count(), claimedIDs.begin()));
44
45 auto resetBindings = [&] {
46 int i = 0;
47 for (int u = 0; u < numUnits; ++u) {
48 GL(ActiveTexture(GR_GL_TEXTURE0 + u));
49 for (auto target : targets) {
50 GL(BindTexture(target.fName, claimedIDs[i++]));
51 }
52 }
53 };
54 auto checkBindings = [&] {
55 int i = 0;
56 for (int u = 0; u < numUnits; ++u) {
57 GL(ActiveTexture(GR_GL_TEXTURE0 + u));
58 for (auto target : targets) {
59 GrGLuint boundID = ~0;
60 GL(GetIntegerv(target.fQuery, reinterpret_cast<GrGLint*>(&boundID)));
61 if (boundID != claimedIDs[i] && boundID != 0) {
62 ERRORF(reporter, "Unit %d, target 0x%04x has ID %d bound. Expected %d or 0.", u,
63 target.fName, boundID, claimedIDs[i]);
64 return;
65 }
66 ++i;
67 }
68 }
69 };
70
71 // Initialize texture unit/target combo bindings to 0.
Greg Daniel0a2464f2020-05-14 15:45:44 -040072 context->flushAndSubmit();
Brian Salomon1f05d452019-02-08 12:33:08 -050073 resetBindings();
74 context->resetContext();
75
76 // Test creating a texture and then resetting bindings.
Brian Salomona56a7462020-02-07 14:17:25 -050077 static constexpr SkISize kDims = {10, 10};
Brian Salomon4eb38b72019-08-05 12:58:39 -040078 auto format = gpu->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kNo);
Brian Salomona56a7462020-02-07 14:17:25 -050079 auto tex = gpu->createTexture(kDims, format, GrRenderable::kNo, 1, GrMipMapped::kNo,
Brian Salomona90382f2019-09-17 09:01:56 -040080 SkBudgeted::kNo, GrProtected::kNo);
Brian Salomon1f05d452019-02-08 12:33:08 -050081 REPORTER_ASSERT(reporter, tex);
82 context->resetGLTextureBindings();
83 checkBindings();
84 resetBindings();
85 context->resetContext();
86
87 // Test drawing and then resetting bindings. This should force a MIP regeneration if MIP
88 // maps are supported as well.
89 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
90 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
91 surf->getCanvas()->clear(0x80FF0000);
92 auto img = surf->makeImageSnapshot();
93 surf->getCanvas()->clear(SK_ColorBLUE);
94 surf->getCanvas()->save();
95 surf->getCanvas()->scale(0.25, 0.25);
96 SkPaint paint;
97 paint.setFilterQuality(kHigh_SkFilterQuality);
98 surf->getCanvas()->drawImage(img, 0, 0, &paint);
99 surf->getCanvas()->restore();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400100 surf->flushAndSubmit();
Brian Salomon1f05d452019-02-08 12:33:08 -0500101 context->resetGLTextureBindings();
102 checkBindings();
103 resetBindings();
104 context->resetContext();
105
106 if (supportExternal) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400107 GrBackendTexture texture2D = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400108 10, 10, kRGBA_8888_SkColorType,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400109 SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
Brian Salomon1f05d452019-02-08 12:33:08 -0500110 GrGLTextureInfo info2D;
111 REPORTER_ASSERT(reporter, texture2D.getGLTextureInfo(&info2D));
112 GrEGLImage eglImage = ctxInfo.glContext()->texture2DToEGLImage(info2D.fID);
113 REPORTER_ASSERT(reporter, eglImage);
114 GrGLTextureInfo infoExternal;
115 infoExternal.fID = ctxInfo.glContext()->eglImageToExternalTexture(eglImage);
116 infoExternal.fTarget = GR_GL_TEXTURE_EXTERNAL;
117 infoExternal.fFormat = info2D.fFormat;
118 REPORTER_ASSERT(reporter, infoExternal.fID);
119 GrBackendTexture backendTexture(10, 10, GrMipMapped::kNo, infoExternal);
120 // Above texture creation will have messed with GL state and bindings.
121 resetBindings();
122 context->resetContext();
123 img = SkImage::MakeFromTexture(context, backendTexture, kTopLeft_GrSurfaceOrigin,
124 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
125 REPORTER_ASSERT(reporter, img);
126 surf->getCanvas()->drawImage(img, 0, 0);
127 img.reset();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400128 surf->flushAndSubmit();
Brian Salomon1f05d452019-02-08 12:33:08 -0500129 context->resetGLTextureBindings();
130 checkBindings();
131 resetBindings();
132 GL(DeleteTextures(1, &infoExternal.fID));
133 ctxInfo.glContext()->destroyEGLImage(eglImage);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400134 context->deleteBackendTexture(texture2D);
Brian Salomon1f05d452019-02-08 12:33:08 -0500135 context->resetContext();
136 }
137
138 if (supportRectangle) {
Brian Salomon0f396992020-06-19 19:51:21 -0400139 auto format = GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_RECTANGLE);
140 GrBackendTexture rectangleTexture =
141 context->createBackendTexture(10, 10, format, GrMipMapped::kNo, GrRenderable::kNo);
142 if (rectangleTexture.isValid()) {
143 img = SkImage::MakeFromTexture(context, rectangleTexture, kTopLeft_GrSurfaceOrigin,
Brian Salomon1f05d452019-02-08 12:33:08 -0500144 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
145 REPORTER_ASSERT(reporter, img);
146 surf->getCanvas()->drawImage(img, 0, 0);
147 img.reset();
Greg Daniel0a2464f2020-05-14 15:45:44 -0400148 surf->flushAndSubmit();
Brian Salomon1f05d452019-02-08 12:33:08 -0500149 context->resetGLTextureBindings();
150 checkBindings();
151 resetBindings();
Brian Salomon0f396992020-06-19 19:51:21 -0400152 context->deleteBackendTexture(rectangleTexture);
Brian Salomon1f05d452019-02-08 12:33:08 -0500153 }
154 }
155
156 GL(DeleteTextures(claimedIDs.count(), claimedIDs.begin()));
157
158#undef GL
159}
John Rosascoa9b348f2019-11-08 13:18:15 -0800160
161#endif // SK_GL