1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <string.h>
#ifdef FREEBSD
#include <sys/types.h>
#endif
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <osl/endian.h>
#include <sal/log.hxx>
#include <tools/helpers.hxx>
#include <tools/debug.hxx>
#include <vcl/bitmap.hxx>
#include <com/sun/star/beans/XFastPropertySet.hpp>
#include <unx/saldisp.hxx>
#include <unx/salbmp.h>
#include <unx/salinst.h>
#include <unx/x11/xlimits.hxx>
#include <o3tl/safeint.hxx>
#include <opengl/salbmp.hxx>
#include <vcl/opengl/OpenGLHelper.hxx>
#include <config_features.h>
#if HAVE_FEATURE_SKIA
#include <vcl/skia/SkiaHelper.hxx>
#include <skia/salbmp.hxx>
#endif
#if defined HAVE_VALGRIND_HEADERS
#include <valgrind/valgrind.h>
#endif
#include <memory>
std::shared_ptr<SalBitmap> X11SalInstance::CreateSalBitmap()
{
#if HAVE_FEATURE_SKIA
if (SkiaHelper::isVCLSkiaEnabled())
return std::make_shared<SkiaSalBitmap>();
else
#endif
if (OpenGLHelper::isVCLOpenGLEnabled())
return std::make_shared<OpenGLSalBitmap>();
else
return std::make_shared<X11SalBitmap>();
}
ImplSalBitmapCache* X11SalBitmap::mpCache = nullptr;
unsigned int X11SalBitmap::mnCacheInstCount = 0;
X11SalBitmap::X11SalBitmap()
: mbGrey( false )
{
}
X11SalBitmap::~X11SalBitmap()
{
Destroy();
}
void X11SalBitmap::ImplCreateCache()
{
if( !mnCacheInstCount++ )
mpCache = new ImplSalBitmapCache;
}
void X11SalBitmap::ImplDestroyCache()
{
SAL_WARN_IF( !mnCacheInstCount, "vcl", "X11SalBitmap::ImplDestroyCache(): underflow" );
if( mnCacheInstCount && !--mnCacheInstCount )
{
delete mpCache;
mpCache = nullptr;
}
}
void X11SalBitmap::ImplRemovedFromCache()
{
mpDDB.reset();
}
#if defined HAVE_VALGRIND_HEADERS
namespace
{
void blankExtraSpace(BitmapBuffer* pDIB)
{
size_t nExtraSpaceInScanLine = pDIB->mnScanlineSize - pDIB->mnWidth * pDIB->mnBitCount / 8;
if (nExtraSpaceInScanLine)
{
for (long i = 0; i < pDIB->mnHeight; ++i)
{
sal_uInt8 *pRow = pDIB->mpBits + (i * pDIB->mnScanlineSize);
memset(pRow + (pDIB->mnScanlineSize - nExtraSpaceInScanLine), 0, nExtraSpaceInScanLine);
}
}
}
}
#endif
std::unique_ptr<BitmapBuffer> X11SalBitmap::ImplCreateDIB(
const Size& rSize,
sal_uInt16 nBitCount,
const BitmapPalette& rPal)
{
DBG_ASSERT(
nBitCount == 1
|| nBitCount == 4
|| nBitCount == 8
|| nBitCount == 24
, "Unsupported BitCount!"
);
std::unique_ptr<BitmapBuffer> pDIB;
if( !rSize.Width() || !rSize.Height() )
return nullptr;
try
{
pDIB.reset(new BitmapBuffer);
}
catch (const std::bad_alloc&)
{
return nullptr;
}
const sal_uInt16 nColors = ( nBitCount <= 8 ) ? ( 1 << nBitCount ) : 0;
pDIB->mnFormat = ScanlineFormat::NONE;
switch( nBitCount )
{
case 1: pDIB->mnFormat |= ScanlineFormat::N1BitMsbPal; break;
case 4: pDIB->mnFormat |= ScanlineFormat::N4BitMsnPal; break;
case 8: pDIB->mnFormat |= ScanlineFormat::N8BitPal; break;
case 24: pDIB->mnFormat |= ScanlineFormat::N24BitTcBgr; break;
default:
SAL_WARN("vcl.gdi", "32-bit images not supported, converting to 24-bit");
nBitCount = 24;
pDIB->mnFormat |= ScanlineFormat::N24BitTcBgr;
break;
}
pDIB->mnWidth = rSize.Width();
pDIB->mnHeight = rSize.Height();
long nScanlineBase;
bool bFail = o3tl::checked_multiply<long>(pDIB->mnWidth, nBitCount, nScanlineBase);
if (bFail)
{
SAL_WARN("vcl.gdi", "checked multiply failed");
return nullptr;
}
pDIB->mnScanlineSize = AlignedWidth4Bytes(nScanlineBase);
if (pDIB->mnScanlineSize < nScanlineBase/8)
{
SAL_WARN("vcl.gdi", "scanline calculation wraparound");
return nullptr;
}
pDIB->mnBitCount = nBitCount;
if( nColors )
{
pDIB->maPalette = rPal;
pDIB->maPalette.SetEntryCount( nColors );
}
try
{
pDIB->mpBits = new sal_uInt8[ pDIB->mnScanlineSize * pDIB->mnHeight ];
#if defined HAVE_VALGRIND_HEADERS
if (RUNNING_ON_VALGRIND)
blankExtraSpace(pDIB.get());
#endif
}
catch (const std::bad_alloc&)
{
return nullptr;
}
return pDIB;
}
std::unique_ptr<BitmapBuffer> X11SalBitmap::ImplCreateDIB(
Drawable aDrawable,
SalX11Screen nScreen,
long nDrawableDepth,
long nX,
long nY,
long nWidth,
long nHeight,
bool bGrey
) {
std::unique_ptr<BitmapBuffer> pDIB;
if( aDrawable && nWidth && nHeight && nDrawableDepth )
{
SalDisplay* pSalDisp = vcl_sal::getSalDisplay(GetGenericUnixSalData());
Display* pXDisp = pSalDisp->GetDisplay();
// do not die on XError here
// alternatively one could check the coordinates for being offscreen
// but this call can actually work on servers with backing store
// defaults even if the rectangle is offscreen
// so better catch the XError
GetGenericUnixSalData()->ErrorTrapPush();
XImage* pImage = XGetImage( pXDisp, aDrawable, nX, nY, nWidth, nHeight, AllPlanes, ZPixmap );
bool bWasError = GetGenericUnixSalData()->ErrorTrapPop( false );
if( ! bWasError && pImage && pImage->data )
{
const SalTwoRect aTwoRect = { 0, 0, nWidth, nHeight, 0, 0, nWidth, nHeight };
BitmapBuffer aSrcBuf;
const BitmapPalette* pDstPal = nullptr;
aSrcBuf.mnFormat = ScanlineFormat::TopDown;
aSrcBuf.mnWidth = nWidth;
aSrcBuf.mnHeight = nHeight;
aSrcBuf.mnBitCount = pImage->bits_per_pixel;
aSrcBuf.mnScanlineSize = pImage->bytes_per_line;
aSrcBuf.mpBits = reinterpret_cast<sal_uInt8*>(pImage->data);
pImage->red_mask = pSalDisp->GetVisual( nScreen ).red_mask;
pImage->green_mask = pSalDisp->GetVisual( nScreen ).green_mask;
pImage->blue_mask = pSalDisp->GetVisual( nScreen ).blue_mask;
switch( aSrcBuf.mnBitCount )
{
case 1:
{
aSrcBuf.mnFormat |= ( LSBFirst == pImage->bitmap_bit_order
? ScanlineFormat::N1BitLsbPal
: ScanlineFormat::N1BitMsbPal
);
}
break;
case 4:
{
aSrcBuf.mnFormat |= ( LSBFirst == pImage->bitmap_bit_order
? ScanlineFormat::N4BitLsnPal
: ScanlineFormat::N4BitMsnPal
);
}
break;
case 8:
{
aSrcBuf.mnFormat |= ScanlineFormat::N8BitPal;
}
break;
case 24:
{
if( ( LSBFirst == pImage->byte_order ) && ( pImage->red_mask == 0xFF ) )
aSrcBuf.mnFormat |= ScanlineFormat::N24BitTcRgb;
else
aSrcBuf.mnFormat |= ScanlineFormat::N24BitTcBgr;
}
break;
case 32:
{
if( LSBFirst == pImage->byte_order )
aSrcBuf.mnFormat |= ( pSalDisp->GetVisual(nScreen).red_mask == 0xFF
? ScanlineFormat::N32BitTcRgba
: ScanlineFormat::N32BitTcBgra
);
else
aSrcBuf.mnFormat |= ( pSalDisp->GetVisual(nScreen).red_mask == 0xFF
? ScanlineFormat::N32BitTcAbgr
: ScanlineFormat::N32BitTcArgb
);
}
break;
default: assert(false);
}
BitmapPalette& rPal = aSrcBuf.maPalette;
if( aSrcBuf.mnBitCount == 1 )
{
rPal.SetEntryCount( 2 );
pDstPal = &rPal;
rPal[ 0 ] = COL_BLACK;
rPal[ 1 ] = COL_WHITE;
}
else if( pImage->depth == 8 && bGrey )
{
rPal.SetEntryCount( 256 );
pDstPal = &rPal;
for( sal_uInt16 i = 0; i < 256; i++ )
{
BitmapColor& rBmpCol = rPal[ i ];
rBmpCol.SetRed( i );
rBmpCol.SetGreen( i );
rBmpCol.SetBlue( i );
}
}
else if( aSrcBuf.mnBitCount <= 8 )
{
const SalColormap& rColMap = pSalDisp->GetColormap( nScreen );
const sal_uInt16 nCols = std::min(static_cast<sal_uLong>(rColMap.GetUsed()),
sal_uLong(1) << nDrawableDepth);
rPal.SetEntryCount( nCols );
pDstPal = &rPal;
for( sal_uInt16 i = 0; i < nCols; i++ )
{
const Color nColor( rColMap.GetColor( i ) );
BitmapColor& rBmpCol = rPal[ i ];
rBmpCol.SetRed( nColor.GetRed() );
rBmpCol.SetGreen( nColor.GetGreen() );
rBmpCol.SetBlue( nColor.GetBlue() );
}
}
pDIB = StretchAndConvert( aSrcBuf, aTwoRect, aSrcBuf.mnFormat,
pDstPal, &aSrcBuf.maColorMask );
XDestroyImage( pImage );
}
}
return pDIB;
}
XImage* X11SalBitmap::ImplCreateXImage(
SalDisplay const *pSalDisp,
SalX11Screen nScreen,
long nDepth,
const SalTwoRect& rTwoRect
) const
{
XImage* pImage = nullptr;
if( !mpDIB && mpDDB )
{
const_cast<X11SalBitmap*>(this)->mpDIB =
ImplCreateDIB( mpDDB->ImplGetPixmap(),
mpDDB->ImplGetScreen(),
mpDDB->ImplGetDepth(),
0, 0,
mpDDB->ImplGetWidth(),
mpDDB->ImplGetHeight(),
mbGrey );
}
if( mpDIB && mpDIB->mnWidth && mpDIB->mnHeight )
{
Display* pXDisp = pSalDisp->GetDisplay();
long nWidth = rTwoRect.mnDestWidth;
long nHeight = rTwoRect.mnDestHeight;
if( 1 == GetBitCount() )
nDepth = 1;
pImage = XCreateImage( pXDisp, pSalDisp->GetVisual( nScreen ).GetVisual(),
nDepth, ( 1 == nDepth ) ? XYBitmap :ZPixmap, 0, nullptr,
nWidth, nHeight, 32, 0 );
if( pImage )
{
std::unique_ptr<BitmapBuffer> pDstBuf;
ScanlineFormat nDstFormat = ScanlineFormat::TopDown;
std::unique_ptr<BitmapPalette> xPal;
std::unique_ptr<ColorMask> xMask;
switch( pImage->bits_per_pixel )
{
case 1:
nDstFormat |= ( LSBFirst == pImage->bitmap_bit_order
? ScanlineFormat::N1BitLsbPal
: ScanlineFormat::N1BitMsbPal
);
break;
case 4:
nDstFormat |= ( LSBFirst == pImage->bitmap_bit_order
? ScanlineFormat::N4BitLsnPal
: ScanlineFormat::N4BitMsnPal
);
break;
case 8:
nDstFormat |= ScanlineFormat::N8BitPal;
break;
case 24:
{
if( ( LSBFirst == pImage->byte_order ) && ( pImage->red_mask == 0xFF ) )
nDstFormat |= ScanlineFormat::N24BitTcRgb;
else
nDstFormat |= ScanlineFormat::N24BitTcBgr;
}
break;
case 32:
{
if( LSBFirst == pImage->byte_order )
nDstFormat |= ( pImage->red_mask == 0xFF
? ScanlineFormat::N32BitTcRgba
: ScanlineFormat::N32BitTcBgra
);
else
nDstFormat |= ( pImage->red_mask == 0xFF
? ScanlineFormat::N32BitTcAbgr
: ScanlineFormat::N32BitTcArgb
);
}
break;
default: assert(false);
}
if( pImage->depth == 1 )
{
xPal.reset(new BitmapPalette( 2 ));
(*xPal)[ 0 ] = COL_BLACK;
(*xPal)[ 1 ] = COL_WHITE;
}
else if( pImage->depth == 8 && mbGrey )
{
xPal.reset(new BitmapPalette( 256 ));
for( sal_uInt16 i = 0; i < 256; i++ )
{
BitmapColor& rBmpCol = (*xPal)[ i ];
rBmpCol.SetRed( i );
rBmpCol.SetGreen( i );
rBmpCol.SetBlue( i );
}
}
else if( pImage->depth <= 8 )
{
const SalColormap& rColMap = pSalDisp->GetColormap( nScreen );
const sal_uInt16 nCols = std::min( static_cast<sal_uLong>(rColMap.GetUsed())
, static_cast<sal_uLong>(1 << pImage->depth)
);
xPal.reset(new BitmapPalette( nCols ));
for( sal_uInt16 i = 0; i < nCols; i++ )
{
const Color nColor( rColMap.GetColor( i ) );
BitmapColor& rBmpCol = (*xPal)[ i ];
rBmpCol.SetRed( nColor.GetRed() );
rBmpCol.SetGreen( nColor.GetGreen() );
rBmpCol.SetBlue( nColor.GetBlue() );
}
}
pDstBuf = StretchAndConvert( *mpDIB, rTwoRect, nDstFormat, xPal.get(), xMask.get() );
xPal.reset();
xMask.reset();
if( pDstBuf && pDstBuf->mpBits )
{
#if defined HAVE_VALGRIND_HEADERS
if (RUNNING_ON_VALGRIND)
blankExtraSpace(pDstBuf.get());
#endif
// set data in buffer as data member in pImage
pImage->data = reinterpret_cast<char*>(pDstBuf->mpBits);
}
else
{
XDestroyImage( pImage );
pImage = nullptr;
}
// note that pDstBuf it deleted here, but that doesn't destroy allocated data in buffer
}
}
return pImage;
}
bool X11SalBitmap::ImplCreateFromDrawable(
Drawable aDrawable,
SalX11Screen nScreen,
long nDrawableDepth,
long nX,
long nY,
long nWidth,
long nHeight
) {
Destroy();
if( aDrawable && nWidth && nHeight && nDrawableDepth )
mpDDB.reset(new ImplSalDDB( aDrawable, nScreen, nDrawableDepth, nX, nY, nWidth, nHeight ));
return( mpDDB != nullptr );
}
ImplSalDDB* X11SalBitmap::ImplGetDDB(
Drawable aDrawable,
SalX11Screen nXScreen,
long nDrawableDepth,
const SalTwoRect& rTwoRect
) const
{
if( !mpDDB || !mpDDB->ImplMatches( nXScreen, nDrawableDepth, rTwoRect ) )
{
if( mpDDB )
{
// do we already have a DIB? if not, create aDIB from current DDB first
if( !mpDIB )
{
const_cast<X11SalBitmap*>(this)->mpDIB = ImplCreateDIB( mpDDB->ImplGetPixmap(),
mpDDB->ImplGetScreen(),
mpDDB->ImplGetDepth(),
0, 0,
mpDDB->ImplGetWidth(),
mpDDB->ImplGetHeight(),
mbGrey );
}
mpDDB.reset();
}
if( mpCache )
mpCache->ImplRemove( this );
SalTwoRect aTwoRect( rTwoRect );
if( aTwoRect.mnSrcX < 0 )
{
aTwoRect.mnSrcWidth += aTwoRect.mnSrcX;
aTwoRect.mnSrcX = 0;
}
if( aTwoRect.mnSrcY < 0 )
{
aTwoRect.mnSrcHeight += aTwoRect.mnSrcY;
aTwoRect.mnSrcY = 0;
}
// create new DDB from DIB
const Size aSize( GetSize() );
if( aTwoRect.mnSrcWidth == aTwoRect.mnDestWidth &&
aTwoRect.mnSrcHeight == aTwoRect.mnDestHeight )
{
aTwoRect.mnSrcX = aTwoRect.mnSrcY = aTwoRect.mnDestX = aTwoRect.mnDestY = 0;
aTwoRect.mnSrcWidth = aTwoRect.mnDestWidth = aSize.Width();
aTwoRect.mnSrcHeight = aTwoRect.mnDestHeight = aSize.Height();
}
else if( aTwoRect.mnSrcWidth+aTwoRect.mnSrcX > aSize.Width() ||
aTwoRect.mnSrcHeight+aTwoRect.mnSrcY > aSize.Height() )
{
// #i47823# this should not happen at all, but does nonetheless
// because BitmapEx allows for mask bitmaps of different size
// than image bitmap (broken)
if( aTwoRect.mnSrcX >= aSize.Width() ||
aTwoRect.mnSrcY >= aSize.Height() )
return nullptr; // this would be a really mad case
if( aTwoRect.mnSrcWidth+aTwoRect.mnSrcX > aSize.Width() )
{
aTwoRect.mnSrcWidth = aSize.Width()-aTwoRect.mnSrcX;
if( aTwoRect.mnSrcWidth < 1 )
{
aTwoRect.mnSrcX = 0;
aTwoRect.mnSrcWidth = aSize.Width();
}
}
if( aTwoRect.mnSrcHeight+aTwoRect.mnSrcY > aSize.Height() )
{
aTwoRect.mnSrcHeight = aSize.Height() - aTwoRect.mnSrcY;
if( aTwoRect.mnSrcHeight < 1 )
{
aTwoRect.mnSrcY = 0;
aTwoRect.mnSrcHeight = aSize.Height();
}
}
}
XImage* pImage = ImplCreateXImage( vcl_sal::getSalDisplay(GetGenericUnixSalData()), nXScreen,
nDrawableDepth, aTwoRect );
if( pImage )
{
mpDDB.reset(new ImplSalDDB( pImage, aDrawable, nXScreen, aTwoRect ));
delete[] pImage->data;
pImage->data = nullptr;
XDestroyImage( pImage );
if( mpCache )
mpCache->ImplAdd( const_cast<X11SalBitmap*>(this) );
}
}
return mpDDB.get();
}
void X11SalBitmap::ImplDraw(
Drawable aDrawable,
SalX11Screen nXScreen,
long nDrawableDepth,
const SalTwoRect& rTwoRect,
const GC& rGC
) const
{
ImplGetDDB( aDrawable, nXScreen, nDrawableDepth, rTwoRect );
if( mpDDB )
mpDDB->ImplDraw( aDrawable, rTwoRect, rGC );
}
bool X11SalBitmap::Create( const Size& rSize, sal_uInt16 nBitCount, const BitmapPalette& rPal )
{
Destroy();
mpDIB = ImplCreateDIB( rSize, nBitCount, rPal );
return( mpDIB != nullptr );
}
bool X11SalBitmap::Create( const SalBitmap& rSSalBmp )
{
Destroy();
if( dynamic_cast<const X11SalBitmap*>( &rSSalBmp ) == nullptr )
return false;
const X11SalBitmap& rSalBmp = static_cast<const X11SalBitmap&>( rSSalBmp );
if( rSalBmp.mpDIB )
{
// TODO: reference counting...
mpDIB.reset(new BitmapBuffer( *rSalBmp.mpDIB ));
// TODO: get rid of this when BitmapBuffer gets copy constructor
try
{
mpDIB->mpBits = new sal_uInt8[ mpDIB->mnScanlineSize * mpDIB->mnHeight ];
#if defined HAVE_VALGRIND_HEADERS
if (RUNNING_ON_VALGRIND)
blankExtraSpace(mpDIB.get());
#endif
}
catch (const std::bad_alloc&)
{
mpDIB.reset();
}
if( mpDIB )
memcpy( mpDIB->mpBits, rSalBmp.mpDIB->mpBits, mpDIB->mnScanlineSize * mpDIB->mnHeight );
}
else if( rSalBmp.mpDDB )
ImplCreateFromDrawable( rSalBmp.mpDDB->ImplGetPixmap(),
rSalBmp.mpDDB->ImplGetScreen(),
rSalBmp.mpDDB->ImplGetDepth(),
0, 0, rSalBmp.mpDDB->ImplGetWidth(), rSalBmp.mpDDB->ImplGetHeight() );
return( ( !rSalBmp.mpDIB && !rSalBmp.mpDDB ) ||
( rSalBmp.mpDIB && ( mpDIB != nullptr ) ) ||
( rSalBmp.mpDDB && ( mpDDB != nullptr ) ) );
}
bool X11SalBitmap::Create( const SalBitmap&, SalGraphics* )
{
return false;
}
bool X11SalBitmap::Create( const SalBitmap&, sal_uInt16 )
{
return false;
}
bool X11SalBitmap::Create(
const css::uno::Reference< css::rendering::XBitmapCanvas >& rBitmapCanvas,
Size& rSize,
bool bMask
) {
css::uno::Reference< css::beans::XFastPropertySet > xFastPropertySet( rBitmapCanvas, css::uno::UNO_QUERY );
if( xFastPropertySet ) {
sal_Int32 depth;
css::uno::Sequence< css::uno::Any > args;
if( xFastPropertySet->getFastPropertyValue(bMask ? 2 : 1) >>= args ) {
long pixmapHandle = {}; // spurious -Werror=maybe-uninitialized
if( ( args[1] >>= pixmapHandle ) && ( args[2] >>= depth ) ) {
mbGrey = bMask;
bool bSuccess = ImplCreateFromDrawable(
pixmapHandle,
// FIXME: this seems multi-screen broken to me
SalX11Screen( 0 ),
depth,
0,
0,
rSize.Width(),
rSize.Height()
);
bool bFreePixmap = false;<--- Assignment 'bFreePixmap=false', assigned value is 0
if( bSuccess && (args[0] >>= bFreePixmap) && bFreePixmap )<--- Condition 'bFreePixmap' is always false<--- Variable 'args[0]' is assigned a value that is never used.
XFreePixmap( vcl_sal::getSalDisplay(GetGenericUnixSalData())->GetDisplay(), pixmapHandle );
return bSuccess;
}
}
}
return false;
}
void X11SalBitmap::Destroy()
{
if( mpDIB )
{
delete[] mpDIB->mpBits;
mpDIB.reset();
}
mpDDB.reset();
if( mpCache )
mpCache->ImplRemove( this );
}
Size X11SalBitmap::GetSize() const
{
Size aSize;
if( mpDIB )
{
aSize.setWidth( mpDIB->mnWidth );
aSize.setHeight( mpDIB->mnHeight );
}
else if( mpDDB )
{
aSize.setWidth( mpDDB->ImplGetWidth() );
aSize.setHeight( mpDDB->ImplGetHeight() );
}
return aSize;
}
sal_uInt16 X11SalBitmap::GetBitCount() const
{
sal_uInt16 nBitCount;
if( mpDIB )
nBitCount = mpDIB->mnBitCount;
else if( mpDDB )
nBitCount = mpDDB->ImplGetDepth();
else
nBitCount = 0;
return nBitCount;
}
BitmapBuffer* X11SalBitmap::AcquireBuffer( BitmapAccessMode /*nMode*/ )
{
if( !mpDIB && mpDDB )
{
mpDIB = ImplCreateDIB(
mpDDB->ImplGetPixmap(),
mpDDB->ImplGetScreen(),
mpDDB->ImplGetDepth(),
0, 0,
mpDDB->ImplGetWidth(),
mpDDB->ImplGetHeight(),
mbGrey
);
}
return mpDIB.get();
}
void X11SalBitmap::ReleaseBuffer( BitmapBuffer*, BitmapAccessMode nMode )
{
if( nMode == BitmapAccessMode::Write )
{
mpDDB.reset();
if( mpCache )
mpCache->ImplRemove( this );
InvalidateChecksum();
}
}
bool X11SalBitmap::GetSystemData( BitmapSystemData& rData )
{
if( mpDDB )
{
// Rename/retype pDummy to your likings (though X11 Pixmap is
// prolly not a good idea, since it's accessed from
// non-platform aware code in vcl/bitmap.hxx)
rData.aPixmap = reinterpret_cast<void*>(mpDDB->ImplGetPixmap());
rData.mnWidth = mpDDB->ImplGetWidth ();
rData.mnHeight = mpDDB->ImplGetHeight ();
return true;
}
return false;
}
bool X11SalBitmap::ScalingSupported() const
{
return false;
}
bool X11SalBitmap::Scale( const double& /*rScaleX*/, const double& /*rScaleY*/, BmpScaleFlag /*nScaleFlag*/ )
{
return false;
}
bool X11SalBitmap::Replace( const Color& /*rSearchColor*/, const Color& /*rReplaceColor*/, sal_uInt8 /*nTol*/ )
{
return false;
}
ImplSalDDB::ImplSalDDB( XImage* pImage, Drawable aDrawable,
SalX11Screen nXScreen, const SalTwoRect& rTwoRect )
: maPixmap ( 0 )
, maTwoRect ( rTwoRect )
, mnDepth ( pImage->depth )
, mnXScreen ( nXScreen )
{
SalDisplay* pSalDisp = vcl_sal::getSalDisplay(GetGenericUnixSalData());
Display* pXDisp = pSalDisp->GetDisplay();
if( (maPixmap = limitXCreatePixmap( pXDisp, aDrawable, ImplGetWidth(), ImplGetHeight(), ImplGetDepth() )) )
{
XGCValues aValues;
GC aGC;
int nValues = GCFunction;
aValues.function = GXcopy;
if( 1 == mnDepth )
{
nValues |= ( GCForeground | GCBackground );
aValues.foreground = 1;
aValues.background = 0;
}
aGC = XCreateGC( pXDisp, maPixmap, nValues, &aValues );
XPutImage( pXDisp, maPixmap, aGC, pImage, 0, 0, 0, 0, maTwoRect.mnDestWidth, maTwoRect.mnDestHeight );
XFreeGC( pXDisp, aGC );
}
}
ImplSalDDB::ImplSalDDB(
Drawable aDrawable,
SalX11Screen nXScreen,
long nDrawableDepth,
long nX,
long nY,
long nWidth,
long nHeight
) : maTwoRect(0, 0, nWidth, nHeight, 0, 0, nWidth, nHeight)
, mnDepth( nDrawableDepth )
, mnXScreen( nXScreen )
{
SalDisplay* pSalDisp = vcl_sal::getSalDisplay(GetGenericUnixSalData());
Display* pXDisp = pSalDisp->GetDisplay();
if( (maPixmap = limitXCreatePixmap( pXDisp, aDrawable, nWidth, nHeight, nDrawableDepth )) )
{
XGCValues aValues;
GC aGC;
int nValues = GCFunction;
aValues.function = GXcopy;
if( 1 == mnDepth )
{
nValues |= ( GCForeground | GCBackground );
aValues.foreground = 1;
aValues.background = 0;
}
aGC = XCreateGC( pXDisp, maPixmap, nValues, &aValues );
ImplDraw( aDrawable, nDrawableDepth, maPixmap,
nX, nY, nWidth, nHeight, 0, 0, aGC );
XFreeGC( pXDisp, aGC );
}
else
{
maTwoRect.mnSrcWidth = maTwoRect.mnDestWidth = 0;
maTwoRect.mnSrcHeight = maTwoRect.mnDestHeight = 0;
}
}
ImplSalDDB::~ImplSalDDB()
{
if( maPixmap && ImplGetSVData() )
XFreePixmap( vcl_sal::getSalDisplay(GetGenericUnixSalData())->GetDisplay(), maPixmap );
}
bool ImplSalDDB::ImplMatches( SalX11Screen nXScreen, long nDepth, const SalTwoRect& rTwoRect ) const
{
bool bRet = false;
if( ( maPixmap != 0 ) && ( ( mnDepth == nDepth ) || ( 1 == mnDepth ) ) && nXScreen == mnXScreen)
{
if ( rTwoRect.mnSrcX == maTwoRect.mnSrcX
&& rTwoRect.mnSrcY == maTwoRect.mnSrcY
&& rTwoRect.mnSrcWidth == maTwoRect.mnSrcWidth
&& rTwoRect.mnSrcHeight == maTwoRect.mnSrcHeight
&& rTwoRect.mnDestWidth == maTwoRect.mnDestWidth
&& rTwoRect.mnDestHeight == maTwoRect.mnDestHeight
)
{
// absolutely identically
bRet = true;
}
else if( rTwoRect.mnSrcWidth == rTwoRect.mnDestWidth
&& rTwoRect.mnSrcHeight == rTwoRect.mnDestHeight
&& maTwoRect.mnSrcWidth == maTwoRect.mnDestWidth
&& maTwoRect.mnSrcHeight == maTwoRect.mnDestHeight
&& rTwoRect.mnSrcX >= maTwoRect.mnSrcX
&& rTwoRect.mnSrcY >= maTwoRect.mnSrcY
&& ( rTwoRect.mnSrcX + rTwoRect.mnSrcWidth ) <= ( maTwoRect.mnSrcX + maTwoRect.mnSrcWidth )
&& ( rTwoRect.mnSrcY + rTwoRect.mnSrcHeight ) <= ( maTwoRect.mnSrcY + maTwoRect.mnSrcHeight )
)
{
bRet = true;
}
}
return bRet;
}
void ImplSalDDB::ImplDraw(
Drawable aDrawable,
const SalTwoRect& rTwoRect,
const GC& rGC
) const
{
ImplDraw( maPixmap, mnDepth, aDrawable,
rTwoRect.mnSrcX - maTwoRect.mnSrcX, rTwoRect.mnSrcY - maTwoRect.mnSrcY,
rTwoRect.mnDestWidth, rTwoRect.mnDestHeight,
rTwoRect.mnDestX, rTwoRect.mnDestY, rGC );
}
void ImplSalDDB::ImplDraw(
Drawable aSrcDrawable,
long nSrcDrawableDepth,
Drawable aDstDrawable,
long nSrcX,
long nSrcY,
long nDestWidth,
long nDestHeight,
long nDestX,
long nDestY,
const GC& rGC
) {
SalDisplay* pSalDisp = vcl_sal::getSalDisplay(GetGenericUnixSalData());
Display* pXDisp = pSalDisp->GetDisplay();
if( 1 == nSrcDrawableDepth )
{
XCopyPlane( pXDisp, aSrcDrawable, aDstDrawable, rGC,
nSrcX, nSrcY, nDestWidth, nDestHeight, nDestX, nDestY, 1 );
}
else
{
XCopyArea( pXDisp, aSrcDrawable, aDstDrawable, rGC,
nSrcX, nSrcY, nDestWidth, nDestHeight, nDestX, nDestY );
}
}
ImplSalBitmapCache::ImplSalBitmapCache()
{
}
ImplSalBitmapCache::~ImplSalBitmapCache()
{
ImplClear();
}
void ImplSalBitmapCache::ImplAdd( X11SalBitmap* pBmp )
{
for(auto pObj : maBmpList)
{
if( pObj == pBmp )<--- Consider using std::any_of algorithm instead of a raw loop.
return;
}
maBmpList.push_back( pBmp );
}
void ImplSalBitmapCache::ImplRemove( X11SalBitmap const * pBmp )
{
auto it = std::find(maBmpList.begin(), maBmpList.end(), pBmp);
if( it != maBmpList.end() )
{
(*it)->ImplRemovedFromCache();
maBmpList.erase( it );
}
}
void ImplSalBitmapCache::ImplClear()
{
for(auto pObj : maBmpList)
{
pObj->ImplRemovedFromCache();
}
maBmpList.clear();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|