GrPixelConfig readConfig,
std::function<void (GrGLenum, GrGLint*)> getIntegerv,
std::function<bool ()> bindRenderTarget) const {
- SkASSERT(this->isConfigRenderable(rtConfig, false));
+ // If it's not possible to even have a render target of rtConfig then read pixels is
+ // not supported regardless of readConfig.
+ if (!this->isConfigRenderable(rtConfig, false)) {
+ return false;
+ }
GrGLenum readFormat;
GrGLenum readType;
}
if (kGL_GrGLStandard == 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;
}
*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;
}
}
}
+static bool requires_srgb_conversion(GrPixelConfig a, GrPixelConfig b) {
+ if (GrPixelConfigIsSRGB(a)) {
+ return !GrPixelConfigIsSRGB(b) && !GrPixelConfigIsAlphaOnly(b);
+ } else if (GrPixelConfigIsSRGB(b)) {
+ return !GrPixelConfigIsSRGB(a) && !GrPixelConfigIsAlphaOnly(a);
+ }
+ return false;
+}
+
bool GrGLGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
GrPixelConfig readConfig, DrawPreference* drawPreference,
ReadPixelTempDrawInfo* tempDrawInfo) {
- GrRenderTarget* srcAsRT = srcSurface->asRenderTarget();
-
- // 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 (!srcAsRT) {
- ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
- }
-
- if (GrPixelConfigIsSRGB(srcSurface->config()) != GrPixelConfigIsSRGB(readConfig)) {
- ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
- }
-
- tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
- tempDrawInfo->fReadConfig = readConfig;
+ GrPixelConfig srcConfig = srcSurface->config();
- // These settings we will always want if a temp draw is performed. The config is set below
- // depending on whether we want to do a R/B swap or not.
+ // These settings we will always want if a temp draw is performed.
tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
tempDrawInfo->fTempSurfaceDesc.fWidth = width;
tempDrawInfo->fTempSurfaceDesc.fHeight = height;
tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
tempDrawInfo->fUseExactScratch = this->glCaps().partialFBOReadIsSlow();
- // Start off assuming that any temp draw should be to the readConfig, then check if that will
- // be inefficient.
- GrPixelConfig srcConfig = srcSurface->config();
- tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
+ // For now assume no swizzling, we may change that below.
+ tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
+
+ // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
+ // from will be srcConfig and we will read readConfig pixels from it.
+ // Not that if we require a draw and return a non-renderable format for the temp surface the
+ // base class will fail for us.
+ tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
+ tempDrawInfo->fReadConfig = readConfig;
+
+ if (requires_srgb_conversion(srcConfig, readConfig)) {
+ if (!this->readPixelsSupported(readConfig, readConfig)) {
+ return false;
+ }
+ // Draw to do srgb to linear conversion or vice versa.
+ ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
+ tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
+ tempDrawInfo->fReadConfig = readConfig;
+ return true;
+ }
+
+ GrRenderTarget* srcAsRT = srcSurface->asRenderTarget();
+ if (!srcAsRT) {
+ // For now keep assuming the draw is not a format transformation, just a draw to get to a
+ // RT. We may add additional transformations below.
+ ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
+ }
if (this->glCaps().rgba8888PixelsOpsAreSlow() && kRGBA_8888_GrPixelConfig == readConfig &&
this->readPixelsSupported(kBGRA_8888_GrPixelConfig, kBGRA_8888_GrPixelConfig)) {
if (readConfig == kBGRA_8888_GrPixelConfig &&
this->glCaps().isConfigRenderable(kRGBA_8888_GrPixelConfig, false) &&
this->readPixelsSupported(kRGBA_8888_GrPixelConfig, kRGBA_8888_GrPixelConfig)) {
-
+ // We're trying to read BGRA but it's not supported. If RGBA is renderable and
+ // we can read it back, then do a swizzling draw to a RGBA and read it back (which
+ // will effectively be BGRA).
tempDrawInfo->fTempSurfaceDesc.fConfig = kRGBA_8888_GrPixelConfig;
tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
tempDrawInfo->fReadConfig = kRGBA_8888_GrPixelConfig;
ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
+ } else if (readConfig == kAlpha_8_GrPixelConfig) {
+ // onReadPixels implements a fallback for cases where we are want to read kAlpha_8,
+ // it's unsupported, but 32bit RGBA reads are supported.
+ // Don't attempt to do any srgb conversions since we only care about alpha.
+ GrPixelConfig cpuTempConfig = kRGBA_8888_GrPixelConfig;
+ if (GrPixelConfigIsSRGB(srcSurface->config())) {
+ cpuTempConfig = kSRGBA_8888_GrPixelConfig;
+ }
+ if (!this->readPixelsSupported(srcSurface, cpuTempConfig)) {
+ // If we can't read RGBA from the src try to draw to a kRGBA_8888 (or kSRGBA_8888)
+ // first and then onReadPixels will read that to a 32bit temporary buffer.
+ if (this->caps()->isConfigRenderable(cpuTempConfig, false)) {
+ ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
+ tempDrawInfo->fTempSurfaceDesc.fConfig = cpuTempConfig;
+ tempDrawInfo->fReadConfig = kAlpha_8_GrPixelConfig;
+ } else {
+ return false;
+ }
+ } else {
+ SkASSERT(tempDrawInfo->fTempSurfaceDesc.fConfig == srcConfig);
+ SkASSERT(tempDrawInfo->fReadConfig == kAlpha_8_GrPixelConfig);
+ }
} else {
return false;
}
size_t rowBytes) {
SkASSERT(surface);
- GrGLRenderTarget* tgt = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
- if (!tgt) {
+ GrGLRenderTarget* renderTarget = static_cast<GrGLRenderTarget*>(surface->asRenderTarget());
+ if (!renderTarget) {
return false;
}
// OpenGL doesn't do sRGB <-> linear conversions when reading and writing pixels.
- if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
+ if (requires_srgb_conversion(surface->config(), config)) {
+ return false;
+ }
+
+ // We have a special case fallback for reading eight bit alpha. We will read back all four 8
+ // bit channels as RGBA and then extract A.
+ if (!this->readPixelsSupported(renderTarget, config)) {
+ // Don't attempt to do any srgb conversions since we only care about alpha.
+ GrPixelConfig tempConfig = kRGBA_8888_GrPixelConfig;
+ if (GrPixelConfigIsSRGB(renderTarget->config())) {
+ tempConfig = kSRGBA_8888_GrPixelConfig;
+ }
+ if (kAlpha_8_GrPixelConfig == config &&
+ this->readPixelsSupported(renderTarget, tempConfig)) {
+ SkAutoTDeleteArray<uint32_t> temp(new uint32_t[width * height * 4]);
+ if (this->onReadPixels(renderTarget, left, top, width, height, tempConfig, 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;
}
GrGLenum externalFormat;
GrGLenum externalType;
- if (!this->glCaps().getReadPixelsFormat(surface->config(), config, &externalFormat,
+ if (!this->glCaps().getReadPixelsFormat(renderTarget->config(), config, &externalFormat,
&externalType)) {
return false;
}
bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
// resolve the render target if necessary
- switch (tgt->getResolveType()) {
+ switch (renderTarget->getResolveType()) {
case GrGLRenderTarget::kCantResolve_ResolveType:
return false;
case GrGLRenderTarget::kAutoResolves_ResolveType:
- this->flushRenderTarget(tgt, &SkIRect::EmptyIRect());
+ this->flushRenderTarget(renderTarget, &SkIRect::EmptyIRect());
break;
case GrGLRenderTarget::kCanResolve_ResolveType:
- this->onResolveRenderTarget(tgt);
+ this->onResolveRenderTarget(renderTarget);
// we don't track the state of the READ FBO ID.
fStats.incRenderTargetBinds();
- GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, tgt->textureFBOID()));
+ GL_CALL(BindFramebuffer(GR_GL_READ_FRAMEBUFFER, renderTarget->textureFBOID()));
break;
default:
SkFAIL("Unknown resolve type");
}
- const GrGLIRect& glvp = tgt->getViewport();
+ const GrGLIRect& glvp = renderTarget->getViewport();
// the read rect is viewport-relative
GrGLIRect readRect;
- readRect.setRelativeTo(glvp, left, top, width, height, tgt->origin());
+ readRect.setRelativeTo(glvp, left, top, width, height, renderTarget->origin());
size_t tightRowBytes = GrBytesPerPixel(config) * width;
#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"
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);
+ bool match;
+ unsigned char readback[X_SIZE][Y_SIZE];
- // 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;
+ 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;
+
+ // 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;
}
- }
- // upload the texture
- texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
- textureData, 0);
+ // create a distinctive texture
+ for (int y = 0; y < Y_SIZE; ++y) {
+ for (int x = 0; x < X_SIZE; ++x) {
+ alphaData[x][y] = y*X_SIZE+x;
+ }
+ }
- unsigned char readback[X_SIZE][Y_SIZE];
+ // 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);
+ // 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);
+ // read the texture back
+ texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
+ readback, 0);
- // make sure the original & read back versions match
- bool match = true;
+ // make sure the original & read back versions match
+ match = true;
- 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;
+ 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;
+ }
}
}
- }
-
- REPORTER_ASSERT(reporter, match);
- // 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);
+ // 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;
+ SkPaint paint;
- const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
+ const SkRect rect = SkRect::MakeLTRB(-10, -10, X_SIZE + 10, Y_SIZE + 10);
- paint.setColor(SK_ColorWHITE);
+ paint.setColor(SK_ColorWHITE);
- canvas.drawRect(rect, paint);
+ canvas.drawRect(rect, paint);
- texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
- readback, 0);
+ texture->readPixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig, readback, 0);
- match = true;
+ 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 (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);
+ static const GrPixelConfig kRGBAConfigs[] {
+ kRGBA_8888_GrPixelConfig,
+ kBGRA_8888_GrPixelConfig,
+ kSRGBA_8888_GrPixelConfig
+ };
+
+ // Attempt to read back just alpha from a RGBA/BGRA texture. Once with a texture-only src and
+ // once with a render target.
+ for (auto cfg : kRGBAConfigs) {
+ for (int rt = 0; rt < 2; ++rt) {
+ GrSurfaceDesc desc;
+ desc.fFlags = rt ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
+ desc.fConfig = cfg;
+ desc.fWidth = X_SIZE;
+ desc.fHeight = Y_SIZE;
+
+ 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;
+ }
+
+ // clear readback to something non-zero so we can detect readback failures
+ memset(readback, 0x0, X_SIZE * Y_SIZE);
+
+ // read the texture back
+ texture->readPixels(0, 0, desc.fWidth, desc.fHeight, kAlpha_8_GrPixelConfig, readback,
+ 0);
+
+ 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]) {
+ 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;
+ }
+ }
+ }
+ }
+ }
}
#endif