Related: tdf#137748 "Update" should have use-underline
[LibreOffice.git] / vcl / headless / svpgdi.cxx
blob6041678c15c3994662b20a37e4fad3d4c6b5a695
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 <saldatabasic.hxx>
31 #include <sal/log.hxx>
32 #include <tools/helpers.hxx>
33 #include <o3tl/safeint.hxx>
34 #include <vcl/BitmapTools.hxx>
35 #include <vcl/sysdata.hxx>
36 #include <vcl/gradient.hxx>
37 #include <config_cairo_canvas.h>
38 #include <basegfx/numeric/ftools.hxx>
39 #include <basegfx/range/b2drange.hxx>
40 #include <basegfx/range/b2ibox.hxx>
41 #include <basegfx/range/b2irange.hxx>
42 #include <basegfx/polygon/b2dpolypolygon.hxx>
43 #include <basegfx/polygon/b2dpolypolygontools.hxx>
44 #include <basegfx/polygon/b2dpolygon.hxx>
45 #include <basegfx/polygon/b2dpolygontools.hxx>
46 #include <basegfx/matrix/b2dhommatrix.hxx>
47 #include <basegfx/utils/canvastools.hxx>
48 #include <basegfx/utils/systemdependentdata.hxx>
49 #include <basegfx/matrix/b2dhommatrixtools.hxx>
50 #include <comphelper/lok.hxx>
51 #include <unx/gendata.hxx>
52 #include <dlfcn.h>
54 #if ENABLE_CAIRO_CANVAS
55 # if defined CAIRO_VERSION && CAIRO_VERSION < CAIRO_VERSION_ENCODE(1, 10, 0)
56 # define CAIRO_OPERATOR_DIFFERENCE (static_cast<cairo_operator_t>(23))
57 # endif
58 #endif
60 namespace
62 basegfx::B2DRange getClipBox(cairo_t* cr)
64 double x1, y1, x2, y2;
66 cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
68 // support B2DRange::isEmpty()
69 if(0.0 != x1 || 0.0 != y1 || 0.0 != x2 || 0.0 != y2)
71 return basegfx::B2DRange(x1, y1, x2, y2);
74 return basegfx::B2DRange();
77 basegfx::B2DRange getFillDamage(cairo_t* cr)
79 double x1, y1, x2, y2;
81 // this is faster than cairo_fill_extents, at the cost of some overdraw
82 cairo_path_extents(cr, &x1, &y1, &x2, &y2);
84 // support B2DRange::isEmpty()
85 if(0.0 != x1 || 0.0 != y1 || 0.0 != x2 || 0.0 != y2)
87 return basegfx::B2DRange(x1, y1, x2, y2);
90 return basegfx::B2DRange();
93 basegfx::B2DRange getClippedFillDamage(cairo_t* cr)
95 basegfx::B2DRange aDamageRect(getFillDamage(cr));
96 aDamageRect.intersect(getClipBox(cr));
97 return aDamageRect;
100 basegfx::B2DRange getStrokeDamage(cairo_t* cr)
102 double x1, y1, x2, y2;
104 // less accurate, but much faster
105 cairo_path_extents(cr, &x1, &y1, &x2, &y2);
107 // support B2DRange::isEmpty()
108 if(0.0 != x1 || 0.0 != y1 || 0.0 != x2 || 0.0 != y2)
110 return basegfx::B2DRange(x1, y1, x2, y2);
113 return basegfx::B2DRange();
116 basegfx::B2DRange getClippedStrokeDamage(cairo_t* cr)
118 basegfx::B2DRange aDamageRect(getStrokeDamage(cr));
119 aDamageRect.intersect(getClipBox(cr));
120 return aDamageRect;
124 bool SvpSalGraphics::blendBitmap( const SalTwoRect&, const SalBitmap& /*rBitmap*/ )
126 return false;
129 bool SvpSalGraphics::blendAlphaBitmap( const SalTwoRect&, const SalBitmap&, const SalBitmap&, const SalBitmap& )
131 return false;
134 namespace
136 cairo_format_t getCairoFormat(const BitmapBuffer& rBuffer)
138 cairo_format_t nFormat;
139 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
140 assert(rBuffer.mnBitCount == 32 || rBuffer.mnBitCount == 24 || rBuffer.mnBitCount == 1);
141 #else
142 assert(rBuffer.mnBitCount == 32 || rBuffer.mnBitCount == 1);
143 #endif
145 if (rBuffer.mnBitCount == 32)
146 nFormat = CAIRO_FORMAT_ARGB32;
147 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
148 else if (rBuffer.mnBitCount == 24)
149 nFormat = CAIRO_FORMAT_RGB24_888;
150 #endif
151 else
152 nFormat = CAIRO_FORMAT_A1;
153 return nFormat;
156 void Toggle1BitTransparency(const BitmapBuffer& rBuf)
158 assert(rBuf.maPalette.GetBestIndex(BitmapColor(COL_BLACK)) == 0);
159 // TODO: make upper layers use standard alpha
160 if (getCairoFormat(rBuf) == CAIRO_FORMAT_A1)
162 const int nImageSize = rBuf.mnHeight * rBuf.mnScanlineSize;
163 unsigned char* pDst = rBuf.mpBits;
164 for (int i = nImageSize; --i >= 0; ++pDst)
165 *pDst = ~*pDst;
169 std::unique_ptr<BitmapBuffer> FastConvert24BitRgbTo32BitCairo(const BitmapBuffer* pSrc)
171 if (pSrc == nullptr)
172 return nullptr;
174 assert(pSrc->mnFormat == SVP_24BIT_FORMAT);
175 const tools::Long nWidth = pSrc->mnWidth;
176 const tools::Long nHeight = pSrc->mnHeight;
177 std::unique_ptr<BitmapBuffer> pDst(new BitmapBuffer);
178 pDst->mnFormat = (ScanlineFormat::N32BitTcArgb | ScanlineFormat::TopDown);
179 pDst->mnWidth = nWidth;
180 pDst->mnHeight = nHeight;
181 pDst->mnBitCount = 32;
182 pDst->maColorMask = pSrc->maColorMask;
183 pDst->maPalette = pSrc->maPalette;
185 tools::Long nScanlineBase;
186 const bool bFail = o3tl::checked_multiply<tools::Long>(pDst->mnBitCount, nWidth, nScanlineBase);
187 if (bFail)
189 SAL_WARN("vcl.gdi", "checked multiply failed");
190 pDst->mpBits = nullptr;
191 return nullptr;
194 pDst->mnScanlineSize = AlignedWidth4Bytes(nScanlineBase);
195 if (pDst->mnScanlineSize < nScanlineBase/8)
197 SAL_WARN("vcl.gdi", "scanline calculation wraparound");
198 pDst->mpBits = nullptr;
199 return nullptr;
204 pDst->mpBits = new sal_uInt8[ pDst->mnScanlineSize * nHeight ];
206 catch (const std::bad_alloc&)
208 // memory exception, clean up
209 pDst->mpBits = nullptr;
210 return nullptr;
213 for (tools::Long y = 0; y < nHeight; ++y)
215 sal_uInt8* pS = pSrc->mpBits + y * pSrc->mnScanlineSize;
216 sal_uInt8* pD = pDst->mpBits + y * pDst->mnScanlineSize;
217 for (tools::Long x = 0; x < nWidth; ++x)
219 #if defined(ANDROID) && !HAVE_FEATURE_ANDROID_LOK
220 static_assert((SVP_CAIRO_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N32BitTcRgba, "Expected SVP_CAIRO_FORMAT set to N32BitTcBgra");
221 static_assert((SVP_24BIT_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N24BitTcRgb, "Expected SVP_24BIT_FORMAT set to N24BitTcRgb");
222 pD[0] = pS[0];
223 pD[1] = pS[1];
224 pD[2] = pS[2];
225 pD[3] = 0xff; // Alpha
226 #elif defined OSL_BIGENDIAN
227 static_assert((SVP_CAIRO_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N32BitTcArgb, "Expected SVP_CAIRO_FORMAT set to N32BitTcBgra");
228 static_assert((SVP_24BIT_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N24BitTcRgb, "Expected SVP_24BIT_FORMAT set to N24BitTcRgb");
229 pD[0] = 0xff; // Alpha
230 pD[1] = pS[0];
231 pD[2] = pS[1];
232 pD[3] = pS[2];
233 #else
234 static_assert((SVP_CAIRO_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N32BitTcBgra, "Expected SVP_CAIRO_FORMAT set to N32BitTcBgra");
235 static_assert((SVP_24BIT_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N24BitTcBgr, "Expected SVP_24BIT_FORMAT set to N24BitTcBgr");
236 pD[0] = pS[0];
237 pD[1] = pS[1];
238 pD[2] = pS[2];
239 pD[3] = 0xff; // Alpha
240 #endif
242 pS += 3;
243 pD += 4;
247 return pDst;
250 // check for env var that decides for using downscale pattern
251 const char* pDisableDownScale(getenv("SAL_DISABLE_CAIRO_DOWNSCALE"));
252 bool bDisableDownScale(nullptr != pDisableDownScale);
254 class SurfaceHelper
256 private:
257 cairo_surface_t* pSurface;
258 std::unordered_map<sal_uInt64, cairo_surface_t*> maDownscaled;
260 SurfaceHelper(const SurfaceHelper&) = delete;
261 SurfaceHelper& operator=(const SurfaceHelper&) = delete;
263 cairo_surface_t* implCreateOrReuseDownscale(
264 unsigned long nTargetWidth,
265 unsigned long nTargetHeight)
267 const unsigned long nSourceWidth(cairo_image_surface_get_width(pSurface));
268 const unsigned long nSourceHeight(cairo_image_surface_get_height(pSurface));
270 // zoomed in, need to stretch at paint, no pre-scale useful
271 if(nTargetWidth >= nSourceWidth || nTargetHeight >= nSourceHeight)
273 return pSurface;
276 // calculate downscale factor
277 unsigned long nWFactor(1);
278 unsigned long nW((nSourceWidth + 1) / 2);
279 unsigned long nHFactor(1);
280 unsigned long nH((nSourceHeight + 1) / 2);
282 while(nW > nTargetWidth && nW > 1)
284 nW = (nW + 1) / 2;
285 nWFactor *= 2;
288 while(nH > nTargetHeight && nH > 1)
290 nH = (nH + 1) / 2;
291 nHFactor *= 2;
294 if(1 == nWFactor && 1 == nHFactor)
296 // original size *is* best binary size, use it
297 return pSurface;
300 // go up one scale again - look for no change
301 nW = (1 == nWFactor) ? nTargetWidth : nW * 2;
302 nH = (1 == nHFactor) ? nTargetHeight : nH * 2;
304 // check if we have a downscaled version of required size
305 // bail out if the multiplication for the key would overflow
306 if( nW >= SAL_MAX_UINT32 || nH >= SAL_MAX_UINT32 )
307 return pSurface;
308 const sal_uInt64 key((nW * static_cast<sal_uInt64>(SAL_MAX_UINT32)) + 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)
365 ~SurfaceHelper()
367 cairo_surface_destroy(pSurface);
368 for(auto& candidate : maDownscaled)
370 cairo_surface_destroy(candidate.second);
373 cairo_surface_t* getSurface(
374 unsigned long nTargetWidth = 0,
375 unsigned long nTargetHeight = 0) const
377 if (bDisableDownScale || 0 == nTargetWidth || 0 == nTargetHeight || !pSurface || isTrivial())
379 // caller asks for original or disabled or trivial (smaller then a minimal square size)
380 // also excludes zero cases for width/height after this point if need to prescale
381 return pSurface;
384 return const_cast<SurfaceHelper*>(this)->implCreateOrReuseDownscale(
385 nTargetWidth,
386 nTargetHeight);
390 class BitmapHelper : public SurfaceHelper
392 private:
393 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
394 const bool m_bForceARGB32;
395 #endif
396 SvpSalBitmap aTmpBmp;
398 public:
399 explicit BitmapHelper(
400 const SalBitmap& rSourceBitmap,
401 const bool bForceARGB32 = false)
402 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
403 : m_bForceARGB32(bForceARGB32)
404 #endif
406 const SvpSalBitmap& rSrcBmp = static_cast<const SvpSalBitmap&>(rSourceBitmap);
407 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
408 if ((rSrcBmp.GetBitCount() != 32 && rSrcBmp.GetBitCount() != 24) || bForceARGB32)
409 #else
410 (void)bForceARGB32;
411 if (rSrcBmp.GetBitCount() != 32)
412 #endif
414 //big stupid copy here
415 const BitmapBuffer* pSrc = rSrcBmp.GetBuffer();
416 const SalTwoRect aTwoRect = { 0, 0, pSrc->mnWidth, pSrc->mnHeight,
417 0, 0, pSrc->mnWidth, pSrc->mnHeight };
418 std::unique_ptr<BitmapBuffer> pTmp = (pSrc->mnFormat == SVP_24BIT_FORMAT
419 ? FastConvert24BitRgbTo32BitCairo(pSrc)
420 : StretchAndConvert(*pSrc, aTwoRect, SVP_CAIRO_FORMAT));
421 aTmpBmp.Create(std::move(pTmp));
423 assert(aTmpBmp.GetBitCount() == 32);
424 implSetSurface(SvpSalGraphics::createCairoSurface(aTmpBmp.GetBuffer()));
426 else
428 implSetSurface(SvpSalGraphics::createCairoSurface(rSrcBmp.GetBuffer()));
431 void mark_dirty()
433 cairo_surface_mark_dirty(implGetSurface());
435 unsigned char* getBits(sal_Int32 &rStride)
437 cairo_surface_flush(implGetSurface());
439 unsigned char *mask_data = cairo_image_surface_get_data(implGetSurface());
441 const cairo_format_t nFormat = cairo_image_surface_get_format(implGetSurface());
442 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
443 if (!m_bForceARGB32)
444 assert(nFormat == CAIRO_FORMAT_RGB24_888 && "Expected RGB24_888 image");
445 else
446 #endif
448 assert(nFormat == CAIRO_FORMAT_ARGB32 && "need to implement CAIRO_FORMAT_A1 after all here");
451 rStride = cairo_format_stride_for_width(nFormat, cairo_image_surface_get_width(implGetSurface()));
453 return mask_data;
457 sal_Int64 estimateUsageInBytesForSurfaceHelper(const SurfaceHelper* pHelper)
459 sal_Int64 nRetval(0);
461 if(nullptr != pHelper)
463 cairo_surface_t* pSurface(pHelper->getSurface());
465 if(pSurface)
467 const tools::Long nStride(cairo_image_surface_get_stride(pSurface));
468 const tools::Long nHeight(cairo_image_surface_get_height(pSurface));
470 nRetval = nStride * nHeight;
472 // if we do downscale, size will grow by 1/4 + 1/16 + 1/32 + ...,
473 // rough estimation just multiplies by 1.25, should be good enough
474 // for estimation of buffer survival time
475 if(!bDisableDownScale)
477 nRetval = (nRetval * 5) / 4;
482 return nRetval;
485 class SystemDependentData_BitmapHelper : public basegfx::SystemDependentData
487 private:
488 std::shared_ptr<BitmapHelper> maBitmapHelper;
490 public:
491 SystemDependentData_BitmapHelper(
492 basegfx::SystemDependentDataManager& rSystemDependentDataManager,
493 const std::shared_ptr<BitmapHelper>& rBitmapHelper)
494 : basegfx::SystemDependentData(rSystemDependentDataManager),
495 maBitmapHelper(rBitmapHelper)
499 const std::shared_ptr<BitmapHelper>& getBitmapHelper() const { return maBitmapHelper; };
500 virtual sal_Int64 estimateUsageInBytes() const override;
503 sal_Int64 SystemDependentData_BitmapHelper::estimateUsageInBytes() const
505 return estimateUsageInBytesForSurfaceHelper(maBitmapHelper.get());
508 class MaskHelper : public SurfaceHelper
510 private:
511 std::unique_ptr<unsigned char[]> pAlphaBits;
513 public:
514 explicit MaskHelper(const SalBitmap& rAlphaBitmap)
516 const SvpSalBitmap& rMask = static_cast<const SvpSalBitmap&>(rAlphaBitmap);
517 const BitmapBuffer* pMaskBuf = rMask.GetBuffer();
519 if (rAlphaBitmap.GetBitCount() == 8)
521 // the alpha values need to be inverted for Cairo
522 // so big stupid copy and invert here
523 const int nImageSize = pMaskBuf->mnHeight * pMaskBuf->mnScanlineSize;
524 pAlphaBits.reset( new unsigned char[nImageSize] );
525 memcpy(pAlphaBits.get(), pMaskBuf->mpBits, nImageSize);
527 // TODO: make upper layers use standard alpha
528 sal_uInt32* pLDst = reinterpret_cast<sal_uInt32*>(pAlphaBits.get());
529 for( int i = nImageSize/sizeof(sal_uInt32); --i >= 0; ++pLDst )
530 *pLDst = ~*pLDst;
531 assert(reinterpret_cast<unsigned char*>(pLDst) == pAlphaBits.get()+nImageSize);
533 implSetSurface(
534 cairo_image_surface_create_for_data(
535 pAlphaBits.get(),
536 CAIRO_FORMAT_A8,
537 pMaskBuf->mnWidth,
538 pMaskBuf->mnHeight,
539 pMaskBuf->mnScanlineSize));
541 else
543 // the alpha values need to be inverted for Cairo
544 // so big stupid copy and invert here
545 const int nImageSize = pMaskBuf->mnHeight * pMaskBuf->mnScanlineSize;
546 pAlphaBits.reset( new unsigned char[nImageSize] );
547 memcpy(pAlphaBits.get(), pMaskBuf->mpBits, nImageSize);
549 const sal_Int32 nBlackIndex = pMaskBuf->maPalette.GetBestIndex(BitmapColor(COL_BLACK));
550 if (nBlackIndex == 0)
552 // TODO: make upper layers use standard alpha
553 unsigned char* pDst = pAlphaBits.get();
554 for (int i = nImageSize; --i >= 0; ++pDst)
555 *pDst = ~*pDst;
558 implSetSurface(
559 cairo_image_surface_create_for_data(
560 pAlphaBits.get(),
561 CAIRO_FORMAT_A1,
562 pMaskBuf->mnWidth,
563 pMaskBuf->mnHeight,
564 pMaskBuf->mnScanlineSize));
569 class SystemDependentData_MaskHelper : public basegfx::SystemDependentData
571 private:
572 std::shared_ptr<MaskHelper> maMaskHelper;
574 public:
575 SystemDependentData_MaskHelper(
576 basegfx::SystemDependentDataManager& rSystemDependentDataManager,
577 const std::shared_ptr<MaskHelper>& rMaskHelper)
578 : basegfx::SystemDependentData(rSystemDependentDataManager),
579 maMaskHelper(rMaskHelper)
583 const std::shared_ptr<MaskHelper>& getMaskHelper() const { return maMaskHelper; };
584 virtual sal_Int64 estimateUsageInBytes() const override;
587 sal_Int64 SystemDependentData_MaskHelper::estimateUsageInBytes() const
589 return estimateUsageInBytesForSurfaceHelper(maMaskHelper.get());
592 // MM02 decide to use buffers or not
593 const char* pDisableMM02Goodies(getenv("SAL_DISABLE_MM02_GOODIES"));
594 bool bUseBuffer(nullptr == pDisableMM02Goodies);
595 const tools::Long nMinimalSquareSizeToBuffer(64*64);
597 void tryToUseSourceBuffer(
598 const SalBitmap& rSourceBitmap,
599 std::shared_ptr<BitmapHelper>& rSurface)
601 // MM02 try to access buffered BitmapHelper
602 std::shared_ptr<SystemDependentData_BitmapHelper> pSystemDependentData_BitmapHelper;
603 const bool bBufferSource(bUseBuffer
604 && rSourceBitmap.GetSize().Width() * rSourceBitmap.GetSize().Height() > nMinimalSquareSizeToBuffer);
606 if(bBufferSource)
608 const SvpSalBitmap& rSrcBmp(static_cast<const SvpSalBitmap&>(rSourceBitmap));
609 pSystemDependentData_BitmapHelper = rSrcBmp.getSystemDependentData<SystemDependentData_BitmapHelper>();
611 if(pSystemDependentData_BitmapHelper)
613 // reuse buffered data
614 rSurface = pSystemDependentData_BitmapHelper->getBitmapHelper();
618 if(rSurface)
619 return;
621 // create data on-demand
622 rSurface = std::make_shared<BitmapHelper>(rSourceBitmap);
624 if(bBufferSource)
626 // add to buffering mechanism to potentially reuse next time
627 const SvpSalBitmap& rSrcBmp(static_cast<const SvpSalBitmap&>(rSourceBitmap));
628 rSrcBmp.addOrReplaceSystemDependentData<SystemDependentData_BitmapHelper>(
629 ImplGetSystemDependentDataManager(),
630 rSurface);
634 void tryToUseMaskBuffer(
635 const SalBitmap& rMaskBitmap,
636 std::shared_ptr<MaskHelper>& rMask)
638 // MM02 try to access buffered MaskHelper
639 std::shared_ptr<SystemDependentData_MaskHelper> pSystemDependentData_MaskHelper;
640 const bool bBufferMask(bUseBuffer
641 && rMaskBitmap.GetSize().Width() * rMaskBitmap.GetSize().Height() > nMinimalSquareSizeToBuffer);
643 if(bBufferMask)
645 const SvpSalBitmap& rSrcBmp(static_cast<const SvpSalBitmap&>(rMaskBitmap));
646 pSystemDependentData_MaskHelper = rSrcBmp.getSystemDependentData<SystemDependentData_MaskHelper>();
648 if(pSystemDependentData_MaskHelper)
650 // reuse buffered data
651 rMask = pSystemDependentData_MaskHelper->getMaskHelper();
655 if(rMask)
656 return;
658 // create data on-demand
659 rMask = std::make_shared<MaskHelper>(rMaskBitmap);
661 if(bBufferMask)
663 // add to buffering mechanism to potentially reuse next time
664 const SvpSalBitmap& rSrcBmp(static_cast<const SvpSalBitmap&>(rMaskBitmap));
665 rSrcBmp.addOrReplaceSystemDependentData<SystemDependentData_MaskHelper>(
666 ImplGetSystemDependentDataManager(),
667 rMask);
672 bool SvpSalGraphics::drawAlphaBitmap( const SalTwoRect& rTR, const SalBitmap& rSourceBitmap, const SalBitmap& rAlphaBitmap )
674 if (rAlphaBitmap.GetBitCount() != 8 && rAlphaBitmap.GetBitCount() != 1)
676 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawAlphaBitmap alpha depth case: " << rAlphaBitmap.GetBitCount());
677 return false;
680 // MM02 try to access buffered BitmapHelper
681 std::shared_ptr<BitmapHelper> aSurface;
682 tryToUseSourceBuffer(rSourceBitmap, aSurface);
683 cairo_surface_t* source = aSurface->getSurface(
684 rTR.mnDestWidth,
685 rTR.mnDestHeight);
687 if (!source)
689 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawAlphaBitmap case");
690 return false;
693 // MM02 try to access buffered MaskHelper
694 std::shared_ptr<MaskHelper> aMask;
695 tryToUseMaskBuffer(rAlphaBitmap, aMask);
696 cairo_surface_t *mask = aMask->getSurface(
697 rTR.mnDestWidth,
698 rTR.mnDestHeight);
700 if (!mask)
702 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawAlphaBitmap case");
703 return false;
706 cairo_t* cr = getCairoContext(false);
707 clipRegion(cr);
709 cairo_rectangle(cr, rTR.mnDestX, rTR.mnDestY, rTR.mnDestWidth, rTR.mnDestHeight);
711 basegfx::B2DRange extents = getClippedFillDamage(cr);
713 cairo_clip(cr);
715 cairo_pattern_t* maskpattern = cairo_pattern_create_for_surface(mask);
716 cairo_translate(cr, rTR.mnDestX, rTR.mnDestY);
717 double fXScale = static_cast<double>(rTR.mnDestWidth)/rTR.mnSrcWidth;
718 double fYScale = static_cast<double>(rTR.mnDestHeight)/rTR.mnSrcHeight;
719 cairo_scale(cr, fXScale, fYScale);
720 cairo_set_source_surface(cr, source, -rTR.mnSrcX, -rTR.mnSrcY);
722 //tdf#114117 when stretching a single pixel width/height source to fit an area
723 //set extend and filter to stretch it with simplest expected interpolation
724 if ((fXScale != 1.0 && rTR.mnSrcWidth == 1) || (fYScale != 1.0 && rTR.mnSrcHeight == 1))
726 cairo_pattern_t* sourcepattern = cairo_get_source(cr);
727 cairo_pattern_set_extend(sourcepattern, CAIRO_EXTEND_REPEAT);
728 cairo_pattern_set_filter(sourcepattern, CAIRO_FILTER_NEAREST);
729 cairo_pattern_set_extend(maskpattern, CAIRO_EXTEND_REPEAT);
730 cairo_pattern_set_filter(maskpattern, CAIRO_FILTER_NEAREST);
733 //this block is just "cairo_mask_surface", but we have to make it explicit
734 //because of the cairo_pattern_set_filter etc we may want applied
735 cairo_matrix_t matrix;
736 cairo_matrix_init_translate(&matrix, rTR.mnSrcX, rTR.mnSrcY);
737 cairo_pattern_set_matrix(maskpattern, &matrix);
738 cairo_mask(cr, maskpattern);
740 cairo_pattern_destroy(maskpattern);
742 releaseCairoContext(cr, false, extents);
744 return true;
747 bool SvpSalGraphics::drawTransformedBitmap(
748 const basegfx::B2DPoint& rNull,
749 const basegfx::B2DPoint& rX,
750 const basegfx::B2DPoint& rY,
751 const SalBitmap& rSourceBitmap,
752 const SalBitmap* pAlphaBitmap,
753 double fAlpha)
755 if (pAlphaBitmap && pAlphaBitmap->GetBitCount() != 8 && pAlphaBitmap->GetBitCount() != 1)
757 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawTransformedBitmap alpha depth case: " << pAlphaBitmap->GetBitCount());
758 return false;
761 if( fAlpha != 1.0 )
762 return false;
764 // MM02 try to access buffered BitmapHelper
765 std::shared_ptr<BitmapHelper> aSurface;
766 tryToUseSourceBuffer(rSourceBitmap, aSurface);
767 const tools::Long nDestWidth(basegfx::fround(basegfx::B2DVector(rX - rNull).getLength()));
768 const tools::Long nDestHeight(basegfx::fround(basegfx::B2DVector(rY - rNull).getLength()));
769 cairo_surface_t* source(
770 aSurface->getSurface(
771 nDestWidth,
772 nDestHeight));
774 if(!source)
776 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawTransformedBitmap case");
777 return false;
780 // MM02 try to access buffered MaskHelper
781 std::shared_ptr<MaskHelper> aMask;
782 if(nullptr != pAlphaBitmap)
784 tryToUseMaskBuffer(*pAlphaBitmap, aMask);
787 // access cairo_surface_t from MaskHelper
788 cairo_surface_t* mask(nullptr);
789 if(aMask)
791 mask = aMask->getSurface(
792 nDestWidth,
793 nDestHeight);
796 if(nullptr != pAlphaBitmap && nullptr == mask)
798 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawTransformedBitmap case");
799 return false;
802 const Size aSize = rSourceBitmap.GetSize();
803 cairo_t* cr = getCairoContext(false);
804 clipRegion(cr);
806 // setup the image transformation
807 // using the rNull,rX,rY points as destinations for the (0,0),(0,Width),(Height,0) source points
808 const basegfx::B2DVector aXRel = rX - rNull;
809 const basegfx::B2DVector aYRel = rY - rNull;
810 cairo_matrix_t matrix;
811 cairo_matrix_init(&matrix,
812 aXRel.getX()/aSize.Width(), aXRel.getY()/aSize.Width(),
813 aYRel.getX()/aSize.Height(), aYRel.getY()/aSize.Height(),
814 rNull.getX(), rNull.getY());
816 cairo_transform(cr, &matrix);
818 cairo_rectangle(cr, 0, 0, aSize.Width(), aSize.Height());
819 basegfx::B2DRange extents = getClippedFillDamage(cr);
820 cairo_clip(cr);
822 cairo_set_source_surface(cr, source, 0, 0);
823 if (mask)
824 cairo_mask_surface(cr, mask, 0, 0);
825 else
826 cairo_paint(cr);
828 releaseCairoContext(cr, false, extents);
830 return true;
833 bool SvpSalGraphics::hasFastDrawTransformedBitmap() const
835 return false;
838 void SvpSalGraphics::clipRegion(cairo_t* cr, const vcl::Region& rClipRegion)
840 RectangleVector aRectangles;
841 if (!rClipRegion.IsEmpty())
843 rClipRegion.GetRegionRectangles(aRectangles);
845 if (!aRectangles.empty())
847 for (auto const& rectangle : aRectangles)
849 cairo_rectangle(cr, rectangle.Left(), rectangle.Top(), rectangle.GetWidth(), rectangle.GetHeight());
851 cairo_clip(cr);
855 void SvpSalGraphics::clipRegion(cairo_t* cr)
857 SvpSalGraphics::clipRegion(cr, m_aClipRegion);
860 bool SvpSalGraphics::drawAlphaRect(tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight, sal_uInt8 nTransparency)
862 const bool bHasFill(m_aFillColor != SALCOLOR_NONE);
863 const bool bHasLine(m_aLineColor != SALCOLOR_NONE);
865 if(!(bHasFill || bHasLine))
867 return true;
870 cairo_t* cr = getCairoContext(false);
871 clipRegion(cr);
873 const double fTransparency = nTransparency * (1.0/100);
875 // To make releaseCairoContext work, use empty extents
876 basegfx::B2DRange extents;
878 if (bHasFill)
880 cairo_rectangle(cr, nX, nY, nWidth, nHeight);
882 applyColor(cr, m_aFillColor, fTransparency);
884 // set FillDamage
885 extents = getClippedFillDamage(cr);
887 cairo_fill(cr);
890 if (bHasLine)
892 // PixelOffset used: Set PixelOffset as linear transformation
893 // Note: Was missing here - probably not by purpose (?)
894 cairo_matrix_t aMatrix;
895 cairo_matrix_init_translate(&aMatrix, 0.5, 0.5);
896 cairo_set_matrix(cr, &aMatrix);
898 cairo_rectangle(cr, nX, nY, nWidth, nHeight);
900 applyColor(cr, m_aLineColor, fTransparency);
902 // expand with possible StrokeDamage
903 basegfx::B2DRange stroke_extents = getClippedStrokeDamage(cr);
904 stroke_extents.transform(basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5));
905 extents.expand(stroke_extents);
907 cairo_stroke(cr);
910 releaseCairoContext(cr, false, extents);
912 return true;
915 SvpSalGraphics::SvpSalGraphics()
916 : m_pSurface(nullptr)
917 , m_fScale(1.0)
918 , m_aLineColor(Color(0x00, 0x00, 0x00))
919 , m_aFillColor(Color(0xFF, 0xFF, 0XFF))
920 , m_ePaintMode(PaintMode::Over)
921 , m_aTextRenderImpl(*this)
923 bool bLOKActive = comphelper::LibreOfficeKit::isActive();
924 initWidgetDrawBackends(bLOKActive);
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)
1415 // tdf#129845 only create a copy of the path when nSizeMeasure is
1416 // bigger than some decent threshold
1417 if(nSizeMeasure > 50)
1419 mpCairoPath = cairo_copy_path(cr);
1421 if(nullptr != pStroke)
1423 maStroke = *pStroke;
1428 SystemDependentData_CairoPath::~SystemDependentData_CairoPath()
1430 if(nullptr != mpCairoPath)
1432 cairo_path_destroy(mpCairoPath);
1433 mpCairoPath = nullptr;
1437 sal_Int64 SystemDependentData_CairoPath::estimateUsageInBytes() const
1439 // tdf#129845 by using the default return value of zero when no path
1440 // was created, SystemDependentData::calculateCombinedHoldCyclesInSeconds
1441 // will do the right thing and not buffer this entry at all
1442 sal_Int64 nRetval(0);
1444 if(nullptr != mpCairoPath)
1446 // per node
1447 // - num_data incarnations of
1448 // - sizeof(cairo_path_data_t) which is a union of defines and point data
1449 // thus may 2 x sizeof(double)
1450 nRetval = mpCairoPath->num_data * sizeof(cairo_path_data_t);
1453 return nRetval;
1456 bool SvpSalGraphics::drawPolyLine(
1457 const basegfx::B2DHomMatrix& rObjectToDevice,
1458 const basegfx::B2DPolygon& rPolyLine,
1459 double fTransparency,
1460 double fLineWidth,
1461 const std::vector< double >* pStroke, // MM01
1462 basegfx::B2DLineJoin eLineJoin,
1463 css::drawing::LineCap eLineCap,
1464 double fMiterMinimumAngle,
1465 bool bPixelSnapHairline)
1467 // short circuit if there is nothing to do
1468 if(0 == rPolyLine.count() || fTransparency < 0.0 || fTransparency >= 1.0)
1470 return true;
1473 // Wrap call to static version of ::drawPolyLine by
1474 // preparing/getting some local data and parameters
1475 // due to usage in vcl/unx/generic/gdi/salgdi.cxx.
1476 // This is mainly about extended handling of extents
1477 // and the way destruction of CairoContext is handled
1478 // due to current XOR stuff
1479 cairo_t* cr = getCairoContext(false);
1480 basegfx::B2DRange aExtents;
1481 clipRegion(cr);
1483 bool bRetval(
1484 drawPolyLine(
1486 &aExtents,
1487 m_aLineColor,
1488 getAntiAlias(),
1489 rObjectToDevice,
1490 rPolyLine,
1491 fTransparency,
1492 fLineWidth,
1493 pStroke, // MM01
1494 eLineJoin,
1495 eLineCap,
1496 fMiterMinimumAngle,
1497 bPixelSnapHairline));
1499 releaseCairoContext(cr, false, aExtents);
1501 return bRetval;
1504 bool SvpSalGraphics::drawPolyLine(
1505 cairo_t* cr,
1506 basegfx::B2DRange* pExtents,
1507 const Color& rLineColor,
1508 bool bAntiAlias,
1509 const basegfx::B2DHomMatrix& rObjectToDevice,
1510 const basegfx::B2DPolygon& rPolyLine,
1511 double fTransparency,
1512 double fLineWidth,
1513 const std::vector< double >* pStroke, // MM01
1514 basegfx::B2DLineJoin eLineJoin,
1515 css::drawing::LineCap eLineCap,
1516 double fMiterMinimumAngle,
1517 bool bPixelSnapHairline)
1519 // short circuit if there is nothing to do
1520 if(0 == rPolyLine.count() || fTransparency < 0.0 || fTransparency >= 1.0)
1522 return true;
1525 // need to check/handle LineWidth when ObjectToDevice transformation is used
1526 const bool bObjectToDeviceIsIdentity(rObjectToDevice.isIdentity());
1528 // tdf#124848 calculate-back logical LineWidth for a hairline
1529 // since this implementation hands over the transformation to
1530 // the graphic sub-system
1531 if(fLineWidth == 0)
1533 fLineWidth = 1.0;
1535 if(!bObjectToDeviceIsIdentity)
1537 basegfx::B2DHomMatrix aObjectToDeviceInv(rObjectToDevice);
1538 aObjectToDeviceInv.invert();
1539 fLineWidth = (aObjectToDeviceInv * basegfx::B2DVector(fLineWidth, 0)).getLength();
1543 // PixelOffset used: Need to reflect in linear transformation
1544 cairo_matrix_t aMatrix;
1545 basegfx::B2DHomMatrix aDamageMatrix(basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5));
1547 if (bObjectToDeviceIsIdentity)
1549 // Set PixelOffset as requested
1550 cairo_matrix_init_translate(&aMatrix, 0.5, 0.5);
1552 else
1554 // Prepare ObjectToDevice transformation. Take PixelOffset for Lines into
1555 // account: Multiply from left to act in DeviceCoordinates
1556 aDamageMatrix = aDamageMatrix * rObjectToDevice;
1557 cairo_matrix_init(
1558 &aMatrix,
1559 aDamageMatrix.get( 0, 0 ),
1560 aDamageMatrix.get( 1, 0 ),
1561 aDamageMatrix.get( 0, 1 ),
1562 aDamageMatrix.get( 1, 1 ),
1563 aDamageMatrix.get( 0, 2 ),
1564 aDamageMatrix.get( 1, 2 ));
1567 // set linear transformation
1568 cairo_set_matrix(cr, &aMatrix);
1570 // setup line attributes
1571 cairo_line_join_t eCairoLineJoin = CAIRO_LINE_JOIN_MITER;
1572 switch (eLineJoin)
1574 case basegfx::B2DLineJoin::Bevel:
1575 eCairoLineJoin = CAIRO_LINE_JOIN_BEVEL;
1576 break;
1577 case basegfx::B2DLineJoin::Round:
1578 eCairoLineJoin = CAIRO_LINE_JOIN_ROUND;
1579 break;
1580 case basegfx::B2DLineJoin::NONE:
1581 case basegfx::B2DLineJoin::Miter:
1582 eCairoLineJoin = CAIRO_LINE_JOIN_MITER;
1583 break;
1586 // convert miter minimum angle to miter limit
1587 double fMiterLimit = 1.0 / sin(std::max(fMiterMinimumAngle, 0.01 * M_PI) / 2.0);
1589 // setup cap attribute
1590 cairo_line_cap_t eCairoLineCap(CAIRO_LINE_CAP_BUTT);
1592 switch (eLineCap)
1594 default: // css::drawing::LineCap_BUTT:
1596 eCairoLineCap = CAIRO_LINE_CAP_BUTT;
1597 break;
1599 case css::drawing::LineCap_ROUND:
1601 eCairoLineCap = CAIRO_LINE_CAP_ROUND;
1602 break;
1604 case css::drawing::LineCap_SQUARE:
1606 eCairoLineCap = CAIRO_LINE_CAP_SQUARE;
1607 break;
1611 cairo_set_source_rgba(
1613 rLineColor.GetRed()/255.0,
1614 rLineColor.GetGreen()/255.0,
1615 rLineColor.GetBlue()/255.0,
1616 1.0-fTransparency);
1618 cairo_set_line_join(cr, eCairoLineJoin);
1619 cairo_set_line_cap(cr, eCairoLineCap);
1620 cairo_set_line_width(cr, fLineWidth);
1621 cairo_set_miter_limit(cr, fMiterLimit);
1623 // try to access buffered data
1624 std::shared_ptr<SystemDependentData_CairoPath> pSystemDependentData_CairoPath(
1625 rPolyLine.getSystemDependentData<SystemDependentData_CairoPath>());
1627 // MM01 need to do line dashing as fallback stuff here now
1628 const double fDotDashLength(nullptr != pStroke ? std::accumulate(pStroke->begin(), pStroke->end(), 0.0) : 0.0);
1629 const bool bStrokeUsed(0.0 != fDotDashLength);
1630 assert(!bStrokeUsed || (bStrokeUsed && pStroke));
1632 // MM01 decide if to stroke directly
1633 static const bool bDoDirectCairoStroke(true);
1635 // MM01 activate to stroke directly
1636 if(bDoDirectCairoStroke && bStrokeUsed)
1638 cairo_set_dash(cr, pStroke->data(), pStroke->size(), 0.0);
1641 if(!bDoDirectCairoStroke && pSystemDependentData_CairoPath)
1643 // MM01 - check on stroke change. Used against not used, or if both used,
1644 // equal or different?
1645 const bool bStrokeWasUsed(!pSystemDependentData_CairoPath->getStroke().empty());
1647 if(bStrokeWasUsed != bStrokeUsed
1648 || (bStrokeUsed && *pStroke != pSystemDependentData_CairoPath->getStroke()))
1650 // data invalid, forget
1651 pSystemDependentData_CairoPath.reset();
1655 // check for basegfx::B2DLineJoin::NONE to react accordingly
1656 const bool bNoJoin((basegfx::B2DLineJoin::NONE == eLineJoin
1657 && basegfx::fTools::more(fLineWidth, 0.0)));
1659 if(pSystemDependentData_CairoPath)
1661 // check data validity
1662 if(nullptr == pSystemDependentData_CairoPath->getCairoPath()
1663 || pSystemDependentData_CairoPath->getNoJoin() != bNoJoin
1664 || pSystemDependentData_CairoPath->getAntiAlias() != bAntiAlias
1665 || bPixelSnapHairline /*tdf#124700*/ )
1667 // data invalid, forget
1668 pSystemDependentData_CairoPath.reset();
1672 if(pSystemDependentData_CairoPath)
1674 // re-use data
1675 cairo_append_path(cr, pSystemDependentData_CairoPath->getCairoPath());
1677 else
1679 // create data
1680 size_t nSizeMeasure(0);
1682 // MM01 need to do line dashing as fallback stuff here now
1683 basegfx::B2DPolyPolygon aPolyPolygonLine;
1685 if(!bDoDirectCairoStroke && bStrokeUsed)
1687 // apply LineStyle
1688 basegfx::utils::applyLineDashing(
1689 rPolyLine, // source
1690 *pStroke, // pattern
1691 &aPolyPolygonLine, // target for lines
1692 nullptr, // target for gaps
1693 fDotDashLength); // full length if available
1695 else
1697 // no line dashing or direct stroke, just copy
1698 aPolyPolygonLine.append(rPolyLine);
1701 // MM01 checked/verified for Cairo
1702 for(sal_uInt32 a(0); a < aPolyPolygonLine.count(); a++)
1704 const basegfx::B2DPolygon aPolyLine(aPolyPolygonLine.getB2DPolygon(a));
1706 if (!bNoJoin)
1708 // PixelOffset now reflected in linear transformation used
1709 nSizeMeasure += AddPolygonToPath(
1711 aPolyLine,
1712 rObjectToDevice, // ObjectToDevice *without* LineDraw-Offset
1713 !bAntiAlias,
1714 bPixelSnapHairline);
1716 else
1718 const sal_uInt32 nPointCount(aPolyLine.count());
1719 const sal_uInt32 nEdgeCount(aPolyLine.isClosed() ? nPointCount : nPointCount - 1);
1720 basegfx::B2DPolygon aEdge;
1722 aEdge.append(aPolyLine.getB2DPoint(0));
1723 aEdge.append(basegfx::B2DPoint(0.0, 0.0));
1725 for (sal_uInt32 i(0); i < nEdgeCount; i++)
1727 const sal_uInt32 nNextIndex((i + 1) % nPointCount);
1728 aEdge.setB2DPoint(1, aPolyLine.getB2DPoint(nNextIndex));
1729 aEdge.setNextControlPoint(0, aPolyLine.getNextControlPoint(i));
1730 aEdge.setPrevControlPoint(1, aPolyLine.getPrevControlPoint(nNextIndex));
1732 // PixelOffset now reflected in linear transformation used
1733 nSizeMeasure += AddPolygonToPath(
1735 aEdge,
1736 rObjectToDevice, // ObjectToDevice *without* LineDraw-Offset
1737 !bAntiAlias,
1738 bPixelSnapHairline);
1740 // prepare next step
1741 aEdge.setB2DPoint(0, aEdge.getB2DPoint(1));
1746 // copy and add to buffering mechanism
1747 if (!bPixelSnapHairline /*tdf#124700*/)
1749 pSystemDependentData_CairoPath = rPolyLine.addOrReplaceSystemDependentData<SystemDependentData_CairoPath>(
1750 ImplGetSystemDependentDataManager(),
1751 nSizeMeasure,
1753 bNoJoin,
1754 bAntiAlias,
1755 pStroke);
1759 // extract extents
1760 if (pExtents)
1762 *pExtents = getClippedStrokeDamage(cr);
1763 // transform also extents (ranges) of damage so they can be correctly redrawn
1764 pExtents->transform(aDamageMatrix);
1767 // draw and consume
1768 cairo_stroke(cr);
1770 return true;
1773 bool SvpSalGraphics::drawPolyLineBezier( sal_uInt32,
1774 const Point*,
1775 const PolyFlags* )
1777 SAL_INFO("vcl.gdi", "unsupported SvpSalGraphics::drawPolyLineBezier case");
1778 return false;
1781 bool SvpSalGraphics::drawPolygonBezier( sal_uInt32,
1782 const Point*,
1783 const PolyFlags* )
1785 SAL_INFO("vcl.gdi", "unsupported SvpSalGraphics::drawPolygonBezier case");
1786 return false;
1789 bool SvpSalGraphics::drawPolyPolygonBezier( sal_uInt32,
1790 const sal_uInt32*,
1791 const Point* const*,
1792 const PolyFlags* const* )
1794 SAL_INFO("vcl.gdi", "unsupported SvpSalGraphics::drawPolyPolygonBezier case");
1795 return false;
1798 namespace
1800 void add_polygon_path(cairo_t* cr, const basegfx::B2DPolyPolygon& rPolyPolygon, const basegfx::B2DHomMatrix& rObjectToDevice, bool bPixelSnap)
1802 // try to access buffered data
1803 std::shared_ptr<SystemDependentData_CairoPath> pSystemDependentData_CairoPath(
1804 rPolyPolygon.getSystemDependentData<SystemDependentData_CairoPath>());
1806 if(pSystemDependentData_CairoPath)
1808 // re-use data
1809 cairo_append_path(cr, pSystemDependentData_CairoPath->getCairoPath());
1811 else
1813 // create data
1814 size_t nSizeMeasure(0);
1816 for (const auto & rPoly : rPolyPolygon)
1818 // PixelOffset used: Was dependent of 'm_aLineColor != SALCOLOR_NONE'
1819 // Adapt setupPolyPolygon-users to set a linear transformation to achieve PixelOffset
1820 nSizeMeasure += AddPolygonToPath(
1822 rPoly,
1823 rObjectToDevice,
1824 bPixelSnap,
1825 false);
1828 // copy and add to buffering mechanism
1829 // for decisions how/what to buffer, see Note in WinSalGraphicsImpl::drawPolyPolygon
1830 pSystemDependentData_CairoPath = rPolyPolygon.addOrReplaceSystemDependentData<SystemDependentData_CairoPath>(
1831 ImplGetSystemDependentDataManager(),
1832 nSizeMeasure,
1834 false,
1835 false,
1836 nullptr);
1841 bool SvpSalGraphics::drawPolyPolygon(
1842 const basegfx::B2DHomMatrix& rObjectToDevice,
1843 const basegfx::B2DPolyPolygon& rPolyPolygon,
1844 double fTransparency)
1846 const bool bHasFill(m_aFillColor != SALCOLOR_NONE);
1847 const bool bHasLine(m_aLineColor != SALCOLOR_NONE);
1849 if(0 == rPolyPolygon.count() || !(bHasFill || bHasLine) || fTransparency < 0.0 || fTransparency >= 1.0)
1851 return true;
1854 cairo_t* cr = getCairoContext(true);
1855 clipRegion(cr);
1857 // Set full (Object-to-Device) transformation - if used
1858 if(!rObjectToDevice.isIdentity())
1860 cairo_matrix_t aMatrix;
1862 cairo_matrix_init(
1863 &aMatrix,
1864 rObjectToDevice.get( 0, 0 ),
1865 rObjectToDevice.get( 1, 0 ),
1866 rObjectToDevice.get( 0, 1 ),
1867 rObjectToDevice.get( 1, 1 ),
1868 rObjectToDevice.get( 0, 2 ),
1869 rObjectToDevice.get( 1, 2 ));
1870 cairo_set_matrix(cr, &aMatrix);
1873 // To make releaseCairoContext work, use empty extents
1874 basegfx::B2DRange extents;
1876 if (bHasFill)
1878 add_polygon_path(cr, rPolyPolygon, rObjectToDevice, !getAntiAlias());
1880 applyColor(cr, m_aFillColor, fTransparency);
1881 // Get FillDamage (will be extended for LineDamage below)
1882 extents = getClippedFillDamage(cr);
1884 cairo_fill(cr);
1887 if (bHasLine)
1889 // PixelOffset used: Set PixelOffset as linear transformation
1890 cairo_matrix_t aMatrix;
1891 cairo_matrix_init_translate(&aMatrix, 0.5, 0.5);
1892 cairo_set_matrix(cr, &aMatrix);
1894 add_polygon_path(cr, rPolyPolygon, rObjectToDevice, !getAntiAlias());
1896 applyColor(cr, m_aLineColor, fTransparency);
1898 // expand with possible StrokeDamage
1899 basegfx::B2DRange stroke_extents = getClippedStrokeDamage(cr);
1900 stroke_extents.transform(basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5));
1901 extents.expand(stroke_extents);
1903 cairo_stroke(cr);
1906 // if transformation has been applied, transform also extents (ranges)
1907 // of damage so they can be correctly redrawn
1908 extents.transform(rObjectToDevice);
1909 releaseCairoContext(cr, true, extents);
1911 return true;
1914 bool SvpSalGraphics::drawGradient(const tools::PolyPolygon& rPolyPolygon, const Gradient& rGradient)
1916 if (rGradient.GetStyle() != GradientStyle::Linear
1917 && rGradient.GetStyle() != GradientStyle::Radial)
1918 return false; // unsupported
1919 if (rGradient.GetSteps() != 0)
1920 return false; // We can't tell cairo how many colors to use in the gradient.
1922 cairo_t* cr = getCairoContext(true);
1923 clipRegion(cr);
1925 tools::Rectangle aInputRect(rPolyPolygon.GetBoundRect());
1926 if( rPolyPolygon.IsRect())
1928 // Rect->Polygon conversion loses the right and bottom edge, fix that.
1929 aInputRect.AdjustRight( 1 );
1930 aInputRect.AdjustBottom( 1 );
1931 basegfx::B2DHomMatrix rObjectToDevice;
1932 AddPolygonToPath(cr, tools::Polygon(aInputRect).getB2DPolygon(), rObjectToDevice, !getAntiAlias(), false);
1934 else
1936 basegfx::B2DPolyPolygon aB2DPolyPolygon(rPolyPolygon.getB2DPolyPolygon());
1937 for (auto const & rPolygon : std::as_const(aB2DPolyPolygon))
1939 basegfx::B2DHomMatrix rObjectToDevice;
1940 AddPolygonToPath(cr, rPolygon, rObjectToDevice, !getAntiAlias(), false);
1944 Gradient aGradient(rGradient);
1946 tools::Rectangle aBoundRect;
1947 Point aCenter;
1949 aGradient.SetAngle(aGradient.GetAngle() + 2700_deg10);
1950 aGradient.GetBoundRect(aInputRect, aBoundRect, aCenter);
1951 Color aStartColor = aGradient.GetStartColor();
1952 Color aEndColor = aGradient.GetEndColor();
1954 cairo_pattern_t* pattern;
1955 if (rGradient.GetStyle() == GradientStyle::Linear)
1957 tools::Polygon aPoly(aBoundRect);
1958 aPoly.Rotate(aCenter, aGradient.GetAngle() % 3600_deg10);
1959 pattern = cairo_pattern_create_linear(aPoly[0].X(), aPoly[0].Y(), aPoly[1].X(), aPoly[1].Y());
1961 else
1963 double radius = std::max(aBoundRect.GetWidth() / 2.0, aBoundRect.GetHeight() / 2.0);
1964 // Move the center a bit to the top-left (the default VCL algorithm is a bit off-center that way,
1965 // cairo is the opposite way).
1966 pattern = cairo_pattern_create_radial(aCenter.X() - 0.5, aCenter.Y() - 0.5, 0,
1967 aCenter.X() - 0.5, aCenter.Y() - 0.5, radius);
1968 std::swap( aStartColor, aEndColor );
1971 cairo_pattern_add_color_stop_rgba(pattern, aGradient.GetBorder() / 100.0,
1972 aStartColor.GetRed() * aGradient.GetStartIntensity() / 25500.0,
1973 aStartColor.GetGreen() * aGradient.GetStartIntensity() / 25500.0,
1974 aStartColor.GetBlue() * aGradient.GetStartIntensity() / 25500.0,
1975 1.0);
1977 cairo_pattern_add_color_stop_rgba(pattern, 1.0,
1978 aEndColor.GetRed() * aGradient.GetEndIntensity() / 25500.0,
1979 aEndColor.GetGreen() * aGradient.GetEndIntensity() / 25500.0,
1980 aEndColor.GetBlue() * aGradient.GetEndIntensity() / 25500.0,
1981 1.0);
1983 cairo_set_source(cr, pattern);
1984 cairo_pattern_destroy(pattern);
1986 basegfx::B2DRange extents = getClippedFillDamage(cr);
1987 cairo_fill_preserve(cr);
1989 releaseCairoContext(cr, true, extents);
1991 return true;
1994 bool SvpSalGraphics::implDrawGradient(basegfx::B2DPolyPolygon const & rPolyPolygon, SalGradient const & rGradient)
1996 cairo_t* cr = getCairoContext(true);
1997 clipRegion(cr);
1999 basegfx::B2DHomMatrix rObjectToDevice;
2001 for (auto const & rPolygon : rPolyPolygon)
2002 AddPolygonToPath(cr, rPolygon, rObjectToDevice, !getAntiAlias(), false);
2004 cairo_pattern_t* pattern = cairo_pattern_create_linear(rGradient.maPoint1.getX(), rGradient.maPoint1.getY(), rGradient.maPoint2.getX(), rGradient.maPoint2.getY());
2006 for (SalGradientStop const & rStop : rGradient.maStops)
2008 double r = rStop.maColor.GetRed() / 255.0;
2009 double g = rStop.maColor.GetGreen() / 255.0;
2010 double b = rStop.maColor.GetBlue() / 255.0;
2011 double a = rStop.maColor.GetAlpha() / 255.0;
2012 double offset = rStop.mfOffset;
2014 cairo_pattern_add_color_stop_rgba(pattern, offset, r, g, b, a);
2016 cairo_set_source(cr, pattern);
2017 cairo_pattern_destroy(pattern);
2019 basegfx::B2DRange extents = getClippedFillDamage(cr);
2020 cairo_fill_preserve(cr);
2022 releaseCairoContext(cr, true, extents);
2024 return true;
2027 void SvpSalGraphics::applyColor(cairo_t *cr, Color aColor, double fTransparency)
2029 if (cairo_surface_get_content(m_pSurface) == CAIRO_CONTENT_COLOR_ALPHA)
2031 cairo_set_source_rgba(cr, aColor.GetRed()/255.0,
2032 aColor.GetGreen()/255.0,
2033 aColor.GetBlue()/255.0,
2034 1.0 - fTransparency);
2036 else
2038 double fSet = aColor == COL_BLACK ? 1.0 : 0.0;
2039 cairo_set_source_rgba(cr, 1, 1, 1, fSet);
2040 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
2044 void SvpSalGraphics::copyArea( tools::Long nDestX,
2045 tools::Long nDestY,
2046 tools::Long nSrcX,
2047 tools::Long nSrcY,
2048 tools::Long nSrcWidth,
2049 tools::Long nSrcHeight,
2050 bool /*bWindowInvalidate*/ )
2052 SalTwoRect aTR(nSrcX, nSrcY, nSrcWidth, nSrcHeight, nDestX, nDestY, nSrcWidth, nSrcHeight);
2053 copyBits(aTR, this);
2056 static basegfx::B2DRange renderWithOperator(cairo_t* cr, const SalTwoRect& rTR,
2057 cairo_surface_t* source, cairo_operator_t eOperator = CAIRO_OPERATOR_SOURCE)
2059 cairo_rectangle(cr, rTR.mnDestX, rTR.mnDestY, rTR.mnDestWidth, rTR.mnDestHeight);
2061 basegfx::B2DRange extents = getClippedFillDamage(cr);
2063 cairo_clip(cr);
2065 cairo_translate(cr, rTR.mnDestX, rTR.mnDestY);
2066 double fXScale = 1.0f;
2067 double fYScale = 1.0f;
2068 if (rTR.mnSrcWidth != 0 && rTR.mnSrcHeight != 0) {
2069 fXScale = static_cast<double>(rTR.mnDestWidth)/rTR.mnSrcWidth;
2070 fYScale = static_cast<double>(rTR.mnDestHeight)/rTR.mnSrcHeight;
2071 cairo_scale(cr, fXScale, fYScale);
2074 cairo_save(cr);
2075 cairo_set_source_surface(cr, source, -rTR.mnSrcX, -rTR.mnSrcY);
2076 if ((fXScale != 1.0 && rTR.mnSrcWidth == 1) || (fYScale != 1.0 && rTR.mnSrcHeight == 1))
2078 cairo_pattern_t* sourcepattern = cairo_get_source(cr);
2079 cairo_pattern_set_extend(sourcepattern, CAIRO_EXTEND_REPEAT);
2080 cairo_pattern_set_filter(sourcepattern, CAIRO_FILTER_NEAREST);
2082 cairo_set_operator(cr, eOperator);
2083 cairo_paint(cr);
2084 cairo_restore(cr);
2086 return extents;
2089 static basegfx::B2DRange renderSource(cairo_t* cr, const SalTwoRect& rTR,
2090 cairo_surface_t* source)
2092 return renderWithOperator(cr, rTR, source, CAIRO_OPERATOR_SOURCE);
2095 void SvpSalGraphics::copyWithOperator( const SalTwoRect& rTR, cairo_surface_t* source,
2096 cairo_operator_t eOp )
2098 cairo_t* cr = getCairoContext(false);
2099 clipRegion(cr);
2101 basegfx::B2DRange extents = renderWithOperator(cr, rTR, source, eOp);
2103 releaseCairoContext(cr, false, extents);
2106 void SvpSalGraphics::copySource( const SalTwoRect& rTR, cairo_surface_t* source )
2108 copyWithOperator(rTR, source, CAIRO_OPERATOR_SOURCE);
2111 void SvpSalGraphics::copyBits( const SalTwoRect& rTR,
2112 SalGraphics* pSrcGraphics )
2114 SalTwoRect aTR(rTR);
2116 SvpSalGraphics* pSrc = pSrcGraphics ?
2117 static_cast<SvpSalGraphics*>(pSrcGraphics) : this;
2119 cairo_surface_t* source = pSrc->m_pSurface;
2121 cairo_surface_t *pCopy = nullptr;
2122 if (pSrc == this)
2124 //self copy is a problem, so dup source in that case
2125 pCopy = cairo_surface_create_similar(source,
2126 cairo_surface_get_content(m_pSurface),
2127 aTR.mnSrcWidth * m_fScale,
2128 aTR.mnSrcHeight * m_fScale);
2129 dl_cairo_surface_set_device_scale(pCopy, m_fScale, m_fScale);
2130 cairo_t* cr = cairo_create(pCopy);
2131 cairo_set_source_surface(cr, source, -aTR.mnSrcX, -aTR.mnSrcY);
2132 cairo_rectangle(cr, 0, 0, aTR.mnSrcWidth, aTR.mnSrcHeight);
2133 cairo_fill(cr);
2134 cairo_destroy(cr);
2136 source = pCopy;
2138 aTR.mnSrcX = 0;
2139 aTR.mnSrcY = 0;
2142 copySource(aTR, source);
2144 if (pCopy)
2145 cairo_surface_destroy(pCopy);
2148 void SvpSalGraphics::drawBitmap(const SalTwoRect& rTR, const SalBitmap& rSourceBitmap)
2150 // MM02 try to access buffered BitmapHelper
2151 std::shared_ptr<BitmapHelper> aSurface;
2152 tryToUseSourceBuffer(rSourceBitmap, aSurface);
2153 cairo_surface_t* source = aSurface->getSurface(
2154 rTR.mnDestWidth,
2155 rTR.mnDestHeight);
2157 if (!source)
2159 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawAlphaBitmap case");
2160 return;
2163 #if 0 // LO code is not yet bitmap32-ready.
2164 // if m_bSupportsBitmap32 becomes true for Svp revisit this
2165 copyWithOperator(rTR, source, CAIRO_OPERATOR_OVER);
2166 #else
2167 copyWithOperator(rTR, source, CAIRO_OPERATOR_SOURCE);
2168 #endif
2171 void SvpSalGraphics::drawBitmap(const SalTwoRect& rTR, const BitmapBuffer* pBuffer, cairo_operator_t eOp)
2173 cairo_surface_t* source = createCairoSurface( pBuffer );
2174 copyWithOperator(rTR, source, eOp);
2175 cairo_surface_destroy(source);
2178 void SvpSalGraphics::drawBitmap( const SalTwoRect& rTR,
2179 const SalBitmap& rSourceBitmap,
2180 const SalBitmap& rTransparentBitmap )
2182 drawAlphaBitmap(rTR, rSourceBitmap, rTransparentBitmap);
2185 void SvpSalGraphics::drawMask( const SalTwoRect& rTR,
2186 const SalBitmap& rSalBitmap,
2187 Color nMaskColor )
2189 /** creates an image from the given rectangle, replacing all black pixels
2190 * with nMaskColor and make all other full transparent */
2191 // MM02 here decided *against* using buffered BitmapHelper
2192 // because the data gets somehow 'unmuliplied'. This may also be
2193 // done just once, but I am not sure if this is safe to do.
2194 // So for now dispense re-using data here.
2195 BitmapHelper aSurface(rSalBitmap, true); // The mask is argb32
2196 if (!aSurface.getSurface())
2198 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawMask case");
2199 return;
2201 sal_Int32 nStride;
2202 unsigned char *mask_data = aSurface.getBits(nStride);
2203 vcl::bitmap::lookup_table const & unpremultiply_table = vcl::bitmap::get_unpremultiply_table();
2204 for (tools::Long y = rTR.mnSrcY ; y < rTR.mnSrcY + rTR.mnSrcHeight; ++y)
2206 unsigned char *row = mask_data + (nStride*y);
2207 unsigned char *data = row + (rTR.mnSrcX * 4);
2208 for (tools::Long x = rTR.mnSrcX; x < rTR.mnSrcX + rTR.mnSrcWidth; ++x)
2210 sal_uInt8 a = data[SVP_CAIRO_ALPHA];
2211 sal_uInt8 b = unpremultiply_table[a][data[SVP_CAIRO_BLUE]];
2212 sal_uInt8 g = unpremultiply_table[a][data[SVP_CAIRO_GREEN]];
2213 sal_uInt8 r = unpremultiply_table[a][data[SVP_CAIRO_RED]];
2214 if (r == 0 && g == 0 && b == 0)
2216 data[0] = nMaskColor.GetBlue();
2217 data[1] = nMaskColor.GetGreen();
2218 data[2] = nMaskColor.GetRed();
2219 data[3] = 0xff;
2221 else
2223 data[0] = 0;
2224 data[1] = 0;
2225 data[2] = 0;
2226 data[3] = 0;
2228 data+=4;
2231 aSurface.mark_dirty();
2233 cairo_t* cr = getCairoContext(false);
2234 clipRegion(cr);
2236 cairo_rectangle(cr, rTR.mnDestX, rTR.mnDestY, rTR.mnDestWidth, rTR.mnDestHeight);
2238 basegfx::B2DRange extents = getClippedFillDamage(cr);
2240 cairo_clip(cr);
2242 cairo_translate(cr, rTR.mnDestX, rTR.mnDestY);
2243 double fXScale = static_cast<double>(rTR.mnDestWidth)/rTR.mnSrcWidth;
2244 double fYScale = static_cast<double>(rTR.mnDestHeight)/rTR.mnSrcHeight;
2245 cairo_scale(cr, fXScale, fYScale);
2246 cairo_set_source_surface(cr, aSurface.getSurface(), -rTR.mnSrcX, -rTR.mnSrcY);
2247 if ((fXScale != 1.0 && rTR.mnSrcWidth == 1) || (fYScale != 1.0 && rTR.mnSrcHeight == 1))
2249 cairo_pattern_t* sourcepattern = cairo_get_source(cr);
2250 cairo_pattern_set_extend(sourcepattern, CAIRO_EXTEND_REPEAT);
2251 cairo_pattern_set_filter(sourcepattern, CAIRO_FILTER_NEAREST);
2253 cairo_paint(cr);
2255 releaseCairoContext(cr, false, extents);
2258 std::shared_ptr<SalBitmap> SvpSalGraphics::getBitmap( tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight )
2260 std::shared_ptr<SvpSalBitmap> pBitmap = std::make_shared<SvpSalBitmap>();
2261 BitmapPalette aPal;
2262 vcl::PixelFormat ePixelFormat = vcl::PixelFormat::INVALID;
2263 if (GetBitCount() == 1)
2265 ePixelFormat = vcl::PixelFormat::N1_BPP;
2266 aPal.SetEntryCount(2);
2267 aPal[0] = COL_BLACK;
2268 aPal[1] = COL_WHITE;
2270 else
2272 ePixelFormat = vcl::PixelFormat::N32_BPP;
2275 if (!pBitmap->Create(Size(nWidth, nHeight), ePixelFormat, aPal))
2277 SAL_WARN("vcl.gdi", "SvpSalGraphics::getBitmap, cannot create bitmap");
2278 return nullptr;
2281 cairo_surface_t* target = SvpSalGraphics::createCairoSurface(pBitmap->GetBuffer());
2282 if (!target)
2284 SAL_WARN("vcl.gdi", "SvpSalGraphics::getBitmap, cannot create cairo surface");
2285 return nullptr;
2287 cairo_t* cr = cairo_create(target);
2289 SalTwoRect aTR(nX, nY, nWidth, nHeight, 0, 0, nWidth, nHeight);
2290 renderSource(cr, aTR, m_pSurface);
2292 cairo_destroy(cr);
2293 cairo_surface_destroy(target);
2295 Toggle1BitTransparency(*pBitmap->GetBuffer());
2297 return pBitmap;
2300 Color SvpSalGraphics::getPixel( tools::Long nX, tools::Long nY )
2302 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0)
2303 cairo_surface_t *target = cairo_surface_create_similar_image(m_pSurface, CAIRO_FORMAT_ARGB32, 1, 1);
2304 #else
2305 cairo_surface_t *target = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
2306 #endif
2308 cairo_t* cr = cairo_create(target);
2310 cairo_rectangle(cr, 0, 0, 1, 1);
2311 cairo_set_source_surface(cr, m_pSurface, -nX, -nY);
2312 cairo_paint(cr);
2313 cairo_destroy(cr);
2315 cairo_surface_flush(target);
2316 vcl::bitmap::lookup_table const & unpremultiply_table = vcl::bitmap::get_unpremultiply_table();
2317 unsigned char *data = cairo_image_surface_get_data(target);
2318 sal_uInt8 a = data[SVP_CAIRO_ALPHA];
2319 sal_uInt8 b = unpremultiply_table[a][data[SVP_CAIRO_BLUE]];
2320 sal_uInt8 g = unpremultiply_table[a][data[SVP_CAIRO_GREEN]];
2321 sal_uInt8 r = unpremultiply_table[a][data[SVP_CAIRO_RED]];
2322 Color aColor(ColorAlpha, a, r, g, b);
2323 cairo_surface_destroy(target);
2325 return aColor;
2328 namespace
2330 cairo_pattern_t * create_stipple()
2332 static unsigned char data[16] = { 0xFF, 0xFF, 0x00, 0x00,
2333 0xFF, 0xFF, 0x00, 0x00,
2334 0x00, 0x00, 0xFF, 0xFF,
2335 0x00, 0x00, 0xFF, 0xFF };
2336 cairo_surface_t* surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_A8, 4, 4, 4);
2337 cairo_pattern_t* pattern = cairo_pattern_create_for_surface(surface);
2338 cairo_surface_destroy(surface);
2339 cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
2340 cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
2341 return pattern;
2345 void SvpSalGraphics::invert(const basegfx::B2DPolygon &rPoly, SalInvert nFlags)
2347 cairo_t* cr = getCairoContext(false);
2348 clipRegion(cr);
2350 // To make releaseCairoContext work, use empty extents
2351 basegfx::B2DRange extents;
2353 AddPolygonToPath(
2355 rPoly,
2356 basegfx::B2DHomMatrix(),
2357 !getAntiAlias(),
2358 false);
2360 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
2362 if (cairo_version() >= CAIRO_VERSION_ENCODE(1, 10, 0))
2364 cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
2366 else
2368 SAL_WARN("vcl.gdi", "SvpSalGraphics::invert, archaic cairo");
2371 if (nFlags & SalInvert::TrackFrame)
2373 cairo_set_line_width(cr, 2.0);
2374 const double dashLengths[2] = { 4.0, 4.0 };
2375 cairo_set_dash(cr, dashLengths, 2, 0);
2377 extents = getClippedStrokeDamage(cr);
2378 //see tdf#106577 under wayland, some pixel droppings seen, maybe we're
2379 //out by one somewhere, or cairo_stroke_extents is confused by
2380 //dashes/line width
2381 if(!extents.isEmpty())
2383 extents.grow(1);
2386 cairo_stroke(cr);
2388 else
2390 extents = getClippedFillDamage(cr);
2392 cairo_clip(cr);
2394 if (nFlags & SalInvert::N50)
2396 cairo_pattern_t *pattern = create_stipple();
2397 cairo_surface_t* surface = cairo_surface_create_similar(m_pSurface,
2398 cairo_surface_get_content(m_pSurface),
2399 extents.getWidth() * m_fScale,
2400 extents.getHeight() * m_fScale);
2402 dl_cairo_surface_set_device_scale(surface, m_fScale, m_fScale);
2403 cairo_t* stipple_cr = cairo_create(surface);
2404 cairo_set_source_rgb(stipple_cr, 1.0, 1.0, 1.0);
2405 cairo_mask(stipple_cr, pattern);
2406 cairo_pattern_destroy(pattern);
2407 cairo_destroy(stipple_cr);
2408 cairo_mask_surface(cr, surface, extents.getMinX(), extents.getMinY());
2409 cairo_surface_destroy(surface);
2411 else
2413 cairo_paint(cr);
2417 releaseCairoContext(cr, false, extents);
2420 void SvpSalGraphics::invert( tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight, SalInvert nFlags )
2422 basegfx::B2DPolygon aRect = basegfx::utils::createPolygonFromRect(basegfx::B2DRectangle(nX, nY, nX+nWidth, nY+nHeight));
2424 invert(aRect, nFlags);
2427 void SvpSalGraphics::invert(sal_uInt32 nPoints, const Point* pPtAry, SalInvert nFlags)
2429 basegfx::B2DPolygon aPoly;
2430 aPoly.append(basegfx::B2DPoint(pPtAry->getX(), pPtAry->getY()), nPoints);
2431 for (sal_uInt32 i = 1; i < nPoints; ++i)
2432 aPoly.setB2DPoint(i, basegfx::B2DPoint(pPtAry[i].getX(), pPtAry[i].getY()));
2433 aPoly.setClosed(true);
2435 invert(aPoly, nFlags);
2438 bool SvpSalGraphics::drawEPS( tools::Long, tools::Long, tools::Long, tools::Long, void*, sal_uInt32 )
2440 return false;
2443 namespace
2445 bool isCairoCompatible(const BitmapBuffer* pBuffer)
2447 if (!pBuffer)
2448 return false;
2450 // We use Cairo that supports 24-bit RGB.
2451 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
2452 if (pBuffer->mnBitCount != 32 && pBuffer->mnBitCount != 24 && pBuffer->mnBitCount != 1)
2453 #else
2454 if (pBuffer->mnBitCount != 32 && pBuffer->mnBitCount != 1)
2455 #endif
2456 return false;
2458 cairo_format_t nFormat = getCairoFormat(*pBuffer);
2459 return (cairo_format_stride_for_width(nFormat, pBuffer->mnWidth) == pBuffer->mnScanlineSize);
2463 cairo_surface_t* SvpSalGraphics::createCairoSurface(const BitmapBuffer *pBuffer)
2465 if (!isCairoCompatible(pBuffer))
2466 return nullptr;
2468 cairo_format_t nFormat = getCairoFormat(*pBuffer);
2469 cairo_surface_t *target =
2470 cairo_image_surface_create_for_data(pBuffer->mpBits,
2471 nFormat,
2472 pBuffer->mnWidth, pBuffer->mnHeight,
2473 pBuffer->mnScanlineSize);
2474 if (cairo_surface_status(target) != CAIRO_STATUS_SUCCESS)
2476 cairo_surface_destroy(target);
2477 return nullptr;
2479 return target;
2482 cairo_t* SvpSalGraphics::createTmpCompatibleCairoContext() const
2484 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0)
2485 cairo_surface_t *target = cairo_surface_create_similar_image(m_pSurface,
2486 #else
2487 cairo_surface_t *target = cairo_image_surface_create(
2488 #endif
2489 CAIRO_FORMAT_ARGB32,
2490 m_aFrameSize.getX() * m_fScale,
2491 m_aFrameSize.getY() * m_fScale);
2493 dl_cairo_surface_set_device_scale(target, m_fScale, m_fScale);
2495 return cairo_create(target);
2498 cairo_t* SvpSalGraphics::getCairoContext(bool bXorModeAllowed) const
2500 cairo_t* cr;
2501 if (m_ePaintMode == PaintMode::Xor && bXorModeAllowed)
2502 cr = createTmpCompatibleCairoContext();
2503 else
2504 cr = cairo_create(m_pSurface);
2505 cairo_set_line_width(cr, 1);
2506 cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
2507 cairo_set_antialias(cr, getAntiAlias() ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE);
2508 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
2510 // ensure no linear transformation and no PathInfo in local cairo_path_t
2511 cairo_identity_matrix(cr);
2512 cairo_new_path(cr);
2514 return cr;
2517 cairo_user_data_key_t* SvpSalGraphics::getDamageKey()
2519 static cairo_user_data_key_t aDamageKey;
2520 return &aDamageKey;
2523 void SvpSalGraphics::releaseCairoContext(cairo_t* cr, bool bXorModeAllowed, const basegfx::B2DRange& rExtents) const
2525 const bool bXoring = (m_ePaintMode == PaintMode::Xor && bXorModeAllowed);
2527 if (rExtents.isEmpty())
2529 //nothing changed, return early
2530 if (bXoring)
2532 cairo_surface_t* surface = cairo_get_target(cr);
2533 cairo_surface_destroy(surface);
2535 cairo_destroy(cr);
2536 return;
2539 basegfx::B2IRange aIntExtents(basegfx::unotools::b2ISurroundingRangeFromB2DRange(rExtents));
2540 sal_Int32 nExtentsLeft(aIntExtents.getMinX()), nExtentsTop(aIntExtents.getMinY());
2541 sal_Int32 nExtentsRight(aIntExtents.getMaxX()), nExtentsBottom(aIntExtents.getMaxY());
2542 sal_Int32 nWidth = m_aFrameSize.getX();
2543 sal_Int32 nHeight = m_aFrameSize.getY();
2544 nExtentsLeft = std::max<sal_Int32>(nExtentsLeft, 0);
2545 nExtentsTop = std::max<sal_Int32>(nExtentsTop, 0);
2546 nExtentsRight = std::min<sal_Int32>(nExtentsRight, nWidth);
2547 nExtentsBottom = std::min<sal_Int32>(nExtentsBottom, nHeight);
2549 cairo_surface_t* surface = cairo_get_target(cr);
2550 cairo_surface_flush(surface);
2552 //For the most part we avoid the use of XOR these days, but there
2553 //are some edge cases where legacy stuff still supports it, so
2554 //emulate it (slowly) here.
2555 if (bXoring)
2557 cairo_surface_t* target_surface = m_pSurface;
2558 if (cairo_surface_get_type(target_surface) != CAIRO_SURFACE_TYPE_IMAGE)
2560 //in the unlikely case we can't use m_pSurface directly, copy contents
2561 //to another temp image surface
2562 cairo_t* copycr = createTmpCompatibleCairoContext();
2563 cairo_rectangle(copycr, nExtentsLeft, nExtentsTop,
2564 nExtentsRight - nExtentsLeft,
2565 nExtentsBottom - nExtentsTop);
2566 cairo_set_source_surface(copycr, m_pSurface, 0, 0);
2567 cairo_paint(copycr);
2568 target_surface = cairo_get_target(copycr);
2569 cairo_destroy(copycr);
2572 cairo_surface_flush(target_surface);
2573 unsigned char *target_surface_data = cairo_image_surface_get_data(target_surface);
2574 unsigned char *xor_surface_data = cairo_image_surface_get_data(surface);
2576 cairo_format_t nFormat = cairo_image_surface_get_format(target_surface);
2577 assert(nFormat == CAIRO_FORMAT_ARGB32 && "need to implement CAIRO_FORMAT_A1 after all here");
2578 sal_Int32 nStride = cairo_format_stride_for_width(nFormat, nWidth * m_fScale);
2579 sal_Int32 nUnscaledExtentsLeft = nExtentsLeft * m_fScale;
2580 sal_Int32 nUnscaledExtentsRight = nExtentsRight * m_fScale;
2581 sal_Int32 nUnscaledExtentsTop = nExtentsTop * m_fScale;
2582 sal_Int32 nUnscaledExtentsBottom = nExtentsBottom * m_fScale;
2584 // Handle headless size forced to (1,1) by SvpSalFrame::GetSurfaceFrameSize().
2585 int target_surface_width = cairo_image_surface_get_width(target_surface);
2586 if (nUnscaledExtentsLeft > target_surface_width)
2587 nUnscaledExtentsLeft = target_surface_width;
2588 if (nUnscaledExtentsRight > target_surface_width)
2589 nUnscaledExtentsRight = target_surface_width;
2590 int target_surface_height = cairo_image_surface_get_height(target_surface);
2591 if (nUnscaledExtentsTop > target_surface_height)
2592 nUnscaledExtentsTop = target_surface_height;
2593 if (nUnscaledExtentsBottom > target_surface_height)
2594 nUnscaledExtentsBottom = target_surface_height;
2596 vcl::bitmap::lookup_table const & unpremultiply_table
2597 = vcl::bitmap::get_unpremultiply_table();
2598 vcl::bitmap::lookup_table const & premultiply_table = vcl::bitmap::get_premultiply_table();
2599 for (sal_Int32 y = nUnscaledExtentsTop; y < nUnscaledExtentsBottom; ++y)
2601 unsigned char *true_row = target_surface_data + (nStride*y);
2602 unsigned char *xor_row = xor_surface_data + (nStride*y);
2603 unsigned char *true_data = true_row + (nUnscaledExtentsLeft * 4);
2604 unsigned char *xor_data = xor_row + (nUnscaledExtentsLeft * 4);
2605 for (sal_Int32 x = nUnscaledExtentsLeft; x < nUnscaledExtentsRight; ++x)
2607 sal_uInt8 a = true_data[SVP_CAIRO_ALPHA];
2608 sal_uInt8 xor_a = xor_data[SVP_CAIRO_ALPHA];
2609 sal_uInt8 b = unpremultiply_table[a][true_data[SVP_CAIRO_BLUE]] ^
2610 unpremultiply_table[xor_a][xor_data[SVP_CAIRO_BLUE]];
2611 sal_uInt8 g = unpremultiply_table[a][true_data[SVP_CAIRO_GREEN]] ^
2612 unpremultiply_table[xor_a][xor_data[SVP_CAIRO_GREEN]];
2613 sal_uInt8 r = unpremultiply_table[a][true_data[SVP_CAIRO_RED]] ^
2614 unpremultiply_table[xor_a][xor_data[SVP_CAIRO_RED]];
2615 true_data[SVP_CAIRO_BLUE] = premultiply_table[a][b];
2616 true_data[SVP_CAIRO_GREEN] = premultiply_table[a][g];
2617 true_data[SVP_CAIRO_RED] = premultiply_table[a][r];
2618 true_data+=4;
2619 xor_data+=4;
2622 cairo_surface_mark_dirty(target_surface);
2624 if (target_surface != m_pSurface)
2626 cairo_t* copycr = cairo_create(m_pSurface);
2627 //unlikely case we couldn't use m_pSurface directly, copy contents
2628 //back from image surface
2629 cairo_rectangle(copycr, nExtentsLeft, nExtentsTop,
2630 nExtentsRight - nExtentsLeft,
2631 nExtentsBottom - nExtentsTop);
2632 cairo_set_source_surface(copycr, target_surface, 0, 0);
2633 cairo_paint(copycr);
2634 cairo_destroy(copycr);
2635 cairo_surface_destroy(target_surface);
2638 cairo_surface_destroy(surface);
2641 cairo_destroy(cr); // unref
2643 DamageHandler* pDamage = static_cast<DamageHandler*>(cairo_surface_get_user_data(m_pSurface, getDamageKey()));
2645 if (pDamage)
2647 pDamage->damaged(pDamage->handle, nExtentsLeft, nExtentsTop,
2648 nExtentsRight - nExtentsLeft,
2649 nExtentsBottom - nExtentsTop);
2653 #if ENABLE_CAIRO_CANVAS
2654 bool SvpSalGraphics::SupportsCairo() const
2656 return false;
2659 cairo::SurfaceSharedPtr SvpSalGraphics::CreateSurface(const cairo::CairoSurfaceSharedPtr& /*rSurface*/) const
2661 return cairo::SurfaceSharedPtr();
2664 cairo::SurfaceSharedPtr SvpSalGraphics::CreateSurface(const OutputDevice& /*rRefDevice*/, int /*x*/, int /*y*/, int /*width*/, int /*height*/) const
2666 return cairo::SurfaceSharedPtr();
2669 cairo::SurfaceSharedPtr SvpSalGraphics::CreateBitmapSurface(const OutputDevice& /*rRefDevice*/, const BitmapSystemData& /*rData*/, const Size& /*rSize*/) const
2671 return cairo::SurfaceSharedPtr();
2674 css::uno::Any SvpSalGraphics::GetNativeSurfaceHandle(cairo::SurfaceSharedPtr& /*rSurface*/, const basegfx::B2ISize& /*rSize*/) const
2676 return css::uno::Any();
2679 #endif // ENABLE_CAIRO_CANVAS
2681 SystemGraphicsData SvpSalGraphics::GetGraphicsData() const
2683 return SystemGraphicsData();
2686 bool SvpSalGraphics::supportsOperation(OutDevSupportType eType) const
2688 switch (eType)
2690 case OutDevSupportType::TransparentRect:
2691 case OutDevSupportType::B2DDraw:
2692 return true;
2694 return false;
2697 void dl_cairo_surface_set_device_scale(cairo_surface_t *surface, double x_scale, double y_scale)
2699 #ifdef ANDROID
2700 cairo_surface_set_device_scale(surface, x_scale, y_scale);
2701 #else
2702 static auto func = reinterpret_cast<void(*)(cairo_surface_t*, double, double)>(
2703 dlsym(nullptr, "cairo_surface_set_device_scale"));
2704 if (func)
2705 func(surface, x_scale, y_scale);
2706 #endif
2709 void dl_cairo_surface_get_device_scale(cairo_surface_t *surface, double* x_scale, double* y_scale)
2711 #ifdef ANDROID
2712 cairo_surface_get_device_scale(surface, x_scale, y_scale);
2713 #else
2714 static auto func = reinterpret_cast<void(*)(cairo_surface_t*, double*, double*)>(
2715 dlsym(nullptr, "cairo_surface_get_device_scale"));
2716 if (func)
2717 func(surface, x_scale, y_scale);
2718 else
2720 if (x_scale)
2721 *x_scale = 1.0;
2722 if (y_scale)
2723 *y_scale = 1.0;
2725 #endif
2728 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */