From 555e1ff4cc84682ea73f9976f8e3c6f1d0f22590 Mon Sep 17 00:00:00 2001 From: Caolán McNamara Date: Tue, 7 Feb 2017 16:17:02 +0000 Subject: bubble the original gtk surface type through rendering this may make scrolling a tad faster Change-Id: I0c2cc9df85932e25dbfed88727d3b83d299671c7 --- vcl/headless/svpgdi.cxx | 131 +++++++++++++++++++++++++++++++----------------- 1 file changed, 84 insertions(+), 47 deletions(-) (limited to 'vcl/headless/svpgdi.cxx') diff --git a/vcl/headless/svpgdi.cxx b/vcl/headless/svpgdi.cxx index 34bb744a76ee..e05617db51f7 100644 --- a/vcl/headless/svpgdi.cxx +++ b/vcl/headless/svpgdi.cxx @@ -133,6 +133,22 @@ namespace { return source; } + void mark_dirty() + { + cairo_surface_mark_dirty(source); + } + unsigned char* getBits(sal_Int32 &rStride) + { + cairo_surface_flush(source); + + unsigned char *mask_data = cairo_image_surface_get_data(source); + + cairo_format_t nFormat = cairo_image_surface_get_format(source); + assert(nFormat == CAIRO_FORMAT_ARGB32 && "need to implement CAIRO_FORMAT_A1 after all here"); + rStride = cairo_format_stride_for_width(nFormat, cairo_image_surface_get_width(source)); + + return mask_data; + } private: SvpSalBitmap aTmpBmp; cairo_surface_t* source; @@ -386,9 +402,10 @@ SvpSalGraphics::~SvpSalGraphics() { } -void SvpSalGraphics::setSurface(cairo_surface_t* pSurface) +void SvpSalGraphics::setSurface(cairo_surface_t* pSurface, const basegfx::B2IVector& rSize) { m_pSurface = pSurface; + m_aFrameSize = rSize; #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 14, 0) cairo_surface_get_device_scale(pSurface, &m_fScale, nullptr); #endif @@ -402,14 +419,14 @@ void SvpSalGraphics::GetResolution( sal_Int32& rDPIX, sal_Int32& rDPIY ) sal_uInt16 SvpSalGraphics::GetBitCount() const { - if (CAIRO_FORMAT_A1 == cairo_image_surface_get_format(m_pSurface)) + if (cairo_surface_get_content(m_pSurface) != CAIRO_CONTENT_COLOR_ALPHA) return 1; return 32; } long SvpSalGraphics::GetGraphicsWidth() const { - return m_pSurface ? cairo_image_surface_get_width(m_pSurface) / m_fScale : 0; + return m_pSurface ? m_aFrameSize.getX() : 0; } void SvpSalGraphics::ResetClipRegion() @@ -885,7 +902,7 @@ bool SvpSalGraphics::drawPolyPolygon(const basegfx::B2DPolyPolygon& rPolyPoly, d void SvpSalGraphics::applyColor(cairo_t *cr, SalColor aColor) { - if (CAIRO_FORMAT_ARGB32 == cairo_image_surface_get_format(m_pSurface)) + if (cairo_surface_get_content(m_pSurface) == CAIRO_CONTENT_COLOR_ALPHA) { cairo_set_source_rgba(cr, SALCOLOR_RED(aColor)/255.0, SALCOLOR_GREEN(aColor)/255.0, @@ -982,17 +999,10 @@ void SvpSalGraphics::copyBits( const SalTwoRect& rTR, if (pSrc == this) { //self copy is a problem, so dup source in that case -#if CAIRO_VERSION < CAIRO_VERSION_ENCODE(1, 12, 0) pCopy = cairo_surface_create_similar(source, cairo_surface_get_content(m_pSurface), aTR.mnSrcWidth * m_fScale, aTR.mnSrcHeight * m_fScale); -#else - pCopy = cairo_surface_create_similar_image(source, - cairo_image_surface_get_format(m_pSurface), - aTR.mnSrcWidth * m_fScale, - aTR.mnSrcHeight * m_fScale); -#endif #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 14, 0) cairo_surface_set_device_scale(pCopy, m_fScale, m_fScale); #endif @@ -1049,16 +1059,8 @@ void SvpSalGraphics::drawMask( const SalTwoRect& rTR, /** creates an image from the given rectangle, replacing all black pixels * with nMaskColor and make all other full transparent */ SourceHelper aSurface(rSalBitmap); - cairo_surface_t* mask = aSurface.getSurface(); - - cairo_surface_flush(mask); - - unsigned char *mask_data = cairo_image_surface_get_data(mask); - - cairo_format_t nFormat = cairo_image_surface_get_format(mask); - assert(nFormat == CAIRO_FORMAT_ARGB32 && "need to implement CAIRO_FORMAT_A1 after all here"); - sal_Int32 nStride = cairo_format_stride_for_width(nFormat, - cairo_image_surface_get_width(mask)); + sal_Int32 nStride; + unsigned char *mask_data = aSurface.getBits(nStride); for (sal_Int32 y = rTR.mnSrcY ; y < rTR.mnSrcY + rTR.mnSrcHeight; ++y) { unsigned char *row = mask_data + (nStride*y); @@ -1085,7 +1087,7 @@ void SvpSalGraphics::drawMask( const SalTwoRect& rTR, data+=4; } } - cairo_surface_mark_dirty(mask); + aSurface.mark_dirty(); cairo_t* cr = getCairoContext(false); clipRegion(cr); @@ -1098,7 +1100,7 @@ void SvpSalGraphics::drawMask( const SalTwoRect& rTR, cairo_translate(cr, rTR.mnDestX, rTR.mnDestY); cairo_scale(cr, (double)(rTR.mnDestWidth)/rTR.mnSrcWidth, ((double)rTR.mnDestHeight)/rTR.mnSrcHeight); - cairo_set_source_surface(cr, mask, -rTR.mnSrcX, -rTR.mnSrcY); + cairo_set_source_surface(cr, aSurface.getSurface(), -rTR.mnSrcX, -rTR.mnSrcY); cairo_paint(cr); releaseCairoContext(cr, false, extents); @@ -1123,18 +1125,24 @@ SalBitmap* SvpSalGraphics::getBitmap( long nX, long nY, long nWidth, long nHeigh SalColor SvpSalGraphics::getPixel( long nX, long nY ) { - cairo_surface_flush(m_pSurface); - cairo_format_t nFormat = cairo_image_surface_get_format(m_pSurface); - assert(nFormat == CAIRO_FORMAT_ARGB32 && "need to implement CAIRO_FORMAT_A1 after all here"); - sal_Int32 nStride = cairo_format_stride_for_width(nFormat, - cairo_image_surface_get_width(m_pSurface)); - unsigned char *surface_data = cairo_image_surface_get_data(m_pSurface); - unsigned char *row = surface_data + (nStride*nY); - unsigned char *data = row + (nX * 4); + cairo_surface_t *target = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1); + cairo_t* cr = cairo_create(target); + + cairo_rectangle(cr, 0, 0, 1, 1); + cairo_set_source_surface(cr, m_pSurface, -nX, -nY); + cairo_paint(cr); + cairo_destroy(cr); + + cairo_surface_flush(target); + unsigned char *data = cairo_image_surface_get_data(target); sal_uInt8 b = unpremultiply(data[SVP_CAIRO_BLUE], data[SVP_CAIRO_ALPHA]); sal_uInt8 g = unpremultiply(data[SVP_CAIRO_GREEN], data[SVP_CAIRO_ALPHA]); sal_uInt8 r = unpremultiply(data[SVP_CAIRO_RED], data[SVP_CAIRO_ALPHA]); - return MAKE_SALCOLOR(r, g, b); + SalColor nRet = MAKE_SALCOLOR(r, g, b); + + cairo_surface_destroy(target); + + return nRet; } namespace @@ -1268,17 +1276,17 @@ cairo_surface_t* SvpSalGraphics::createCairoSurface(const BitmapBuffer *pBuffer) return target; } -static cairo_t* createTmpCompatibleCairoContext(cairo_surface_t* pSurface, double fScale) +cairo_t* SvpSalGraphics::createTmpCompatibleCairoContext() const { - cairo_surface_t *target = cairo_image_surface_create( - cairo_image_surface_get_format(pSurface), - cairo_image_surface_get_width(pSurface), - cairo_image_surface_get_height(pSurface)); + cairo_surface_t *target = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, + m_aFrameSize.getX() * m_fScale, + m_aFrameSize.getY() * m_fScale); #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 14, 0) - cairo_surface_set_device_scale(target, fScale, fScale); + cairo_surface_set_device_scale(target, m_fScale, m_fScale); #else (void)fScale; #endif + return cairo_create(target); } @@ -1286,7 +1294,7 @@ cairo_t* SvpSalGraphics::getCairoContext(bool bXorModeAllowed) const { cairo_t* cr; if (m_ePaintMode == XOR && bXorModeAllowed) - cr = createTmpCompatibleCairoContext(m_pSurface, m_fScale); + cr = createTmpCompatibleCairoContext(); else cr = cairo_create(m_pSurface); cairo_set_line_width(cr, 1); @@ -1316,8 +1324,8 @@ void SvpSalGraphics::releaseCairoContext(cairo_t* cr, bool bXorModeAllowed, cons sal_Int32 nExtentsLeft(rExtents.getMinX()), nExtentsTop(rExtents.getMinY()); sal_Int32 nExtentsRight(rExtents.getMaxX()), nExtentsBottom(rExtents.getMaxY()); - sal_Int32 nWidth = cairo_image_surface_get_width(m_pSurface); - sal_Int32 nHeight = cairo_image_surface_get_height(m_pSurface); + sal_Int32 nWidth = m_aFrameSize.getX(); + sal_Int32 nHeight = m_aFrameSize.getY(); nExtentsLeft = std::max(nExtentsLeft, 0); nExtentsTop = std::max(nExtentsTop, 0); nExtentsRight = std::min(nExtentsRight, nWidth); @@ -1331,21 +1339,35 @@ void SvpSalGraphics::releaseCairoContext(cairo_t* cr, bool bXorModeAllowed, cons //emulate it (slowly) here. if (m_ePaintMode == XOR && bXorModeAllowed) { - cairo_surface_t* true_surface = m_pSurface; - cairo_surface_flush(true_surface); - unsigned char *true_surface_data = cairo_image_surface_get_data(true_surface); + cairo_surface_t* target_surface = m_pSurface; + if (cairo_surface_get_type(target_surface) != CAIRO_SURFACE_TYPE_IMAGE) + { + //in the unlikely case we can't use m_pSurface directly, copy contents + //to another temp image surface + cairo_t* copycr = createTmpCompatibleCairoContext(); + cairo_rectangle(copycr, nExtentsLeft, nExtentsTop, + nExtentsRight - nExtentsLeft, + nExtentsBottom - nExtentsTop); + cairo_set_source_surface(copycr, m_pSurface, 0, 0); + cairo_paint(copycr); + target_surface = cairo_get_target(copycr); + cairo_destroy(copycr); + } + + cairo_surface_flush(target_surface); + unsigned char *target_surface_data = cairo_image_surface_get_data(target_surface); unsigned char *xor_surface_data = cairo_image_surface_get_data(surface); cairo_format_t nFormat = cairo_image_surface_get_format(m_pSurface); assert(nFormat == CAIRO_FORMAT_ARGB32 && "need to implement CAIRO_FORMAT_A1 after all here"); - sal_Int32 nStride = cairo_format_stride_for_width(nFormat, nWidth); + sal_Int32 nStride = cairo_format_stride_for_width(nFormat, nWidth * m_fScale); sal_Int32 nUnscaledExtentsLeft = nExtentsLeft * m_fScale; sal_Int32 nUnscaledExtentsRight = nExtentsRight * m_fScale; sal_Int32 nUnscaledExtentsTop = nExtentsTop * m_fScale; sal_Int32 nUnscaledExtentsBottom = nExtentsBottom * m_fScale; for (sal_Int32 y = nUnscaledExtentsTop; y < nUnscaledExtentsBottom; ++y) { - unsigned char *true_row = true_surface_data + (nStride*y); + unsigned char *true_row = target_surface_data + (nStride*y); unsigned char *xor_row = xor_surface_data + (nStride*y); unsigned char *true_data = true_row + (nUnscaledExtentsLeft * 4); unsigned char *xor_data = xor_row + (nUnscaledExtentsLeft * 4); @@ -1364,7 +1386,22 @@ void SvpSalGraphics::releaseCairoContext(cairo_t* cr, bool bXorModeAllowed, cons xor_data+=4; } } - cairo_surface_mark_dirty(true_surface); + cairo_surface_mark_dirty(target_surface); + + if (target_surface != m_pSurface) + { + cairo_t* copycr = cairo_create(m_pSurface); + //unlikely case we couldn't use m_pSurface directly, copy contents + //back from image surface + cairo_rectangle(copycr, nExtentsLeft, nExtentsTop, + nExtentsRight - nExtentsLeft, + nExtentsBottom - nExtentsTop); + cairo_set_source_surface(copycr, target_surface, 0, 0); + cairo_paint(copycr); + cairo_destroy(copycr); + cairo_surface_destroy(target_surface); + } + cairo_surface_destroy(surface); } -- cgit v1.2.3