nss: upgrade to release 3.73
[LibreOffice.git] / vcl / headless / svpgdi.cxx
blob0b62ccf467c13b2b5d0e898ce0bcbff956a3b3ae
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_features.h>
22 #include <memory>
23 #include <numeric>
25 #include <headless/svpgdi.hxx>
26 #include <headless/svpbmp.hxx>
27 #include <headless/svpframe.hxx>
28 #include <headless/svpcairotextrender.hxx>
29 #include <headless/CustomWidgetDraw.hxx>
30 #include <saldatabasic.hxx>
32 #include <sal/log.hxx>
33 #include <tools/helpers.hxx>
34 #include <o3tl/safeint.hxx>
35 #include <vcl/BitmapTools.hxx>
36 #include <vcl/sysdata.hxx>
37 #include <vcl/gradient.hxx>
38 #include <config_cairo_canvas.h>
39 #include <basegfx/numeric/ftools.hxx>
40 #include <basegfx/range/b2drange.hxx>
41 #include <basegfx/range/b2ibox.hxx>
42 #include <basegfx/range/b2irange.hxx>
43 #include <basegfx/polygon/b2dpolypolygon.hxx>
44 #include <basegfx/polygon/b2dpolypolygontools.hxx>
45 #include <basegfx/polygon/b2dpolygon.hxx>
46 #include <basegfx/polygon/b2dpolygontools.hxx>
47 #include <basegfx/matrix/b2dhommatrix.hxx>
48 #include <basegfx/utils/canvastools.hxx>
49 #include <basegfx/utils/systemdependentdata.hxx>
50 #include <basegfx/matrix/b2dhommatrixtools.hxx>
51 #include <comphelper/lok.hxx>
52 #include <unx/gendata.hxx>
53 #include <dlfcn.h>
55 #if ENABLE_CAIRO_CANVAS
56 # if defined CAIRO_VERSION && CAIRO_VERSION < CAIRO_VERSION_ENCODE(1, 10, 0)
57 # define CAIRO_OPERATOR_DIFFERENCE (static_cast<cairo_operator_t>(23))
58 # endif
59 #endif
61 namespace
63 basegfx::B2DRange getClipBox(cairo_t* cr)
65 double x1, y1, x2, y2;
67 cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
69 // support B2DRange::isEmpty()
70 if(0.0 != x1 || 0.0 != y1 || 0.0 != x2 || 0.0 != y2)
72 return basegfx::B2DRange(x1, y1, x2, y2);
75 return basegfx::B2DRange();
78 basegfx::B2DRange getFillDamage(cairo_t* cr)
80 double x1, y1, x2, y2;
82 // this is faster than cairo_fill_extents, at the cost of some overdraw
83 cairo_path_extents(cr, &x1, &y1, &x2, &y2);
85 // support B2DRange::isEmpty()
86 if(0.0 != x1 || 0.0 != y1 || 0.0 != x2 || 0.0 != y2)
88 return basegfx::B2DRange(x1, y1, x2, y2);
91 return basegfx::B2DRange();
94 basegfx::B2DRange getClippedFillDamage(cairo_t* cr)
96 basegfx::B2DRange aDamageRect(getFillDamage(cr));
97 aDamageRect.intersect(getClipBox(cr));
98 return aDamageRect;
101 basegfx::B2DRange getStrokeDamage(cairo_t* cr)
103 double x1, y1, x2, y2;
105 // less accurate, but much faster
106 cairo_path_extents(cr, &x1, &y1, &x2, &y2);
108 // support B2DRange::isEmpty()
109 if(0.0 != x1 || 0.0 != y1 || 0.0 != x2 || 0.0 != y2)
111 return basegfx::B2DRange(x1, y1, x2, y2);
114 return basegfx::B2DRange();
117 basegfx::B2DRange getClippedStrokeDamage(cairo_t* cr)
119 basegfx::B2DRange aDamageRect(getStrokeDamage(cr));
120 aDamageRect.intersect(getClipBox(cr));
121 return aDamageRect;
125 bool SvpSalGraphics::blendBitmap( const SalTwoRect&, const SalBitmap& /*rBitmap*/ )
127 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::blendBitmap case");
128 return false;
131 bool SvpSalGraphics::blendAlphaBitmap( const SalTwoRect&, const SalBitmap&, const SalBitmap&, const SalBitmap& )
133 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::blendAlphaBitmap case");
134 return false;
137 namespace
139 cairo_format_t getCairoFormat(const BitmapBuffer& rBuffer)
141 cairo_format_t nFormat;
142 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
143 assert(rBuffer.mnBitCount == 32 || rBuffer.mnBitCount == 24 || rBuffer.mnBitCount == 1);
144 #else
145 assert(rBuffer.mnBitCount == 32 || rBuffer.mnBitCount == 1);
146 #endif
148 if (rBuffer.mnBitCount == 32)
149 nFormat = CAIRO_FORMAT_ARGB32;
150 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
151 else if (rBuffer.mnBitCount == 24)
152 nFormat = CAIRO_FORMAT_RGB24_888;
153 #endif
154 else
155 nFormat = CAIRO_FORMAT_A1;
156 return nFormat;
159 void Toggle1BitTransparency(const BitmapBuffer& rBuf)
161 assert(rBuf.maPalette.GetBestIndex(BitmapColor(COL_BLACK)) == 0);
162 // TODO: make upper layers use standard alpha
163 if (getCairoFormat(rBuf) == CAIRO_FORMAT_A1)
165 const int nImageSize = rBuf.mnHeight * rBuf.mnScanlineSize;
166 unsigned char* pDst = rBuf.mpBits;
167 for (int i = nImageSize; --i >= 0; ++pDst)
168 *pDst = ~*pDst;
172 std::unique_ptr<BitmapBuffer> FastConvert24BitRgbTo32BitCairo(const BitmapBuffer* pSrc)
174 if (pSrc == nullptr)
175 return nullptr;
177 assert(pSrc->mnFormat == SVP_24BIT_FORMAT);
178 const tools::Long nWidth = pSrc->mnWidth;
179 const tools::Long nHeight = pSrc->mnHeight;
180 std::unique_ptr<BitmapBuffer> pDst(new BitmapBuffer);
181 pDst->mnFormat = (ScanlineFormat::N32BitTcArgb | ScanlineFormat::TopDown);
182 pDst->mnWidth = nWidth;
183 pDst->mnHeight = nHeight;
184 pDst->mnBitCount = 32;
185 pDst->maColorMask = pSrc->maColorMask;
186 pDst->maPalette = pSrc->maPalette;
188 tools::Long nScanlineBase;
189 const bool bFail = o3tl::checked_multiply<tools::Long>(pDst->mnBitCount, nWidth, nScanlineBase);
190 if (bFail)
192 SAL_WARN("vcl.gdi", "checked multiply failed");
193 pDst->mpBits = nullptr;
194 return nullptr;
197 pDst->mnScanlineSize = AlignedWidth4Bytes(nScanlineBase);
198 if (pDst->mnScanlineSize < nScanlineBase/8)
200 SAL_WARN("vcl.gdi", "scanline calculation wraparound");
201 pDst->mpBits = nullptr;
202 return nullptr;
207 pDst->mpBits = new sal_uInt8[ pDst->mnScanlineSize * nHeight ];
209 catch (const std::bad_alloc&)
211 // memory exception, clean up
212 pDst->mpBits = nullptr;
213 return nullptr;
216 for (tools::Long y = 0; y < nHeight; ++y)
218 sal_uInt8* pS = pSrc->mpBits + y * pSrc->mnScanlineSize;
219 sal_uInt8* pD = pDst->mpBits + y * pDst->mnScanlineSize;
220 for (tools::Long x = 0; x < nWidth; ++x)
222 #if defined(ANDROID) && !HAVE_FEATURE_ANDROID_LOK
223 static_assert((SVP_CAIRO_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N32BitTcRgba, "Expected SVP_CAIRO_FORMAT set to N32BitTcBgra");
224 static_assert((SVP_24BIT_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N24BitTcRgb, "Expected SVP_24BIT_FORMAT set to N24BitTcRgb");
225 pD[0] = pS[0];
226 pD[1] = pS[1];
227 pD[2] = pS[2];
228 pD[3] = 0xff; // Alpha
229 #elif defined OSL_BIGENDIAN
230 static_assert((SVP_CAIRO_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N32BitTcArgb, "Expected SVP_CAIRO_FORMAT set to N32BitTcBgra");
231 static_assert((SVP_24BIT_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N24BitTcRgb, "Expected SVP_24BIT_FORMAT set to N24BitTcRgb");
232 pD[0] = 0xff; // Alpha
233 pD[1] = pS[0];
234 pD[2] = pS[1];
235 pD[3] = pS[2];
236 #else
237 static_assert((SVP_CAIRO_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N32BitTcBgra, "Expected SVP_CAIRO_FORMAT set to N32BitTcBgra");
238 static_assert((SVP_24BIT_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N24BitTcBgr, "Expected SVP_24BIT_FORMAT set to N24BitTcBgr");
239 pD[0] = pS[0];
240 pD[1] = pS[1];
241 pD[2] = pS[2];
242 pD[3] = 0xff; // Alpha
243 #endif
245 pS += 3;
246 pD += 4;
250 return pDst;
253 // check for env var that decides for using downscale pattern
254 const char* pDisableDownScale(getenv("SAL_DISABLE_CAIRO_DOWNSCALE"));
255 bool bDisableDownScale(nullptr != pDisableDownScale);
257 class SurfaceHelper
259 private:
260 cairo_surface_t* pSurface;
261 std::unordered_map<unsigned long long, cairo_surface_t*> maDownscaled;
263 SurfaceHelper(const SurfaceHelper&) = delete;
264 SurfaceHelper& operator=(const SurfaceHelper&) = delete;
266 cairo_surface_t* implCreateOrReuseDownscale(
267 unsigned long nTargetWidth,
268 unsigned long nTargetHeight)
270 const unsigned long nSourceWidth(cairo_image_surface_get_width(pSurface));
271 const unsigned long nSourceHeight(cairo_image_surface_get_height(pSurface));
273 // zoomed in, need to stretch at paint, no pre-scale useful
274 if(nTargetWidth >= nSourceWidth || nTargetHeight >= nSourceHeight)
276 return pSurface;
279 // calculate downscale factor
280 unsigned long nWFactor(1);
281 unsigned long nW((nSourceWidth + 1) / 2);
282 unsigned long nHFactor(1);
283 unsigned long nH((nSourceHeight + 1) / 2);
285 while(nW > nTargetWidth && nW > 1)
287 nW = (nW + 1) / 2;
288 nWFactor *= 2;
291 while(nH > nTargetHeight && nH > 1)
293 nH = (nH + 1) / 2;
294 nHFactor *= 2;
297 if(1 == nWFactor && 1 == nHFactor)
299 // original size *is* best binary size, use it
300 return pSurface;
303 // go up one scale again - look for no change
304 nW = (1 == nWFactor) ? nTargetWidth : nW * 2;
305 nH = (1 == nHFactor) ? nTargetHeight : nH * 2;
307 // check if we have a downscaled version of required size
308 const unsigned long long key((nW * LONG_MAX) + nH);
309 auto isHit(maDownscaled.find(key));
311 if(isHit != maDownscaled.end())
313 return isHit->second;
316 // create new surface in the targeted size
317 cairo_surface_t* pSurfaceTarget = cairo_surface_create_similar(
318 pSurface,
319 cairo_surface_get_content(pSurface),
321 nH);
323 // made a version to scale self first that worked well, but would've
324 // been hard to support CAIRO_FORMAT_A1 including bit shifting, so
325 // I decided to go with cairo itself - use CAIRO_FILTER_FAST or
326 // CAIRO_FILTER_GOOD though. Please modify as needed for
327 // performance/quality
328 cairo_t* cr = cairo_create(pSurfaceTarget);
329 const double fScaleX(static_cast<double>(nW)/static_cast<double>(nSourceWidth));
330 const double fScaleY(static_cast<double>(nH)/static_cast<double>(nSourceHeight));
331 cairo_scale(cr, fScaleX, fScaleY);
332 cairo_set_source_surface(cr, pSurface, 0.0, 0.0);
333 cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_GOOD);
334 cairo_paint(cr);
335 cairo_destroy(cr);
337 // need to set device_scale for downscale surfaces to get
338 // them handled correctly
339 cairo_surface_set_device_scale(pSurfaceTarget, fScaleX, fScaleY);
341 // add entry to cached entries
342 maDownscaled[key] = pSurfaceTarget;
344 return pSurfaceTarget;
347 protected:
348 cairo_surface_t* implGetSurface() const { return pSurface; }
349 void implSetSurface(cairo_surface_t* pNew) { pSurface = pNew; }
351 bool isTrivial() const
353 constexpr unsigned long nMinimalSquareSizeToBuffer(64*64);
354 const unsigned long nSourceWidth(cairo_image_surface_get_width(pSurface));
355 const unsigned long nSourceHeight(cairo_image_surface_get_height(pSurface));
357 return nSourceWidth * nSourceHeight < nMinimalSquareSizeToBuffer;
360 public:
361 explicit SurfaceHelper()
362 : pSurface(nullptr),
363 maDownscaled()
366 ~SurfaceHelper()
368 cairo_surface_destroy(pSurface);
369 for(auto& candidate : maDownscaled)
371 cairo_surface_destroy(candidate.second);
374 cairo_surface_t* getSurface(
375 unsigned long nTargetWidth = 0,
376 unsigned long nTargetHeight = 0) const
378 if (bDisableDownScale || 0 == nTargetWidth || 0 == nTargetHeight || !pSurface || isTrivial())
380 // caller asks for original or disabled or trivial (smaller then a minimal square size)
381 // also excludes zero cases for width/height after this point if need to prescale
382 return pSurface;
385 return const_cast<SurfaceHelper*>(this)->implCreateOrReuseDownscale(
386 nTargetWidth,
387 nTargetHeight);
391 class BitmapHelper : public SurfaceHelper
393 private:
394 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
395 const bool m_bForceARGB32;
396 #endif
397 SvpSalBitmap aTmpBmp;
399 public:
400 explicit BitmapHelper(
401 const SalBitmap& rSourceBitmap,
402 const bool bForceARGB32 = false)
403 : SurfaceHelper(),
404 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
405 m_bForceARGB32(bForceARGB32),
406 #endif
407 aTmpBmp()
409 const SvpSalBitmap& rSrcBmp = static_cast<const SvpSalBitmap&>(rSourceBitmap);
410 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
411 if ((rSrcBmp.GetBitCount() != 32 && rSrcBmp.GetBitCount() != 24) || bForceARGB32)
412 #else
413 (void)bForceARGB32;
414 if (rSrcBmp.GetBitCount() != 32)
415 #endif
417 //big stupid copy here
418 const BitmapBuffer* pSrc = rSrcBmp.GetBuffer();
419 const SalTwoRect aTwoRect = { 0, 0, pSrc->mnWidth, pSrc->mnHeight,
420 0, 0, pSrc->mnWidth, pSrc->mnHeight };
421 std::unique_ptr<BitmapBuffer> pTmp = (pSrc->mnFormat == SVP_24BIT_FORMAT
422 ? FastConvert24BitRgbTo32BitCairo(pSrc)
423 : StretchAndConvert(*pSrc, aTwoRect, SVP_CAIRO_FORMAT));
424 aTmpBmp.Create(std::move(pTmp));
426 assert(aTmpBmp.GetBitCount() == 32);
427 implSetSurface(SvpSalGraphics::createCairoSurface(aTmpBmp.GetBuffer()));
429 else
431 implSetSurface(SvpSalGraphics::createCairoSurface(rSrcBmp.GetBuffer()));
434 void mark_dirty()
436 cairo_surface_mark_dirty(implGetSurface());
438 unsigned char* getBits(sal_Int32 &rStride)
440 cairo_surface_flush(implGetSurface());
442 unsigned char *mask_data = cairo_image_surface_get_data(implGetSurface());
444 const cairo_format_t nFormat = cairo_image_surface_get_format(implGetSurface());
445 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
446 if (!m_bForceARGB32)
447 assert(nFormat == CAIRO_FORMAT_RGB24_888 && "Expected RGB24_888 image");
448 else
449 #endif
451 assert(nFormat == CAIRO_FORMAT_ARGB32 && "need to implement CAIRO_FORMAT_A1 after all here");
454 rStride = cairo_format_stride_for_width(nFormat, cairo_image_surface_get_width(implGetSurface()));
456 return mask_data;
460 sal_Int64 estimateUsageInBytesForSurfaceHelper(const SurfaceHelper* pHelper)
462 sal_Int64 nRetval(0);
464 if(nullptr != pHelper)
466 cairo_surface_t* pSurface(pHelper->getSurface());
468 if(pSurface)
470 const tools::Long nStride(cairo_image_surface_get_stride(pSurface));
471 const tools::Long nHeight(cairo_image_surface_get_height(pSurface));
473 nRetval = nStride * nHeight;
475 // if we do downscale, size will grow by 1/4 + 1/16 + 1/32 + ...,
476 // rough estimation just multiplies by 1.25, should be good enough
477 // for estimation of buffer survival time
478 if(!bDisableDownScale)
480 nRetval = (nRetval * 5) / 4;
485 return nRetval;
488 class SystemDependentData_BitmapHelper : public basegfx::SystemDependentData
490 private:
491 std::shared_ptr<BitmapHelper> maBitmapHelper;
493 public:
494 SystemDependentData_BitmapHelper(
495 basegfx::SystemDependentDataManager& rSystemDependentDataManager,
496 const std::shared_ptr<BitmapHelper>& rBitmapHelper)
497 : basegfx::SystemDependentData(rSystemDependentDataManager),
498 maBitmapHelper(rBitmapHelper)
502 const std::shared_ptr<BitmapHelper>& getBitmapHelper() const { return maBitmapHelper; };
503 virtual sal_Int64 estimateUsageInBytes() const override;
506 sal_Int64 SystemDependentData_BitmapHelper::estimateUsageInBytes() const
508 return estimateUsageInBytesForSurfaceHelper(maBitmapHelper.get());
511 class MaskHelper : public SurfaceHelper
513 private:
514 std::unique_ptr<unsigned char[]> pAlphaBits;
516 public:
517 explicit MaskHelper(const SalBitmap& rAlphaBitmap)
518 : SurfaceHelper(),
519 pAlphaBits()
521 const SvpSalBitmap& rMask = static_cast<const SvpSalBitmap&>(rAlphaBitmap);
522 const BitmapBuffer* pMaskBuf = rMask.GetBuffer();
524 if (rAlphaBitmap.GetBitCount() == 8)
526 // the alpha values need to be inverted for Cairo
527 // so big stupid copy and invert here
528 const int nImageSize = pMaskBuf->mnHeight * pMaskBuf->mnScanlineSize;
529 pAlphaBits.reset( new unsigned char[nImageSize] );
530 memcpy(pAlphaBits.get(), pMaskBuf->mpBits, nImageSize);
532 // TODO: make upper layers use standard alpha
533 sal_uInt32* pLDst = reinterpret_cast<sal_uInt32*>(pAlphaBits.get());
534 for( int i = nImageSize/sizeof(sal_uInt32); --i >= 0; ++pLDst )
535 *pLDst = ~*pLDst;
536 assert(reinterpret_cast<unsigned char*>(pLDst) == pAlphaBits.get()+nImageSize);
538 implSetSurface(
539 cairo_image_surface_create_for_data(
540 pAlphaBits.get(),
541 CAIRO_FORMAT_A8,
542 pMaskBuf->mnWidth,
543 pMaskBuf->mnHeight,
544 pMaskBuf->mnScanlineSize));
546 else
548 // the alpha values need to be inverted for Cairo
549 // so big stupid copy and invert here
550 const int nImageSize = pMaskBuf->mnHeight * pMaskBuf->mnScanlineSize;
551 pAlphaBits.reset( new unsigned char[nImageSize] );
552 memcpy(pAlphaBits.get(), pMaskBuf->mpBits, nImageSize);
554 const sal_Int32 nBlackIndex = pMaskBuf->maPalette.GetBestIndex(BitmapColor(COL_BLACK));
555 if (nBlackIndex == 0)
557 // TODO: make upper layers use standard alpha
558 unsigned char* pDst = pAlphaBits.get();
559 for (int i = nImageSize; --i >= 0; ++pDst)
560 *pDst = ~*pDst;
563 implSetSurface(
564 cairo_image_surface_create_for_data(
565 pAlphaBits.get(),
566 CAIRO_FORMAT_A1,
567 pMaskBuf->mnWidth,
568 pMaskBuf->mnHeight,
569 pMaskBuf->mnScanlineSize));
574 class SystemDependentData_MaskHelper : public basegfx::SystemDependentData
576 private:
577 std::shared_ptr<MaskHelper> maMaskHelper;
579 public:
580 SystemDependentData_MaskHelper(
581 basegfx::SystemDependentDataManager& rSystemDependentDataManager,
582 const std::shared_ptr<MaskHelper>& rMaskHelper)
583 : basegfx::SystemDependentData(rSystemDependentDataManager),
584 maMaskHelper(rMaskHelper)
588 const std::shared_ptr<MaskHelper>& getMaskHelper() const { return maMaskHelper; };
589 virtual sal_Int64 estimateUsageInBytes() const override;
592 sal_Int64 SystemDependentData_MaskHelper::estimateUsageInBytes() const
594 return estimateUsageInBytesForSurfaceHelper(maMaskHelper.get());
597 // MM02 decide to use buffers or not
598 const char* pDisableMM02Goodies(getenv("SAL_DISABLE_MM02_GOODIES"));
599 bool bUseBuffer(nullptr == pDisableMM02Goodies);
600 tools::Long nMinimalSquareSizeToBuffer(64*64);
602 void tryToUseSourceBuffer(
603 const SalBitmap& rSourceBitmap,
604 std::shared_ptr<BitmapHelper>& rSurface)
606 // MM02 try to access buffered BitmapHelper
607 std::shared_ptr<SystemDependentData_BitmapHelper> pSystemDependentData_BitmapHelper;
608 const bool bBufferSource(bUseBuffer
609 && rSourceBitmap.GetSize().Width() * rSourceBitmap.GetSize().Height() > nMinimalSquareSizeToBuffer);
611 if(bBufferSource)
613 const SvpSalBitmap& rSrcBmp(static_cast<const SvpSalBitmap&>(rSourceBitmap));
614 pSystemDependentData_BitmapHelper = rSrcBmp.getSystemDependentData<SystemDependentData_BitmapHelper>();
616 if(pSystemDependentData_BitmapHelper)
618 // reuse buffered data
619 rSurface = pSystemDependentData_BitmapHelper->getBitmapHelper();
623 if(rSurface)
624 return;
626 // create data on-demand
627 rSurface = std::make_shared<BitmapHelper>(rSourceBitmap);
629 if(bBufferSource)
631 // add to buffering mechanism to potentially reuse next time
632 const SvpSalBitmap& rSrcBmp(static_cast<const SvpSalBitmap&>(rSourceBitmap));
633 rSrcBmp.addOrReplaceSystemDependentData<SystemDependentData_BitmapHelper>(
634 ImplGetSystemDependentDataManager(),
635 rSurface);
639 void tryToUseMaskBuffer(
640 const SalBitmap& rMaskBitmap,
641 std::shared_ptr<MaskHelper>& rMask)
643 // MM02 try to access buffered MaskHelper
644 std::shared_ptr<SystemDependentData_MaskHelper> pSystemDependentData_MaskHelper;
645 const bool bBufferMask(bUseBuffer
646 && rMaskBitmap.GetSize().Width() * rMaskBitmap.GetSize().Height() > nMinimalSquareSizeToBuffer);
648 if(bBufferMask)
650 const SvpSalBitmap& rSrcBmp(static_cast<const SvpSalBitmap&>(rMaskBitmap));
651 pSystemDependentData_MaskHelper = rSrcBmp.getSystemDependentData<SystemDependentData_MaskHelper>();
653 if(pSystemDependentData_MaskHelper)
655 // reuse buffered data
656 rMask = pSystemDependentData_MaskHelper->getMaskHelper();
660 if(rMask)
661 return;
663 // create data on-demand
664 rMask = std::make_shared<MaskHelper>(rMaskBitmap);
666 if(bBufferMask)
668 // add to buffering mechanism to potentially reuse next time
669 const SvpSalBitmap& rSrcBmp(static_cast<const SvpSalBitmap&>(rMaskBitmap));
670 rSrcBmp.addOrReplaceSystemDependentData<SystemDependentData_MaskHelper>(
671 ImplGetSystemDependentDataManager(),
672 rMask);
677 bool SvpSalGraphics::drawAlphaBitmap( const SalTwoRect& rTR, const SalBitmap& rSourceBitmap, const SalBitmap& rAlphaBitmap )
679 if (rAlphaBitmap.GetBitCount() != 8 && rAlphaBitmap.GetBitCount() != 1)
681 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawAlphaBitmap alpha depth case: " << rAlphaBitmap.GetBitCount());
682 return false;
685 // MM02 try to access buffered BitmapHelper
686 std::shared_ptr<BitmapHelper> aSurface;
687 tryToUseSourceBuffer(rSourceBitmap, aSurface);
688 cairo_surface_t* source = aSurface->getSurface(
689 rTR.mnDestWidth,
690 rTR.mnDestHeight);
692 if (!source)
694 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawAlphaBitmap case");
695 return false;
698 // MM02 try to access buffered MaskHelper
699 std::shared_ptr<MaskHelper> aMask;
700 tryToUseMaskBuffer(rAlphaBitmap, aMask);
701 cairo_surface_t *mask = aMask->getSurface(
702 rTR.mnDestWidth,
703 rTR.mnDestHeight);
705 if (!mask)
707 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawAlphaBitmap case");
708 return false;
711 cairo_t* cr = getCairoContext(false);
712 clipRegion(cr);
714 cairo_rectangle(cr, rTR.mnDestX, rTR.mnDestY, rTR.mnDestWidth, rTR.mnDestHeight);
716 basegfx::B2DRange extents = getClippedFillDamage(cr);
718 cairo_clip(cr);
720 cairo_pattern_t* maskpattern = cairo_pattern_create_for_surface(mask);
721 cairo_translate(cr, rTR.mnDestX, rTR.mnDestY);
722 double fXScale = static_cast<double>(rTR.mnDestWidth)/rTR.mnSrcWidth;
723 double fYScale = static_cast<double>(rTR.mnDestHeight)/rTR.mnSrcHeight;
724 cairo_scale(cr, fXScale, fYScale);
725 cairo_set_source_surface(cr, source, -rTR.mnSrcX, -rTR.mnSrcY);
727 //tdf#114117 when stretching a single pixel width/height source to fit an area
728 //set extend and filter to stretch it with simplest expected interpolation
729 if ((fXScale != 1.0 && rTR.mnSrcWidth == 1) || (fYScale != 1.0 && rTR.mnSrcHeight == 1))
731 cairo_pattern_t* sourcepattern = cairo_get_source(cr);
732 cairo_pattern_set_extend(sourcepattern, CAIRO_EXTEND_REPEAT);
733 cairo_pattern_set_filter(sourcepattern, CAIRO_FILTER_NEAREST);
734 cairo_pattern_set_extend(maskpattern, CAIRO_EXTEND_REPEAT);
735 cairo_pattern_set_filter(maskpattern, CAIRO_FILTER_NEAREST);
738 //this block is just "cairo_mask_surface", but we have to make it explicit
739 //because of the cairo_pattern_set_filter etc we may want applied
740 cairo_matrix_t matrix;
741 cairo_matrix_init_translate(&matrix, rTR.mnSrcX, rTR.mnSrcY);
742 cairo_pattern_set_matrix(maskpattern, &matrix);
743 cairo_mask(cr, maskpattern);
745 cairo_pattern_destroy(maskpattern);
747 releaseCairoContext(cr, false, extents);
749 return true;
752 bool SvpSalGraphics::drawTransformedBitmap(
753 const basegfx::B2DPoint& rNull,
754 const basegfx::B2DPoint& rX,
755 const basegfx::B2DPoint& rY,
756 const SalBitmap& rSourceBitmap,
757 const SalBitmap* pAlphaBitmap)
759 if (pAlphaBitmap && pAlphaBitmap->GetBitCount() != 8 && pAlphaBitmap->GetBitCount() != 1)
761 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawTransformedBitmap alpha depth case: " << pAlphaBitmap->GetBitCount());
762 return false;
765 // MM02 try to access buffered BitmapHelper
766 std::shared_ptr<BitmapHelper> aSurface;
767 tryToUseSourceBuffer(rSourceBitmap, aSurface);
768 const tools::Long nDestWidth(basegfx::fround(basegfx::B2DVector(rX - rNull).getLength()));
769 const tools::Long nDestHeight(basegfx::fround(basegfx::B2DVector(rY - rNull).getLength()));
770 cairo_surface_t* source(
771 aSurface->getSurface(
772 nDestWidth,
773 nDestHeight));
775 if(!source)
777 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawTransformedBitmap case");
778 return false;
781 // MM02 try to access buffered MaskHelper
782 std::shared_ptr<MaskHelper> aMask;
783 if(nullptr != pAlphaBitmap)
785 tryToUseMaskBuffer(*pAlphaBitmap, aMask);
788 // access cairo_surface_t from MaskHelper
789 cairo_surface_t* mask(nullptr);
790 if(aMask)
792 mask = aMask->getSurface(
793 nDestWidth,
794 nDestHeight);
797 if(nullptr != pAlphaBitmap && nullptr == mask)
799 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawTransformedBitmap case");
800 return false;
803 const Size aSize = rSourceBitmap.GetSize();
804 cairo_t* cr = getCairoContext(false);
805 clipRegion(cr);
807 // setup the image transformation
808 // using the rNull,rX,rY points as destinations for the (0,0),(0,Width),(Height,0) source points
809 const basegfx::B2DVector aXRel = rX - rNull;
810 const basegfx::B2DVector aYRel = rY - rNull;
811 cairo_matrix_t matrix;
812 cairo_matrix_init(&matrix,
813 aXRel.getX()/aSize.Width(), aXRel.getY()/aSize.Width(),
814 aYRel.getX()/aSize.Height(), aYRel.getY()/aSize.Height(),
815 rNull.getX(), rNull.getY());
817 cairo_transform(cr, &matrix);
819 cairo_rectangle(cr, 0, 0, aSize.Width(), aSize.Height());
820 basegfx::B2DRange extents = getClippedFillDamage(cr);
821 cairo_clip(cr);
823 cairo_set_source_surface(cr, source, 0, 0);
824 if (mask)
825 cairo_mask_surface(cr, mask, 0, 0);
826 else
827 cairo_paint(cr);
829 releaseCairoContext(cr, false, extents);
831 return true;
834 void SvpSalGraphics::clipRegion(cairo_t* cr, const vcl::Region& rClipRegion)
836 RectangleVector aRectangles;
837 if (!rClipRegion.IsEmpty())
839 rClipRegion.GetRegionRectangles(aRectangles);
841 if (!aRectangles.empty())
843 for (auto const& rectangle : aRectangles)
845 cairo_rectangle(cr, rectangle.Left(), rectangle.Top(), rectangle.GetWidth(), rectangle.GetHeight());
847 cairo_clip(cr);
851 void SvpSalGraphics::clipRegion(cairo_t* cr)
853 SvpSalGraphics::clipRegion(cr, m_aClipRegion);
856 bool SvpSalGraphics::drawAlphaRect(tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight, sal_uInt8 nTransparency)
858 const bool bHasFill(m_aFillColor != SALCOLOR_NONE);
859 const bool bHasLine(m_aLineColor != SALCOLOR_NONE);
861 if(!(bHasFill || bHasLine))
863 return true;
866 cairo_t* cr = getCairoContext(false);
867 clipRegion(cr);
869 const double fTransparency = nTransparency * (1.0/100);
871 // To make releaseCairoContext work, use empty extents
872 basegfx::B2DRange extents;
874 if (bHasFill)
876 cairo_rectangle(cr, nX, nY, nWidth, nHeight);
878 applyColor(cr, m_aFillColor, fTransparency);
880 // set FillDamage
881 extents = getClippedFillDamage(cr);
883 cairo_fill(cr);
886 if (bHasLine)
888 // PixelOffset used: Set PixelOffset as linear transformation
889 // Note: Was missing here - probably not by purpose (?)
890 cairo_matrix_t aMatrix;
891 cairo_matrix_init_translate(&aMatrix, 0.5, 0.5);
892 cairo_set_matrix(cr, &aMatrix);
894 cairo_rectangle(cr, nX, nY, nWidth, nHeight);
896 applyColor(cr, m_aLineColor, fTransparency);
898 // expand with possible StrokeDamage
899 basegfx::B2DRange stroke_extents = getClippedStrokeDamage(cr);
900 stroke_extents.transform(basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5));
901 extents.expand(stroke_extents);
903 cairo_stroke(cr);
906 releaseCairoContext(cr, false, extents);
908 return true;
911 SvpSalGraphics::SvpSalGraphics()
912 : m_pSurface(nullptr)
913 , m_fScale(1.0)
914 , m_aLineColor(Color(0x00, 0x00, 0x00))
915 , m_aFillColor(Color(0xFF, 0xFF, 0XFF))
916 , m_ePaintMode(PaintMode::Over)
917 , m_aTextRenderImpl(*this)
919 bool bLOKActive = comphelper::LibreOfficeKit::isActive();
920 if (!initWidgetDrawBackends(bLOKActive))
922 if (bLOKActive)
923 m_pWidgetDraw.reset(new vcl::CustomWidgetDraw(*this));
927 SvpSalGraphics::~SvpSalGraphics()
929 ReleaseFonts();
932 void SvpSalGraphics::setSurface(cairo_surface_t* pSurface, const basegfx::B2IVector& rSize)
934 m_pSurface = pSurface;
935 m_aFrameSize = rSize;
936 dl_cairo_surface_get_device_scale(pSurface, &m_fScale, nullptr);
937 ResetClipRegion();
940 void SvpSalGraphics::GetResolution( sal_Int32& rDPIX, sal_Int32& rDPIY )
942 rDPIX = rDPIY = 96;
945 sal_uInt16 SvpSalGraphics::GetBitCount() const
947 if (cairo_surface_get_content(m_pSurface) != CAIRO_CONTENT_COLOR_ALPHA)
948 return 1;
949 return 32;
952 tools::Long SvpSalGraphics::GetGraphicsWidth() const
954 return m_pSurface ? m_aFrameSize.getX() : 0;
957 void SvpSalGraphics::ResetClipRegion()
959 m_aClipRegion.SetNull();
962 bool SvpSalGraphics::setClipRegion( const vcl::Region& i_rClip )
964 m_aClipRegion = i_rClip;
965 return true;
968 void SvpSalGraphics::SetLineColor()
970 m_aLineColor = SALCOLOR_NONE;
973 void SvpSalGraphics::SetLineColor( Color nColor )
975 m_aLineColor = nColor;
978 void SvpSalGraphics::SetFillColor()
980 m_aFillColor = SALCOLOR_NONE;
983 void SvpSalGraphics::SetFillColor( Color nColor )
985 m_aFillColor = nColor;
988 void SvpSalGraphics::SetXORMode(bool bSet, bool )
990 m_ePaintMode = bSet ? PaintMode::Xor : PaintMode::Over;
993 void SvpSalGraphics::SetROPLineColor( SalROPColor nROPColor )
995 switch( nROPColor )
997 case SalROPColor::N0:
998 m_aLineColor = Color(0, 0, 0);
999 break;
1000 case SalROPColor::N1:
1001 m_aLineColor = Color(0xff, 0xff, 0xff);
1002 break;
1003 case SalROPColor::Invert:
1004 m_aLineColor = Color(0xff, 0xff, 0xff);
1005 break;
1009 void SvpSalGraphics::SetROPFillColor( SalROPColor nROPColor )
1011 switch( nROPColor )
1013 case SalROPColor::N0:
1014 m_aFillColor = Color(0, 0, 0);
1015 break;
1016 case SalROPColor::N1:
1017 m_aFillColor = Color(0xff, 0xff, 0xff);
1018 break;
1019 case SalROPColor::Invert:
1020 m_aFillColor = Color(0xff, 0xff, 0xff);
1021 break;
1025 void SvpSalGraphics::drawPixel( tools::Long nX, tools::Long nY )
1027 if (m_aLineColor != SALCOLOR_NONE)
1029 drawPixel(nX, nY, m_aLineColor);
1033 void SvpSalGraphics::drawPixel( tools::Long nX, tools::Long nY, Color aColor )
1035 cairo_t* cr = getCairoContext(true);
1036 clipRegion(cr);
1038 cairo_rectangle(cr, nX, nY, 1, 1);
1039 applyColor(cr, aColor, 0.0);
1040 cairo_fill(cr);
1042 basegfx::B2DRange extents = getClippedFillDamage(cr);
1043 releaseCairoContext(cr, true, extents);
1046 void SvpSalGraphics::drawRect( tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight )
1048 // because of the -1 hack we have to do fill and draw separately
1049 Color aOrigFillColor = m_aFillColor;
1050 Color aOrigLineColor = m_aLineColor;
1051 m_aFillColor = SALCOLOR_NONE;
1052 m_aLineColor = SALCOLOR_NONE;
1054 if (aOrigFillColor != SALCOLOR_NONE)
1056 basegfx::B2DPolygon aRect = basegfx::utils::createPolygonFromRect(basegfx::B2DRectangle(nX, nY, nX+nWidth, nY+nHeight));
1057 m_aFillColor = aOrigFillColor;
1059 drawPolyPolygon(
1060 basegfx::B2DHomMatrix(),
1061 basegfx::B2DPolyPolygon(aRect),
1062 0.0);
1064 m_aFillColor = SALCOLOR_NONE;
1067 if (aOrigLineColor != SALCOLOR_NONE)
1069 // need same -1 hack as X11SalGraphicsImpl::drawRect
1070 basegfx::B2DPolygon aRect = basegfx::utils::createPolygonFromRect(basegfx::B2DRectangle( nX, nY, nX+nWidth-1, nY+nHeight-1));
1071 m_aLineColor = aOrigLineColor;
1073 drawPolyPolygon(
1074 basegfx::B2DHomMatrix(),
1075 basegfx::B2DPolyPolygon(aRect),
1076 0.0);
1078 m_aLineColor = SALCOLOR_NONE;
1081 m_aFillColor = aOrigFillColor;
1082 m_aLineColor = aOrigLineColor;
1085 void SvpSalGraphics::drawPolyLine(sal_uInt32 nPoints, const Point* pPtAry)
1087 basegfx::B2DPolygon aPoly;
1088 aPoly.append(basegfx::B2DPoint(pPtAry->getX(), pPtAry->getY()), nPoints);
1089 for (sal_uInt32 i = 1; i < nPoints; ++i)
1090 aPoly.setB2DPoint(i, basegfx::B2DPoint(pPtAry[i].getX(), pPtAry[i].getY()));
1091 aPoly.setClosed(false);
1093 drawPolyLine(
1094 basegfx::B2DHomMatrix(),
1095 aPoly,
1096 0.0,
1097 1.0,
1098 nullptr, // MM01
1099 basegfx::B2DLineJoin::Miter,
1100 css::drawing::LineCap_BUTT,
1101 basegfx::deg2rad(15.0) /*default*/,
1102 false);
1105 void SvpSalGraphics::drawPolygon(sal_uInt32 nPoints, const Point* pPtAry)
1107 basegfx::B2DPolygon aPoly;
1108 aPoly.append(basegfx::B2DPoint(pPtAry->getX(), pPtAry->getY()), nPoints);
1109 for (sal_uInt32 i = 1; i < nPoints; ++i)
1110 aPoly.setB2DPoint(i, basegfx::B2DPoint(pPtAry[i].getX(), pPtAry[i].getY()));
1112 drawPolyPolygon(
1113 basegfx::B2DHomMatrix(),
1114 basegfx::B2DPolyPolygon(aPoly),
1115 0.0);
1118 void SvpSalGraphics::drawPolyPolygon(sal_uInt32 nPoly,
1119 const sal_uInt32* pPointCounts,
1120 const Point** pPtAry)
1122 basegfx::B2DPolyPolygon aPolyPoly;
1123 for(sal_uInt32 nPolygon = 0; nPolygon < nPoly; ++nPolygon)
1125 sal_uInt32 nPoints = pPointCounts[nPolygon];
1126 if (nPoints)
1128 const Point* pPoints = pPtAry[nPolygon];
1129 basegfx::B2DPolygon aPoly;
1130 aPoly.append( basegfx::B2DPoint(pPoints->getX(), pPoints->getY()), nPoints);
1131 for (sal_uInt32 i = 1; i < nPoints; ++i)
1132 aPoly.setB2DPoint(i, basegfx::B2DPoint( pPoints[i].getX(), pPoints[i].getY()));
1134 aPolyPoly.append(aPoly);
1138 drawPolyPolygon(
1139 basegfx::B2DHomMatrix(),
1140 aPolyPoly,
1141 0.0);
1144 static basegfx::B2DPoint impPixelSnap(
1145 const basegfx::B2DPolygon& rPolygon,
1146 const basegfx::B2DHomMatrix& rObjectToDevice,
1147 basegfx::B2DHomMatrix& rObjectToDeviceInv,
1148 sal_uInt32 nIndex)
1150 const sal_uInt32 nCount(rPolygon.count());
1152 // get the data
1153 const basegfx::B2ITuple aPrevTuple(basegfx::fround(rObjectToDevice * rPolygon.getB2DPoint((nIndex + nCount - 1) % nCount)));
1154 const basegfx::B2DPoint aCurrPoint(rObjectToDevice * rPolygon.getB2DPoint(nIndex));
1155 const basegfx::B2ITuple aCurrTuple(basegfx::fround(aCurrPoint));
1156 const basegfx::B2ITuple aNextTuple(basegfx::fround(rObjectToDevice * rPolygon.getB2DPoint((nIndex + 1) % nCount)));
1158 // get the states
1159 const bool bPrevVertical(aPrevTuple.getX() == aCurrTuple.getX());
1160 const bool bNextVertical(aNextTuple.getX() == aCurrTuple.getX());
1161 const bool bPrevHorizontal(aPrevTuple.getY() == aCurrTuple.getY());
1162 const bool bNextHorizontal(aNextTuple.getY() == aCurrTuple.getY());
1163 const bool bSnapX(bPrevVertical || bNextVertical);
1164 const bool bSnapY(bPrevHorizontal || bNextHorizontal);
1166 if(bSnapX || bSnapY)
1168 basegfx::B2DPoint aSnappedPoint(
1169 bSnapX ? aCurrTuple.getX() : aCurrPoint.getX(),
1170 bSnapY ? aCurrTuple.getY() : aCurrPoint.getY());
1172 if(rObjectToDeviceInv.isIdentity())
1174 rObjectToDeviceInv = rObjectToDevice;
1175 rObjectToDeviceInv.invert();
1178 aSnappedPoint *= rObjectToDeviceInv;
1180 return aSnappedPoint;
1183 return rPolygon.getB2DPoint(nIndex);
1186 // Remove bClosePath: Checked that the already used mechanism for Win using
1187 // Gdiplus already relies on rPolygon.isClosed(), so should be safe to replace
1188 // this.
1189 // For PixelSnap we need the ObjectToDevice transformation here now. This is a
1190 // special case relative to the also executed LineDraw-Offset of (0.5, 0.5) in
1191 // DeviceCoordinates: The LineDraw-Offset is applied *after* the snap, so we
1192 // need the ObjectToDevice transformation *without* that offset here to do the
1193 // same. The LineDraw-Offset will be applied by the callers using a linear
1194 // transformation for Cairo now
1195 // For support of PixelSnapHairline we also need the ObjectToDevice transformation
1196 // and a method (same as in gdiimpl.cxx for Win and Gdiplus). This is needed e.g.
1197 // for Chart-content visualization. CAUTION: It's not the same as PixelSnap (!)
1198 // tdf#129845 add reply value to allow counting a point/byte/size measurement to
1199 // be included
1200 static size_t AddPolygonToPath(
1201 cairo_t* cr,
1202 const basegfx::B2DPolygon& rPolygon,
1203 const basegfx::B2DHomMatrix& rObjectToDevice,
1204 bool bPixelSnap,
1205 bool bPixelSnapHairline)
1207 // short circuit if there is nothing to do
1208 const sal_uInt32 nPointCount(rPolygon.count());
1209 size_t nSizeMeasure(0);
1211 if(0 == nPointCount)
1213 return nSizeMeasure;
1216 const bool bHasCurves(rPolygon.areControlPointsUsed());
1217 const bool bClosePath(rPolygon.isClosed());
1218 const bool bObjectToDeviceUsed(!rObjectToDevice.isIdentity());
1219 basegfx::B2DHomMatrix aObjectToDeviceInv;
1220 basegfx::B2DPoint aLast;
1222 for( sal_uInt32 nPointIdx = 0, nPrevIdx = 0;; nPrevIdx = nPointIdx++ )
1224 int nClosedIdx = nPointIdx;
1225 if( nPointIdx >= nPointCount )
1227 // prepare to close last curve segment if needed
1228 if( bClosePath && (nPointIdx == nPointCount) )
1230 nClosedIdx = 0;
1232 else
1234 break;
1238 basegfx::B2DPoint aPoint(rPolygon.getB2DPoint(nClosedIdx));
1240 if(bPixelSnap)
1242 // snap device coordinates to full pixels
1243 if(bObjectToDeviceUsed)
1245 // go to DeviceCoordinates
1246 aPoint *= rObjectToDevice;
1249 // snap by rounding
1250 aPoint.setX( basegfx::fround( aPoint.getX() ) );
1251 aPoint.setY( basegfx::fround( aPoint.getY() ) );
1253 if(bObjectToDeviceUsed)
1255 if(aObjectToDeviceInv.isIdentity())
1257 aObjectToDeviceInv = rObjectToDevice;
1258 aObjectToDeviceInv.invert();
1261 // go back to ObjectCoordinates
1262 aPoint *= aObjectToDeviceInv;
1266 if(bPixelSnapHairline)
1268 // snap horizontal and vertical lines (mainly used in Chart for
1269 // 'nicer' AAing)
1270 aPoint = impPixelSnap(rPolygon, rObjectToDevice, aObjectToDeviceInv, nClosedIdx);
1273 if( !nPointIdx )
1275 // first point => just move there
1276 cairo_move_to(cr, aPoint.getX(), aPoint.getY());
1277 aLast = aPoint;
1278 continue;
1281 bool bPendingCurve(false);
1283 if( bHasCurves )
1285 bPendingCurve = rPolygon.isNextControlPointUsed( nPrevIdx );
1286 bPendingCurve |= rPolygon.isPrevControlPointUsed( nClosedIdx );
1289 if( !bPendingCurve ) // line segment
1291 cairo_line_to(cr, aPoint.getX(), aPoint.getY());
1292 nSizeMeasure++;
1294 else // cubic bezier segment
1296 basegfx::B2DPoint aCP1 = rPolygon.getNextControlPoint( nPrevIdx );
1297 basegfx::B2DPoint aCP2 = rPolygon.getPrevControlPoint( nClosedIdx );
1299 // tdf#99165 if the control points are 'empty', create the mathematical
1300 // correct replacement ones to avoid problems with the graphical sub-system
1301 // tdf#101026 The 1st attempt to create a mathematically correct replacement control
1302 // vector was wrong. Best alternative is one as close as possible which means short.
1303 if (aCP1.equal(aLast))
1305 aCP1 = aLast + ((aCP2 - aLast) * 0.0005);
1308 if(aCP2.equal(aPoint))
1310 aCP2 = aPoint + ((aCP1 - aPoint) * 0.0005);
1313 cairo_curve_to(cr, aCP1.getX(), aCP1.getY(), aCP2.getX(), aCP2.getY(),
1314 aPoint.getX(), aPoint.getY());
1315 // take some bigger measure for curve segments - too expensive to subdivide
1316 // here and that precision not needed, but four (2 points, 2 control-points)
1317 // would be a too low weight
1318 nSizeMeasure += 10;
1321 aLast = aPoint;
1324 if( bClosePath )
1326 cairo_close_path(cr);
1329 return nSizeMeasure;
1332 void SvpSalGraphics::drawLine( tools::Long nX1, tools::Long nY1, tools::Long nX2, tools::Long nY2 )
1334 basegfx::B2DPolygon aPoly;
1336 // PixelOffset used: To not mix with possible PixelSnap, cannot do
1337 // directly on coordinates as tried before - despite being already 'snapped'
1338 // due to being integer. If it would be directly added here, it would be
1339 // 'snapped' again when !getAntiAlias(), losing the (0.5, 0.5) offset
1340 aPoly.append(basegfx::B2DPoint(nX1, nY1));
1341 aPoly.append(basegfx::B2DPoint(nX2, nY2));
1343 cairo_t* cr = getCairoContext(false);
1344 clipRegion(cr);
1346 // PixelOffset used: Set PixelOffset as linear transformation
1347 cairo_matrix_t aMatrix;
1348 cairo_matrix_init_translate(&aMatrix, 0.5, 0.5);
1349 cairo_set_matrix(cr, &aMatrix);
1351 AddPolygonToPath(
1353 aPoly,
1354 basegfx::B2DHomMatrix(),
1355 !getAntiAlias(),
1356 false);
1358 applyColor(cr, m_aLineColor);
1360 basegfx::B2DRange extents = getClippedStrokeDamage(cr);
1361 extents.transform(basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5));
1363 cairo_stroke(cr);
1365 releaseCairoContext(cr, false, extents);
1368 namespace {
1370 class SystemDependentData_CairoPath : public basegfx::SystemDependentData
1372 private:
1373 // the path data itself
1374 cairo_path_t* mpCairoPath;
1376 // all other values the path data is based on and
1377 // need to be compared with to check for data validity
1378 bool mbNoJoin;
1379 bool mbAntiAlias;
1380 std::vector< double > maStroke;
1382 public:
1383 SystemDependentData_CairoPath(
1384 basegfx::SystemDependentDataManager& rSystemDependentDataManager,
1385 size_t nSizeMeasure,
1386 cairo_t* cr,
1387 bool bNoJoin,
1388 bool bAntiAlias,
1389 const std::vector< double >* pStroke); // MM01
1390 virtual ~SystemDependentData_CairoPath() override;
1392 // read access
1393 cairo_path_t* getCairoPath() { return mpCairoPath; }
1394 bool getNoJoin() const { return mbNoJoin; }
1395 bool getAntiAlias() const { return mbAntiAlias; }
1396 const std::vector< double >& getStroke() const { return maStroke; }
1398 virtual sal_Int64 estimateUsageInBytes() const override;
1403 SystemDependentData_CairoPath::SystemDependentData_CairoPath(
1404 basegfx::SystemDependentDataManager& rSystemDependentDataManager,
1405 size_t nSizeMeasure,
1406 cairo_t* cr,
1407 bool bNoJoin,
1408 bool bAntiAlias,
1409 const std::vector< double >* pStroke)
1410 : basegfx::SystemDependentData(rSystemDependentDataManager),
1411 mpCairoPath(nullptr),
1412 mbNoJoin(bNoJoin),
1413 mbAntiAlias(bAntiAlias),
1414 maStroke()
1416 // tdf#129845 only create a copy of the path when nSizeMeasure is
1417 // bigger than some decent threshold
1418 if(nSizeMeasure > 50)
1420 mpCairoPath = cairo_copy_path(cr);
1422 if(nullptr != pStroke)
1424 maStroke = *pStroke;
1429 SystemDependentData_CairoPath::~SystemDependentData_CairoPath()
1431 if(nullptr != mpCairoPath)
1433 cairo_path_destroy(mpCairoPath);
1434 mpCairoPath = nullptr;
1438 sal_Int64 SystemDependentData_CairoPath::estimateUsageInBytes() const
1440 // tdf#129845 by using the default return value of zero when no path
1441 // was created, SystemDependentData::calculateCombinedHoldCyclesInSeconds
1442 // will do the right thing and not buffer this entry at all
1443 sal_Int64 nRetval(0);
1445 if(nullptr != mpCairoPath)
1447 // per node
1448 // - num_data incarnations of
1449 // - sizeof(cairo_path_data_t) which is a union of defines and point data
1450 // thus may 2 x sizeof(double)
1451 nRetval = mpCairoPath->num_data * sizeof(cairo_path_data_t);
1454 return nRetval;
1457 bool SvpSalGraphics::drawPolyLine(
1458 const basegfx::B2DHomMatrix& rObjectToDevice,
1459 const basegfx::B2DPolygon& rPolyLine,
1460 double fTransparency,
1461 double fLineWidth,
1462 const std::vector< double >* pStroke, // MM01
1463 basegfx::B2DLineJoin eLineJoin,
1464 css::drawing::LineCap eLineCap,
1465 double fMiterMinimumAngle,
1466 bool bPixelSnapHairline)
1468 // short circuit if there is nothing to do
1469 if(0 == rPolyLine.count() || fTransparency < 0.0 || fTransparency >= 1.0)
1471 return true;
1474 // Wrap call to static version of ::drawPolyLine by
1475 // preparing/getting some local data and parameters
1476 // due to usage in vcl/unx/generic/gdi/salgdi.cxx.
1477 // This is mainly about extended handling of extents
1478 // and the way destruction of CairoContext is handled
1479 // due to current XOR stuff
1480 cairo_t* cr = getCairoContext(false);
1481 basegfx::B2DRange aExtents;
1482 clipRegion(cr);
1484 bool bRetval(
1485 drawPolyLine(
1487 &aExtents,
1488 m_aLineColor,
1489 getAntiAlias(),
1490 rObjectToDevice,
1491 rPolyLine,
1492 fTransparency,
1493 fLineWidth,
1494 pStroke, // MM01
1495 eLineJoin,
1496 eLineCap,
1497 fMiterMinimumAngle,
1498 bPixelSnapHairline));
1500 releaseCairoContext(cr, false, aExtents);
1502 return bRetval;
1505 bool SvpSalGraphics::drawPolyLine(
1506 cairo_t* cr,
1507 basegfx::B2DRange* pExtents,
1508 const Color& rLineColor,
1509 bool bAntiAlias,
1510 const basegfx::B2DHomMatrix& rObjectToDevice,
1511 const basegfx::B2DPolygon& rPolyLine,
1512 double fTransparency,
1513 double fLineWidth,
1514 const std::vector< double >* pStroke, // MM01
1515 basegfx::B2DLineJoin eLineJoin,
1516 css::drawing::LineCap eLineCap,
1517 double fMiterMinimumAngle,
1518 bool bPixelSnapHairline)
1520 // short circuit if there is nothing to do
1521 if(0 == rPolyLine.count() || fTransparency < 0.0 || fTransparency >= 1.0)
1523 return true;
1526 // need to check/handle LineWidth when ObjectToDevice transformation is used
1527 const bool bObjectToDeviceIsIdentity(rObjectToDevice.isIdentity());
1529 // tdf#124848 calculate-back logical LineWidth for a hairline
1530 // since this implementation hands over the transformation to
1531 // the graphic sub-system
1532 if(fLineWidth == 0)
1534 fLineWidth = 1.0;
1536 if(!bObjectToDeviceIsIdentity)
1538 basegfx::B2DHomMatrix aObjectToDeviceInv(rObjectToDevice);
1539 aObjectToDeviceInv.invert();
1540 fLineWidth = (aObjectToDeviceInv * basegfx::B2DVector(fLineWidth, 0)).getLength();
1544 // PixelOffset used: Need to reflect in linear transformation
1545 cairo_matrix_t aMatrix;
1546 basegfx::B2DHomMatrix aDamageMatrix(basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5));
1548 if (bObjectToDeviceIsIdentity)
1550 // Set PixelOffset as requested
1551 cairo_matrix_init_translate(&aMatrix, 0.5, 0.5);
1553 else
1555 // Prepare ObjectToDevice transformation. Take PixelOffset for Lines into
1556 // account: Multiply from left to act in DeviceCoordinates
1557 aDamageMatrix = aDamageMatrix * rObjectToDevice;
1558 cairo_matrix_init(
1559 &aMatrix,
1560 aDamageMatrix.get( 0, 0 ),
1561 aDamageMatrix.get( 1, 0 ),
1562 aDamageMatrix.get( 0, 1 ),
1563 aDamageMatrix.get( 1, 1 ),
1564 aDamageMatrix.get( 0, 2 ),
1565 aDamageMatrix.get( 1, 2 ));
1568 // set linear transformation
1569 cairo_set_matrix(cr, &aMatrix);
1571 // setup line attributes
1572 cairo_line_join_t eCairoLineJoin = CAIRO_LINE_JOIN_MITER;
1573 switch (eLineJoin)
1575 case basegfx::B2DLineJoin::Bevel:
1576 eCairoLineJoin = CAIRO_LINE_JOIN_BEVEL;
1577 break;
1578 case basegfx::B2DLineJoin::Round:
1579 eCairoLineJoin = CAIRO_LINE_JOIN_ROUND;
1580 break;
1581 case basegfx::B2DLineJoin::NONE:
1582 case basegfx::B2DLineJoin::Miter:
1583 eCairoLineJoin = CAIRO_LINE_JOIN_MITER;
1584 break;
1587 // convert miter minimum angle to miter limit
1588 double fMiterLimit = 1.0 / sin( fMiterMinimumAngle / 2.0);
1590 // setup cap attribute
1591 cairo_line_cap_t eCairoLineCap(CAIRO_LINE_CAP_BUTT);
1593 switch (eLineCap)
1595 default: // css::drawing::LineCap_BUTT:
1597 eCairoLineCap = CAIRO_LINE_CAP_BUTT;
1598 break;
1600 case css::drawing::LineCap_ROUND:
1602 eCairoLineCap = CAIRO_LINE_CAP_ROUND;
1603 break;
1605 case css::drawing::LineCap_SQUARE:
1607 eCairoLineCap = CAIRO_LINE_CAP_SQUARE;
1608 break;
1612 cairo_set_source_rgba(
1614 rLineColor.GetRed()/255.0,
1615 rLineColor.GetGreen()/255.0,
1616 rLineColor.GetBlue()/255.0,
1617 1.0-fTransparency);
1619 cairo_set_line_join(cr, eCairoLineJoin);
1620 cairo_set_line_cap(cr, eCairoLineCap);
1621 cairo_set_line_width(cr, fLineWidth);
1622 cairo_set_miter_limit(cr, fMiterLimit);
1624 // try to access buffered data
1625 std::shared_ptr<SystemDependentData_CairoPath> pSystemDependentData_CairoPath(
1626 rPolyLine.getSystemDependentData<SystemDependentData_CairoPath>());
1628 // MM01 need to do line dashing as fallback stuff here now
1629 const double fDotDashLength(nullptr != pStroke ? std::accumulate(pStroke->begin(), pStroke->end(), 0.0) : 0.0);
1630 const bool bStrokeUsed(0.0 != fDotDashLength);
1631 assert(!bStrokeUsed || (bStrokeUsed && pStroke));
1633 // MM01 decide if to stroke directly
1634 static bool bDoDirectCairoStroke(true);
1636 // MM01 activate to stroke directly
1637 if(bDoDirectCairoStroke && bStrokeUsed)
1639 cairo_set_dash(cr, pStroke->data(), pStroke->size(), 0.0);
1642 if(!bDoDirectCairoStroke && pSystemDependentData_CairoPath)
1644 // MM01 - check on stroke change. Used against not used, or if both used,
1645 // equal or different?
1646 const bool bStrokeWasUsed(!pSystemDependentData_CairoPath->getStroke().empty());
1648 if(bStrokeWasUsed != bStrokeUsed
1649 || (bStrokeUsed && *pStroke != pSystemDependentData_CairoPath->getStroke()))
1651 // data invalid, forget
1652 pSystemDependentData_CairoPath.reset();
1656 // check for basegfx::B2DLineJoin::NONE to react accordingly
1657 const bool bNoJoin((basegfx::B2DLineJoin::NONE == eLineJoin
1658 && basegfx::fTools::more(fLineWidth, 0.0)));
1660 if(pSystemDependentData_CairoPath)
1662 // check data validity
1663 if(nullptr == pSystemDependentData_CairoPath->getCairoPath()
1664 || pSystemDependentData_CairoPath->getNoJoin() != bNoJoin
1665 || pSystemDependentData_CairoPath->getAntiAlias() != bAntiAlias
1666 || bPixelSnapHairline /*tdf#124700*/ )
1668 // data invalid, forget
1669 pSystemDependentData_CairoPath.reset();
1673 if(pSystemDependentData_CairoPath)
1675 // re-use data
1676 cairo_append_path(cr, pSystemDependentData_CairoPath->getCairoPath());
1678 else
1680 // create data
1681 size_t nSizeMeasure(0);
1683 // MM01 need to do line dashing as fallback stuff here now
1684 basegfx::B2DPolyPolygon aPolyPolygonLine;
1686 if(!bDoDirectCairoStroke && bStrokeUsed)
1688 // apply LineStyle
1689 basegfx::utils::applyLineDashing(
1690 rPolyLine, // source
1691 *pStroke, // pattern
1692 &aPolyPolygonLine, // target for lines
1693 nullptr, // target for gaps
1694 fDotDashLength); // full length if available
1696 else
1698 // no line dashing or direct stroke, just copy
1699 aPolyPolygonLine.append(rPolyLine);
1702 // MM01 checked/verified for Cairo
1703 for(sal_uInt32 a(0); a < aPolyPolygonLine.count(); a++)
1705 const basegfx::B2DPolygon aPolyLine(aPolyPolygonLine.getB2DPolygon(a));
1707 if (!bNoJoin)
1709 // PixelOffset now reflected in linear transformation used
1710 nSizeMeasure += AddPolygonToPath(
1712 aPolyLine,
1713 rObjectToDevice, // ObjectToDevice *without* LineDraw-Offset
1714 !bAntiAlias,
1715 bPixelSnapHairline);
1717 else
1719 const sal_uInt32 nPointCount(aPolyLine.count());
1720 const sal_uInt32 nEdgeCount(aPolyLine.isClosed() ? nPointCount : nPointCount - 1);
1721 basegfx::B2DPolygon aEdge;
1723 aEdge.append(aPolyLine.getB2DPoint(0));
1724 aEdge.append(basegfx::B2DPoint(0.0, 0.0));
1726 for (sal_uInt32 i(0); i < nEdgeCount; i++)
1728 const sal_uInt32 nNextIndex((i + 1) % nPointCount);
1729 aEdge.setB2DPoint(1, aPolyLine.getB2DPoint(nNextIndex));
1730 aEdge.setNextControlPoint(0, aPolyLine.getNextControlPoint(i));
1731 aEdge.setPrevControlPoint(1, aPolyLine.getPrevControlPoint(nNextIndex));
1733 // PixelOffset now reflected in linear transformation used
1734 nSizeMeasure += AddPolygonToPath(
1736 aEdge,
1737 rObjectToDevice, // ObjectToDevice *without* LineDraw-Offset
1738 !bAntiAlias,
1739 bPixelSnapHairline);
1741 // prepare next step
1742 aEdge.setB2DPoint(0, aEdge.getB2DPoint(1));
1747 // copy and add to buffering mechanism
1748 if (!bPixelSnapHairline /*tdf#124700*/)
1750 pSystemDependentData_CairoPath = rPolyLine.addOrReplaceSystemDependentData<SystemDependentData_CairoPath>(
1751 ImplGetSystemDependentDataManager(),
1752 nSizeMeasure,
1754 bNoJoin,
1755 bAntiAlias,
1756 pStroke);
1760 // extract extents
1761 if (pExtents)
1763 *pExtents = getClippedStrokeDamage(cr);
1764 // transform also extents (ranges) of damage so they can be correctly redrawn
1765 pExtents->transform(aDamageMatrix);
1768 // draw and consume
1769 cairo_stroke(cr);
1771 return true;
1774 bool SvpSalGraphics::drawPolyLineBezier( sal_uInt32,
1775 const Point*,
1776 const PolyFlags* )
1778 SAL_INFO("vcl.gdi", "unsupported SvpSalGraphics::drawPolyLineBezier case");
1779 return false;
1782 bool SvpSalGraphics::drawPolygonBezier( sal_uInt32,
1783 const Point*,
1784 const PolyFlags* )
1786 SAL_INFO("vcl.gdi", "unsupported SvpSalGraphics::drawPolygonBezier case");
1787 return false;
1790 bool SvpSalGraphics::drawPolyPolygonBezier( sal_uInt32,
1791 const sal_uInt32*,
1792 const Point* const*,
1793 const PolyFlags* const* )
1795 SAL_INFO("vcl.gdi", "unsupported SvpSalGraphics::drawPolyPolygonBezier case");
1796 return false;
1799 namespace
1801 void add_polygon_path(cairo_t* cr, const basegfx::B2DPolyPolygon& rPolyPolygon, const basegfx::B2DHomMatrix& rObjectToDevice, bool bPixelSnap)
1803 // try to access buffered data
1804 std::shared_ptr<SystemDependentData_CairoPath> pSystemDependentData_CairoPath(
1805 rPolyPolygon.getSystemDependentData<SystemDependentData_CairoPath>());
1807 if(pSystemDependentData_CairoPath)
1809 // re-use data
1810 cairo_append_path(cr, pSystemDependentData_CairoPath->getCairoPath());
1812 else
1814 // create data
1815 size_t nSizeMeasure(0);
1817 for (const auto & rPoly : rPolyPolygon)
1819 // PixelOffset used: Was dependent of 'm_aLineColor != SALCOLOR_NONE'
1820 // Adapt setupPolyPolygon-users to set a linear transformation to achieve PixelOffset
1821 nSizeMeasure += AddPolygonToPath(
1823 rPoly,
1824 rObjectToDevice,
1825 bPixelSnap,
1826 false);
1829 // copy and add to buffering mechanism
1830 // for decisions how/what to buffer, see Note in WinSalGraphicsImpl::drawPolyPolygon
1831 pSystemDependentData_CairoPath = rPolyPolygon.addOrReplaceSystemDependentData<SystemDependentData_CairoPath>(
1832 ImplGetSystemDependentDataManager(),
1833 nSizeMeasure,
1835 false,
1836 false,
1837 nullptr);
1842 bool SvpSalGraphics::drawPolyPolygon(
1843 const basegfx::B2DHomMatrix& rObjectToDevice,
1844 const basegfx::B2DPolyPolygon& rPolyPolygon,
1845 double fTransparency)
1847 const bool bHasFill(m_aFillColor != SALCOLOR_NONE);
1848 const bool bHasLine(m_aLineColor != SALCOLOR_NONE);
1850 if(0 == rPolyPolygon.count() || !(bHasFill || bHasLine) || fTransparency < 0.0 || fTransparency >= 1.0)
1852 return true;
1855 cairo_t* cr = getCairoContext(true);
1856 clipRegion(cr);
1858 // Set full (Object-to-Device) transformation - if used
1859 if(!rObjectToDevice.isIdentity())
1861 cairo_matrix_t aMatrix;
1863 cairo_matrix_init(
1864 &aMatrix,
1865 rObjectToDevice.get( 0, 0 ),
1866 rObjectToDevice.get( 1, 0 ),
1867 rObjectToDevice.get( 0, 1 ),
1868 rObjectToDevice.get( 1, 1 ),
1869 rObjectToDevice.get( 0, 2 ),
1870 rObjectToDevice.get( 1, 2 ));
1871 cairo_set_matrix(cr, &aMatrix);
1874 // To make releaseCairoContext work, use empty extents
1875 basegfx::B2DRange extents;
1877 if (bHasFill)
1879 add_polygon_path(cr, rPolyPolygon, rObjectToDevice, !getAntiAlias());
1881 applyColor(cr, m_aFillColor, fTransparency);
1882 // Get FillDamage (will be extended for LineDamage below)
1883 extents = getClippedFillDamage(cr);
1885 cairo_fill(cr);
1888 if (bHasLine)
1890 // PixelOffset used: Set PixelOffset as linear transformation
1891 cairo_matrix_t aMatrix;
1892 cairo_matrix_init_translate(&aMatrix, 0.5, 0.5);
1893 cairo_set_matrix(cr, &aMatrix);
1895 add_polygon_path(cr, rPolyPolygon, rObjectToDevice, !getAntiAlias());
1897 applyColor(cr, m_aLineColor, fTransparency);
1899 // expand with possible StrokeDamage
1900 basegfx::B2DRange stroke_extents = getClippedStrokeDamage(cr);
1901 stroke_extents.transform(basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5));
1902 extents.expand(stroke_extents);
1904 cairo_stroke(cr);
1907 // if transformation has been applied, transform also extents (ranges)
1908 // of damage so they can be correctly redrawn
1909 extents.transform(rObjectToDevice);
1910 releaseCairoContext(cr, true, extents);
1912 return true;
1915 bool SvpSalGraphics::drawGradient(const tools::PolyPolygon& rPolyPolygon, const Gradient& rGradient)
1917 if (rGradient.GetStyle() != GradientStyle::Linear
1918 && rGradient.GetStyle() != GradientStyle::Radial)
1919 return false; // unsupported
1920 if (rGradient.GetSteps() != 0)
1921 return false; // We can't tell cairo how many colors to use in the gradient.
1923 cairo_t* cr = getCairoContext(true);
1924 clipRegion(cr);
1926 tools::Rectangle aInputRect(rPolyPolygon.GetBoundRect());
1927 if( rPolyPolygon.IsRect())
1929 // Rect->Polygon conversion loses the right and bottom edge, fix that.
1930 aInputRect.AdjustRight( 1 );
1931 aInputRect.AdjustBottom( 1 );
1932 basegfx::B2DHomMatrix rObjectToDevice;
1933 AddPolygonToPath(cr, tools::Polygon(aInputRect).getB2DPolygon(), rObjectToDevice, !getAntiAlias(), false);
1935 else
1937 basegfx::B2DPolyPolygon aB2DPolyPolygon(rPolyPolygon.getB2DPolyPolygon());
1938 for (auto const & rPolygon : aB2DPolyPolygon)
1940 basegfx::B2DHomMatrix rObjectToDevice;
1941 AddPolygonToPath(cr, rPolygon, rObjectToDevice, !getAntiAlias(), false);
1945 Gradient aGradient(rGradient);
1947 tools::Rectangle aBoundRect;
1948 Point aCenter;
1950 aGradient.SetAngle(aGradient.GetAngle() + Degree10(2700));
1951 aGradient.GetBoundRect(aInputRect, aBoundRect, aCenter);
1952 Color aStartColor = aGradient.GetStartColor();
1953 Color aEndColor = aGradient.GetEndColor();
1955 cairo_pattern_t* pattern;
1956 if (rGradient.GetStyle() == GradientStyle::Linear)
1958 tools::Polygon aPoly(aBoundRect);
1959 aPoly.Rotate(aCenter, aGradient.GetAngle() % Degree10(3600));
1960 pattern = cairo_pattern_create_linear(aPoly[0].X(), aPoly[0].Y(), aPoly[1].X(), aPoly[1].Y());
1962 else
1964 double radius = std::max(aBoundRect.GetWidth() / 2.0, aBoundRect.GetHeight() / 2.0);
1965 // Move the center a bit to the top-left (the default VCL algorithm is a bit off-center that way,
1966 // cairo is the opposite way).
1967 pattern = cairo_pattern_create_radial(aCenter.X() - 0.5, aCenter.Y() - 0.5, 0,
1968 aCenter.X() - 0.5, aCenter.Y() - 0.5, radius);
1969 std::swap( aStartColor, aEndColor );
1972 cairo_pattern_add_color_stop_rgba(pattern, aGradient.GetBorder() / 100.0,
1973 aStartColor.GetRed() * aGradient.GetStartIntensity() / 25500.0,
1974 aStartColor.GetGreen() * aGradient.GetStartIntensity() / 25500.0,
1975 aStartColor.GetBlue() * aGradient.GetStartIntensity() / 25500.0,
1976 1.0);
1978 cairo_pattern_add_color_stop_rgba(pattern, 1.0,
1979 aEndColor.GetRed() * aGradient.GetEndIntensity() / 25500.0,
1980 aEndColor.GetGreen() * aGradient.GetEndIntensity() / 25500.0,
1981 aEndColor.GetBlue() * aGradient.GetEndIntensity() / 25500.0,
1982 1.0);
1984 cairo_set_source(cr, pattern);
1985 cairo_pattern_destroy(pattern);
1987 basegfx::B2DRange extents = getClippedFillDamage(cr);
1988 cairo_fill_preserve(cr);
1990 releaseCairoContext(cr, true, extents);
1992 return true;
1995 bool SvpSalGraphics::implDrawGradient(basegfx::B2DPolyPolygon const & rPolyPolygon, SalGradient const & rGradient)
1997 cairo_t* cr = getCairoContext(true);
1998 clipRegion(cr);
2000 basegfx::B2DHomMatrix rObjectToDevice;
2002 for (auto const & rPolygon : rPolyPolygon)
2003 AddPolygonToPath(cr, rPolygon, rObjectToDevice, !getAntiAlias(), false);
2005 cairo_pattern_t* pattern = cairo_pattern_create_linear(rGradient.maPoint1.getX(), rGradient.maPoint1.getY(), rGradient.maPoint2.getX(), rGradient.maPoint2.getY());
2007 for (SalGradientStop const & rStop : rGradient.maStops)
2009 double r = rStop.maColor.GetRed() / 255.0;
2010 double g = rStop.maColor.GetGreen() / 255.0;
2011 double b = rStop.maColor.GetBlue() / 255.0;
2012 double a = (0xFF - rStop.maColor.GetTransparency()) / 255.0;
2013 double offset = rStop.mfOffset;
2015 cairo_pattern_add_color_stop_rgba(pattern, offset, r, g, b, a);
2017 cairo_set_source(cr, pattern);
2018 cairo_pattern_destroy(pattern);
2020 basegfx::B2DRange extents = getClippedFillDamage(cr);
2021 cairo_fill_preserve(cr);
2023 releaseCairoContext(cr, true, extents);
2025 return true;
2028 void SvpSalGraphics::applyColor(cairo_t *cr, Color aColor, double fTransparency)
2030 if (cairo_surface_get_content(m_pSurface) == CAIRO_CONTENT_COLOR_ALPHA)
2032 cairo_set_source_rgba(cr, aColor.GetRed()/255.0,
2033 aColor.GetGreen()/255.0,
2034 aColor.GetBlue()/255.0,
2035 1.0 - fTransparency);
2037 else
2039 double fSet = aColor == COL_BLACK ? 1.0 : 0.0;
2040 cairo_set_source_rgba(cr, 1, 1, 1, fSet);
2041 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2045 void SvpSalGraphics::copyArea( tools::Long nDestX,
2046 tools::Long nDestY,
2047 tools::Long nSrcX,
2048 tools::Long nSrcY,
2049 tools::Long nSrcWidth,
2050 tools::Long nSrcHeight,
2051 bool /*bWindowInvalidate*/ )
2053 SalTwoRect aTR(nSrcX, nSrcY, nSrcWidth, nSrcHeight, nDestX, nDestY, nSrcWidth, nSrcHeight);
2054 copyBits(aTR, this);
2057 static basegfx::B2DRange renderWithOperator(cairo_t* cr, const SalTwoRect& rTR,
2058 cairo_surface_t* source, cairo_operator_t eOperator = CAIRO_OPERATOR_SOURCE)
2060 cairo_rectangle(cr, rTR.mnDestX, rTR.mnDestY, rTR.mnDestWidth, rTR.mnDestHeight);
2062 basegfx::B2DRange extents = getClippedFillDamage(cr);
2064 cairo_clip(cr);
2066 cairo_translate(cr, rTR.mnDestX, rTR.mnDestY);
2067 double fXScale = 1.0f;
2068 double fYScale = 1.0f;
2069 if (rTR.mnSrcWidth != 0 && rTR.mnSrcHeight != 0) {
2070 fXScale = static_cast<double>(rTR.mnDestWidth)/rTR.mnSrcWidth;
2071 fYScale = static_cast<double>(rTR.mnDestHeight)/rTR.mnSrcHeight;
2072 cairo_scale(cr, fXScale, fYScale);
2075 cairo_save(cr);
2076 cairo_set_source_surface(cr, source, -rTR.mnSrcX, -rTR.mnSrcY);
2077 if ((fXScale != 1.0 && rTR.mnSrcWidth == 1) || (fYScale != 1.0 && rTR.mnSrcHeight == 1))
2079 cairo_pattern_t* sourcepattern = cairo_get_source(cr);
2080 cairo_pattern_set_extend(sourcepattern, CAIRO_EXTEND_REPEAT);
2081 cairo_pattern_set_filter(sourcepattern, CAIRO_FILTER_NEAREST);
2083 cairo_set_operator(cr, eOperator);
2084 cairo_paint(cr);
2085 cairo_restore(cr);
2087 return extents;
2090 static basegfx::B2DRange renderSource(cairo_t* cr, const SalTwoRect& rTR,
2091 cairo_surface_t* source)
2093 return renderWithOperator(cr, rTR, source, CAIRO_OPERATOR_SOURCE);
2096 void SvpSalGraphics::copyWithOperator( const SalTwoRect& rTR, cairo_surface_t* source,
2097 cairo_operator_t eOp )
2099 cairo_t* cr = getCairoContext(false);
2100 clipRegion(cr);
2102 basegfx::B2DRange extents = renderWithOperator(cr, rTR, source, eOp);
2104 releaseCairoContext(cr, false, extents);
2107 void SvpSalGraphics::copySource( const SalTwoRect& rTR, cairo_surface_t* source )
2109 copyWithOperator(rTR, source, CAIRO_OPERATOR_SOURCE);
2112 void SvpSalGraphics::copyBits( const SalTwoRect& rTR,
2113 SalGraphics* pSrcGraphics )
2115 SalTwoRect aTR(rTR);
2117 SvpSalGraphics* pSrc = pSrcGraphics ?
2118 static_cast<SvpSalGraphics*>(pSrcGraphics) : this;
2120 cairo_surface_t* source = pSrc->m_pSurface;
2122 cairo_surface_t *pCopy = nullptr;
2123 if (pSrc == this)
2125 //self copy is a problem, so dup source in that case
2126 pCopy = cairo_surface_create_similar(source,
2127 cairo_surface_get_content(m_pSurface),
2128 aTR.mnSrcWidth * m_fScale,
2129 aTR.mnSrcHeight * m_fScale);
2130 dl_cairo_surface_set_device_scale(pCopy, m_fScale, m_fScale);
2131 cairo_t* cr = cairo_create(pCopy);
2132 cairo_set_source_surface(cr, source, -aTR.mnSrcX, -aTR.mnSrcY);
2133 cairo_rectangle(cr, 0, 0, aTR.mnSrcWidth, aTR.mnSrcHeight);
2134 cairo_fill(cr);
2135 cairo_destroy(cr);
2137 source = pCopy;
2139 aTR.mnSrcX = 0;
2140 aTR.mnSrcY = 0;
2143 copySource(aTR, source);
2145 if (pCopy)
2146 cairo_surface_destroy(pCopy);
2149 void SvpSalGraphics::drawBitmap(const SalTwoRect& rTR, const SalBitmap& rSourceBitmap)
2151 // MM02 try to access buffered BitmapHelper
2152 std::shared_ptr<BitmapHelper> aSurface;
2153 tryToUseSourceBuffer(rSourceBitmap, aSurface);
2154 cairo_surface_t* source = aSurface->getSurface(
2155 rTR.mnDestWidth,
2156 rTR.mnDestHeight);
2158 if (!source)
2160 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawAlphaBitmap case");
2161 return;
2164 copyWithOperator(rTR, source, CAIRO_OPERATOR_OVER);
2167 void SvpSalGraphics::drawBitmap(const SalTwoRect& rTR, const BitmapBuffer* pBuffer, cairo_operator_t eOp)
2169 cairo_surface_t* source = createCairoSurface( pBuffer );
2170 copyWithOperator(rTR, source, eOp);
2171 cairo_surface_destroy(source);
2174 void SvpSalGraphics::drawBitmap( const SalTwoRect& rTR,
2175 const SalBitmap& rSourceBitmap,
2176 const SalBitmap& rTransparentBitmap )
2178 drawAlphaBitmap(rTR, rSourceBitmap, rTransparentBitmap);
2181 void SvpSalGraphics::drawMask( const SalTwoRect& rTR,
2182 const SalBitmap& rSalBitmap,
2183 Color nMaskColor )
2185 /** creates an image from the given rectangle, replacing all black pixels
2186 * with nMaskColor and make all other full transparent */
2187 // MM02 here decided *against* using buffered BitmapHelper
2188 // because the data gets somehow 'unmuliplied'. This may also be
2189 // done just once, but I am not sure if this is safe to do.
2190 // So for now dispense re-using data here.
2191 BitmapHelper aSurface(rSalBitmap, true); // The mask is argb32
2192 if (!aSurface.getSurface())
2194 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawMask case");
2195 return;
2197 sal_Int32 nStride;
2198 unsigned char *mask_data = aSurface.getBits(nStride);
2199 vcl::bitmap::lookup_table unpremultiply_table = vcl::bitmap::get_unpremultiply_table();
2200 for (tools::Long y = rTR.mnSrcY ; y < rTR.mnSrcY + rTR.mnSrcHeight; ++y)
2202 unsigned char *row = mask_data + (nStride*y);
2203 unsigned char *data = row + (rTR.mnSrcX * 4);
2204 for (tools::Long x = rTR.mnSrcX; x < rTR.mnSrcX + rTR.mnSrcWidth; ++x)
2206 sal_uInt8 a = data[SVP_CAIRO_ALPHA];
2207 sal_uInt8 b = unpremultiply_table[a][data[SVP_CAIRO_BLUE]];
2208 sal_uInt8 g = unpremultiply_table[a][data[SVP_CAIRO_GREEN]];
2209 sal_uInt8 r = unpremultiply_table[a][data[SVP_CAIRO_RED]];
2210 if (r == 0 && g == 0 && b == 0)
2212 data[0] = nMaskColor.GetBlue();
2213 data[1] = nMaskColor.GetGreen();
2214 data[2] = nMaskColor.GetRed();
2215 data[3] = 0xff;
2217 else
2219 data[0] = 0;
2220 data[1] = 0;
2221 data[2] = 0;
2222 data[3] = 0;
2224 data+=4;
2227 aSurface.mark_dirty();
2229 cairo_t* cr = getCairoContext(false);
2230 clipRegion(cr);
2232 cairo_rectangle(cr, rTR.mnDestX, rTR.mnDestY, rTR.mnDestWidth, rTR.mnDestHeight);
2234 basegfx::B2DRange extents = getClippedFillDamage(cr);
2236 cairo_clip(cr);
2238 cairo_translate(cr, rTR.mnDestX, rTR.mnDestY);
2239 double fXScale = static_cast<double>(rTR.mnDestWidth)/rTR.mnSrcWidth;
2240 double fYScale = static_cast<double>(rTR.mnDestHeight)/rTR.mnSrcHeight;
2241 cairo_scale(cr, fXScale, fYScale);
2242 cairo_set_source_surface(cr, aSurface.getSurface(), -rTR.mnSrcX, -rTR.mnSrcY);
2243 if ((fXScale != 1.0 && rTR.mnSrcWidth == 1) || (fYScale != 1.0 && rTR.mnSrcHeight == 1))
2245 cairo_pattern_t* sourcepattern = cairo_get_source(cr);
2246 cairo_pattern_set_extend(sourcepattern, CAIRO_EXTEND_REPEAT);
2247 cairo_pattern_set_filter(sourcepattern, CAIRO_FILTER_NEAREST);
2249 cairo_paint(cr);
2251 releaseCairoContext(cr, false, extents);
2254 std::shared_ptr<SalBitmap> SvpSalGraphics::getBitmap( tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight )
2256 std::shared_ptr<SvpSalBitmap> pBitmap = std::make_shared<SvpSalBitmap>();
2257 BitmapPalette aPal;
2258 if (GetBitCount() == 1)
2260 aPal.SetEntryCount(2);
2261 aPal[0] = COL_BLACK;
2262 aPal[1] = COL_WHITE;
2265 if (!pBitmap->Create(Size(nWidth, nHeight), GetBitCount(), aPal))
2267 SAL_WARN("vcl.gdi", "SvpSalGraphics::getBitmap, cannot create bitmap");
2268 return nullptr;
2271 cairo_surface_t* target = SvpSalGraphics::createCairoSurface(pBitmap->GetBuffer());
2272 if (!target)
2274 SAL_WARN("vcl.gdi", "SvpSalGraphics::getBitmap, cannot create cairo surface");
2275 return nullptr;
2277 cairo_t* cr = cairo_create(target);
2279 SalTwoRect aTR(nX, nY, nWidth, nHeight, 0, 0, nWidth, nHeight);
2280 renderSource(cr, aTR, m_pSurface);
2282 cairo_destroy(cr);
2283 cairo_surface_destroy(target);
2285 Toggle1BitTransparency(*pBitmap->GetBuffer());
2287 return pBitmap;
2290 Color SvpSalGraphics::getPixel( tools::Long nX, tools::Long nY )
2292 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0)
2293 cairo_surface_t *target = cairo_surface_create_similar_image(m_pSurface, CAIRO_FORMAT_ARGB32, 1, 1);
2294 #else
2295 cairo_surface_t *target = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
2296 #endif
2298 cairo_t* cr = cairo_create(target);
2300 cairo_rectangle(cr, 0, 0, 1, 1);
2301 cairo_set_source_surface(cr, m_pSurface, -nX, -nY);
2302 cairo_paint(cr);
2303 cairo_destroy(cr);
2305 cairo_surface_flush(target);
2306 vcl::bitmap::lookup_table unpremultiply_table = vcl::bitmap::get_unpremultiply_table();
2307 unsigned char *data = cairo_image_surface_get_data(target);
2308 sal_uInt8 a = data[SVP_CAIRO_ALPHA];
2309 sal_uInt8 b = unpremultiply_table[a][data[SVP_CAIRO_BLUE]];
2310 sal_uInt8 g = unpremultiply_table[a][data[SVP_CAIRO_GREEN]];
2311 sal_uInt8 r = unpremultiply_table[a][data[SVP_CAIRO_RED]];
2312 Color aColor(0xFF - a, r, g, b);
2313 cairo_surface_destroy(target);
2315 return aColor;
2318 namespace
2320 cairo_pattern_t * create_stipple()
2322 static unsigned char data[16] = { 0xFF, 0xFF, 0x00, 0x00,
2323 0xFF, 0xFF, 0x00, 0x00,
2324 0x00, 0x00, 0xFF, 0xFF,
2325 0x00, 0x00, 0xFF, 0xFF };
2326 cairo_surface_t* surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_A8, 4, 4, 4);
2327 cairo_pattern_t* pattern = cairo_pattern_create_for_surface(surface);
2328 cairo_surface_destroy(surface);
2329 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
2330 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
2331 return pattern;
2335 void SvpSalGraphics::invert(const basegfx::B2DPolygon &rPoly, SalInvert nFlags)
2337 cairo_t* cr = getCairoContext(false);
2338 clipRegion(cr);
2340 // To make releaseCairoContext work, use empty extents
2341 basegfx::B2DRange extents;
2343 AddPolygonToPath(
2345 rPoly,
2346 basegfx::B2DHomMatrix(),
2347 !getAntiAlias(),
2348 false);
2350 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2352 if (cairo_version() >= CAIRO_VERSION_ENCODE(1, 10, 0))
2354 cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
2356 else
2358 SAL_WARN("vcl.gdi", "SvpSalGraphics::invert, archaic cairo");
2361 if (nFlags & SalInvert::TrackFrame)
2363 cairo_set_line_width(cr, 2.0);
2364 const double dashLengths[2] = { 4.0, 4.0 };
2365 cairo_set_dash(cr, dashLengths, 2, 0);
2367 extents = getClippedStrokeDamage(cr);
2368 //see tdf#106577 under wayland, some pixel droppings seen, maybe we're
2369 //out by one somewhere, or cairo_stroke_extents is confused by
2370 //dashes/line width
2371 if(!extents.isEmpty())
2373 extents.grow(1);
2376 cairo_stroke(cr);
2378 else
2380 extents = getClippedFillDamage(cr);
2382 cairo_clip(cr);
2384 if (nFlags & SalInvert::N50)
2386 cairo_pattern_t *pattern = create_stipple();
2387 cairo_surface_t* surface = cairo_surface_create_similar(m_pSurface,
2388 cairo_surface_get_content(m_pSurface),
2389 extents.getWidth() * m_fScale,
2390 extents.getHeight() * m_fScale);
2392 dl_cairo_surface_set_device_scale(surface, m_fScale, m_fScale);
2393 cairo_t* stipple_cr = cairo_create(surface);
2394 cairo_set_source_rgb(stipple_cr, 1.0, 1.0, 1.0);
2395 cairo_mask(stipple_cr, pattern);
2396 cairo_pattern_destroy(pattern);
2397 cairo_destroy(stipple_cr);
2398 cairo_mask_surface(cr, surface, extents.getMinX(), extents.getMinY());
2399 cairo_surface_destroy(surface);
2401 else
2403 cairo_paint(cr);
2407 releaseCairoContext(cr, false, extents);
2410 void SvpSalGraphics::invert( tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight, SalInvert nFlags )
2412 basegfx::B2DPolygon aRect = basegfx::utils::createPolygonFromRect(basegfx::B2DRectangle(nX, nY, nX+nWidth, nY+nHeight));
2414 invert(aRect, nFlags);
2417 void SvpSalGraphics::invert(sal_uInt32 nPoints, const Point* pPtAry, SalInvert nFlags)
2419 basegfx::B2DPolygon aPoly;
2420 aPoly.append(basegfx::B2DPoint(pPtAry->getX(), pPtAry->getY()), nPoints);
2421 for (sal_uInt32 i = 1; i < nPoints; ++i)
2422 aPoly.setB2DPoint(i, basegfx::B2DPoint(pPtAry[i].getX(), pPtAry[i].getY()));
2423 aPoly.setClosed(true);
2425 invert(aPoly, nFlags);
2428 bool SvpSalGraphics::drawEPS( tools::Long, tools::Long, tools::Long, tools::Long, void*, sal_uInt32 )
2430 return false;
2433 namespace
2435 bool isCairoCompatible(const BitmapBuffer* pBuffer)
2437 if (!pBuffer)
2438 return false;
2440 // We use Cairo that supports 24-bit RGB.
2441 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
2442 if (pBuffer->mnBitCount != 32 && pBuffer->mnBitCount != 24 && pBuffer->mnBitCount != 1)
2443 #else
2444 if (pBuffer->mnBitCount != 32 && pBuffer->mnBitCount != 1)
2445 #endif
2446 return false;
2448 cairo_format_t nFormat = getCairoFormat(*pBuffer);
2449 return (cairo_format_stride_for_width(nFormat, pBuffer->mnWidth) == pBuffer->mnScanlineSize);
2453 cairo_surface_t* SvpSalGraphics::createCairoSurface(const BitmapBuffer *pBuffer)
2455 if (!isCairoCompatible(pBuffer))
2456 return nullptr;
2458 cairo_format_t nFormat = getCairoFormat(*pBuffer);
2459 cairo_surface_t *target =
2460 cairo_image_surface_create_for_data(pBuffer->mpBits,
2461 nFormat,
2462 pBuffer->mnWidth, pBuffer->mnHeight,
2463 pBuffer->mnScanlineSize);
2464 if (cairo_surface_status(target) != CAIRO_STATUS_SUCCESS)
2466 cairo_surface_destroy(target);
2467 return nullptr;
2469 return target;
2472 cairo_t* SvpSalGraphics::createTmpCompatibleCairoContext() const
2474 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0)
2475 cairo_surface_t *target = cairo_surface_create_similar_image(m_pSurface,
2476 #else
2477 cairo_surface_t *target = cairo_image_surface_create(
2478 #endif
2479 CAIRO_FORMAT_ARGB32,
2480 m_aFrameSize.getX() * m_fScale,
2481 m_aFrameSize.getY() * m_fScale);
2483 dl_cairo_surface_set_device_scale(target, m_fScale, m_fScale);
2485 return cairo_create(target);
2488 cairo_t* SvpSalGraphics::getCairoContext(bool bXorModeAllowed) const
2490 cairo_t* cr;
2491 if (m_ePaintMode == PaintMode::Xor && bXorModeAllowed)
2492 cr = createTmpCompatibleCairoContext();
2493 else
2494 cr = cairo_create(m_pSurface);
2495 cairo_set_line_width(cr, 1);
2496 cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
2497 cairo_set_antialias(cr, getAntiAlias() ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE);
2498 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2500 // ensure no linear transformation and no PathInfo in local cairo_path_t
2501 cairo_identity_matrix(cr);
2502 cairo_new_path(cr);
2504 return cr;
2507 cairo_user_data_key_t* SvpSalGraphics::getDamageKey()
2509 static cairo_user_data_key_t aDamageKey;
2510 return &aDamageKey;
2513 void SvpSalGraphics::releaseCairoContext(cairo_t* cr, bool bXorModeAllowed, const basegfx::B2DRange& rExtents) const
2515 const bool bXoring = (m_ePaintMode == PaintMode::Xor && bXorModeAllowed);
2517 if (rExtents.isEmpty())
2519 //nothing changed, return early
2520 if (bXoring)
2522 cairo_surface_t* surface = cairo_get_target(cr);
2523 cairo_surface_destroy(surface);
2525 cairo_destroy(cr);
2526 return;
2529 basegfx::B2IRange aIntExtents(basegfx::unotools::b2ISurroundingRangeFromB2DRange(rExtents));
2530 sal_Int32 nExtentsLeft(aIntExtents.getMinX()), nExtentsTop(aIntExtents.getMinY());
2531 sal_Int32 nExtentsRight(aIntExtents.getMaxX()), nExtentsBottom(aIntExtents.getMaxY());
2532 sal_Int32 nWidth = m_aFrameSize.getX();
2533 sal_Int32 nHeight = m_aFrameSize.getY();
2534 nExtentsLeft = std::max<sal_Int32>(nExtentsLeft, 0);
2535 nExtentsTop = std::max<sal_Int32>(nExtentsTop, 0);
2536 nExtentsRight = std::min<sal_Int32>(nExtentsRight, nWidth);
2537 nExtentsBottom = std::min<sal_Int32>(nExtentsBottom, nHeight);
2539 cairo_surface_t* surface = cairo_get_target(cr);
2540 cairo_surface_flush(surface);
2542 //For the most part we avoid the use of XOR these days, but there
2543 //are some edge cases where legacy stuff still supports it, so
2544 //emulate it (slowly) here.
2545 if (bXoring)
2547 cairo_surface_t* target_surface = m_pSurface;
2548 if (cairo_surface_get_type(target_surface) != CAIRO_SURFACE_TYPE_IMAGE)
2550 //in the unlikely case we can't use m_pSurface directly, copy contents
2551 //to another temp image surface
2552 cairo_t* copycr = createTmpCompatibleCairoContext();
2553 cairo_rectangle(copycr, nExtentsLeft, nExtentsTop,
2554 nExtentsRight - nExtentsLeft,
2555 nExtentsBottom - nExtentsTop);
2556 cairo_set_source_surface(copycr, m_pSurface, 0, 0);
2557 cairo_paint(copycr);
2558 target_surface = cairo_get_target(copycr);
2559 cairo_destroy(copycr);
2562 cairo_surface_flush(target_surface);
2563 unsigned char *target_surface_data = cairo_image_surface_get_data(target_surface);
2564 unsigned char *xor_surface_data = cairo_image_surface_get_data(surface);
2566 cairo_format_t nFormat = cairo_image_surface_get_format(target_surface);
2567 assert(nFormat == CAIRO_FORMAT_ARGB32 && "need to implement CAIRO_FORMAT_A1 after all here");
2568 sal_Int32 nStride = cairo_format_stride_for_width(nFormat, nWidth * m_fScale);
2569 sal_Int32 nUnscaledExtentsLeft = nExtentsLeft * m_fScale;
2570 sal_Int32 nUnscaledExtentsRight = nExtentsRight * m_fScale;
2571 sal_Int32 nUnscaledExtentsTop = nExtentsTop * m_fScale;
2572 sal_Int32 nUnscaledExtentsBottom = nExtentsBottom * m_fScale;
2573 vcl::bitmap::lookup_table unpremultiply_table = vcl::bitmap::get_unpremultiply_table();
2574 vcl::bitmap::lookup_table premultiply_table = vcl::bitmap::get_premultiply_table();
2575 for (sal_Int32 y = nUnscaledExtentsTop; y < nUnscaledExtentsBottom; ++y)
2577 unsigned char *true_row = target_surface_data + (nStride*y);
2578 unsigned char *xor_row = xor_surface_data + (nStride*y);
2579 unsigned char *true_data = true_row + (nUnscaledExtentsLeft * 4);
2580 unsigned char *xor_data = xor_row + (nUnscaledExtentsLeft * 4);
2581 for (sal_Int32 x = nUnscaledExtentsLeft; x < nUnscaledExtentsRight; ++x)
2583 sal_uInt8 a = true_data[SVP_CAIRO_ALPHA];
2584 sal_uInt8 xor_a = xor_data[SVP_CAIRO_ALPHA];
2585 sal_uInt8 b = unpremultiply_table[a][true_data[SVP_CAIRO_BLUE]] ^
2586 unpremultiply_table[xor_a][xor_data[SVP_CAIRO_BLUE]];
2587 sal_uInt8 g = unpremultiply_table[a][true_data[SVP_CAIRO_GREEN]] ^
2588 unpremultiply_table[xor_a][xor_data[SVP_CAIRO_GREEN]];
2589 sal_uInt8 r = unpremultiply_table[a][true_data[SVP_CAIRO_RED]] ^
2590 unpremultiply_table[xor_a][xor_data[SVP_CAIRO_RED]];
2591 true_data[SVP_CAIRO_BLUE] = premultiply_table[a][b];
2592 true_data[SVP_CAIRO_GREEN] = premultiply_table[a][g];
2593 true_data[SVP_CAIRO_RED] = premultiply_table[a][r];
2594 true_data+=4;
2595 xor_data+=4;
2598 cairo_surface_mark_dirty(target_surface);
2600 if (target_surface != m_pSurface)
2602 cairo_t* copycr = cairo_create(m_pSurface);
2603 //unlikely case we couldn't use m_pSurface directly, copy contents
2604 //back from image surface
2605 cairo_rectangle(copycr, nExtentsLeft, nExtentsTop,
2606 nExtentsRight - nExtentsLeft,
2607 nExtentsBottom - nExtentsTop);
2608 cairo_set_source_surface(copycr, target_surface, 0, 0);
2609 cairo_paint(copycr);
2610 cairo_destroy(copycr);
2611 cairo_surface_destroy(target_surface);
2614 cairo_surface_destroy(surface);
2617 cairo_destroy(cr); // unref
2619 DamageHandler* pDamage = static_cast<DamageHandler*>(cairo_surface_get_user_data(m_pSurface, getDamageKey()));
2621 if (pDamage)
2623 pDamage->damaged(pDamage->handle, nExtentsLeft, nExtentsTop,
2624 nExtentsRight - nExtentsLeft,
2625 nExtentsBottom - nExtentsTop);
2629 #if ENABLE_CAIRO_CANVAS
2630 bool SvpSalGraphics::SupportsCairo() const
2632 return false;
2635 cairo::SurfaceSharedPtr SvpSalGraphics::CreateSurface(const cairo::CairoSurfaceSharedPtr& /*rSurface*/) const
2637 return cairo::SurfaceSharedPtr();
2640 cairo::SurfaceSharedPtr SvpSalGraphics::CreateSurface(const OutputDevice& /*rRefDevice*/, int /*x*/, int /*y*/, int /*width*/, int /*height*/) const
2642 return cairo::SurfaceSharedPtr();
2645 cairo::SurfaceSharedPtr SvpSalGraphics::CreateBitmapSurface(const OutputDevice& /*rRefDevice*/, const BitmapSystemData& /*rData*/, const Size& /*rSize*/) const
2647 return cairo::SurfaceSharedPtr();
2650 css::uno::Any SvpSalGraphics::GetNativeSurfaceHandle(cairo::SurfaceSharedPtr& /*rSurface*/, const basegfx::B2ISize& /*rSize*/) const
2652 return css::uno::Any();
2655 #endif // ENABLE_CAIRO_CANVAS
2657 SystemGraphicsData SvpSalGraphics::GetGraphicsData() const
2659 return SystemGraphicsData();
2662 bool SvpSalGraphics::supportsOperation(OutDevSupportType eType) const
2664 switch (eType)
2666 case OutDevSupportType::TransparentRect:
2667 case OutDevSupportType::B2DDraw:
2668 return true;
2670 return false;
2673 void dl_cairo_surface_set_device_scale(cairo_surface_t *surface, double x_scale, double y_scale)
2675 #ifdef ANDROID
2676 cairo_surface_set_device_scale(surface, x_scale, y_scale);
2677 #else
2678 static auto func = reinterpret_cast<void(*)(cairo_surface_t*, double, double)>(
2679 dlsym(nullptr, "cairo_surface_set_device_scale"));
2680 if (func)
2681 func(surface, x_scale, y_scale);
2682 #endif
2685 void dl_cairo_surface_get_device_scale(cairo_surface_t *surface, double* x_scale, double* y_scale)
2687 #ifdef ANDROID
2688 cairo_surface_get_device_scale(surface, x_scale, y_scale);
2689 #else
2690 static auto func = reinterpret_cast<void(*)(cairo_surface_t*, double*, double*)>(
2691 dlsym(nullptr, "cairo_surface_get_device_scale"));
2692 if (func)
2693 func(surface, x_scale, y_scale);
2694 else
2696 if (x_scale)
2697 *x_scale = 1.0;
2698 if (y_scale)
2699 *y_scale = 1.0;
2701 #endif
2704 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */