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
|
/* -*- 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/.
*/
#include <skia/win/gdiimpl.hxx>
#include <tools/sk_app/win/WindowContextFactory_win.h>
#include <tools/sk_app/WindowContext.h>
#include <SkColorFilter.h>
#include <SkPixelRef.h>
WinSkiaSalGraphicsImpl::WinSkiaSalGraphicsImpl(WinSalGraphics& rGraphics,
SalGeometryProvider* mpProvider)
: SkiaSalGraphicsImpl(rGraphics, mpProvider)
, mWinParent(rGraphics)
{
}
void WinSkiaSalGraphicsImpl::Init()
{
#if 0 // TODO
if (!IsOffscreen() && mpContext.is() && mpContext->isInitialized())
{
const GLWinWindow& rGLWindow = static_cast<const GLWinWindow&>(mpContext->getOpenGLWindow());
if (rGLWindow.hWnd != mrWinParent.mhWnd || rGLWindow.hDC == mrWinParent.mhLocalDC)
{
// This can legitimately happen, SalFrame keeps 2x
// SalGraphics which share the same hWnd and hDC.
// The shape 'Area' dialog does reparenting to trigger this.
SAL_WARN("vcl.opengl", "Unusual: Windows handle / DC changed without DeInit");
DeInit();
}
}
#endif
SkiaSalGraphicsImpl::Init();
}
void WinSkiaSalGraphicsImpl::createSurface()
{
if (isOffscreen())
return SkiaSalGraphicsImpl::createSurface();
// When created, Init() gets called with size (0,0), which is invalid size
// for Skia. Creating the actual surface is delayed, so the size should be always
// valid here, but better check.
assert(GetWidth() != 0 && GetHeight() != 0);
sk_app::DisplayParams displayParams;
destroySurface();
switch (renderMethodToUse())
{
case RenderRaster:
mWindowContext = sk_app::window_context_factory::MakeRasterForWin(mWinParent.gethWnd(),
displayParams);
break;
case RenderVulkan:
mWindowContext = sk_app::window_context_factory::MakeVulkanForWin(mWinParent.gethWnd(),
displayParams);
break;
}
assert(SkToBool(mWindowContext)); // TODO
mSurface = mWindowContext->getBackbufferSurface();
assert(mSurface.get());
}
void WinSkiaSalGraphicsImpl::DeInit()
{
SkiaSalGraphicsImpl::DeInit();
mWindowContext.reset();
}
void WinSkiaSalGraphicsImpl::freeResources() {}
void WinSkiaSalGraphicsImpl::performFlush()
{
if (mWindowContext)
mWindowContext->swapBuffers();
}
bool WinSkiaSalGraphicsImpl::TryRenderCachedNativeControl(ControlCacheKey const& rControlCacheKey,
int nX, int nY)
{
(void)rControlCacheKey;
(void)nX;
(void)nY;
return false; // TODO
}
bool WinSkiaSalGraphicsImpl::RenderAndCacheNativeControl(CompatibleDC& rWhite, CompatibleDC& rBlack,
int nX, int nY,
ControlCacheKey& aControlCacheKey)
{
(void)rWhite;
(void)rBlack;
(void)nX;
(void)nY;
(void)aControlCacheKey;
return false; // TODO
}
void WinSkiaSalGraphicsImpl::PreDrawText() { preDraw(); }
void WinSkiaSalGraphicsImpl::PostDrawText() { postDraw(); }
SkColor toSkColor(Color color)
{
return SkColorSetARGB(255 - color.GetTransparency(), color.GetRed(), color.GetGreen(),
color.GetBlue());
}
void WinSkiaSalGraphicsImpl::DeferredTextDraw(const CompatibleDC::Texture* pTexture,
Color aMaskColor, const SalTwoRect& rPosAry)
{
assert(dynamic_cast<const SkiaCompatibleDC::Texture*>(pTexture));
preDraw();
SkPaint paint;
// The glyph is painted as white, modulate it to be of the appropriate color.
// SkiaCompatibleDC::wantsTextColorWhite() ensures the glyph is white.
// TODO maybe other black/white in WinFontInstance::CacheGlyphToAtlas() should be swapped.
paint.setColorFilter(SkColorFilters::Blend(toSkColor(aMaskColor), SkBlendMode::kModulate));
mSurface->getCanvas()->drawBitmapRect(
static_cast<const SkiaCompatibleDC::Texture*>(pTexture)->bitmap,
SkRect::MakeXYWH(rPosAry.mnSrcX, rPosAry.mnSrcY, rPosAry.mnSrcWidth, rPosAry.mnSrcHeight),
SkRect::MakeXYWH(rPosAry.mnDestX, rPosAry.mnDestY, rPosAry.mnDestWidth,
rPosAry.mnDestHeight),
&paint);
postDraw();
}
void WinSkiaSalGraphicsImpl::DrawTextMask(CompatibleDC::Texture* pTexture, Color nMaskColor,
const SalTwoRect& rPosAry)
{
assert(dynamic_cast<SkiaCompatibleDC::Texture*>(pTexture));
const SkBitmap& bitmap = static_cast<const SkiaCompatibleDC::Texture*>(pTexture)->bitmap;
preDraw();
SkBitmap tmpBitmap;
if (!tmpBitmap.tryAllocN32Pixels(bitmap.width(), bitmap.height()))
abort();
tmpBitmap.eraseColor(toSkColor(nMaskColor));
SkPaint paint;
// Draw the color with the given mask.
// TODO figure out the right blend mode to avoid the temporary bitmap
paint.setBlendMode(SkBlendMode::kDstOut);
SkCanvas canvas(tmpBitmap);
canvas.drawBitmap(bitmap, 0, 0, &paint);
mSurface->getCanvas()->drawBitmapRect(
tmpBitmap,
SkRect::MakeXYWH(rPosAry.mnSrcX, rPosAry.mnSrcY, rPosAry.mnSrcWidth, rPosAry.mnSrcHeight),
SkRect::MakeXYWH(rPosAry.mnDestX, rPosAry.mnDestY, rPosAry.mnDestWidth,
rPosAry.mnDestHeight),
nullptr);
postDraw();
}
SkiaCompatibleDC::SkiaCompatibleDC(SalGraphics& rGraphics, int x, int y, int width, int height)
: CompatibleDC(rGraphics, x, y, width, height, false)
{
}
std::unique_ptr<CompatibleDC::Texture> SkiaCompatibleDC::getAsMaskTexture()
{
auto ret = std::make_unique<SkiaCompatibleDC::Texture>();
// mpData is in the BGRA format, with A unused (and set to 0), and RGB are grey,
// so convert it to Skia format, then to 8bit and finally use as alpha mask
SkBitmap tmpBitmap;
if (!tmpBitmap.installPixels(SkImageInfo::Make(maRects.mnSrcWidth, maRects.mnSrcHeight,
kBGRA_8888_SkColorType, kOpaque_SkAlphaType),
mpData, maRects.mnSrcWidth * 4))
abort();
SkBitmap bitmap8;
if (!bitmap8.tryAllocPixels(SkImageInfo::Make(maRects.mnSrcWidth, maRects.mnSrcHeight,
kGray_8_SkColorType, kOpaque_SkAlphaType)))
abort();
SkCanvas canvas8(bitmap8);
SkPaint paint8;
paint8.setBlendMode(SkBlendMode::kSrc); // copy and convert depth
// The data we got is upside-down.
SkMatrix matrix;
matrix.preTranslate(0, maRects.mnSrcHeight);
matrix.setConcat(matrix, SkMatrix::MakeScale(1, -1));
canvas8.concat(matrix);
canvas8.drawBitmap(tmpBitmap, 0, 0, &paint8);
// use the 8bit data as an alpha channel
SkBitmap alpha;
alpha.setInfo(bitmap8.info().makeColorType(kAlpha_8_SkColorType), bitmap8.rowBytes());
alpha.setPixelRef(sk_ref_sp(bitmap8.pixelRef()), bitmap8.pixelRefOrigin().x(),
bitmap8.pixelRefOrigin().y());
ret->bitmap = alpha;
return ret;
}
bool SkiaCompatibleDC::copyToTexture(CompatibleDC::Texture& aTexture)
{
assert(mpImpl);
assert(dynamic_cast<SkiaCompatibleDC::Texture*>(&aTexture));
SkBitmap tmpBitmap;
if (!tmpBitmap.installPixels(SkImageInfo::Make(maRects.mnSrcWidth, maRects.mnSrcHeight,
kBGRA_8888_SkColorType, kUnpremul_SkAlphaType),
mpData, maRects.mnSrcWidth * 4))
abort();
SkBitmap& bitmap = static_cast<SkiaCompatibleDC::Texture&>(aTexture).bitmap;
SkPaint paint;
paint.setBlendMode(SkBlendMode::kSrc); // set as is, including alpha
SkCanvas canvas(bitmap);
// The data we got is upside-down.
SkMatrix matrix;
matrix.preTranslate(0, maRects.mnSrcHeight);
matrix.setConcat(matrix, SkMatrix::MakeScale(1, -1));
canvas.concat(matrix);
canvas.drawBitmapRect(tmpBitmap,
SkRect::MakeXYWH(0, 0, maRects.mnSrcWidth, maRects.mnSrcHeight),
SkRect::MakeXYWH(0, 0, maRects.mnSrcWidth, maRects.mnSrcHeight), &paint);
return true;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|