aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/SkGpuDevice.cpp
diff options
context:
space:
mode:
authorGravatar senorblanco <senorblanco@chromium.org>2015-04-02 04:54:56 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-04-02 04:54:57 -0700
commitd0d37cace08f12abf8d316e6949e947551d418e6 (patch)
treef78c9aa1fb8d77822aebe681b7386c7f006aaba4 /src/gpu/SkGpuDevice.cpp
parent160ebb2bfa0794d72d31a786793b13413289863d (diff)
Implement approx-match support in image filter saveLayer() offscreen.
Currently, the GPU-side image filter implementation creates exact-match textures for the offscreen backing stores for saveLayer(). This is because several filters have GPU implementations which depend on the texture coordinates being 0..1. The fix is three-fold: 1) Store the actual requested size in the SkGpuDevice, so that when wrapping it in an SkBitmap for passing to filterImage(), we can give it the original size. 2) Fix the filters (SkMagnifierImageFilter, SkLightingImageFilter, SkMatrixConvolutionImageFilter, SkMatrixImageFilter) whose GPU implementation depends on 0..1 texture coordinates. 3) Remove the exception for GPU-side image filters in SkCanvas::internalSaveLayer(). For the lighting filters, there were two bugs which were cancelling each other out: the sobel filter matrix was being computed upside down, but then we'd negate the resulting normal. This worked fine in the exact-match case, but in the approx-match case we'd sample garbage along the edge pixels. Also, we never implemented the edge pixels according to spec in the GPU case. It requires a different fragment shader for each edge of the nine-patch, which meant we couldn't use asFragmentProcessor(), and had to implement the drawing via a filterImageGPU() override. In order to avoid polluting the public API, I inserted a new base class, SkLightingImageFilterInternal above Sk[Diffuse|Specular]LightingImageFilter to handle the implementation. For the SkMatrixConvolutionImageFilter, it seems the GLSL clamp() function occasionally returns values outside the clamped range, resulting in access of garbage texels even in GL_NEAREST. The fix here is to clamp to a rect inset by half a texel. There was also a bug in the unpremultiply step when fConvolveAlpha is false. For SkMatrixImageFilter, the fix was to make the generic draw path be more careful about when to use texture domain. If the bitmap already has a texture, use texture domain if the srcRect is smaller than the entire texture (not the entire bitmap). N.B.: this change will cause some minor pixel diffs in the GPU results of the following GMs (and possibly more): matriximagefilter, matrixconvolution, imagefiltersscaled, lighting, imagemagnifier, filterfastbounds, complexclip_aa_Layer_invert, complexclip_aa_layer, complexclip_bw_layer_invert, complexclip_bw_layer. BUG=skia:3532 Committed: https://skia.googlesource.com/skia/+/b97dafefe63ea0a1bbce8e8b209f4920983fb8b9 Committed: https://skia.googlesource.com/skia/+/f5f8518fe0bbd2703e4ffc1b11ad7b4312ff7641 Committed: https://skia.googlesource.com/skia/+/46112cf2a7c7307f1c9eebb5f881cbda15aa460c Review URL: https://codereview.chromium.org/1034733002
Diffstat (limited to 'src/gpu/SkGpuDevice.cpp')
-rw-r--r--src/gpu/SkGpuDevice.cpp53
1 files changed, 34 insertions, 19 deletions
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index 38a22e69f5..a3f26149e8 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -121,10 +121,15 @@ public:
///////////////////////////////////////////////////////////////////////////////
SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags) {
+ return SkGpuDevice::Create(rt, rt->width(), rt->height(), props, flags);
+}
+
+SkGpuDevice* SkGpuDevice::Create(GrRenderTarget* rt, int width, int height,
+ const SkSurfaceProps* props, unsigned flags) {
if (!rt || rt->wasDestroyed()) {
return NULL;
}
- return SkNEW_ARGS(SkGpuDevice, (rt, props, flags));
+ return SkNEW_ARGS(SkGpuDevice, (rt, width, height, props, flags));
}
static SkDeviceProperties surfaceprops_to_deviceprops(const SkSurfaceProps* props) {
@@ -143,7 +148,8 @@ static SkSurfaceProps copy_or_default_props(const SkSurfaceProps* props) {
}
}
-SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, const SkSurfaceProps* props, unsigned flags)
+SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, int width, int height,
+ const SkSurfaceProps* props, unsigned flags)
: INHERITED(surfaceprops_to_deviceprops(props))
, fSurfaceProps(copy_or_default_props(props))
{
@@ -154,7 +160,7 @@ SkGpuDevice::SkGpuDevice(GrRenderTarget* rt, const SkSurfaceProps* props, unsign
fRenderTarget = SkRef(rt);
- SkImageInfo info = rt->surfacePriv().info();
+ SkImageInfo info = rt->surfacePriv().info().makeWH(width, height);
SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, rt));
fLegacyBitmap.setInfo(info);
fLegacyBitmap.setPixelRef(pr)->unref();
@@ -211,7 +217,7 @@ SkGpuDevice* SkGpuDevice::Create(GrContext* context, SkSurface::Budgeted budgete
return NULL;
}
- return SkNEW_ARGS(SkGpuDevice, (rt, props, flags));
+ return SkNEW_ARGS(SkGpuDevice, (rt, info.width(), info.height(), props, flags));
}
SkGpuDevice::~SkGpuDevice() {
@@ -736,9 +742,9 @@ GrTexture* create_mask_GPU(GrContext* context,
return mask;
}
-SkBitmap wrap_texture(GrTexture* texture) {
+SkBitmap wrap_texture(GrTexture* texture, int width, int height) {
SkBitmap result;
- result.setInfo(texture->surfacePriv().info());
+ result.setInfo(SkImageInfo::MakeN32Premul(width, height));
result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
return result;
}
@@ -1097,11 +1103,14 @@ static bool needs_texture_domain(const SkBitmap& bitmap,
const SkMatrix& contextMatrix,
bool bicubic) {
bool needsTextureDomain = false;
+ GrTexture* tex = bitmap.getTexture();
+ int width = tex ? tex->width() : bitmap.width();
+ int height = tex ? tex->height() : bitmap.height();
if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
// Need texture domain if drawing a sub rect
- needsTextureDomain = srcRect.width() < bitmap.width() ||
- srcRect.height() < bitmap.height();
+ needsTextureDomain = srcRect.width() < width ||
+ srcRect.height() < height;
if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
// sampling is axis-aligned
SkRect transformedRect;
@@ -1136,15 +1145,17 @@ void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
dstSize.fWidth = w;
dstSize.fHeight = h;
srcRect.set(0, 0, w, h);
- flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
} else {
SkASSERT(dstSizePtr);
srcRect = *srcRectPtr;
dstSize = *dstSizePtr;
- if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
- srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
- flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
- }
+ }
+ GrTexture* tex = bitmap.getTexture();
+ int width = tex ? tex->width() : bitmap.width();
+ int height = tex ? tex->height() : bitmap.height();
+ if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
+ srcRect.fRight >= width && srcRect.fBottom >= height) {
+ flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
}
// If the render target is not msaa and draw is antialiased, we call
@@ -1474,6 +1485,7 @@ void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
}
bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
+ int width, int height,
const SkImageFilter* filter,
const SkImageFilter::Context& ctx,
SkBitmap* result, SkIPoint* offset) {
@@ -1484,7 +1496,8 @@ bool SkGpuDevice::filterTexture(GrContext* context, GrTexture* texture,
SkDeviceImageFilterProxy proxy(this, SkSurfaceProps(0, getLeakyProperties().pixelGeometry()));
if (filter->canFilterImageGPU()) {
- return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
+ return filter->filterImageGPU(&proxy, wrap_texture(texture, width, height),
+ ctx, result, offset);
} else {
return false;
}
@@ -1523,7 +1536,7 @@ void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
// This cache is transient, and is freed (along with all its contained
// textures) when it goes out of scope.
SkImageFilter::Context ctx(matrix, clipBounds, cache);
- if (this->filterTexture(fContext, texture, filter, ctx, &filteredBitmap,
+ if (this->filterTexture(fContext, texture, w, h, filter, ctx, &filteredBitmap,
&offset)) {
texture = (GrTexture*) filteredBitmap.getTexture();
w = filteredBitmap.width();
@@ -1637,8 +1650,8 @@ void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
// textures) when it goes out of scope.
SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
SkImageFilter::Context ctx(matrix, clipBounds, cache);
- if (this->filterTexture(fContext, devTex, filter, ctx, &filteredBitmap,
- &offset)) {
+ if (this->filterTexture(fContext, devTex, device->width(), device->height(),
+ filter, ctx, &filteredBitmap, &offset)) {
devTex = filteredBitmap.getTexture();
w = filteredBitmap.width();
h = filteredBitmap.height();
@@ -1691,7 +1704,8 @@ bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
// must be pushed upstack.
AutoBitmapTexture abt(fContext, src, NULL, &texture);
- return this->filterTexture(fContext, texture, filter, ctx, result, offset);
+ return this->filterTexture(fContext, texture, src.width(), src.height(),
+ filter, ctx, result, offset);
}
///////////////////////////////////////////////////////////////////////////////
@@ -1901,7 +1915,8 @@ SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint
if (texture) {
SkSurfaceProps props(fSurfaceProps.flags(), cinfo.fPixelGeometry);
- return SkGpuDevice::Create(texture->asRenderTarget(), &props, flags);
+ return SkGpuDevice::Create(
+ texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, flags);
} else {
SkErrorInternals::SetError( kInternalError_SkError,
"---- failed to create compatible device texture [%d %d]\n",