blob: 04602fface8e0c20c7b687e515c9d58b13c06a6a [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"
9#include "src/gpu/GrContextPriv.h"
10#include "src/gpu/gl/GrGLDefines.h"
11#include "src/gpu/gl/GrGLGpu.h"
12#include "src/gpu/gl/GrGLUtil.h"
13#include "tests/Test.h"
Brian Salomon1f05d452019-02-08 12:33:08 -050014
15DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(TextureBindingsResetTest, reporter, ctxInfo) {
16#define GL(F) GR_GL_CALL(ctxInfo.glContext()->gl(), F)
17
18 GrContext* context = ctxInfo.grContext();
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040019 GrGpu* gpu = context->priv().getGpu();
20 GrGLGpu* glGpu = static_cast<GrGLGpu*>(context->priv().getGpu());
Brian Salomon1f05d452019-02-08 12:33:08 -050021
22 struct Target {
23 GrGLenum fName;
24 GrGLenum fQuery;
25 };
26 SkTDArray<Target> targets;
27 targets.push_back({GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BINDING_2D});
28 bool supportExternal;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040029 if ((supportExternal = glGpu->glCaps().shaderCaps()->externalTextureSupport())) {
Brian Salomon1f05d452019-02-08 12:33:08 -050030 targets.push_back({GR_GL_TEXTURE_EXTERNAL, GR_GL_TEXTURE_BINDING_EXTERNAL});
31 }
32 bool supportRectangle;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040033 if ((supportRectangle = glGpu->glCaps().rectangleTextureSupport())) {
Brian Salomon1f05d452019-02-08 12:33:08 -050034 targets.push_back({GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_BINDING_RECTANGLE});
35 }
36 GrGLint numUnits;
37 GL(GetIntegerv(GR_GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &numUnits));
38 SkTDArray<GrGLuint> claimedIDs;
39 claimedIDs.setCount(numUnits * targets.count());
40 GL(GenTextures(claimedIDs.count(), claimedIDs.begin()));
41
42 auto resetBindings = [&] {
43 int i = 0;
44 for (int u = 0; u < numUnits; ++u) {
45 GL(ActiveTexture(GR_GL_TEXTURE0 + u));
46 for (auto target : targets) {
47 GL(BindTexture(target.fName, claimedIDs[i++]));
48 }
49 }
50 };
51 auto checkBindings = [&] {
52 int i = 0;
53 for (int u = 0; u < numUnits; ++u) {
54 GL(ActiveTexture(GR_GL_TEXTURE0 + u));
55 for (auto target : targets) {
56 GrGLuint boundID = ~0;
57 GL(GetIntegerv(target.fQuery, reinterpret_cast<GrGLint*>(&boundID)));
58 if (boundID != claimedIDs[i] && boundID != 0) {
59 ERRORF(reporter, "Unit %d, target 0x%04x has ID %d bound. Expected %d or 0.", u,
60 target.fName, boundID, claimedIDs[i]);
61 return;
62 }
63 ++i;
64 }
65 }
66 };
67
68 // Initialize texture unit/target combo bindings to 0.
69 context->flush();
70 resetBindings();
71 context->resetContext();
72
73 // Test creating a texture and then resetting bindings.
74 GrSurfaceDesc desc;
75 desc.fWidth = desc.fHeight = 10;
76 desc.fConfig = kRGBA_8888_GrPixelConfig;
Brian Salomon4eb38b72019-08-05 12:58:39 -040077 auto format = gpu->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kNo);
Brian Salomona90382f2019-09-17 09:01:56 -040078 auto tex = gpu->createTexture(desc, format, GrRenderable::kNo, 1, GrMipMapped::kNo,
79 SkBudgeted::kNo, GrProtected::kNo);
Brian Salomon1f05d452019-02-08 12:33:08 -050080 REPORTER_ASSERT(reporter, tex);
81 context->resetGLTextureBindings();
82 checkBindings();
83 resetBindings();
84 context->resetContext();
85
86 // Test drawing and then resetting bindings. This should force a MIP regeneration if MIP
87 // maps are supported as well.
88 auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
89 auto surf = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info, 1, nullptr);
90 surf->getCanvas()->clear(0x80FF0000);
91 auto img = surf->makeImageSnapshot();
92 surf->getCanvas()->clear(SK_ColorBLUE);
93 surf->getCanvas()->save();
94 surf->getCanvas()->scale(0.25, 0.25);
95 SkPaint paint;
96 paint.setFilterQuality(kHigh_SkFilterQuality);
97 surf->getCanvas()->drawImage(img, 0, 0, &paint);
98 surf->getCanvas()->restore();
99 surf->flush();
100 context->resetGLTextureBindings();
101 checkBindings();
102 resetBindings();
103 context->resetContext();
104
105 if (supportExternal) {
Robert Phillips4bdd36f2019-06-04 11:03:06 -0400106 GrBackendTexture texture2D = context->createBackendTexture(
Robert Phillips80626792019-06-04 07:16:10 -0400107 10, 10, kRGBA_8888_SkColorType,
Robert Phillipsda2e67a2019-07-01 15:04:06 -0400108 SkColors::kTransparent, GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
Brian Salomon1f05d452019-02-08 12:33:08 -0500109 GrGLTextureInfo info2D;
110 REPORTER_ASSERT(reporter, texture2D.getGLTextureInfo(&info2D));
111 GrEGLImage eglImage = ctxInfo.glContext()->texture2DToEGLImage(info2D.fID);
112 REPORTER_ASSERT(reporter, eglImage);
113 GrGLTextureInfo infoExternal;
114 infoExternal.fID = ctxInfo.glContext()->eglImageToExternalTexture(eglImage);
115 infoExternal.fTarget = GR_GL_TEXTURE_EXTERNAL;
116 infoExternal.fFormat = info2D.fFormat;
117 REPORTER_ASSERT(reporter, infoExternal.fID);
118 GrBackendTexture backendTexture(10, 10, GrMipMapped::kNo, infoExternal);
119 // Above texture creation will have messed with GL state and bindings.
120 resetBindings();
121 context->resetContext();
122 img = SkImage::MakeFromTexture(context, backendTexture, kTopLeft_GrSurfaceOrigin,
123 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
124 REPORTER_ASSERT(reporter, img);
125 surf->getCanvas()->drawImage(img, 0, 0);
126 img.reset();
127 surf->flush();
128 context->resetGLTextureBindings();
129 checkBindings();
130 resetBindings();
131 GL(DeleteTextures(1, &infoExternal.fID));
132 ctxInfo.glContext()->destroyEGLImage(eglImage);
Robert Phillips5c7a25b2019-05-20 08:38:07 -0400133 context->deleteBackendTexture(texture2D);
Brian Salomon1f05d452019-02-08 12:33:08 -0500134 context->resetContext();
135 }
136
137 if (supportRectangle) {
138 GrGLuint id = ctxInfo.glContext()->createTextureRectangle(10, 10, GR_GL_RGBA, GR_GL_RGBA,
139 GR_GL_UNSIGNED_BYTE, nullptr);
140 // Above texture creation will have messed with GL state and bindings.
141 resetBindings();
142 context->resetContext();
143 if (id) {
144 GrGLTextureInfo info;
145 info.fTarget = GR_GL_TEXTURE_RECTANGLE;
146 info.fFormat = GR_GL_RGBA8;
147 info.fID = id;
148 GrBackendTexture backendTexture(10, 10, GrMipMapped::kNo, info);
149 img = SkImage::MakeFromTexture(context, backendTexture, kTopLeft_GrSurfaceOrigin,
150 kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
151 REPORTER_ASSERT(reporter, img);
152 surf->getCanvas()->drawImage(img, 0, 0);
153 img.reset();
154 surf->flush();
155 context->resetGLTextureBindings();
156 checkBindings();
157 resetBindings();
158 GL(DeleteTextures(1, &id));
159 context->resetContext();
160 }
161 }
162
163 GL(DeleteTextures(claimedIDs.count(), claimedIDs.begin()));
164
165#undef GL
166}