2019-02-08 17:33:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2020-07-06 14:56:46 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2020-10-14 15:23:11 +00:00
|
|
|
#include "src/gpu/GrDirectContextPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/gpu/gl/GrGLDefines.h"
|
|
|
|
#include "src/gpu/gl/GrGLGpu.h"
|
|
|
|
#include "src/gpu/gl/GrGLUtil.h"
|
|
|
|
#include "tests/Test.h"
|
2019-02-08 17:33:08 +00:00
|
|
|
|
2019-11-08 21:18:15 +00:00
|
|
|
#ifdef SK_GL
|
|
|
|
|
2019-02-08 17:33:08 +00:00
|
|
|
DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(TextureBindingsResetTest, reporter, ctxInfo) {
|
|
|
|
#define GL(F) GR_GL_CALL(ctxInfo.glContext()->gl(), F)
|
|
|
|
|
2020-08-11 15:48:49 +00:00
|
|
|
auto dContext = ctxInfo.directContext();
|
|
|
|
GrGpu* gpu = dContext->priv().getGpu();
|
|
|
|
GrGLGpu* glGpu = static_cast<GrGLGpu*>(dContext->priv().getGpu());
|
2019-02-08 17:33:08 +00:00
|
|
|
|
|
|
|
struct Target {
|
|
|
|
GrGLenum fName;
|
|
|
|
GrGLenum fQuery;
|
|
|
|
};
|
|
|
|
SkTDArray<Target> targets;
|
|
|
|
targets.push_back({GR_GL_TEXTURE_2D, GR_GL_TEXTURE_BINDING_2D});
|
|
|
|
bool supportExternal;
|
2019-05-13 14:40:06 +00:00
|
|
|
if ((supportExternal = glGpu->glCaps().shaderCaps()->externalTextureSupport())) {
|
2019-02-08 17:33:08 +00:00
|
|
|
targets.push_back({GR_GL_TEXTURE_EXTERNAL, GR_GL_TEXTURE_BINDING_EXTERNAL});
|
|
|
|
}
|
|
|
|
bool supportRectangle;
|
2019-05-13 14:40:06 +00:00
|
|
|
if ((supportRectangle = glGpu->glCaps().rectangleTextureSupport())) {
|
2019-02-08 17:33:08 +00:00
|
|
|
targets.push_back({GR_GL_TEXTURE_RECTANGLE, GR_GL_TEXTURE_BINDING_RECTANGLE});
|
|
|
|
}
|
|
|
|
GrGLint numUnits;
|
|
|
|
GL(GetIntegerv(GR_GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &numUnits));
|
|
|
|
SkTDArray<GrGLuint> claimedIDs;
|
|
|
|
claimedIDs.setCount(numUnits * targets.count());
|
|
|
|
GL(GenTextures(claimedIDs.count(), claimedIDs.begin()));
|
|
|
|
|
|
|
|
auto resetBindings = [&] {
|
|
|
|
int i = 0;
|
|
|
|
for (int u = 0; u < numUnits; ++u) {
|
|
|
|
GL(ActiveTexture(GR_GL_TEXTURE0 + u));
|
|
|
|
for (auto target : targets) {
|
|
|
|
GL(BindTexture(target.fName, claimedIDs[i++]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
auto checkBindings = [&] {
|
|
|
|
int i = 0;
|
|
|
|
for (int u = 0; u < numUnits; ++u) {
|
|
|
|
GL(ActiveTexture(GR_GL_TEXTURE0 + u));
|
|
|
|
for (auto target : targets) {
|
|
|
|
GrGLuint boundID = ~0;
|
|
|
|
GL(GetIntegerv(target.fQuery, reinterpret_cast<GrGLint*>(&boundID)));
|
|
|
|
if (boundID != claimedIDs[i] && boundID != 0) {
|
|
|
|
ERRORF(reporter, "Unit %d, target 0x%04x has ID %d bound. Expected %d or 0.", u,
|
|
|
|
target.fName, boundID, claimedIDs[i]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialize texture unit/target combo bindings to 0.
|
2020-08-11 15:48:49 +00:00
|
|
|
dContext->flushAndSubmit();
|
2019-02-08 17:33:08 +00:00
|
|
|
resetBindings();
|
2020-08-11 15:48:49 +00:00
|
|
|
dContext->resetContext();
|
2019-02-08 17:33:08 +00:00
|
|
|
|
|
|
|
// Test creating a texture and then resetting bindings.
|
2020-02-07 19:17:25 +00:00
|
|
|
static constexpr SkISize kDims = {10, 10};
|
2019-08-05 16:58:39 +00:00
|
|
|
auto format = gpu->caps()->getDefaultBackendFormat(GrColorType::kRGBA_8888, GrRenderable::kNo);
|
2020-07-21 13:27:25 +00:00
|
|
|
auto tex = gpu->createTexture(kDims, format, GrRenderable::kNo, 1, GrMipmapped::kNo,
|
2019-09-17 13:01:56 +00:00
|
|
|
SkBudgeted::kNo, GrProtected::kNo);
|
2019-02-08 17:33:08 +00:00
|
|
|
REPORTER_ASSERT(reporter, tex);
|
2020-08-11 15:48:49 +00:00
|
|
|
dContext->resetGLTextureBindings();
|
2019-02-08 17:33:08 +00:00
|
|
|
checkBindings();
|
|
|
|
resetBindings();
|
2020-08-11 15:48:49 +00:00
|
|
|
dContext->resetContext();
|
2019-02-08 17:33:08 +00:00
|
|
|
|
|
|
|
// Test drawing and then resetting bindings. This should force a MIP regeneration if MIP
|
|
|
|
// maps are supported as well.
|
|
|
|
auto info = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
|
2020-08-11 15:48:49 +00:00
|
|
|
auto surf = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kYes, info, 1, nullptr);
|
2019-02-08 17:33:08 +00:00
|
|
|
surf->getCanvas()->clear(0x80FF0000);
|
|
|
|
auto img = surf->makeImageSnapshot();
|
|
|
|
surf->getCanvas()->clear(SK_ColorBLUE);
|
|
|
|
surf->getCanvas()->save();
|
|
|
|
surf->getCanvas()->scale(0.25, 0.25);
|
2020-12-23 16:18:24 +00:00
|
|
|
surf->getCanvas()->drawImage(img.get(), 0, 0, SkSamplingOptions({1.0f/3, 1.0f/3}), nullptr);
|
2019-02-08 17:33:08 +00:00
|
|
|
surf->getCanvas()->restore();
|
2020-05-14 19:45:44 +00:00
|
|
|
surf->flushAndSubmit();
|
2020-08-11 15:48:49 +00:00
|
|
|
dContext->resetGLTextureBindings();
|
2019-02-08 17:33:08 +00:00
|
|
|
checkBindings();
|
|
|
|
resetBindings();
|
2020-08-11 15:48:49 +00:00
|
|
|
dContext->resetContext();
|
2019-02-08 17:33:08 +00:00
|
|
|
|
|
|
|
if (supportExternal) {
|
2020-08-11 15:48:49 +00:00
|
|
|
GrBackendTexture texture2D = dContext->createBackendTexture(
|
2019-06-04 11:16:10 +00:00
|
|
|
10, 10, kRGBA_8888_SkColorType,
|
2020-07-21 13:27:25 +00:00
|
|
|
SkColors::kTransparent, GrMipmapped::kNo, GrRenderable::kNo, GrProtected::kNo);
|
2019-02-08 17:33:08 +00:00
|
|
|
GrGLTextureInfo info2D;
|
|
|
|
REPORTER_ASSERT(reporter, texture2D.getGLTextureInfo(&info2D));
|
|
|
|
GrEGLImage eglImage = ctxInfo.glContext()->texture2DToEGLImage(info2D.fID);
|
|
|
|
REPORTER_ASSERT(reporter, eglImage);
|
|
|
|
GrGLTextureInfo infoExternal;
|
|
|
|
infoExternal.fID = ctxInfo.glContext()->eglImageToExternalTexture(eglImage);
|
|
|
|
infoExternal.fTarget = GR_GL_TEXTURE_EXTERNAL;
|
|
|
|
infoExternal.fFormat = info2D.fFormat;
|
|
|
|
REPORTER_ASSERT(reporter, infoExternal.fID);
|
2020-07-21 13:27:25 +00:00
|
|
|
GrBackendTexture backendTexture(10, 10, GrMipmapped::kNo, infoExternal);
|
2019-02-08 17:33:08 +00:00
|
|
|
// Above texture creation will have messed with GL state and bindings.
|
|
|
|
resetBindings();
|
2020-08-11 15:48:49 +00:00
|
|
|
dContext->resetContext();
|
|
|
|
img = SkImage::MakeFromTexture(dContext, backendTexture, kTopLeft_GrSurfaceOrigin,
|
2019-02-08 17:33:08 +00:00
|
|
|
kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
|
|
|
|
REPORTER_ASSERT(reporter, img);
|
|
|
|
surf->getCanvas()->drawImage(img, 0, 0);
|
|
|
|
img.reset();
|
2020-05-14 19:45:44 +00:00
|
|
|
surf->flushAndSubmit();
|
2020-08-11 15:48:49 +00:00
|
|
|
dContext->resetGLTextureBindings();
|
2019-02-08 17:33:08 +00:00
|
|
|
checkBindings();
|
|
|
|
resetBindings();
|
|
|
|
GL(DeleteTextures(1, &infoExternal.fID));
|
|
|
|
ctxInfo.glContext()->destroyEGLImage(eglImage);
|
2020-08-11 15:48:49 +00:00
|
|
|
dContext->deleteBackendTexture(texture2D);
|
|
|
|
dContext->resetContext();
|
2019-02-08 17:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (supportRectangle) {
|
2020-06-19 23:51:21 +00:00
|
|
|
auto format = GrBackendFormat::MakeGL(GR_GL_RGBA8, GR_GL_TEXTURE_RECTANGLE);
|
|
|
|
GrBackendTexture rectangleTexture =
|
2020-08-11 15:48:49 +00:00
|
|
|
dContext->createBackendTexture(10, 10, format, GrMipmapped::kNo, GrRenderable::kNo);
|
2020-06-19 23:51:21 +00:00
|
|
|
if (rectangleTexture.isValid()) {
|
2020-08-11 15:48:49 +00:00
|
|
|
img = SkImage::MakeFromTexture(dContext, rectangleTexture, kTopLeft_GrSurfaceOrigin,
|
2019-02-08 17:33:08 +00:00
|
|
|
kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
|
|
|
|
REPORTER_ASSERT(reporter, img);
|
|
|
|
surf->getCanvas()->drawImage(img, 0, 0);
|
|
|
|
img.reset();
|
2020-05-14 19:45:44 +00:00
|
|
|
surf->flushAndSubmit();
|
2020-08-11 15:48:49 +00:00
|
|
|
dContext->resetGLTextureBindings();
|
2019-02-08 17:33:08 +00:00
|
|
|
checkBindings();
|
|
|
|
resetBindings();
|
2020-08-11 15:48:49 +00:00
|
|
|
dContext->deleteBackendTexture(rectangleTexture);
|
2019-02-08 17:33:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GL(DeleteTextures(claimedIDs.count(), claimedIDs.begin()));
|
|
|
|
|
|
|
|
#undef GL
|
|
|
|
}
|
2019-11-08 21:18:15 +00:00
|
|
|
|
|
|
|
#endif // SK_GL
|