Make A8 readback work in more cases and improve testing.
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1584563002
Review URL: https://codereview.chromium.org/1584563002
diff --git a/src/gpu/gl/GrGLCaps.cpp b/src/gpu/gl/GrGLCaps.cpp
index 678c62a..0c2ed73 100644
--- a/src/gpu/gl/GrGLCaps.cpp
+++ b/src/gpu/gl/GrGLCaps.cpp
@@ -637,7 +637,22 @@
}
if (kGL_GrGLStandard == intf->fStandard) {
- // All of our renderable configs can be converted to each other by glReadPixels in OpenGL.
+ // Some OpenGL implementations allow GL_ALPHA as a format to glReadPixels. However,
+ // the manual (https://www.opengl.org/sdk/docs/man/) says only these formats are allowed:
+ // GL_STENCIL_INDEX, GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL, GL_RED, GL_GREEN, GL_BLUE,
+ // GL_RGB, GL_BGR, GL_RGBA, and GL_BGRA. We check for the subset that we would use.
+ if (readFormat != GR_GL_RED && readFormat != GR_GL_RGB && readFormat != GR_GL_RGBA &&
+ readFormat != GR_GL_BGRA) {
+ return false;
+ }
+ // There is also a set of allowed types, but all the types we use are in the set:
+ // GL_UNSIGNED_BYTE, GL_BYTE, GL_UNSIGNED_SHORT, GL_SHORT, GL_UNSIGNED_INT, GL_INT,
+ // GL_HALF_FLOAT, GL_FLOAT, GL_UNSIGNED_BYTE_3_3_2, GL_UNSIGNED_BYTE_2_3_3_REV,
+ // GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_5_6_5_REV, GL_UNSIGNED_SHORT_4_4_4_4,
+ // GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_UNSIGNED_SHORT_5_5_5_1, GL_UNSIGNED_SHORT_1_5_5_5_REV,
+ // GL_UNSIGNED_INT_8_8_8_8, GL_UNSIGNED_INT_8_8_8_8_REV,GL_UNSIGNED_INT_10_10_10_2,
+ // GL_UNSIGNED_INT_2_10_10_10_REV, GL_UNSIGNED_INT_24_8, GL_UNSIGNED_INT_10F_11F_11F_REV,
+ // GL_UNSIGNED_INT_5_9_9_9_REV, or GL_FLOAT_32_UNSIGNED_INT_24_8_REV.
return true;
}
@@ -1051,6 +1066,17 @@
*externalFormat = fConfigTable[memoryConfig].fFormats.fExternalFormat[usage];
*externalType = fConfigTable[memoryConfig].fFormats.fExternalType;
+ // When GL_RED is supported as a texture format, our alpha-only textures are stored using
+ // GL_RED and we swizzle in order to map all components to 'r'. However, in this case the
+ // surface is not alpha-only and we want alpha to really mean the alpha component of the
+ // texture, not the red component.
+ if (memoryIsAlphaOnly && !surfaceIsAlphaOnly) {
+ if (this->textureRedSupport()) {
+ SkASSERT(GR_GL_RED == *externalFormat);
+ *externalFormat = GR_GL_ALPHA;
+ }
+ }
+
return true;
}
diff --git a/src/gpu/gl/GrGLGpu.cpp b/src/gpu/gl/GrGLGpu.cpp
index 8033ea6..972049a 100644
--- a/src/gpu/gl/GrGLGpu.cpp
+++ b/src/gpu/gl/GrGLGpu.cpp
@@ -1951,7 +1951,8 @@
ReadPixelTempDrawInfo* tempDrawInfo) {
// This subclass can only read pixels from a render target. We could use glTexSubImage2D on
// GL versions that support it but we don't today.
- if (!srcSurface->asRenderTarget()) {
+ GrRenderTarget* srcAsRT = srcSurface->asRenderTarget();
+ if (!srcAsRT) {
ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
}
@@ -1992,13 +1993,20 @@
tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
tempDrawInfo->fSwapRAndB = true;
ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
+ } else if (kRequireDraw_DrawPreference == *drawPreference &&
+ readConfig == kAlpha_8_GrPixelConfig &&
+ !this->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
+ // On some GLs there is no renderable single channel format. In that case we fall back
+ // to rendering to a RGBA and then will extract the alpha from that.
+ if (this->caps()->isConfigRenderable(kRGBA_8888_GrPixelConfig, false)) {
+ tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
+ } else {
+ return false;
+ }
}
- GrRenderTarget* srcAsRT = srcSurface->asRenderTarget();
- if (!srcAsRT) {
- ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
- } else if (read_pixels_pays_for_y_flip(srcAsRT, this->glCaps(), width, height, readConfig,
- rowBytes)) {
+ if (srcAsRT && read_pixels_pays_for_y_flip(srcAsRT, this->glCaps(), width, height, readConfig,
+ rowBytes)) {
ElevateDrawPreference(drawPreference, kGpuPrefersDraw_DrawPreference);
}
@@ -2049,6 +2057,28 @@
SkFAIL("Unknown resolve type");
}
+ // We have a special case fallback for reading alpha from a RGBA/BGRA surface. We will read
+ // back all the channels as RGBA and then extract A.
+ // This must be called after the RT is bound as the FBO above.
+ if (!this->glCaps().readPixelsSupported(this->glInterface(), config, tgt->config())) {
+ if ((tgt->config() == kRGBA_8888_GrPixelConfig ||
+ tgt->config() == kBGRA_8888_GrPixelConfig) &&
+ kAlpha_8_GrPixelConfig == config) {
+ SkAutoTDeleteArray<uint32_t> temp(new uint32_t[width * height * 4]);
+ if (this->onReadPixels(surface, left, top, width, height, kRGBA_8888_GrPixelConfig,
+ temp.get(), width*4)) {
+ uint8_t* dst = reinterpret_cast<uint8_t*>(buffer);
+ for (int j = 0; j < height; ++j) {
+ for (int i = 0; i < width; ++i) {
+ dst[j*rowBytes + i] = (0xFF000000U & temp[j*width+i]) >> 24;
+ }
+ }
+ return true;
+ }
+ }
+ return false;
+ }
+
const GrGLIRect& glvp = tgt->getViewport();
// the read rect is viewport-relative
diff --git a/tests/ReadWriteAlphaTest.cpp b/tests/ReadWriteAlphaTest.cpp
index 1ee6b09..a33d57d 100644
--- a/tests/ReadWriteAlphaTest.cpp
+++ b/tests/ReadWriteAlphaTest.cpp
@@ -8,7 +8,7 @@
#include "Test.h"
// This test is specific to the GPU backend.
-#if SK_SUPPORT_GPU && !defined(SK_BUILD_FOR_ANDROID)
+#if SK_SUPPORT_GPU
#include "GrContext.h"
#include "SkGpuDevice.h"
@@ -18,88 +18,147 @@
static const int Y_SIZE = 13;
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadWriteAlpha, reporter, context) {
- unsigned char textureData[X_SIZE][Y_SIZE];
+ unsigned char alphaData[X_SIZE][Y_SIZE];
- memset(textureData, 0, X_SIZE * Y_SIZE);
+ memset(alphaData, 0, X_SIZE * Y_SIZE);
- GrSurfaceDesc desc;
-
- // let Skia know we will be using this texture as a render target
- desc.fFlags = kRenderTarget_GrSurfaceFlag;
- // it is a single channel texture
- desc.fConfig = kAlpha_8_GrPixelConfig;
- desc.fWidth = X_SIZE;
- desc.fHeight = Y_SIZE;
-
- // We are initializing the texture with zeros here
- GrTexture* texture = context->textureProvider()->createTexture(desc, false, textureData, 0);
- if (!texture) {
- return;
- }
-
- SkAutoTUnref<GrTexture> au(texture);
-
- // create a distinctive texture
- for (int y = 0; y < Y_SIZE; ++y) {
- for (int x = 0; x < X_SIZE; ++x) {
- textureData[x][y] = x*Y_SIZE+y;
- }
- }
-
- // upload the texture
- texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
- textureData, 0);
-
+ bool match;
unsigned char readback[X_SIZE][Y_SIZE];
- // clear readback to something non-zero so we can detect readback failures
- memset(readback, 0x1, X_SIZE * Y_SIZE);
+ for (int rt = 0; rt < 2; ++rt) {
+ GrSurfaceDesc desc;
+ // let Skia know we will be using this texture as a render target
+ desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
+ // it is a single channel texture
+ desc.fConfig = kAlpha_8_GrPixelConfig;
+ desc.fWidth = X_SIZE;
+ desc.fHeight = Y_SIZE;
- // read the texture back
- texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
- readback, 0);
+ // We are initializing the texture with zeros here
+ SkAutoTUnref<GrTexture> texture(
+ context->textureProvider()->createTexture(desc, false, alphaData, 0));
+ if (!texture) {
+ if (!rt) {
+ ERRORF(reporter, "Could not create alpha texture.");
+ }
+ continue;
+ }
- // make sure the original & read back versions match
- bool match = true;
+ // create a distinctive texture
+ for (int y = 0; y < Y_SIZE; ++y) {
+ for (int x = 0; x < X_SIZE; ++x) {
+ alphaData[x][y] = x*Y_SIZE+y;
+ }
+ }
- for (int y = 0; y < Y_SIZE; ++y) {
- for (int x = 0; x < X_SIZE; ++x) {
- if (textureData[x][y] != readback[x][y]) {
- match = false;
+ // upload the texture
+ texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
+ alphaData, 0);
+
+ // clear readback to something non-zero so we can detect readback failures
+ memset(readback, 0x1, X_SIZE * Y_SIZE);
+
+ // read the texture back
+ texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
+ readback, 0);
+
+ // make sure the original & read back versions match
+ match = true;
+
+ for (int y = 0; y < Y_SIZE && match; ++y) {
+ for (int x = 0; x < X_SIZE && match; ++x) {
+ if (alphaData[x][y] != readback[x][y]) {
+ SkDebugf("Failed alpha readback. Expected: 0x%02x, "
+ "Got: 0x%02x at (%d,%d), rt: %d", alphaData[x][y], readback[x][y], x,
+ y, rt);
+ match = false;
+ }
+ }
+ }
+
+ // Now try writing on the single channel texture (if we could create as a RT).
+ if (texture->asRenderTarget()) {
+ SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
+ SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(
+ texture->asRenderTarget(), &props, SkGpuDevice::kUninit_InitContents));
+ SkCanvas canvas(device);
+
+ SkPaint paint;
+
+ const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
+
+ paint.setColor(SK_ColorWHITE);
+
+ canvas.drawRect(rect, paint);
+
+ texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, readback, 0);
+
+ match = true;
+
+ for (int y = 0; y < Y_SIZE && match; ++y) {
+ for (int x = 0; x < X_SIZE && match; ++x) {
+ if (0xFF != readback[x][y]) {
+ ERRORF(reporter,
+ "Failed alpha readback after clear. Expected: 0xFF, Got: 0x%02x at "
+ "(%d,%d)", readback[x][y], x, y);
+ match = false;
+ }
+ }
}
}
}
- REPORTER_ASSERT(reporter, match);
+ // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
+ // once with a render target.
+ for (int cfg = 0; cfg < 2; ++cfg) {
+ for (int rt = 0; rt < 2; ++rt) {
+ GrSurfaceDesc desc;
+ desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
+ desc.fConfig = cfg ? kBGRA_8888_GrPixelConfig : kRGBA_8888_GrPixelConfig;
+ desc.fWidth = X_SIZE;
+ desc.fHeight = Y_SIZE;
- // Now try writing on the single channel texture
- SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType);
- SkAutoTUnref<SkBaseDevice> device(SkGpuDevice::Create(texture->asRenderTarget(), &props,
- SkGpuDevice::kUninit_InitContents));
- SkCanvas canvas(device);
+ uint32_t rgbaData[X_SIZE][Y_SIZE];
+ // Make the alpha channel of the rgba texture come from alphaData.
+ for (int y = 0; y < Y_SIZE; ++y) {
+ for (int x = 0; x < X_SIZE; ++x) {
+ rgbaData[x][y] = GrColorPackRGBA(6, 7, 8, alphaData[x][y]);
+ }
+ }
+ SkAutoTUnref<GrTexture> texture(
+ context->textureProvider()->createTexture(desc, false, rgbaData, 0));
+ if (!texture) {
+ // We always expect to be able to create a RGBA texture
+ if (!rt && kRGBA_8888_GrPixelConfig == desc.fConfig) {
+ ERRORF(reporter, "Failed to create RGBA texture.");
+ }
+ continue;
+ }
- SkPaint paint;
+ // clear readback to something non-zero so we can detect readback failures
+ memset(readback, 0x0, X_SIZE * Y_SIZE);
- const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
+ // read the texture back
+ texture->readPixels(0, 0, desc.fWidth, desc.fHeight, kAlpha_8_GrPixelConfig, readback,
+ 0);
- paint.setColor(SK_ColorWHITE);
+ match = true;
- canvas.drawRect(rect, paint);
-
- texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
- readback, 0);
-
- match = true;
-
- for (int y = 0; y < Y_SIZE; ++y) {
- for (int x = 0; x < X_SIZE; ++x) {
- if (0xFF != readback[x][y]) {
- match = false;
+ for (int y = 0; y < Y_SIZE && match; ++y) {
+ for (int x = 0; x < X_SIZE && match; ++x) {
+ if (alphaData[x][y] != readback[x][y]) {
+ texture->readPixels(0, 0, desc.fWidth, desc.fHeight,
+ kAlpha_8_GrPixelConfig, readback, 0);
+ ERRORF(reporter,
+ "Failed alpha readback from cfg %d. Expected: 0x%02x, Got: 0x%02x at "
+ "(%d,%d), rt:%d", desc.fConfig, alphaData[x][y], readback[x][y], x,
+ y, rt);
+ match = false;
+ }
+ }
}
}
}
-
- REPORTER_ASSERT(reporter, match);
}
#endif