Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkSurface.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 9 | #include "include/gpu/GrDirectContext.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 10 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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 Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 15 | |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 16 | #ifdef SK_GL |
| 17 | |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 18 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(TextureBindingsResetTest, reporter, ctxInfo) { |
| 19 | #define GL(F) GR_GL_CALL(ctxInfo.glContext()->gl(), F) |
| 20 | |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 21 | auto dContext = ctxInfo.directContext(); |
| 22 | GrGpu* gpu = dContext->priv().getGpu(); |
| 23 | GrGLGpu* glGpu = static_cast<GrGLGpu*>(dContext->priv().getGpu()); |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 24 | |
| 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 Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 32 | if ((supportExternal = glGpu->glCaps().shaderCaps()->externalTextureSupport())) { |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 33 | targets.push_back({GR_GL_TEXTURE_EXTERNAL, GR_GL_TEXTURE_BINDING_EXTERNAL}); |
| 34 | } |
| 35 | bool supportRectangle; |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 36 | if ((supportRectangle = glGpu->glCaps().rectangleTextureSupport())) { |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 37 | 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. |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 72 | dContext->flushAndSubmit(); |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 73 | resetBindings(); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 74 | dContext->resetContext(); |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 75 | |
| 76 | // Test creating a texture and then resetting bindings. |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 77 | static constexpr SkISize kDims = {10, 10}; |
Brian Salomon | 4eb38b7 | 2019-08-05 12:58:39 -0400 | [diff] [blame] | 78 | auto format = gpu->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kNo); |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 79 | auto tex = gpu->createTexture(kDims, format, GrRenderable::kNo, 1, GrMipmapped::kNo, |
Brian Salomon | a90382f | 2019-09-17 09:01:56 -0400 | [diff] [blame] | 80 | SkBudgeted::kNo, GrProtected::kNo); |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 81 | REPORTER_ASSERT(reporter, tex); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 82 | dContext->resetGLTextureBindings(); |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 83 | checkBindings(); |
| 84 | resetBindings(); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 85 | dContext->resetContext(); |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 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); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 90 | auto surf = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kYes, info, 1, nullptr); |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 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 Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 100 | surf->flushAndSubmit(); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 101 | dContext->resetGLTextureBindings(); |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 102 | checkBindings(); |
| 103 | resetBindings(); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 104 | dContext->resetContext(); |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 105 | |
| 106 | if (supportExternal) { |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 107 | GrBackendTexture texture2D = dContext->createBackendTexture( |
Robert Phillips | 8062679 | 2019-06-04 07:16:10 -0400 | [diff] [blame] | 108 | 10, 10, kRGBA_8888_SkColorType, |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 109 | SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo); |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 110 | 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); |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 119 | GrBackendTexture backendTexture(10, 10, GrMipmapped::kNo, infoExternal); |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 120 | // Above texture creation will have messed with GL state and bindings. |
| 121 | resetBindings(); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 122 | dContext->resetContext(); |
| 123 | img = SkImage::MakeFromTexture(dContext, backendTexture, kTopLeft_GrSurfaceOrigin, |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 124 | kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr); |
| 125 | REPORTER_ASSERT(reporter, img); |
| 126 | surf->getCanvas()->drawImage(img, 0, 0); |
| 127 | img.reset(); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 128 | surf->flushAndSubmit(); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 129 | dContext->resetGLTextureBindings(); |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 130 | checkBindings(); |
| 131 | resetBindings(); |
| 132 | GL(DeleteTextures(1, &infoExternal.fID)); |
| 133 | ctxInfo.glContext()->destroyEGLImage(eglImage); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 134 | dContext->deleteBackendTexture(texture2D); |
| 135 | dContext->resetContext(); |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | if (supportRectangle) { |
Brian Salomon | 0f39699 | 2020-06-19 19:51:21 -0400 | [diff] [blame] | 139 | auto format = GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_RECTANGLE); |
| 140 | GrBackendTexture rectangleTexture = |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 141 | dContext->createBackendTexture(10, 10, format, GrMipmapped::kNo, GrRenderable::kNo); |
Brian Salomon | 0f39699 | 2020-06-19 19:51:21 -0400 | [diff] [blame] | 142 | if (rectangleTexture.isValid()) { |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 143 | img = SkImage::MakeFromTexture(dContext, rectangleTexture, kTopLeft_GrSurfaceOrigin, |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 144 | kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr); |
| 145 | REPORTER_ASSERT(reporter, img); |
| 146 | surf->getCanvas()->drawImage(img, 0, 0); |
| 147 | img.reset(); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 148 | surf->flushAndSubmit(); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 149 | dContext->resetGLTextureBindings(); |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 150 | checkBindings(); |
| 151 | resetBindings(); |
Adlai Holler | 14dc791 | 2020-08-11 15:48:49 +0000 | [diff] [blame] | 152 | dContext->deleteBackendTexture(rectangleTexture); |
Brian Salomon | 1f05d45 | 2019-02-08 12:33:08 -0500 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | GL(DeleteTextures(claimedIDs.count(), claimedIDs.begin())); |
| 157 | |
| 158 | #undef GL |
| 159 | } |
John Rosasco | a9b348f | 2019-11-08 13:18:15 -0800 | [diff] [blame] | 160 | |
| 161 | #endif // SK_GL |