1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <headless/BitmapHelper.hxx>
21 #include <headless/CairoCommon.hxx>
23 #include <vcl/BitmapTools.hxx>
24 #include <SalGradient.hxx>
26 #include <tools/helpers.hxx>
27 #include <basegfx/utils/canvastools.hxx>
28 #include <basegfx/matrix/b2dhommatrixtools.hxx>
29 #include <basegfx/polygon/b2dpolypolygontools.hxx>
30 #include <basegfx/polygon/b2dpolygontools.hxx>
31 #include <basegfx/range/b2irange.hxx>
32 #include <unotools/configmgr.hxx>
33 #include <sal/log.hxx>
34 #include <osl/module.h>
36 #if CAIRO_VERSION < CAIRO_VERSION_ENCODE(1, 12, 0)
37 #error "require at least cairo 1.12.0"
40 void dl_cairo_surface_set_device_scale(cairo_surface_t
* surface
, double x_scale
, double y_scale
)
43 cairo_surface_set_device_scale(surface
, x_scale
, y_scale
);
45 static auto func
= reinterpret_cast<void (*)(cairo_surface_t
*, double, double)>(
46 osl_getAsciiFunctionSymbol(nullptr, "cairo_surface_set_device_scale"));
48 func(surface
, x_scale
, y_scale
);
52 void dl_cairo_surface_get_device_scale(cairo_surface_t
* surface
, double* x_scale
, double* y_scale
)
55 cairo_surface_get_device_scale(surface
, x_scale
, y_scale
);
57 static auto func
= reinterpret_cast<void (*)(cairo_surface_t
*, double*, double*)>(
58 osl_getAsciiFunctionSymbol(nullptr, "cairo_surface_get_device_scale"));
60 func(surface
, x_scale
, y_scale
);
71 basegfx::B2DRange
getFillDamage(cairo_t
* cr
)
73 double x1
, y1
, x2
, y2
;
75 // this is faster than cairo_fill_extents, at the cost of some overdraw
76 cairo_path_extents(cr
, &x1
, &y1
, &x2
, &y2
);
78 // support B2DRange::isEmpty()
79 if (0.0 != x1
|| 0.0 != y1
|| 0.0 != x2
|| 0.0 != y2
)
81 return basegfx::B2DRange(x1
, y1
, x2
, y2
);
84 return basegfx::B2DRange();
87 basegfx::B2DRange
getClipBox(cairo_t
* cr
)
89 double x1
, y1
, x2
, y2
;
91 cairo_clip_extents(cr
, &x1
, &y1
, &x2
, &y2
);
93 // support B2DRange::isEmpty()
94 if (0.0 != x1
|| 0.0 != y1
|| 0.0 != x2
|| 0.0 != y2
)
96 return basegfx::B2DRange(x1
, y1
, x2
, y2
);
99 return basegfx::B2DRange();
102 basegfx::B2DRange
getClippedFillDamage(cairo_t
* cr
)
104 basegfx::B2DRange
aDamageRect(getFillDamage(cr
));
105 aDamageRect
.intersect(getClipBox(cr
));
109 basegfx::B2DRange
getStrokeDamage(cairo_t
* cr
)
111 double x1
, y1
, x2
, y2
;
113 // less accurate, but much faster
114 cairo_path_extents(cr
, &x1
, &y1
, &x2
, &y2
);
116 // support B2DRange::isEmpty()
117 if (0.0 != x1
|| 0.0 != y1
|| 0.0 != x2
|| 0.0 != y2
)
119 return basegfx::B2DRange(x1
, y1
, x2
, y2
);
122 return basegfx::B2DRange();
125 basegfx::B2DRange
getClippedStrokeDamage(cairo_t
* cr
)
127 basegfx::B2DRange
aDamageRect(getStrokeDamage(cr
));
128 aDamageRect
.intersect(getClipBox(cr
));
132 // Remove bClosePath: Checked that the already used mechanism for Win using
133 // Gdiplus already relies on rPolygon.isClosed(), so should be safe to replace
135 // For PixelSnap we need the ObjectToDevice transformation here now. This is a
136 // special case relative to the also executed LineDraw-Offset of (0.5, 0.5) in
137 // DeviceCoordinates: The LineDraw-Offset is applied *after* the snap, so we
138 // need the ObjectToDevice transformation *without* that offset here to do the
139 // same. The LineDraw-Offset will be applied by the callers using a linear
140 // transformation for Cairo now
141 // For support of PixelSnapHairline we also need the ObjectToDevice transformation
142 // and a method (same as in gdiimpl.cxx for Win and Gdiplus). This is needed e.g.
143 // for Chart-content visualization. CAUTION: It's not the same as PixelSnap (!)
144 // tdf#129845 add reply value to allow counting a point/byte/size measurement to
146 size_t AddPolygonToPath(cairo_t
* cr
, const basegfx::B2DPolygon
& rPolygon
,
147 const basegfx::B2DHomMatrix
& rObjectToDevice
, bool bPixelSnap
,
148 bool bPixelSnapHairline
)
150 // short circuit if there is nothing to do
151 const sal_uInt32
nPointCount(rPolygon
.count());
152 size_t nSizeMeasure(0);
154 if (0 == nPointCount
)
159 const bool bHasCurves(rPolygon
.areControlPointsUsed());
160 const bool bClosePath(rPolygon
.isClosed());
161 const bool bObjectToDeviceUsed(!rObjectToDevice
.isIdentity());
162 basegfx::B2DHomMatrix aObjectToDeviceInv
;
163 basegfx::B2DPoint aLast
;
164 PixelSnapper aSnapper
;
166 for (sal_uInt32 nPointIdx
= 0, nPrevIdx
= 0;; nPrevIdx
= nPointIdx
++)
168 int nClosedIdx
= nPointIdx
;
169 if (nPointIdx
>= nPointCount
)
171 // prepare to close last curve segment if needed
172 if (bClosePath
&& (nPointIdx
== nPointCount
))
182 basegfx::B2DPoint
aPoint(rPolygon
.getB2DPoint(nClosedIdx
));
186 // snap device coordinates to full pixels
187 if (bObjectToDeviceUsed
)
189 // go to DeviceCoordinates
190 aPoint
*= rObjectToDevice
;
194 aPoint
.setX(basegfx::fround(aPoint
.getX()));
195 aPoint
.setY(basegfx::fround(aPoint
.getY()));
197 if (bObjectToDeviceUsed
)
199 if (aObjectToDeviceInv
.isIdentity())
201 aObjectToDeviceInv
= rObjectToDevice
;
202 aObjectToDeviceInv
.invert();
205 // go back to ObjectCoordinates
206 aPoint
*= aObjectToDeviceInv
;
210 if (bPixelSnapHairline
)
212 // snap horizontal and vertical lines (mainly used in Chart for
214 aPoint
= aSnapper
.snap(rPolygon
, rObjectToDevice
, aObjectToDeviceInv
, nClosedIdx
);
219 // first point => just move there
220 cairo_move_to(cr
, aPoint
.getX(), aPoint
.getY());
225 bool bPendingCurve(false);
229 bPendingCurve
= rPolygon
.isNextControlPointUsed(nPrevIdx
);
230 bPendingCurve
|= rPolygon
.isPrevControlPointUsed(nClosedIdx
);
233 if (!bPendingCurve
) // line segment
235 cairo_line_to(cr
, aPoint
.getX(), aPoint
.getY());
238 else // cubic bezier segment
240 basegfx::B2DPoint aCP1
= rPolygon
.getNextControlPoint(nPrevIdx
);
241 basegfx::B2DPoint aCP2
= rPolygon
.getPrevControlPoint(nClosedIdx
);
243 // tdf#99165 if the control points are 'empty', create the mathematical
244 // correct replacement ones to avoid problems with the graphical sub-system
245 // tdf#101026 The 1st attempt to create a mathematically correct replacement control
246 // vector was wrong. Best alternative is one as close as possible which means short.
247 if (aCP1
.equal(aLast
))
249 aCP1
= aLast
+ ((aCP2
- aLast
) * 0.0005);
252 if (aCP2
.equal(aPoint
))
254 aCP2
= aPoint
+ ((aCP1
- aPoint
) * 0.0005);
257 cairo_curve_to(cr
, aCP1
.getX(), aCP1
.getY(), aCP2
.getX(), aCP2
.getY(), aPoint
.getX(),
259 // take some bigger measure for curve segments - too expensive to subdivide
260 // here and that precision not needed, but four (2 points, 2 control-points)
261 // would be a too low weight
270 cairo_close_path(cr
);
276 basegfx::B2DPoint
PixelSnapper::snap(const basegfx::B2DPolygon
& rPolygon
,
277 const basegfx::B2DHomMatrix
& rObjectToDevice
,
278 basegfx::B2DHomMatrix
& rObjectToDeviceInv
, sal_uInt32 nIndex
)
280 const sal_uInt32
nCount(rPolygon
.count());
285 // if it's the first time, we need to calculate everything
286 maPrevPoint
= rObjectToDevice
* rPolygon
.getB2DPoint((nIndex
+ nCount
- 1) % nCount
);
287 maCurrPoint
= rObjectToDevice
* rPolygon
.getB2DPoint(nIndex
);
288 maPrevTuple
= basegfx::fround(maPrevPoint
);
289 maCurrTuple
= basegfx::fround(maCurrPoint
);
293 // but for all other times, we can re-use the previous iteration computations
294 maPrevPoint
= maCurrPoint
;
295 maPrevTuple
= maCurrTuple
;
296 maCurrPoint
= maNextPoint
;
297 maCurrTuple
= maNextTuple
;
299 maNextPoint
= rObjectToDevice
* rPolygon
.getB2DPoint((nIndex
+ 1) % nCount
);
300 maNextTuple
= basegfx::fround(maNextPoint
);
303 const bool bPrevVertical(maPrevTuple
.getX() == maCurrTuple
.getX());
304 const bool bNextVertical(maNextTuple
.getX() == maCurrTuple
.getX());
305 const bool bPrevHorizontal(maPrevTuple
.getY() == maCurrTuple
.getY());
306 const bool bNextHorizontal(maNextTuple
.getY() == maCurrTuple
.getY());
307 const bool bSnapX(bPrevVertical
|| bNextVertical
);
308 const bool bSnapY(bPrevHorizontal
|| bNextHorizontal
);
310 if (bSnapX
|| bSnapY
)
312 basegfx::B2DPoint
aSnappedPoint(bSnapX
? maCurrTuple
.getX() : maCurrPoint
.getX(),
313 bSnapY
? maCurrTuple
.getY() : maCurrPoint
.getY());
315 if (rObjectToDeviceInv
.isIdentity())
317 rObjectToDeviceInv
= rObjectToDevice
;
318 rObjectToDeviceInv
.invert();
321 aSnappedPoint
*= rObjectToDeviceInv
;
323 return aSnappedPoint
;
326 return rPolygon
.getB2DPoint(nIndex
);
329 SystemDependentData_CairoPath::SystemDependentData_CairoPath(size_t nSizeMeasure
, cairo_t
* cr
,
330 bool bNoJoin
, bool bAntiAlias
,
331 const std::vector
<double>* pStroke
)
332 : basegfx::SystemDependentData(Application::GetSystemDependentDataManager())
333 , mpCairoPath(nullptr)
335 , mbAntiAlias(bAntiAlias
)
337 static const bool bFuzzing
= utl::ConfigManager::IsFuzzing();
339 // tdf#129845 only create a copy of the path when nSizeMeasure is
340 // bigger than some decent threshold
341 if (!bFuzzing
&& nSizeMeasure
> 50)
343 mpCairoPath
= cairo_copy_path(cr
);
345 if (nullptr != pStroke
)
352 SystemDependentData_CairoPath::~SystemDependentData_CairoPath()
354 if (nullptr != mpCairoPath
)
356 cairo_path_destroy(mpCairoPath
);
357 mpCairoPath
= nullptr;
361 sal_Int64
SystemDependentData_CairoPath::estimateUsageInBytes() const
363 // tdf#129845 by using the default return value of zero when no path
364 // was created, SystemDependentData::calculateCombinedHoldCyclesInSeconds
365 // will do the right thing and not buffer this entry at all
366 sal_Int64
nRetval(0);
368 if (nullptr != mpCairoPath
)
371 // - num_data incarnations of
372 // - sizeof(cairo_path_data_t) which is a union of defines and point data
373 // thus may 2 x sizeof(double)
374 nRetval
= mpCairoPath
->num_data
* sizeof(cairo_path_data_t
);
380 void add_polygon_path(cairo_t
* cr
, const basegfx::B2DPolyPolygon
& rPolyPolygon
,
381 const basegfx::B2DHomMatrix
& rObjectToDevice
, bool bPixelSnap
)
383 // try to access buffered data
384 std::shared_ptr
<SystemDependentData_CairoPath
> pSystemDependentData_CairoPath(
385 rPolyPolygon
.getSystemDependentData
<SystemDependentData_CairoPath
>());
387 if (pSystemDependentData_CairoPath
)
390 cairo_append_path(cr
, pSystemDependentData_CairoPath
->getCairoPath());
395 size_t nSizeMeasure(0);
397 for (const auto& rPoly
: rPolyPolygon
)
399 // PixelOffset used: Was dependent of 'm_aLineColor != SALCOLOR_NONE'
400 // Adapt setupPolyPolygon-users to set a linear transformation to achieve PixelOffset
401 nSizeMeasure
+= AddPolygonToPath(cr
, rPoly
, rObjectToDevice
, bPixelSnap
, false);
404 // copy and add to buffering mechanism
405 // for decisions how/what to buffer, see Note in WinSalGraphicsImpl::drawPolyPolygon
406 pSystemDependentData_CairoPath
407 = rPolyPolygon
.addOrReplaceSystemDependentData
<SystemDependentData_CairoPath
>(
408 nSizeMeasure
, cr
, false, false, nullptr);
412 cairo_user_data_key_t
* CairoCommon::getDamageKey()
414 static cairo_user_data_key_t aDamageKey
;
418 sal_uInt16
CairoCommon::GetBitCount() const
420 if (cairo_surface_get_content(m_pSurface
) == CAIRO_CONTENT_ALPHA
)
425 cairo_t
* CairoCommon::getCairoContext(bool bXorModeAllowed
, bool bAntiAlias
) const
428 if (m_ePaintMode
== PaintMode::Xor
&& bXorModeAllowed
)
429 cr
= createTmpCompatibleCairoContext();
431 cr
= cairo_create(m_pSurface
);
432 cairo_set_line_width(cr
, 1);
433 cairo_set_fill_rule(cr
, CAIRO_FILL_RULE_EVEN_ODD
);
434 cairo_set_antialias(cr
, bAntiAlias
? CAIRO_ANTIALIAS_DEFAULT
: CAIRO_ANTIALIAS_NONE
);
435 cairo_set_operator(cr
, CAIRO_OPERATOR_OVER
);
437 // ensure no linear transformation and no PathInfo in local cairo_path_t
438 cairo_identity_matrix(cr
);
444 void CairoCommon::releaseCairoContext(cairo_t
* cr
, bool bXorModeAllowed
,
445 const basegfx::B2DRange
& rExtents
) const
447 const bool bXoring
= (m_ePaintMode
== PaintMode::Xor
&& bXorModeAllowed
);
449 if (rExtents
.isEmpty())
451 //nothing changed, return early
454 cairo_surface_t
* surface
= cairo_get_target(cr
);
455 cairo_surface_destroy(surface
);
461 basegfx::B2IRange
aIntExtents(basegfx::unotools::b2ISurroundingRangeFromB2DRange(rExtents
));
462 sal_Int32
nExtentsLeft(aIntExtents
.getMinX()), nExtentsTop(aIntExtents
.getMinY());
463 sal_Int32
nExtentsRight(aIntExtents
.getMaxX()), nExtentsBottom(aIntExtents
.getMaxY());
464 sal_Int32 nWidth
= m_aFrameSize
.getX();
465 sal_Int32 nHeight
= m_aFrameSize
.getY();
466 nExtentsLeft
= std::max
<sal_Int32
>(nExtentsLeft
, 0);
467 nExtentsTop
= std::max
<sal_Int32
>(nExtentsTop
, 0);
468 nExtentsRight
= std::min
<sal_Int32
>(nExtentsRight
, nWidth
);
469 nExtentsBottom
= std::min
<sal_Int32
>(nExtentsBottom
, nHeight
);
471 cairo_surface_t
* surface
= cairo_get_target(cr
);
472 cairo_surface_flush(surface
);
474 //For the most part we avoid the use of XOR these days, but there
475 //are some edge cases where legacy stuff still supports it, so
476 //emulate it (slowly) here.
478 doXorOnRelease(nExtentsLeft
, nExtentsTop
, nExtentsRight
, nExtentsBottom
, surface
, nWidth
);
480 cairo_destroy(cr
); // unref
482 DamageHandler
* pDamage
483 = static_cast<DamageHandler
*>(cairo_surface_get_user_data(m_pSurface
, getDamageKey()));
487 pDamage
->damaged(pDamage
->handle
, nExtentsLeft
, nExtentsTop
, nExtentsRight
- nExtentsLeft
,
488 nExtentsBottom
- nExtentsTop
);
492 void CairoCommon::doXorOnRelease(sal_Int32 nExtentsLeft
, sal_Int32 nExtentsTop
,
493 sal_Int32 nExtentsRight
, sal_Int32 nExtentsBottom
,
494 cairo_surface_t
* const surface
, sal_Int32 nWidth
) const
496 //For the most part we avoid the use of XOR these days, but there
497 //are some edge cases where legacy stuff still supports it, so
498 //emulate it (slowly) here.
499 cairo_surface_t
* target_surface
= m_pSurface
;
500 if (cairo_surface_get_type(target_surface
) != CAIRO_SURFACE_TYPE_IMAGE
)
502 //in the unlikely case we can't use m_pSurface directly, copy contents
503 //to another temp image surface
504 if (cairo_surface_get_content(m_pSurface
) == CAIRO_CONTENT_COLOR_ALPHA
)
505 target_surface
= cairo_surface_map_to_image(target_surface
, nullptr);
508 // for gen, which is CAIRO_FORMAT_RGB24/CAIRO_CONTENT_COLOR I'm getting
509 // visual corruption in vcldemo with cairo_surface_map_to_image
510 cairo_t
* copycr
= createTmpCompatibleCairoContext();
511 cairo_rectangle(copycr
, nExtentsLeft
, nExtentsTop
, nExtentsRight
- nExtentsLeft
,
512 nExtentsBottom
- nExtentsTop
);
513 cairo_set_source_surface(copycr
, m_pSurface
, 0, 0);
515 target_surface
= cairo_get_target(copycr
);
516 cairo_destroy(copycr
);
520 cairo_surface_flush(target_surface
);
521 unsigned char* target_surface_data
= cairo_image_surface_get_data(target_surface
);
522 unsigned char* xor_surface_data
= cairo_image_surface_get_data(surface
);
524 cairo_format_t nFormat
= cairo_image_surface_get_format(target_surface
);
525 assert(nFormat
== CAIRO_FORMAT_ARGB32
&& "need to implement CAIRO_FORMAT_A1 after all here");
526 sal_Int32 nStride
= cairo_format_stride_for_width(nFormat
, nWidth
* m_fScale
);
527 sal_Int32 nUnscaledExtentsLeft
= nExtentsLeft
* m_fScale
;
528 sal_Int32 nUnscaledExtentsRight
= nExtentsRight
* m_fScale
;
529 sal_Int32 nUnscaledExtentsTop
= nExtentsTop
* m_fScale
;
530 sal_Int32 nUnscaledExtentsBottom
= nExtentsBottom
* m_fScale
;
532 // Handle headless size forced to (1,1) by SvpSalFrame::GetSurfaceFrameSize().
533 int target_surface_width
= cairo_image_surface_get_width(target_surface
);
534 if (nUnscaledExtentsLeft
> target_surface_width
)
535 nUnscaledExtentsLeft
= target_surface_width
;
536 if (nUnscaledExtentsRight
> target_surface_width
)
537 nUnscaledExtentsRight
= target_surface_width
;
538 int target_surface_height
= cairo_image_surface_get_height(target_surface
);
539 if (nUnscaledExtentsTop
> target_surface_height
)
540 nUnscaledExtentsTop
= target_surface_height
;
541 if (nUnscaledExtentsBottom
> target_surface_height
)
542 nUnscaledExtentsBottom
= target_surface_height
;
544 #if !ENABLE_WASM_STRIP_PREMULTIPLY
545 vcl::bitmap::lookup_table
const& unpremultiply_table
= vcl::bitmap::get_unpremultiply_table();
546 vcl::bitmap::lookup_table
const& premultiply_table
= vcl::bitmap::get_premultiply_table();
548 for (sal_Int32 y
= nUnscaledExtentsTop
; y
< nUnscaledExtentsBottom
; ++y
)
550 unsigned char* true_row
= target_surface_data
+ (nStride
* y
);
551 unsigned char* xor_row
= xor_surface_data
+ (nStride
* y
);
552 unsigned char* true_data
= true_row
+ (nUnscaledExtentsLeft
* 4);
553 unsigned char* xor_data
= xor_row
+ (nUnscaledExtentsLeft
* 4);
554 for (sal_Int32 x
= nUnscaledExtentsLeft
; x
< nUnscaledExtentsRight
; ++x
)
556 sal_uInt8 a
= true_data
[SVP_CAIRO_ALPHA
];
557 sal_uInt8 xor_a
= xor_data
[SVP_CAIRO_ALPHA
];
558 #if ENABLE_WASM_STRIP_PREMULTIPLY
559 sal_uInt8 b
= vcl::bitmap::unpremultiply(a
, true_data
[SVP_CAIRO_BLUE
])
560 ^ vcl::bitmap::unpremultiply(xor_a
, xor_data
[SVP_CAIRO_BLUE
]);
561 sal_uInt8 g
= vcl::bitmap::unpremultiply(a
, true_data
[SVP_CAIRO_GREEN
])
562 ^ vcl::bitmap::unpremultiply(xor_a
, xor_data
[SVP_CAIRO_GREEN
]);
563 sal_uInt8 r
= vcl::bitmap::unpremultiply(a
, true_data
[SVP_CAIRO_RED
])
564 ^ vcl::bitmap::unpremultiply(xor_a
, xor_data
[SVP_CAIRO_RED
]);
565 true_data
[SVP_CAIRO_BLUE
] = vcl::bitmap::premultiply(a
, b
);
566 true_data
[SVP_CAIRO_GREEN
] = vcl::bitmap::premultiply(a
, g
);
567 true_data
[SVP_CAIRO_RED
] = vcl::bitmap::premultiply(a
, r
);
569 sal_uInt8 b
= unpremultiply_table
[a
][true_data
[SVP_CAIRO_BLUE
]]
570 ^ unpremultiply_table
[xor_a
][xor_data
[SVP_CAIRO_BLUE
]];
571 sal_uInt8 g
= unpremultiply_table
[a
][true_data
[SVP_CAIRO_GREEN
]]
572 ^ unpremultiply_table
[xor_a
][xor_data
[SVP_CAIRO_GREEN
]];
573 sal_uInt8 r
= unpremultiply_table
[a
][true_data
[SVP_CAIRO_RED
]]
574 ^ unpremultiply_table
[xor_a
][xor_data
[SVP_CAIRO_RED
]];
575 true_data
[SVP_CAIRO_BLUE
] = premultiply_table
[a
][b
];
576 true_data
[SVP_CAIRO_GREEN
] = premultiply_table
[a
][g
];
577 true_data
[SVP_CAIRO_RED
] = premultiply_table
[a
][r
];
583 cairo_surface_mark_dirty(target_surface
);
585 if (target_surface
!= m_pSurface
)
587 if (cairo_surface_get_content(m_pSurface
) == CAIRO_CONTENT_COLOR_ALPHA
)
588 cairo_surface_unmap_image(m_pSurface
, target_surface
);
591 cairo_t
* copycr
= cairo_create(m_pSurface
);
592 //copy contents back from image surface
593 cairo_rectangle(copycr
, nExtentsLeft
, nExtentsTop
, nExtentsRight
- nExtentsLeft
,
594 nExtentsBottom
- nExtentsTop
);
595 cairo_set_source_surface(copycr
, target_surface
, 0, 0);
597 cairo_destroy(copycr
);
598 cairo_surface_destroy(target_surface
);
602 cairo_surface_destroy(surface
);
605 cairo_t
* CairoCommon::createTmpCompatibleCairoContext() const
607 cairo_surface_t
* target
= cairo_surface_create_similar_image(m_pSurface
, CAIRO_FORMAT_ARGB32
,
608 m_aFrameSize
.getX() * m_fScale
,
609 m_aFrameSize
.getY() * m_fScale
);
611 dl_cairo_surface_set_device_scale(target
, m_fScale
, m_fScale
);
613 return cairo_create(target
);
616 void CairoCommon::applyColor(cairo_t
* cr
, Color aColor
, double fTransparency
)
618 if (cairo_surface_get_content(cairo_get_target(cr
)) != CAIRO_CONTENT_ALPHA
)
620 cairo_set_source_rgba(cr
, aColor
.GetRed() / 255.0, aColor
.GetGreen() / 255.0,
621 aColor
.GetBlue() / 255.0, 1.0 - fTransparency
);
625 double fSet
= aColor
== COL_BLACK
? 1.0 : 0.0;
626 cairo_set_source_rgba(cr
, 1, 1, 1, fSet
);
627 cairo_set_operator(cr
, CAIRO_OPERATOR_SOURCE
);
631 void CairoCommon::clipRegion(cairo_t
* cr
, const vcl::Region
& rClipRegion
)
633 RectangleVector aRectangles
;
634 if (!rClipRegion
.IsEmpty())
636 rClipRegion
.GetRegionRectangles(aRectangles
);
638 if (!aRectangles
.empty())
641 for (auto const& rectangle
: aRectangles
)
643 if (rectangle
.GetWidth() <= 0 || rectangle
.GetHeight() <= 0)
645 SAL_WARN("vcl.gdi", "bad clip rect of: " << rectangle
);
648 cairo_rectangle(cr
, rectangle
.Left(), rectangle
.Top(), rectangle
.GetWidth(),
649 rectangle
.GetHeight());
657 void CairoCommon::clipRegion(cairo_t
* cr
) { CairoCommon::clipRegion(cr
, m_aClipRegion
); }
659 void CairoCommon::SetXORMode(bool bSet
, bool /*bInvertOnly*/)
661 m_ePaintMode
= bSet
? PaintMode::Xor
: PaintMode::Over
;
664 void CairoCommon::SetROPLineColor(SalROPColor nROPColor
)
668 case SalROPColor::N0
:
669 m_oLineColor
= Color(0, 0, 0);
671 case SalROPColor::N1
:
672 m_oLineColor
= Color(0xff, 0xff, 0xff);
674 case SalROPColor::Invert
:
675 m_oLineColor
= Color(0xff, 0xff, 0xff);
680 void CairoCommon::SetROPFillColor(SalROPColor nROPColor
)
684 case SalROPColor::N0
:
685 m_oFillColor
= Color(0, 0, 0);
687 case SalROPColor::N1
:
688 m_oFillColor
= Color(0xff, 0xff, 0xff);
690 case SalROPColor::Invert
:
691 m_oFillColor
= Color(0xff, 0xff, 0xff);
696 void CairoCommon::drawPixel(const std::optional
<Color
>& rLineColor
, tools::Long nX
, tools::Long nY
,
702 cairo_t
* cr
= getCairoContext(true, bAntiAlias
);
705 cairo_rectangle(cr
, nX
, nY
, 1, 1);
706 CairoCommon::applyColor(cr
, *rLineColor
, 0.0);
709 basegfx::B2DRange extents
= getClippedFillDamage(cr
);
710 releaseCairoContext(cr
, true, extents
);
713 Color
CairoCommon::getPixel(cairo_surface_t
* pSurface
, tools::Long nX
, tools::Long nY
)
715 cairo_surface_t
* target
716 = cairo_surface_create_similar_image(pSurface
, CAIRO_FORMAT_ARGB32
, 1, 1);
718 cairo_t
* cr
= cairo_create(target
);
720 cairo_rectangle(cr
, 0, 0, 1, 1);
721 cairo_set_source_surface(cr
, pSurface
, -nX
, -nY
);
725 cairo_surface_flush(target
);
726 #if !ENABLE_WASM_STRIP_PREMULTIPLY
727 vcl::bitmap::lookup_table
const& unpremultiply_table
= vcl::bitmap::get_unpremultiply_table();
729 unsigned char* data
= cairo_image_surface_get_data(target
);
730 sal_uInt8 a
= data
[SVP_CAIRO_ALPHA
];
731 #if ENABLE_WASM_STRIP_PREMULTIPLY
732 sal_uInt8 b
= vcl::bitmap::unpremultiply(a
, data
[SVP_CAIRO_BLUE
]);
733 sal_uInt8 g
= vcl::bitmap::unpremultiply(a
, data
[SVP_CAIRO_GREEN
]);
734 sal_uInt8 r
= vcl::bitmap::unpremultiply(a
, data
[SVP_CAIRO_RED
]);
736 sal_uInt8 b
= unpremultiply_table
[a
][data
[SVP_CAIRO_BLUE
]];
737 sal_uInt8 g
= unpremultiply_table
[a
][data
[SVP_CAIRO_GREEN
]];
738 sal_uInt8 r
= unpremultiply_table
[a
][data
[SVP_CAIRO_RED
]];
740 Color
aColor(ColorAlpha
, a
, r
, g
, b
);
741 cairo_surface_destroy(target
);
746 void CairoCommon::drawLine(tools::Long nX1
, tools::Long nY1
, tools::Long nX2
, tools::Long nY2
,
749 cairo_t
* cr
= getCairoContext(false, bAntiAlias
);
752 basegfx::B2DPolygon aPoly
;
754 // PixelOffset used: To not mix with possible PixelSnap, cannot do
755 // directly on coordinates as tried before - despite being already 'snapped'
756 // due to being integer. If it would be directly added here, it would be
757 // 'snapped' again when !getAntiAlias(), losing the (0.5, 0.5) offset
758 aPoly
.append(basegfx::B2DPoint(nX1
, nY1
));
759 aPoly
.append(basegfx::B2DPoint(nX2
, nY2
));
761 // PixelOffset used: Set PixelOffset as linear transformation
762 cairo_matrix_t aMatrix
;
763 cairo_matrix_init_translate(&aMatrix
, 0.5, 0.5);
764 cairo_set_matrix(cr
, &aMatrix
);
766 AddPolygonToPath(cr
, aPoly
, basegfx::B2DHomMatrix(), !bAntiAlias
, false);
768 CairoCommon::applyColor(cr
, *m_oLineColor
);
770 basegfx::B2DRange extents
= getClippedStrokeDamage(cr
);
771 extents
.transform(basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5));
775 releaseCairoContext(cr
, false, extents
);
778 // true if we have a fill color and the line color is the same or non-existent
779 static bool onlyFillRect(const std::optional
<Color
>& rFillColor
,
780 const std::optional
<Color
>& rLineColor
)
786 return *rFillColor
== *rLineColor
;
789 void CairoCommon::drawRect(double nX
, double nY
, double nWidth
, double nHeight
, bool bAntiAlias
)
791 // fast path for the common case of simply creating a solid block of color
792 if (onlyFillRect(m_oFillColor
, m_oLineColor
))
794 double fTransparency
= 0;
795 // don't bother trying to draw stuff which is effectively invisible
796 if (nWidth
< 0.1 || nHeight
< 0.1)
799 cairo_t
* cr
= getCairoContext(true, bAntiAlias
);
802 bool bPixelSnap
= !bAntiAlias
;
806 nX
= basegfx::fround(nX
);
807 nY
= basegfx::fround(nY
);
808 nWidth
= basegfx::fround(nWidth
);
809 nHeight
= basegfx::fround(nHeight
);
811 cairo_rectangle(cr
, nX
, nY
, nWidth
, nHeight
);
813 CairoCommon::applyColor(cr
, *m_oFillColor
, fTransparency
);
815 basegfx::B2DRange extents
= getClippedFillDamage(cr
);
819 releaseCairoContext(cr
, true, extents
);
823 // because of the -1 hack we have to do fill and draw separately
824 std::optional
<Color
> aOrigFillColor
= m_oFillColor
;
825 std::optional
<Color
> aOrigLineColor
= m_oLineColor
;
826 m_oFillColor
= std::nullopt
;
827 m_oLineColor
= std::nullopt
;
831 basegfx::B2DPolygon aRect
= basegfx::utils::createPolygonFromRect(
832 basegfx::B2DRectangle(nX
, nY
, nX
+ nWidth
, nY
+ nHeight
));
834 m_oFillColor
= aOrigFillColor
;
835 drawPolyPolygon(basegfx::B2DHomMatrix(), basegfx::B2DPolyPolygon(aRect
), 0.0, bAntiAlias
);
836 m_oFillColor
= std::nullopt
;
841 // need -1 hack to exclude the bottom and right edges to act like wingdi "Rectangle"
842 // function which is what was probably the ultimate origin of this behavior
843 basegfx::B2DPolygon aRect
= basegfx::utils::createPolygonFromRect(
844 basegfx::B2DRectangle(nX
, nY
, nX
+ nWidth
- 1, nY
+ nHeight
- 1));
846 m_oLineColor
= aOrigLineColor
;
847 drawPolyPolygon(basegfx::B2DHomMatrix(), basegfx::B2DPolyPolygon(aRect
), 0.0, bAntiAlias
);
848 m_oLineColor
= std::nullopt
;
851 m_oFillColor
= aOrigFillColor
;
852 m_oLineColor
= aOrigLineColor
;
855 void CairoCommon::drawPolygon(sal_uInt32 nPoints
, const Point
* pPtAry
, bool bAntiAlias
)
857 basegfx::B2DPolygon aPoly
;
858 aPoly
.append(basegfx::B2DPoint(pPtAry
->getX(), pPtAry
->getY()), nPoints
);
859 for (sal_uInt32 i
= 1; i
< nPoints
; ++i
)
860 aPoly
.setB2DPoint(i
, basegfx::B2DPoint(pPtAry
[i
].getX(), pPtAry
[i
].getY()));
862 drawPolyPolygon(basegfx::B2DHomMatrix(), basegfx::B2DPolyPolygon(aPoly
), 0.0, bAntiAlias
);
865 void CairoCommon::drawPolyPolygon(sal_uInt32 nPoly
, const sal_uInt32
* pPointCounts
,
866 const Point
** pPtAry
, bool bAntiAlias
)
868 basegfx::B2DPolyPolygon aPolyPoly
;
869 for (sal_uInt32 nPolygon
= 0; nPolygon
< nPoly
; ++nPolygon
)
871 sal_uInt32 nPoints
= pPointCounts
[nPolygon
];
874 const Point
* pPoints
= pPtAry
[nPolygon
];
875 basegfx::B2DPolygon aPoly
;
876 aPoly
.append(basegfx::B2DPoint(pPoints
->getX(), pPoints
->getY()), nPoints
);
877 for (sal_uInt32 i
= 1; i
< nPoints
; ++i
)
878 aPoly
.setB2DPoint(i
, basegfx::B2DPoint(pPoints
[i
].getX(), pPoints
[i
].getY()));
880 aPolyPoly
.append(aPoly
);
884 drawPolyPolygon(basegfx::B2DHomMatrix(), aPolyPoly
, 0.0, bAntiAlias
);
887 void CairoCommon::drawPolyPolygon(const basegfx::B2DHomMatrix
& rObjectToDevice
,
888 const basegfx::B2DPolyPolygon
& rPolyPolygon
, double fTransparency
,
891 const bool bHasFill(m_oFillColor
.has_value());
892 const bool bHasLine(m_oLineColor
.has_value());
894 if (0 == rPolyPolygon
.count() || !(bHasFill
|| bHasLine
) || fTransparency
< 0.0
895 || fTransparency
>= 1.0)
902 // don't bother trying to draw stuff which is effectively invisible, speeds up
903 // drawing some complex drawings. This optimisation is not valid when we do
904 // the pixel offset thing (i.e. bHasLine)
905 basegfx::B2DRange aPolygonRange
= rPolyPolygon
.getB2DRange();
906 aPolygonRange
.transform(rObjectToDevice
);
907 if (aPolygonRange
.getWidth() < 0.1 || aPolygonRange
.getHeight() < 0.1)
911 cairo_t
* cr
= getCairoContext(true, bAntiAlias
);
912 if (cairo_status(cr
) != CAIRO_STATUS_SUCCESS
)
915 "cannot render to surface: " << cairo_status_to_string(cairo_status(cr
)));
916 releaseCairoContext(cr
, true, basegfx::B2DRange());
921 // Set full (Object-to-Device) transformation - if used
922 if (!rObjectToDevice
.isIdentity())
924 cairo_matrix_t aMatrix
;
926 cairo_matrix_init(&aMatrix
, rObjectToDevice
.get(0, 0), rObjectToDevice
.get(1, 0),
927 rObjectToDevice
.get(0, 1), rObjectToDevice
.get(1, 1),
928 rObjectToDevice
.get(0, 2), rObjectToDevice
.get(1, 2));
929 cairo_set_matrix(cr
, &aMatrix
);
932 // To make releaseCairoContext work, use empty extents
933 basegfx::B2DRange extents
;
937 add_polygon_path(cr
, rPolyPolygon
, rObjectToDevice
, !bAntiAlias
);
939 CairoCommon::applyColor(cr
, *m_oFillColor
, fTransparency
);
940 // Get FillDamage (will be extended for LineDamage below)
941 extents
= getClippedFillDamage(cr
);
948 // PixelOffset used: Set PixelOffset as linear transformation
949 cairo_matrix_t aMatrix
;
950 cairo_matrix_init_translate(&aMatrix
, 0.5, 0.5);
951 cairo_set_matrix(cr
, &aMatrix
);
953 add_polygon_path(cr
, rPolyPolygon
, rObjectToDevice
, !bAntiAlias
);
955 CairoCommon::applyColor(cr
, *m_oLineColor
, fTransparency
);
957 // expand with possible StrokeDamage
958 basegfx::B2DRange stroke_extents
= getClippedStrokeDamage(cr
);
959 stroke_extents
.transform(basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5));
960 extents
.expand(stroke_extents
);
965 // if transformation has been applied, transform also extents (ranges)
966 // of damage so they can be correctly redrawn
967 extents
.transform(rObjectToDevice
);
968 releaseCairoContext(cr
, true, extents
);
971 void CairoCommon::drawPolyLine(sal_uInt32 nPoints
, const Point
* pPtAry
, bool bAntiAlias
)
973 basegfx::B2DPolygon aPoly
;
974 aPoly
.append(basegfx::B2DPoint(pPtAry
->getX(), pPtAry
->getY()), nPoints
);
975 for (sal_uInt32 i
= 1; i
< nPoints
; ++i
)
976 aPoly
.setB2DPoint(i
, basegfx::B2DPoint(pPtAry
[i
].getX(), pPtAry
[i
].getY()));
977 aPoly
.setClosed(false);
979 drawPolyLine(basegfx::B2DHomMatrix(), aPoly
, 0.0, 1.0, nullptr, basegfx::B2DLineJoin::Miter
,
980 css::drawing::LineCap_BUTT
, basegfx::deg2rad(15.0) /*default*/, false, bAntiAlias
);
983 bool CairoCommon::drawPolyLine(const basegfx::B2DHomMatrix
& rObjectToDevice
,
984 const basegfx::B2DPolygon
& rPolyLine
, double fTransparency
,
985 double fLineWidth
, const std::vector
<double>* pStroke
,
986 basegfx::B2DLineJoin eLineJoin
, css::drawing::LineCap eLineCap
,
987 double fMiterMinimumAngle
, bool bPixelSnapHairline
, bool bAntiAlias
)
989 // short circuit if there is nothing to do
990 if (0 == rPolyLine
.count() || fTransparency
< 0.0 || fTransparency
>= 1.0)
995 static const bool bFuzzing
= utl::ConfigManager::IsFuzzing();
998 const basegfx::B2DRange
aRange(basegfx::utils::getRange(rPolyLine
));
999 if (aRange
.getMaxX() - aRange
.getMinX() > 0x10000000
1000 || aRange
.getMaxY() - aRange
.getMinY() > 0x10000000)
1002 SAL_WARN("vcl.gdi", "drawPolyLine, skipping suspicious range of: "
1003 << aRange
<< " for fuzzing performance");
1008 cairo_t
* cr
= getCairoContext(false, bAntiAlias
);
1011 // need to check/handle LineWidth when ObjectToDevice transformation is used
1012 const bool bObjectToDeviceIsIdentity(rObjectToDevice
.isIdentity());
1014 // tdf#124848 calculate-back logical LineWidth for a hairline
1015 // since this implementation hands over the transformation to
1016 // the graphic sub-system
1017 if (fLineWidth
== 0)
1021 if (!bObjectToDeviceIsIdentity
)
1023 basegfx::B2DHomMatrix
aObjectToDeviceInv(rObjectToDevice
);
1024 aObjectToDeviceInv
.invert();
1025 fLineWidth
= (aObjectToDeviceInv
* basegfx::B2DVector(fLineWidth
, 0)).getLength();
1029 // PixelOffset used: Need to reflect in linear transformation
1030 cairo_matrix_t aMatrix
;
1031 basegfx::B2DHomMatrix
aDamageMatrix(basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5));
1033 if (bObjectToDeviceIsIdentity
)
1035 // Set PixelOffset as requested
1036 cairo_matrix_init_translate(&aMatrix
, 0.5, 0.5);
1040 // Prepare ObjectToDevice transformation. Take PixelOffset for Lines into
1041 // account: Multiply from left to act in DeviceCoordinates
1042 aDamageMatrix
= aDamageMatrix
* rObjectToDevice
;
1043 cairo_matrix_init(&aMatrix
, aDamageMatrix
.get(0, 0), aDamageMatrix
.get(1, 0),
1044 aDamageMatrix
.get(0, 1), aDamageMatrix
.get(1, 1), aDamageMatrix
.get(0, 2),
1045 aDamageMatrix
.get(1, 2));
1048 // set linear transformation
1049 cairo_set_matrix(cr
, &aMatrix
);
1051 // setup line attributes
1052 cairo_line_join_t eCairoLineJoin
= CAIRO_LINE_JOIN_MITER
;
1055 case basegfx::B2DLineJoin::Bevel
:
1056 eCairoLineJoin
= CAIRO_LINE_JOIN_BEVEL
;
1058 case basegfx::B2DLineJoin::Round
:
1059 eCairoLineJoin
= CAIRO_LINE_JOIN_ROUND
;
1061 case basegfx::B2DLineJoin::NONE
:
1062 case basegfx::B2DLineJoin::Miter
:
1063 eCairoLineJoin
= CAIRO_LINE_JOIN_MITER
;
1067 // convert miter minimum angle to miter limit
1068 double fMiterLimit
= 1.0 / sin(std::max(fMiterMinimumAngle
, 0.01 * M_PI
) / 2.0);
1070 // setup cap attribute
1071 cairo_line_cap_t
eCairoLineCap(CAIRO_LINE_CAP_BUTT
);
1075 default: // css::drawing::LineCap_BUTT:
1077 eCairoLineCap
= CAIRO_LINE_CAP_BUTT
;
1080 case css::drawing::LineCap_ROUND
:
1082 eCairoLineCap
= CAIRO_LINE_CAP_ROUND
;
1085 case css::drawing::LineCap_SQUARE
:
1087 eCairoLineCap
= CAIRO_LINE_CAP_SQUARE
;
1092 cairo_set_source_rgba(cr
, m_oLineColor
->GetRed() / 255.0, m_oLineColor
->GetGreen() / 255.0,
1093 m_oLineColor
->GetBlue() / 255.0, 1.0 - fTransparency
);
1095 cairo_set_line_join(cr
, eCairoLineJoin
);
1096 cairo_set_line_cap(cr
, eCairoLineCap
);
1098 constexpr int MaxNormalLineWidth
= 64;
1099 if (fLineWidth
> MaxNormalLineWidth
)
1101 const double fLineWidthPixel
1102 = bObjectToDeviceIsIdentity
1104 : (rObjectToDevice
* basegfx::B2DVector(fLineWidth
, 0)).getLength();
1105 if (fLineWidthPixel
> MaxNormalLineWidth
)
1107 SAL_WARN("vcl.gdi", "drawPolyLine, suspicious input line width of: "
1108 << fLineWidth
<< ", will be " << fLineWidthPixel
1109 << " pixels thick");
1112 basegfx::B2DHomMatrix
aObjectToDeviceInv(rObjectToDevice
);
1113 aObjectToDeviceInv
.invert();
1115 = (aObjectToDeviceInv
* basegfx::B2DVector(MaxNormalLineWidth
, 0)).getLength();
1116 fLineWidth
= std::min(fLineWidth
, 2048.0);
1120 cairo_set_line_width(cr
, fLineWidth
);
1121 cairo_set_miter_limit(cr
, fMiterLimit
);
1123 // try to access buffered data
1124 std::shared_ptr
<SystemDependentData_CairoPath
> pSystemDependentData_CairoPath(
1125 rPolyLine
.getSystemDependentData
<SystemDependentData_CairoPath
>());
1127 // MM01 need to do line dashing as fallback stuff here now
1128 const double fDotDashLength(
1129 nullptr != pStroke
? std::accumulate(pStroke
->begin(), pStroke
->end(), 0.0) : 0.0);
1130 const bool bStrokeUsed(0.0 != fDotDashLength
);
1131 assert(!bStrokeUsed
|| (bStrokeUsed
&& pStroke
));
1133 // MM01 decide if to stroke directly
1134 static const bool bDoDirectCairoStroke(true);
1136 // MM01 activate to stroke directly
1137 if (bDoDirectCairoStroke
&& bStrokeUsed
)
1139 cairo_set_dash(cr
, pStroke
->data(), pStroke
->size(), 0.0);
1142 if (!bDoDirectCairoStroke
&& pSystemDependentData_CairoPath
)
1144 // MM01 - check on stroke change. Used against not used, or if both used,
1145 // equal or different?
1146 const bool bStrokeWasUsed(!pSystemDependentData_CairoPath
->getStroke().empty());
1148 if (bStrokeWasUsed
!= bStrokeUsed
1149 || (bStrokeUsed
&& *pStroke
!= pSystemDependentData_CairoPath
->getStroke()))
1151 // data invalid, forget
1152 pSystemDependentData_CairoPath
.reset();
1156 // check for basegfx::B2DLineJoin::NONE to react accordingly
1158 (basegfx::B2DLineJoin::NONE
== eLineJoin
&& basegfx::fTools::more(fLineWidth
, 0.0)));
1160 if (pSystemDependentData_CairoPath
)
1162 // check data validity
1163 if (nullptr == pSystemDependentData_CairoPath
->getCairoPath()
1164 || pSystemDependentData_CairoPath
->getNoJoin() != bNoJoin
1165 || pSystemDependentData_CairoPath
->getAntiAlias() != bAntiAlias
1166 || bPixelSnapHairline
/*tdf#124700*/)
1168 // data invalid, forget
1169 pSystemDependentData_CairoPath
.reset();
1173 if (pSystemDependentData_CairoPath
)
1176 cairo_append_path(cr
, pSystemDependentData_CairoPath
->getCairoPath());
1181 size_t nSizeMeasure(0);
1183 // MM01 need to do line dashing as fallback stuff here now
1184 basegfx::B2DPolyPolygon aPolyPolygonLine
;
1186 if (!bDoDirectCairoStroke
&& bStrokeUsed
)
1189 basegfx::utils::applyLineDashing(rPolyLine
, // source
1190 *pStroke
, // pattern
1191 &aPolyPolygonLine
, // target for lines
1192 nullptr, // target for gaps
1193 fDotDashLength
); // full length if available
1197 // no line dashing or direct stroke, just copy
1198 aPolyPolygonLine
.append(rPolyLine
);
1201 // MM01 checked/verified for Cairo
1202 for (sal_uInt32
a(0); a
< aPolyPolygonLine
.count(); a
++)
1204 const basegfx::B2DPolygon
aPolyLine(aPolyPolygonLine
.getB2DPolygon(a
));
1208 // PixelOffset now reflected in linear transformation used
1210 += AddPolygonToPath(cr
, aPolyLine
,
1211 rObjectToDevice
, // ObjectToDevice *without* LineDraw-Offset
1212 !bAntiAlias
, bPixelSnapHairline
);
1216 const sal_uInt32
nPointCount(aPolyLine
.count());
1217 const sal_uInt32
nEdgeCount(aPolyLine
.isClosed() ? nPointCount
: nPointCount
- 1);
1218 basegfx::B2DPolygon aEdge
;
1220 aEdge
.append(aPolyLine
.getB2DPoint(0));
1221 aEdge
.append(basegfx::B2DPoint(0.0, 0.0));
1223 for (sal_uInt32
i(0); i
< nEdgeCount
; i
++)
1225 const sal_uInt32
nNextIndex((i
+ 1) % nPointCount
);
1226 aEdge
.setB2DPoint(1, aPolyLine
.getB2DPoint(nNextIndex
));
1227 aEdge
.setNextControlPoint(0, aPolyLine
.getNextControlPoint(i
));
1228 aEdge
.setPrevControlPoint(1, aPolyLine
.getPrevControlPoint(nNextIndex
));
1230 // PixelOffset now reflected in linear transformation used
1231 nSizeMeasure
+= AddPolygonToPath(
1233 rObjectToDevice
, // ObjectToDevice *without* LineDraw-Offset
1234 !bAntiAlias
, bPixelSnapHairline
);
1236 // prepare next step
1237 aEdge
.setB2DPoint(0, aEdge
.getB2DPoint(1));
1242 // copy and add to buffering mechanism
1243 if (!bPixelSnapHairline
/*tdf#124700*/)
1245 pSystemDependentData_CairoPath
1246 = rPolyLine
.addOrReplaceSystemDependentData
<SystemDependentData_CairoPath
>(
1247 nSizeMeasure
, cr
, bNoJoin
, bAntiAlias
, pStroke
);
1252 basegfx::B2DRange extents
= getClippedStrokeDamage(cr
);
1253 // transform also extents (ranges) of damage so they can be correctly redrawn
1254 extents
.transform(aDamageMatrix
);
1259 releaseCairoContext(cr
, false, extents
);
1264 bool CairoCommon::drawAlphaRect(tools::Long nX
, tools::Long nY
, tools::Long nWidth
,
1265 tools::Long nHeight
, sal_uInt8 nTransparency
, bool bAntiAlias
)
1267 const bool bHasFill(m_oFillColor
.has_value());
1268 const bool bHasLine(m_oLineColor
.has_value());
1270 if (!bHasFill
&& !bHasLine
)
1273 cairo_t
* cr
= getCairoContext(false, bAntiAlias
);
1276 const double fTransparency
= nTransparency
* (1.0 / 100);
1278 // To make releaseCairoContext work, use empty extents
1279 basegfx::B2DRange extents
;
1283 cairo_rectangle(cr
, nX
, nY
, nWidth
, nHeight
);
1285 applyColor(cr
, *m_oFillColor
, fTransparency
);
1288 extents
= getClippedFillDamage(cr
);
1295 // PixelOffset used: Set PixelOffset as linear transformation
1296 // Note: Was missing here - probably not by purpose (?)
1297 cairo_matrix_t aMatrix
;
1298 cairo_matrix_init_translate(&aMatrix
, 0.5, 0.5);
1299 cairo_set_matrix(cr
, &aMatrix
);
1301 cairo_rectangle(cr
, nX
, nY
, nWidth
, nHeight
);
1303 applyColor(cr
, *m_oLineColor
, fTransparency
);
1305 // expand with possible StrokeDamage
1306 basegfx::B2DRange stroke_extents
= getClippedStrokeDamage(cr
);
1307 stroke_extents
.transform(basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5));
1308 extents
.expand(stroke_extents
);
1313 releaseCairoContext(cr
, false, extents
);
1318 bool CairoCommon::drawGradient(const tools::PolyPolygon
& rPolyPolygon
, const Gradient
& rGradient
,
1321 if (rGradient
.GetStyle() != css::awt::GradientStyle_LINEAR
1322 && rGradient
.GetStyle() != css::awt::GradientStyle_RADIAL
)
1323 return false; // unsupported
1324 if (rGradient
.GetSteps() != 0)
1325 return false; // We can't tell cairo how many colors to use in the gradient.
1327 cairo_t
* cr
= getCairoContext(true, bAntiAlias
);
1330 tools::Rectangle
aInputRect(rPolyPolygon
.GetBoundRect());
1331 if (rPolyPolygon
.IsRect())
1333 // Rect->Polygon conversion loses the right and bottom edge, fix that.
1334 aInputRect
.AdjustRight(1);
1335 aInputRect
.AdjustBottom(1);
1336 basegfx::B2DHomMatrix rObjectToDevice
;
1337 AddPolygonToPath(cr
, tools::Polygon(aInputRect
).getB2DPolygon(), rObjectToDevice
,
1338 !bAntiAlias
, false);
1342 basegfx::B2DPolyPolygon
aB2DPolyPolygon(rPolyPolygon
.getB2DPolyPolygon());
1343 for (auto const& rPolygon
: std::as_const(aB2DPolyPolygon
))
1345 basegfx::B2DHomMatrix rObjectToDevice
;
1346 AddPolygonToPath(cr
, rPolygon
, rObjectToDevice
, !bAntiAlias
, false);
1350 Gradient
aGradient(rGradient
);
1352 tools::Rectangle aBoundRect
;
1355 aGradient
.SetAngle(aGradient
.GetAngle() + 2700_deg10
);
1356 aGradient
.GetBoundRect(aInputRect
, aBoundRect
, aCenter
);
1357 Color aStartColor
= aGradient
.GetStartColor();
1358 Color aEndColor
= aGradient
.GetEndColor();
1360 cairo_pattern_t
* pattern
;
1361 if (rGradient
.GetStyle() == css::awt::GradientStyle_LINEAR
)
1363 tools::Polygon
aPoly(aBoundRect
);
1364 aPoly
.Rotate(aCenter
, aGradient
.GetAngle() % 3600_deg10
);
1366 = cairo_pattern_create_linear(aPoly
[0].X(), aPoly
[0].Y(), aPoly
[1].X(), aPoly
[1].Y());
1370 double radius
= std::max(aBoundRect
.GetWidth() / 2.0, aBoundRect
.GetHeight() / 2.0);
1371 // Move the center a bit to the top-left (the default VCL algorithm is a bit off-center that way,
1372 // cairo is the opposite way).
1373 pattern
= cairo_pattern_create_radial(aCenter
.X() - 0.5, aCenter
.Y() - 0.5, 0,
1374 aCenter
.X() - 0.5, aCenter
.Y() - 0.5, radius
);
1375 std::swap(aStartColor
, aEndColor
);
1378 cairo_pattern_add_color_stop_rgba(
1379 pattern
, aGradient
.GetBorder() / 100.0,
1380 aStartColor
.GetRed() * aGradient
.GetStartIntensity() / 25500.0,
1381 aStartColor
.GetGreen() * aGradient
.GetStartIntensity() / 25500.0,
1382 aStartColor
.GetBlue() * aGradient
.GetStartIntensity() / 25500.0, 1.0);
1384 cairo_pattern_add_color_stop_rgba(
1385 pattern
, 1.0, aEndColor
.GetRed() * aGradient
.GetEndIntensity() / 25500.0,
1386 aEndColor
.GetGreen() * aGradient
.GetEndIntensity() / 25500.0,
1387 aEndColor
.GetBlue() * aGradient
.GetEndIntensity() / 25500.0, 1.0);
1389 cairo_set_source(cr
, pattern
);
1390 cairo_pattern_destroy(pattern
);
1392 basegfx::B2DRange extents
= getClippedFillDamage(cr
);
1393 cairo_fill_preserve(cr
);
1395 releaseCairoContext(cr
, true, extents
);
1400 bool CairoCommon::implDrawGradient(basegfx::B2DPolyPolygon
const& rPolyPolygon
,
1401 SalGradient
const& rGradient
, bool bAntiAlias
)
1403 cairo_t
* cr
= getCairoContext(true, bAntiAlias
);
1405 basegfx::B2DHomMatrix rObjectToDevice
;
1407 for (auto const& rPolygon
: rPolyPolygon
)
1408 AddPolygonToPath(cr
, rPolygon
, rObjectToDevice
, !bAntiAlias
, false);
1410 cairo_pattern_t
* pattern
1411 = cairo_pattern_create_linear(rGradient
.maPoint1
.getX(), rGradient
.maPoint1
.getY(),
1412 rGradient
.maPoint2
.getX(), rGradient
.maPoint2
.getY());
1414 for (SalGradientStop
const& rStop
: rGradient
.maStops
)
1416 double r
= rStop
.maColor
.GetRed() / 255.0;
1417 double g
= rStop
.maColor
.GetGreen() / 255.0;
1418 double b
= rStop
.maColor
.GetBlue() / 255.0;
1419 double a
= rStop
.maColor
.GetAlpha() / 255.0;
1420 double offset
= rStop
.mfOffset
;
1422 cairo_pattern_add_color_stop_rgba(pattern
, offset
, r
, g
, b
, a
);
1424 cairo_set_source(cr
, pattern
);
1425 cairo_pattern_destroy(pattern
);
1427 basegfx::B2DRange extents
= getClippedFillDamage(cr
);
1429 cairo_fill_preserve(cr
);
1431 releaseCairoContext(cr
, true, extents
);
1438 basegfx::B2DRange
renderWithOperator(cairo_t
* cr
, const SalTwoRect
& rTR
, cairo_surface_t
* source
,
1439 cairo_operator_t eOperator
= CAIRO_OPERATOR_SOURCE
)
1441 cairo_rectangle(cr
, rTR
.mnDestX
, rTR
.mnDestY
, rTR
.mnDestWidth
, rTR
.mnDestHeight
);
1443 basegfx::B2DRange extents
= getClippedFillDamage(cr
);
1447 cairo_translate(cr
, rTR
.mnDestX
, rTR
.mnDestY
);
1448 if (rTR
.mnSrcWidth
!= 0 && rTR
.mnSrcHeight
!= 0)
1450 double fXScale
= static_cast<double>(rTR
.mnDestWidth
) / rTR
.mnSrcWidth
;
1451 double fYScale
= static_cast<double>(rTR
.mnDestHeight
) / rTR
.mnSrcHeight
;
1452 cairo_scale(cr
, fXScale
, fYScale
);
1456 cairo_set_source_surface(cr
, source
, -rTR
.mnSrcX
, -rTR
.mnSrcY
);
1458 if (cairo_status(cr
) == CAIRO_STATUS_SUCCESS
)
1460 //tdf#133716 borders of upscaled images should not be blurred
1461 cairo_pattern_t
* sourcepattern
= cairo_get_source(cr
);
1462 cairo_pattern_set_extend(sourcepattern
, CAIRO_EXTEND_PAD
);
1465 cairo_set_operator(cr
, eOperator
);
1472 } // end anonymous ns
1474 basegfx::B2DRange
CairoCommon::renderSource(cairo_t
* cr
, const SalTwoRect
& rTR
,
1475 cairo_surface_t
* source
)
1477 return renderWithOperator(cr
, rTR
, source
, CAIRO_OPERATOR_SOURCE
);
1480 void CairoCommon::copyWithOperator(const SalTwoRect
& rTR
, cairo_surface_t
* source
,
1481 cairo_operator_t eOp
, bool bAntiAlias
)
1483 cairo_t
* cr
= getCairoContext(false, bAntiAlias
);
1486 basegfx::B2DRange extents
= renderWithOperator(cr
, rTR
, source
, eOp
);
1488 releaseCairoContext(cr
, false, extents
);
1491 void CairoCommon::copySource(const SalTwoRect
& rTR
, cairo_surface_t
* source
, bool bAntiAlias
)
1493 copyWithOperator(rTR
, source
, CAIRO_OPERATOR_SOURCE
, bAntiAlias
);
1496 void CairoCommon::copyBitsCairo(const SalTwoRect
& rTR
, cairo_surface_t
* pSourceSurface
,
1499 SalTwoRect
aTR(rTR
);
1501 cairo_surface_t
* pCopy
= nullptr;
1503 if (pSourceSurface
== getSurface())
1505 //self copy is a problem, so dup source in that case
1507 = cairo_surface_create_similar(pSourceSurface
, cairo_surface_get_content(getSurface()),
1508 aTR
.mnSrcWidth
* m_fScale
, aTR
.mnSrcHeight
* m_fScale
);
1509 dl_cairo_surface_set_device_scale(pCopy
, m_fScale
, m_fScale
);
1510 cairo_t
* cr
= cairo_create(pCopy
);
1511 cairo_set_source_surface(cr
, pSourceSurface
, -aTR
.mnSrcX
, -aTR
.mnSrcY
);
1512 cairo_rectangle(cr
, 0, 0, aTR
.mnSrcWidth
, aTR
.mnSrcHeight
);
1516 pSourceSurface
= pCopy
;
1522 copySource(aTR
, pSourceSurface
, bAntiAlias
);
1525 cairo_surface_destroy(pCopy
);
1530 cairo_pattern_t
* create_stipple()
1532 static unsigned char data
[16] = { 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
1533 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF };
1534 cairo_surface_t
* surface
= cairo_image_surface_create_for_data(data
, CAIRO_FORMAT_A8
, 4, 4, 4);
1535 cairo_pattern_t
* pattern
= cairo_pattern_create_for_surface(surface
);
1536 cairo_surface_destroy(surface
);
1537 cairo_pattern_set_extend(pattern
, CAIRO_EXTEND_REPEAT
);
1538 cairo_pattern_set_filter(pattern
, CAIRO_FILTER_NEAREST
);
1541 } // end anonymous ns
1543 void CairoCommon::invert(const basegfx::B2DPolygon
& rPoly
, SalInvert nFlags
, bool bAntiAlias
)
1545 cairo_t
* cr
= getCairoContext(false, bAntiAlias
);
1548 // To make releaseCairoContext work, use empty extents
1549 basegfx::B2DRange extents
;
1551 AddPolygonToPath(cr
, rPoly
, basegfx::B2DHomMatrix(), !bAntiAlias
, false);
1553 cairo_set_source_rgb(cr
, 1.0, 1.0, 1.0);
1555 cairo_set_operator(cr
, CAIRO_OPERATOR_DIFFERENCE
);
1557 if (nFlags
& SalInvert::TrackFrame
)
1559 cairo_set_line_width(cr
, 2.0);
1560 const double dashLengths
[2] = { 4.0, 4.0 };
1561 cairo_set_dash(cr
, dashLengths
, 2, 0);
1563 extents
= getClippedStrokeDamage(cr
);
1564 //see tdf#106577 under wayland, some pixel droppings seen, maybe we're
1565 //out by one somewhere, or cairo_stroke_extents is confused by
1567 if (!extents
.isEmpty())
1576 extents
= getClippedFillDamage(cr
);
1580 if (nFlags
& SalInvert::N50
)
1582 cairo_pattern_t
* pattern
= create_stipple();
1583 cairo_surface_t
* surface
= cairo_surface_create_similar(
1584 m_pSurface
, cairo_surface_get_content(m_pSurface
), extents
.getWidth() * m_fScale
,
1585 extents
.getHeight() * m_fScale
);
1587 dl_cairo_surface_set_device_scale(surface
, m_fScale
, m_fScale
);
1588 cairo_t
* stipple_cr
= cairo_create(surface
);
1589 cairo_set_source_rgb(stipple_cr
, 1.0, 1.0, 1.0);
1590 cairo_mask(stipple_cr
, pattern
);
1591 cairo_pattern_destroy(pattern
);
1592 cairo_destroy(stipple_cr
);
1593 cairo_mask_surface(cr
, surface
, extents
.getMinX(), extents
.getMinY());
1594 cairo_surface_destroy(surface
);
1602 releaseCairoContext(cr
, false, extents
);
1605 void CairoCommon::invert(tools::Long nX
, tools::Long nY
, tools::Long nWidth
, tools::Long nHeight
,
1606 SalInvert nFlags
, bool bAntiAlias
)
1608 basegfx::B2DPolygon aRect
= basegfx::utils::createPolygonFromRect(
1609 basegfx::B2DRectangle(nX
, nY
, nX
+ nWidth
, nY
+ nHeight
));
1611 invert(aRect
, nFlags
, bAntiAlias
);
1614 void CairoCommon::invert(sal_uInt32 nPoints
, const Point
* pPtAry
, SalInvert nFlags
, bool bAntiAlias
)
1616 basegfx::B2DPolygon aPoly
;
1617 aPoly
.append(basegfx::B2DPoint(pPtAry
->getX(), pPtAry
->getY()), nPoints
);
1618 for (sal_uInt32 i
= 1; i
< nPoints
; ++i
)
1619 aPoly
.setB2DPoint(i
, basegfx::B2DPoint(pPtAry
[i
].getX(), pPtAry
[i
].getY()));
1620 aPoly
.setClosed(true);
1622 invert(aPoly
, nFlags
, bAntiAlias
);
1625 void CairoCommon::drawBitmap(const SalTwoRect
& rPosAry
, const SalBitmap
& rSalBitmap
,
1628 // MM02 try to access buffered BitmapHelper
1629 std::shared_ptr
<BitmapHelper
> aSurface
;
1630 tryToUseSourceBuffer(rSalBitmap
, aSurface
);
1631 cairo_surface_t
* source
= aSurface
->getSurface(rPosAry
.mnDestWidth
, rPosAry
.mnDestHeight
);
1635 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawAlphaBitmap case");
1639 #if 0 // LO code is not yet bitmap32-ready.
1640 // if m_bSupportsBitmap32 becomes true for Svp revisit this
1641 copyWithOperator(rPosAry
, source
, CAIRO_OPERATOR_OVER
, bAntiAlias
);
1643 copyWithOperator(rPosAry
, source
, CAIRO_OPERATOR_SOURCE
, bAntiAlias
);
1647 bool CairoCommon::drawAlphaBitmap(const SalTwoRect
& rTR
, const SalBitmap
& rSourceBitmap
,
1648 const SalBitmap
& rAlphaBitmap
, bool bAntiAlias
)
1650 if (rAlphaBitmap
.GetBitCount() != 8 && rAlphaBitmap
.GetBitCount() != 1)
1652 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawAlphaBitmap alpha depth case: "
1653 << rAlphaBitmap
.GetBitCount());
1657 if (!rTR
.mnSrcWidth
|| !rTR
.mnSrcHeight
)
1659 SAL_WARN("vcl.gdi", "not possible to stretch nothing");
1663 // MM02 try to access buffered BitmapHelper
1664 std::shared_ptr
<BitmapHelper
> aSurface
;
1665 tryToUseSourceBuffer(rSourceBitmap
, aSurface
);
1666 cairo_surface_t
* source
= aSurface
->getSurface(rTR
.mnDestWidth
, rTR
.mnDestHeight
);
1670 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawAlphaBitmap case");
1674 // MM02 try to access buffered MaskHelper
1675 std::shared_ptr
<MaskHelper
> aMask
;
1676 tryToUseMaskBuffer(rAlphaBitmap
, aMask
);
1677 cairo_surface_t
* mask
= aMask
->getSurface(rTR
.mnDestWidth
, rTR
.mnDestHeight
);
1681 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawAlphaBitmap case");
1685 cairo_t
* cr
= getCairoContext(false, bAntiAlias
);
1686 if (cairo_status(cr
) != CAIRO_STATUS_SUCCESS
)
1689 "cannot render to surface: " << cairo_status_to_string(cairo_status(cr
)));
1690 releaseCairoContext(cr
, false, basegfx::B2DRange());
1696 cairo_rectangle(cr
, rTR
.mnDestX
, rTR
.mnDestY
, rTR
.mnDestWidth
, rTR
.mnDestHeight
);
1698 basegfx::B2DRange extents
= getClippedFillDamage(cr
);
1702 cairo_pattern_t
* maskpattern
= cairo_pattern_create_for_surface(mask
);
1703 cairo_translate(cr
, rTR
.mnDestX
, rTR
.mnDestY
);
1704 double fXScale
= static_cast<double>(rTR
.mnDestWidth
) / rTR
.mnSrcWidth
;
1705 double fYScale
= static_cast<double>(rTR
.mnDestHeight
) / rTR
.mnSrcHeight
;
1706 cairo_scale(cr
, fXScale
, fYScale
);
1707 cairo_set_source_surface(cr
, source
, -rTR
.mnSrcX
, -rTR
.mnSrcY
);
1709 cairo_pattern_t
* sourcepattern
= cairo_get_source(cr
);
1711 //tdf#133716 borders of upscaled images should not be blurred
1712 //tdf#114117 when stretching a single or multi pixel width/height source to fit an area
1713 //the image will be extended into that size.
1714 cairo_pattern_set_extend(sourcepattern
, CAIRO_EXTEND_PAD
);
1715 cairo_pattern_set_extend(maskpattern
, CAIRO_EXTEND_PAD
);
1717 //this block is just "cairo_mask_surface", but we have to make it explicit
1718 //because of the cairo_pattern_set_filter etc we may want applied
1719 cairo_matrix_t matrix
;
1720 cairo_matrix_init_translate(&matrix
, rTR
.mnSrcX
, rTR
.mnSrcY
);
1721 cairo_pattern_set_matrix(maskpattern
, &matrix
);
1722 cairo_mask(cr
, maskpattern
);
1724 cairo_pattern_destroy(maskpattern
);
1726 releaseCairoContext(cr
, false, extents
);
1731 bool CairoCommon::drawTransformedBitmap(const basegfx::B2DPoint
& rNull
, const basegfx::B2DPoint
& rX
,
1732 const basegfx::B2DPoint
& rY
, const SalBitmap
& rSourceBitmap
,
1733 const SalBitmap
* pAlphaBitmap
, double fAlpha
,
1736 if (pAlphaBitmap
&& pAlphaBitmap
->GetBitCount() != 8 && pAlphaBitmap
->GetBitCount() != 1)
1738 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawTransformedBitmap alpha depth case: "
1739 << pAlphaBitmap
->GetBitCount());
1746 // MM02 try to access buffered BitmapHelper
1747 std::shared_ptr
<BitmapHelper
> aSurface
;
1748 tryToUseSourceBuffer(rSourceBitmap
, aSurface
);
1749 const tools::Long
nDestWidth(basegfx::fround(basegfx::B2DVector(rX
- rNull
).getLength()));
1750 const tools::Long
nDestHeight(basegfx::fround(basegfx::B2DVector(rY
- rNull
).getLength()));
1751 cairo_surface_t
* source(aSurface
->getSurface(nDestWidth
, nDestHeight
));
1755 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawTransformedBitmap case");
1759 // MM02 try to access buffered MaskHelper
1760 std::shared_ptr
<MaskHelper
> aMask
;
1761 if (nullptr != pAlphaBitmap
)
1763 tryToUseMaskBuffer(*pAlphaBitmap
, aMask
);
1766 // access cairo_surface_t from MaskHelper
1767 cairo_surface_t
* mask(nullptr);
1770 mask
= aMask
->getSurface(nDestWidth
, nDestHeight
);
1773 if (nullptr != pAlphaBitmap
&& nullptr == mask
)
1775 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawTransformedBitmap case");
1779 const Size aSize
= rSourceBitmap
.GetSize();
1780 cairo_t
* cr
= getCairoContext(false, bAntiAlias
);
1783 // setup the image transformation
1784 // using the rNull,rX,rY points as destinations for the (0,0),(0,Width),(Height,0) source points
1785 const basegfx::B2DVector aXRel
= rX
- rNull
;
1786 const basegfx::B2DVector aYRel
= rY
- rNull
;
1787 cairo_matrix_t matrix
;
1788 cairo_matrix_init(&matrix
, aXRel
.getX() / aSize
.Width(), aXRel
.getY() / aSize
.Width(),
1789 aYRel
.getX() / aSize
.Height(), aYRel
.getY() / aSize
.Height(), rNull
.getX(),
1792 cairo_transform(cr
, &matrix
);
1794 cairo_rectangle(cr
, 0, 0, aSize
.Width(), aSize
.Height());
1795 basegfx::B2DRange extents
= getClippedFillDamage(cr
);
1798 cairo_set_source_surface(cr
, source
, 0, 0);
1800 cairo_mask_surface(cr
, mask
, 0, 0);
1804 releaseCairoContext(cr
, false, extents
);
1809 void CairoCommon::drawMask(const SalTwoRect
& rTR
, const SalBitmap
& rSalBitmap
, Color nMaskColor
,
1812 /** creates an image from the given rectangle, replacing all black pixels
1813 * with nMaskColor and make all other full transparent */
1814 // MM02 here decided *against* using buffered BitmapHelper
1815 // because the data gets somehow 'unmuliplied'. This may also be
1816 // done just once, but I am not sure if this is safe to do.
1817 // So for now dispense re-using data here.
1818 BitmapHelper
aSurface(rSalBitmap
, true); // The mask is argb32
1819 if (!aSurface
.getSurface())
1821 SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawMask case");
1825 unsigned char* mask_data
= aSurface
.getBits(nStride
);
1826 #if !ENABLE_WASM_STRIP_PREMULTIPLY
1827 vcl::bitmap::lookup_table
const& unpremultiply_table
= vcl::bitmap::get_unpremultiply_table();
1829 for (tools::Long y
= rTR
.mnSrcY
; y
< rTR
.mnSrcY
+ rTR
.mnSrcHeight
; ++y
)
1831 unsigned char* row
= mask_data
+ (nStride
* y
);
1832 unsigned char* data
= row
+ (rTR
.mnSrcX
* 4);
1833 for (tools::Long x
= rTR
.mnSrcX
; x
< rTR
.mnSrcX
+ rTR
.mnSrcWidth
; ++x
)
1835 sal_uInt8 a
= data
[SVP_CAIRO_ALPHA
];
1836 #if ENABLE_WASM_STRIP_PREMULTIPLY
1837 sal_uInt8 b
= vcl::bitmap::unpremultiply(a
, data
[SVP_CAIRO_BLUE
]);
1838 sal_uInt8 g
= vcl::bitmap::unpremultiply(a
, data
[SVP_CAIRO_GREEN
]);
1839 sal_uInt8 r
= vcl::bitmap::unpremultiply(a
, data
[SVP_CAIRO_RED
]);
1841 sal_uInt8 b
= unpremultiply_table
[a
][data
[SVP_CAIRO_BLUE
]];
1842 sal_uInt8 g
= unpremultiply_table
[a
][data
[SVP_CAIRO_GREEN
]];
1843 sal_uInt8 r
= unpremultiply_table
[a
][data
[SVP_CAIRO_RED
]];
1845 if (r
== 0 && g
== 0 && b
== 0)
1847 data
[0] = nMaskColor
.GetBlue();
1848 data
[1] = nMaskColor
.GetGreen();
1849 data
[2] = nMaskColor
.GetRed();
1862 aSurface
.mark_dirty();
1864 cairo_t
* cr
= getCairoContext(false, bAntiAlias
);
1867 cairo_rectangle(cr
, rTR
.mnDestX
, rTR
.mnDestY
, rTR
.mnDestWidth
, rTR
.mnDestHeight
);
1869 basegfx::B2DRange extents
= getClippedFillDamage(cr
);
1873 cairo_translate(cr
, rTR
.mnDestX
, rTR
.mnDestY
);
1874 double fXScale
= static_cast<double>(rTR
.mnDestWidth
) / rTR
.mnSrcWidth
;
1875 double fYScale
= static_cast<double>(rTR
.mnDestHeight
) / rTR
.mnSrcHeight
;
1876 cairo_scale(cr
, fXScale
, fYScale
);
1877 cairo_set_source_surface(cr
, aSurface
.getSurface(), -rTR
.mnSrcX
, -rTR
.mnSrcY
);
1879 if (cairo_status(cr
) == CAIRO_STATUS_SUCCESS
)
1881 //tdf#133716 borders of upscaled images should not be blurred
1882 cairo_pattern_t
* sourcepattern
= cairo_get_source(cr
);
1883 cairo_pattern_set_extend(sourcepattern
, CAIRO_EXTEND_PAD
);
1888 releaseCairoContext(cr
, false, extents
);
1891 std::shared_ptr
<SalBitmap
> CairoCommon::getBitmap(tools::Long nX
, tools::Long nY
,
1892 tools::Long nWidth
, tools::Long nHeight
)
1894 std::shared_ptr
<SvpSalBitmap
> pBitmap
= std::make_shared
<SvpSalBitmap
>();
1896 assert(GetBitCount() != 1 && "not supported anymore");
1897 vcl::PixelFormat ePixelFormat
= vcl::PixelFormat::N32_BPP
;
1899 if (!pBitmap
->ImplCreate(Size(nWidth
, nHeight
), ePixelFormat
, aPal
, false))
1901 SAL_WARN("vcl.gdi", "SvpSalGraphics::getBitmap, cannot create bitmap");
1905 cairo_surface_t
* target
= CairoCommon::createCairoSurface(pBitmap
->GetBuffer());
1908 SAL_WARN("vcl.gdi", "SvpSalGraphics::getBitmap, cannot create cairo surface");
1911 cairo_t
* cr
= cairo_create(target
);
1913 SalTwoRect
aTR(nX
, nY
, nWidth
, nHeight
, 0, 0, nWidth
, nHeight
);
1914 CairoCommon::renderSource(cr
, aTR
, m_pSurface
);
1917 cairo_surface_destroy(target
);
1922 cairo_format_t
getCairoFormat(const BitmapBuffer
& rBuffer
)
1924 cairo_format_t nFormat
;
1925 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
1926 assert(rBuffer
.mnBitCount
== 32 || rBuffer
.mnBitCount
== 24 || rBuffer
.mnBitCount
== 1);
1928 assert(rBuffer
.mnBitCount
== 32 || rBuffer
.mnBitCount
== 1);
1931 if (rBuffer
.mnBitCount
== 32)
1932 nFormat
= CAIRO_FORMAT_ARGB32
;
1933 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
1934 else if (rBuffer
.mnBitCount
== 24)
1935 nFormat
= CAIRO_FORMAT_RGB24_888
;
1938 nFormat
= CAIRO_FORMAT_A1
;
1944 bool isCairoCompatible(const BitmapBuffer
* pBuffer
)
1949 // We use Cairo that supports 24-bit RGB.
1950 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
1951 if (pBuffer
->mnBitCount
!= 32 && pBuffer
->mnBitCount
!= 24 && pBuffer
->mnBitCount
!= 1)
1953 if (pBuffer
->mnBitCount
!= 32 && pBuffer
->mnBitCount
!= 1)
1957 cairo_format_t nFormat
= getCairoFormat(*pBuffer
);
1958 return (cairo_format_stride_for_width(nFormat
, pBuffer
->mnWidth
) == pBuffer
->mnScanlineSize
);
1962 cairo_surface_t
* CairoCommon::createCairoSurface(const BitmapBuffer
* pBuffer
)
1964 if (!isCairoCompatible(pBuffer
))
1967 cairo_format_t nFormat
= getCairoFormat(*pBuffer
);
1968 cairo_surface_t
* target
= cairo_image_surface_create_for_data(
1969 pBuffer
->mpBits
, nFormat
, pBuffer
->mnWidth
, pBuffer
->mnHeight
, pBuffer
->mnScanlineSize
);
1970 if (cairo_surface_status(target
) != CAIRO_STATUS_SUCCESS
)
1972 cairo_surface_destroy(target
);
1978 bool CairoCommon::hasFastDrawTransformedBitmap() { return false; }
1980 bool CairoCommon::supportsOperation(OutDevSupportType eType
)
1984 case OutDevSupportType::TransparentRect
:
1985 case OutDevSupportType::TransparentText
:
1991 std::optional
<BitmapBuffer
> FastConvert24BitRgbTo32BitCairo(const BitmapBuffer
* pSrc
)
1993 if (pSrc
== nullptr)
1994 return std::nullopt
;
1996 assert(pSrc
->mnFormat
== SVP_24BIT_FORMAT
);
1997 const tools::Long nWidth
= pSrc
->mnWidth
;
1998 const tools::Long nHeight
= pSrc
->mnHeight
;
1999 std::optional
<BitmapBuffer
> pDst(std::in_place
);
2000 pDst
->mnFormat
= (ScanlineFormat::N32BitTcArgb
| ScanlineFormat::TopDown
);
2001 pDst
->mnWidth
= nWidth
;
2002 pDst
->mnHeight
= nHeight
;
2003 pDst
->mnBitCount
= 32;
2004 pDst
->maColorMask
= pSrc
->maColorMask
;
2005 pDst
->maPalette
= pSrc
->maPalette
;
2007 tools::Long nScanlineBase
;
2008 const bool bFail
= o3tl::checked_multiply
<tools::Long
>(pDst
->mnBitCount
, nWidth
, nScanlineBase
);
2011 SAL_WARN("vcl.gdi", "checked multiply failed");
2012 pDst
->mpBits
= nullptr;
2013 return std::nullopt
;
2016 pDst
->mnScanlineSize
= AlignedWidth4Bytes(nScanlineBase
);
2017 if (pDst
->mnScanlineSize
< nScanlineBase
/ 8)
2019 SAL_WARN("vcl.gdi", "scanline calculation wraparound");
2020 pDst
->mpBits
= nullptr;
2021 return std::nullopt
;
2026 pDst
->mpBits
= new sal_uInt8
[pDst
->mnScanlineSize
* nHeight
];
2028 catch (const std::bad_alloc
&)
2030 // memory exception, clean up
2031 pDst
->mpBits
= nullptr;
2032 return std::nullopt
;
2035 for (tools::Long y
= 0; y
< nHeight
; ++y
)
2037 sal_uInt8
* pS
= pSrc
->mpBits
+ y
* pSrc
->mnScanlineSize
;
2038 sal_uInt8
* pD
= pDst
->mpBits
+ y
* pDst
->mnScanlineSize
;
2039 for (tools::Long x
= 0; x
< nWidth
; ++x
)
2041 #if ENABLE_CAIRO_RGBA
2042 static_assert((SVP_CAIRO_FORMAT
& ~ScanlineFormat::TopDown
)
2043 == ScanlineFormat::N32BitTcRgba
,
2044 "Expected SVP_CAIRO_FORMAT set to N32BitTcBgra");
2045 static_assert((SVP_24BIT_FORMAT
& ~ScanlineFormat::TopDown
)
2046 == ScanlineFormat::N24BitTcRgb
,
2047 "Expected SVP_24BIT_FORMAT set to N24BitTcRgb");
2051 pD
[3] = 0xff; // Alpha
2052 #elif defined OSL_BIGENDIAN
2053 static_assert((SVP_CAIRO_FORMAT
& ~ScanlineFormat::TopDown
)
2054 == ScanlineFormat::N32BitTcArgb
,
2055 "Expected SVP_CAIRO_FORMAT set to N32BitTcBgra");
2056 static_assert((SVP_24BIT_FORMAT
& ~ScanlineFormat::TopDown
)
2057 == ScanlineFormat::N24BitTcRgb
,
2058 "Expected SVP_24BIT_FORMAT set to N24BitTcRgb");
2059 pD
[0] = 0xff; // Alpha
2064 static_assert((SVP_CAIRO_FORMAT
& ~ScanlineFormat::TopDown
)
2065 == ScanlineFormat::N32BitTcBgra
,
2066 "Expected SVP_CAIRO_FORMAT set to N32BitTcBgra");
2067 static_assert((SVP_24BIT_FORMAT
& ~ScanlineFormat::TopDown
)
2068 == ScanlineFormat::N24BitTcBgr
,
2069 "Expected SVP_24BIT_FORMAT set to N24BitTcBgr");
2073 pD
[3] = 0xff; // Alpha
2086 // check for env var that decides for using downscale pattern
2087 const char* pDisableDownScale(getenv("SAL_DISABLE_CAIRO_DOWNSCALE"));
2088 bool bDisableDownScale(nullptr != pDisableDownScale
);
2091 cairo_surface_t
* SurfaceHelper::implCreateOrReuseDownscale(unsigned long nTargetWidth
,
2092 unsigned long nTargetHeight
)
2094 const unsigned long nSourceWidth(cairo_image_surface_get_width(pSurface
));
2095 const unsigned long nSourceHeight(cairo_image_surface_get_height(pSurface
));
2097 // zoomed in, need to stretch at paint, no pre-scale useful
2098 if (nTargetWidth
>= nSourceWidth
|| nTargetHeight
>= nSourceHeight
)
2103 // calculate downscale factor
2104 unsigned long nWFactor(1);
2105 unsigned long nW((nSourceWidth
+ 1) / 2);
2106 unsigned long nHFactor(1);
2107 unsigned long nH((nSourceHeight
+ 1) / 2);
2109 while (nW
> nTargetWidth
&& nW
> 1)
2115 while (nH
> nTargetHeight
&& nH
> 1)
2121 if (1 == nWFactor
&& 1 == nHFactor
)
2123 // original size *is* best binary size, use it
2127 // go up one scale again - look for no change
2128 nW
= (1 == nWFactor
) ? nTargetWidth
: nW
* 2;
2129 nH
= (1 == nHFactor
) ? nTargetHeight
: nH
* 2;
2131 // check if we have a downscaled version of required size
2132 // bail out if the multiplication for the key would overflow
2133 if (nW
>= SAL_MAX_UINT32
|| nH
>= SAL_MAX_UINT32
)
2135 const sal_uInt64
key((nW
* static_cast<sal_uInt64
>(SAL_MAX_UINT32
)) + nH
);
2136 auto isHit(maDownscaled
.find(key
));
2138 if (isHit
!= maDownscaled
.end())
2140 return isHit
->second
;
2143 // create new surface in the targeted size
2144 cairo_surface_t
* pSurfaceTarget
2145 = cairo_surface_create_similar(pSurface
, cairo_surface_get_content(pSurface
), nW
, nH
);
2147 // made a version to scale self first that worked well, but would've
2148 // been hard to support CAIRO_FORMAT_A1 including bit shifting, so
2149 // I decided to go with cairo itself - use CAIRO_FILTER_FAST or
2150 // CAIRO_FILTER_GOOD though. Please modify as needed for
2151 // performance/quality
2152 cairo_t
* cr
= cairo_create(pSurfaceTarget
);
2153 const double fScaleX(static_cast<double>(nW
) / static_cast<double>(nSourceWidth
));
2154 const double fScaleY(static_cast<double>(nH
) / static_cast<double>(nSourceHeight
));
2155 cairo_scale(cr
, fScaleX
, fScaleY
);
2156 cairo_set_source_surface(cr
, pSurface
, 0.0, 0.0);
2157 cairo_pattern_set_filter(cairo_get_source(cr
), CAIRO_FILTER_GOOD
);
2161 // need to set device_scale for downscale surfaces to get
2162 // them handled correctly
2163 cairo_surface_set_device_scale(pSurfaceTarget
, fScaleX
, fScaleY
);
2165 // add entry to cached entries
2166 maDownscaled
[key
] = pSurfaceTarget
;
2168 return pSurfaceTarget
;
2171 bool SurfaceHelper::isTrivial() const
2173 constexpr unsigned long nMinimalSquareSizeToBuffer(64 * 64);
2174 const unsigned long nSourceWidth(cairo_image_surface_get_width(pSurface
));
2175 const unsigned long nSourceHeight(cairo_image_surface_get_height(pSurface
));
2177 return nSourceWidth
* nSourceHeight
< nMinimalSquareSizeToBuffer
;
2180 SurfaceHelper::SurfaceHelper()
2185 SurfaceHelper::~SurfaceHelper()
2187 cairo_surface_destroy(pSurface
);
2188 for (auto& candidate
: maDownscaled
)
2190 cairo_surface_destroy(candidate
.second
);
2194 cairo_surface_t
* SurfaceHelper::getSurface(unsigned long nTargetWidth
,
2195 unsigned long nTargetHeight
) const
2197 if (bDisableDownScale
|| 0 == nTargetWidth
|| 0 == nTargetHeight
|| !pSurface
|| isTrivial())
2199 // caller asks for original or disabled or trivial (smaller then a minimal square size)
2200 // also excludes zero cases for width/height after this point if need to prescale
2204 return const_cast<SurfaceHelper
*>(this)->implCreateOrReuseDownscale(nTargetWidth
,
2208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */