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 * Some of this code is based on Skia source code, covered by the following
10 * license notice (see readlicense_oo for the full license):
12 * Copyright 2016 Google Inc.
14 * Use of this source code is governed by a BSD-style license that can be
15 * found in the LICENSE file.
19 #include <skia/osx/bitmap.hxx>
21 #include <vcl/bitmapex.hxx>
22 #include <vcl/image.hxx>
24 #include <skia/salbmp.hxx>
25 #include <osx/saldata.hxx>
31 using namespace SkiaHelper
;
35 CGImageRef
createCGImage(const Image
& rImage
)
37 BitmapEx
bitmapEx(rImage
.GetBitmapEx());
38 Bitmap
bitmap(bitmapEx
.GetBitmap());
40 if (bitmap
.IsEmpty() || !bitmap
.ImplGetSalBitmap())
43 assert(dynamic_cast<SkiaSalBitmap
*>(bitmap
.ImplGetSalBitmap().get()) != nullptr);
44 SkiaSalBitmap
* skiaBitmap
= static_cast<SkiaSalBitmap
*>(bitmap
.ImplGetSalBitmap().get());
46 SkBitmap targetBitmap
;
47 if (!targetBitmap
.tryAllocPixels(
48 SkImageInfo::Make(bitmap
.GetSizePixel().getWidth(), bitmap
.GetSizePixel().getHeight(),
49 kRGBA_8888_SkColorType
, kPremul_SkAlphaType
)))
52 paint
.setBlendMode(SkBlendMode::kSrc
); // set as is
53 SkMatrix matrix
; // The image is needed upside-down.
54 matrix
.preTranslate(0, targetBitmap
.height());
55 matrix
.setConcat(matrix
, SkMatrix::Scale(1, -1));
57 if (!bitmapEx
.IsAlpha())
59 SkCanvas
canvas(targetBitmap
);
60 canvas
.concat(matrix
);
61 canvas
.drawImage(skiaBitmap
->GetSkImage(), 0, 0, SkSamplingOptions(), &paint
);
65 AlphaMask
alpha(bitmapEx
.GetAlphaMask());
66 // tdf#156854 invert alpha mask for macOS native menu item images
67 // At the time of this change, only the AquaSalMenu class calls this
68 // function so it should be safe to invert the alpha mask here.
70 Bitmap
alphaBitmap(alpha
.GetBitmap());
71 assert(dynamic_cast<SkiaSalBitmap
*>(alphaBitmap
.ImplGetSalBitmap().get()) != nullptr);
72 SkiaSalBitmap
* skiaAlpha
73 = static_cast<SkiaSalBitmap
*>(alphaBitmap
.ImplGetSalBitmap().get());
75 // Drawing to a bitmap using a shader from a GPU-backed image fails silently.
76 // https://bugs.chromium.org/p/skia/issues/detail?id=12685
77 paint
.setShader(SkShaders::Blend(SkBlendMode::kDstOut
,
78 skiaBitmap
->GetSkShader(SkSamplingOptions()),
79 skiaAlpha
->GetAlphaSkShader(SkSamplingOptions())));
81 sk_sp
<SkImage
> imB
= skiaBitmap
->GetSkImage()->makeNonTextureImage();
82 sk_sp
<SkImage
> imA
= skiaAlpha
->GetAlphaSkImage()->makeNonTextureImage();
83 paint
.setShader(SkShaders::Blend(SkBlendMode::kDstOut
, imB
->makeShader(SkSamplingOptions()),
84 imA
->makeShader(SkSamplingOptions())));
86 SkCanvas
canvas(targetBitmap
);
87 canvas
.concat(matrix
);
88 canvas
.drawPaint(paint
);
91 CGContextRef context
= CGBitmapContextCreate(
92 targetBitmap
.getAddr32(0, 0), targetBitmap
.width(), targetBitmap
.height(), 8,
93 targetBitmap
.rowBytes(), GetSalData()->mxRGBSpace
, kCGImageAlphaPremultipliedLast
);
96 CGImageRef screenImage
= CGBitmapContextCreateImage(context
);
102 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */