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 <quartz/cgutils.h>
24 #include <osx/saldata.hxx>
26 #include <ios/iosinst.hxx>
31 #include <Metal/Metal.h>
35 static void CFRTLFree(void* /*info*/, const void* data, size_t /*size*/)
37 std::free( const_cast<void*>(data) );
40 CGImageRef CreateWithSalBitmapAndMask( const SalBitmap& rBitmap, const SalBitmap& rMask, int nX, int nY, int nWidth, int nHeight )
42 CGImageRef xImage( rBitmap.CreateCroppedImage( nX, nY, nWidth, nHeight ) );
46 CGImageRef xMask = rMask.CreateCroppedImage( nX, nY, nWidth, nHeight );
50 // If xMask is an image (i.e. not a mask), it must be greyscale - a requirement of the
51 // CGImageCreateWithMask() function.
52 if( !CGImageIsMask(xMask) && CGImageGetColorSpace(xMask) != GetSalData()->mxGraySpace )
54 CGImageRef xGrayMask = CGImageCreateCopyWithColorSpace(xMask, GetSalData()->mxGraySpace);
62 // Many gallery images will fail to be converted to a grayscale
63 // colorspace so fall back to old mask creation code
64 const CGRect xImageRect=CGRectMake( 0, 0, nWidth, nHeight );//the rect has no offset
66 // create the alpha mask image fitting our image
67 // TODO: is caching the full mask or the subimage mask worth it?
68 int nMaskBytesPerRow = ((nWidth + 3) & ~3);
69 void* pMaskMem = std::malloc( nMaskBytesPerRow * nHeight );
70 CGContextRef xMaskContext = CGBitmapContextCreate( pMaskMem,
71 nWidth, nHeight, 8, nMaskBytesPerRow, GetSalData()->mxGraySpace, kCGImageAlphaNone );
72 CGContextDrawImage( xMaskContext, xImageRect, xMask );
74 CGDataProviderRef xDataProvider( CGDataProviderCreateWithData( nullptr,
75 pMaskMem, nHeight * nMaskBytesPerRow, &CFRTLFree ) );
77 static const CGFloat* pDecode = nullptr;
78 xMask = CGImageMaskCreate( nWidth, nHeight, 8, 8, nMaskBytesPerRow, xDataProvider, pDecode, false );
79 CFRelease( xDataProvider );
80 CFRelease( xMaskContext );
87 // combine image and alpha mask
88 CGImageRef xMaskedImage = CGImageCreateWithMask( xImage, xMask );
96 bool DefaultMTLDeviceIsSupported()
98 id<MTLDevice> pMetalDevice = MTLCreateSystemDefaultDevice();
99 if (!pMetalDevice || !pMetalDevice.name)
101 SAL_WARN("vcl.skia", "MTLCreateSystemDefaultDevice() returned nil");
105 SAL_WARN("vcl.skia", "Default MTLDevice is \"" << [pMetalDevice.name UTF8String] << "\"");
109 // tdf#156881 Disable Metal with AMD Radeon Pro 5XXX GPUs on macOS Catalina
110 // When running macOS Catalina on a 2019 MacBook Pro, unexpected drawing
111 // artifacts are drawn so disable Metal for the AMD Radeon Pro GPUs listed
112 // for that model in https://support.apple.com/kb/SP809.
113 if (@available(macOS 11, *))
115 // No known problems with macOS Big Sur or later
119 static NSString* pAMDRadeonPro5300Prefix = @"AMD Radeon Pro 5300M";
120 static NSString* pAMDRadeonPro5500Prefix = @"AMD Radeon Pro 5500M";
121 if ([pMetalDevice.name hasPrefix:pAMDRadeonPro5300Prefix] || [pMetalDevice.name hasPrefix:pAMDRadeonPro5500Prefix])
127 // tdf#160590 Disable Metal with Intel HD Graphics 6000
128 // Releasing a Metal buffer resource hangs when fetching pixels from a
129 // Skia surface on this Intel MacBook Air built-in GPU.
130 static NSString* pIntelHDGraphics6000Prefix = @"Intel(R) Iris(TM) Graphics 6000";
131 if ([pMetalDevice.name hasPrefix:pIntelHDGraphics6000Prefix])
135 [pMetalDevice release];
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */