1 // Copyright 2020 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fxge/win32/cgdi_display_driver.h"
8
9 #include <utility>
10
11 #include "core/fxcrt/check.h"
12 #include "core/fxcrt/check_op.h"
13 #include "core/fxcrt/fx_coordinates.h"
14 #include "core/fxcrt/fx_system.h"
15 #include "core/fxge/agg/cfx_agg_imagerenderer.h"
16 #include "core/fxge/dib/cfx_dibbase.h"
17 #include "core/fxge/dib/cfx_dibitmap.h"
18 #include "core/fxge/render_defines.h"
19 #include "core/fxge/win32/cwin32_platform.h"
20
CGdiDisplayDriver(HDC hDC)21 CGdiDisplayDriver::CGdiDisplayDriver(HDC hDC)
22 : CGdiDeviceDriver(hDC, DeviceType::kDisplay) {
23 auto* pPlatform =
24 static_cast<CWin32Platform*>(CFX_GEModule::Get()->GetPlatform());
25 if (pPlatform->m_GdiplusExt.IsAvailable()) {
26 m_RenderCaps |= FXRC_ALPHA_PATH | FXRC_ALPHA_IMAGE;
27 }
28 }
29
30 CGdiDisplayDriver::~CGdiDisplayDriver() = default;
31
GetDeviceCaps(int caps_id) const32 int CGdiDisplayDriver::GetDeviceCaps(int caps_id) const {
33 if (caps_id == FXDC_HORZ_SIZE || caps_id == FXDC_VERT_SIZE)
34 return 0;
35 return CGdiDeviceDriver::GetDeviceCaps(caps_id);
36 }
37
GetDIBits(RetainPtr<CFX_DIBitmap> bitmap,int left,int top) const38 bool CGdiDisplayDriver::GetDIBits(RetainPtr<CFX_DIBitmap> bitmap,
39 int left,
40 int top) const {
41 bool ret = false;
42 int width = bitmap->GetWidth();
43 int height = bitmap->GetHeight();
44 HBITMAP hbmp = CreateCompatibleBitmap(m_hDC, width, height);
45 HDC hDCMemory = CreateCompatibleDC(m_hDC);
46 HBITMAP holdbmp = (HBITMAP)SelectObject(hDCMemory, hbmp);
47 BitBlt(hDCMemory, 0, 0, width, height, m_hDC, left, top, SRCCOPY);
48 SelectObject(hDCMemory, holdbmp);
49 BITMAPINFO bmi = {};
50 bmi.bmiHeader.biSize = sizeof bmi.bmiHeader;
51 bmi.bmiHeader.biBitCount = bitmap->GetBPP();
52 bmi.bmiHeader.biHeight = -height;
53 bmi.bmiHeader.biPlanes = 1;
54 bmi.bmiHeader.biWidth = width;
55 if (bitmap->GetBPP() > 8) {
56 ret = ::GetDIBits(hDCMemory, hbmp, 0, height,
57 bitmap->GetWritableBuffer().data(), &bmi,
58 DIB_RGB_COLORS) == height;
59 if (ret && bitmap->IsAlphaFormat()) {
60 bitmap->SetUniformOpaqueAlpha();
61 }
62 } else {
63 auto rgb_bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
64 if (rgb_bitmap->Create(width, height, FXDIB_Format::kBgr)) {
65 bmi.bmiHeader.biBitCount = 24;
66 ::GetDIBits(hDCMemory, hbmp, 0, height,
67 rgb_bitmap->GetWritableBuffer().data(), &bmi, DIB_RGB_COLORS);
68 ret = bitmap->TransferBitmap(width, height, std::move(rgb_bitmap), 0, 0);
69 } else {
70 ret = false;
71 }
72 }
73
74 DeleteObject(hbmp);
75 DeleteObject(hDCMemory);
76 return ret;
77 }
78
SetDIBits(RetainPtr<const CFX_DIBBase> bitmap,uint32_t color,const FX_RECT & src_rect,int left,int top,BlendMode blend_type)79 bool CGdiDisplayDriver::SetDIBits(RetainPtr<const CFX_DIBBase> bitmap,
80 uint32_t color,
81 const FX_RECT& src_rect,
82 int left,
83 int top,
84 BlendMode blend_type) {
85 DCHECK_EQ(blend_type, BlendMode::kNormal);
86 if (bitmap->IsMaskFormat()) {
87 int width = bitmap->GetWidth();
88 int height = bitmap->GetHeight();
89 int alpha = FXARGB_A(color);
90 if (bitmap->GetBPP() != 1 || alpha != 255) {
91 auto background = pdfium::MakeRetain<CFX_DIBitmap>();
92 if (!background->Create(width, height, FXDIB_Format::kBgrx) ||
93 !GetDIBits(background, left, top) ||
94 !background->CompositeMask(0, 0, width, height, std::move(bitmap),
95 color, 0, 0, BlendMode::kNormal, nullptr,
96 false)) {
97 return false;
98 }
99 FX_RECT alpha_src_rect(0, 0, width, height);
100 return SetDIBits(std::move(background), /*color=*/0, alpha_src_rect, left,
101 top, BlendMode::kNormal);
102 }
103 FX_RECT clip_rect(left, top, left + src_rect.Width(),
104 top + src_rect.Height());
105 return StretchDIBits(std::move(bitmap), color, left - src_rect.left,
106 top - src_rect.top, width, height, &clip_rect,
107 FXDIB_ResampleOptions(), BlendMode::kNormal);
108 }
109 int width = src_rect.Width();
110 int height = src_rect.Height();
111 if (bitmap->IsAlphaFormat()) {
112 auto rgb_bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
113 if (!rgb_bitmap->Create(width, height, FXDIB_Format::kBgr) ||
114 !GetDIBits(rgb_bitmap, left, top) ||
115 !rgb_bitmap->CompositeBitmap(0, 0, width, height, std::move(bitmap),
116 src_rect.left, src_rect.top,
117 BlendMode::kNormal, nullptr, false)) {
118 return false;
119 }
120 FX_RECT alpha_src_rect(0, 0, width, height);
121 return SetDIBits(std::move(rgb_bitmap), /*color=*/0, alpha_src_rect, left,
122 top, BlendMode::kNormal);
123 }
124 return GDI_SetDIBits(std::move(bitmap), src_rect, left, top);
125 }
126
UseFoxitStretchEngine(RetainPtr<const CFX_DIBBase> bitmap,uint32_t color,int dest_left,int dest_top,int dest_width,int dest_height,const FX_RECT * pClipRect,const FXDIB_ResampleOptions & options)127 bool CGdiDisplayDriver::UseFoxitStretchEngine(
128 RetainPtr<const CFX_DIBBase> bitmap,
129 uint32_t color,
130 int dest_left,
131 int dest_top,
132 int dest_width,
133 int dest_height,
134 const FX_RECT* pClipRect,
135 const FXDIB_ResampleOptions& options) {
136 FX_RECT bitmap_clip = *pClipRect;
137 if (dest_width < 0)
138 dest_left += dest_width;
139
140 if (dest_height < 0)
141 dest_top += dest_height;
142
143 bitmap_clip.Offset(-dest_left, -dest_top);
144 bitmap = bitmap->StretchTo(dest_width, dest_height, options, &bitmap_clip);
145 if (!bitmap) {
146 return true;
147 }
148
149 FX_RECT src_rect(0, 0, bitmap->GetWidth(), bitmap->GetHeight());
150 return SetDIBits(std::move(bitmap), color, src_rect, pClipRect->left,
151 pClipRect->top, BlendMode::kNormal);
152 }
153
StretchDIBits(RetainPtr<const CFX_DIBBase> bitmap,uint32_t color,int dest_left,int dest_top,int dest_width,int dest_height,const FX_RECT * pClipRect,const FXDIB_ResampleOptions & options,BlendMode blend_type)154 bool CGdiDisplayDriver::StretchDIBits(RetainPtr<const CFX_DIBBase> bitmap,
155 uint32_t color,
156 int dest_left,
157 int dest_top,
158 int dest_width,
159 int dest_height,
160 const FX_RECT* pClipRect,
161 const FXDIB_ResampleOptions& options,
162 BlendMode blend_type) {
163 DCHECK(bitmap);
164 DCHECK(pClipRect);
165
166 if (options.HasAnyOptions() || dest_width > 10000 || dest_width < -10000 ||
167 dest_height > 10000 || dest_height < -10000) {
168 return UseFoxitStretchEngine(std::move(bitmap), color, dest_left, dest_top,
169 dest_width, dest_height, pClipRect, options);
170 }
171 if (bitmap->IsMaskFormat()) {
172 FX_RECT image_rect;
173 image_rect.left = dest_width > 0 ? dest_left : dest_left + dest_width;
174 image_rect.right = dest_width > 0 ? dest_left + dest_width : dest_left;
175 image_rect.top = dest_height > 0 ? dest_top : dest_top + dest_height;
176 image_rect.bottom = dest_height > 0 ? dest_top + dest_height : dest_top;
177 FX_RECT clip_rect = image_rect;
178 clip_rect.Intersect(*pClipRect);
179 clip_rect.Offset(-image_rect.left, -image_rect.top);
180 int clip_width = clip_rect.Width(), clip_height = clip_rect.Height();
181 bitmap = bitmap->StretchTo(dest_width, dest_height, FXDIB_ResampleOptions(),
182 &clip_rect);
183 if (!bitmap) {
184 return true;
185 }
186
187 auto background = pdfium::MakeRetain<CFX_DIBitmap>();
188 if (!background->Create(clip_width, clip_height, FXDIB_Format::kBgrx) ||
189 !GetDIBits(background, image_rect.left + clip_rect.left,
190 image_rect.top + clip_rect.top) ||
191 !background->CompositeMask(0, 0, clip_width, clip_height,
192 std::move(bitmap), color, 0, 0,
193 BlendMode::kNormal, nullptr, false)) {
194 return false;
195 }
196
197 FX_RECT src_rect(0, 0, clip_width, clip_height);
198 return SetDIBits(background, 0, src_rect, image_rect.left + clip_rect.left,
199 image_rect.top + clip_rect.top, BlendMode::kNormal);
200 }
201 if (bitmap->IsAlphaFormat()) {
202 auto* pPlatform =
203 static_cast<CWin32Platform*>(CFX_GEModule::Get()->GetPlatform());
204 if (pPlatform->m_GdiplusExt.IsAvailable()) {
205 return pPlatform->m_GdiplusExt.StretchDIBits(m_hDC, std::move(bitmap),
206 dest_left, dest_top,
207 dest_width, dest_height);
208 }
209 return UseFoxitStretchEngine(std::move(bitmap), color, dest_left, dest_top,
210 dest_width, dest_height, pClipRect,
211 FXDIB_ResampleOptions());
212 }
213 return GDI_StretchDIBits(std::move(bitmap), dest_left, dest_top, dest_width,
214 dest_height, FXDIB_ResampleOptions());
215 }
216
StartDIBits(RetainPtr<const CFX_DIBBase> bitmap,float alpha,uint32_t color,const CFX_Matrix & matrix,const FXDIB_ResampleOptions & options,BlendMode blend_type)217 RenderDeviceDriverIface::StartResult CGdiDisplayDriver::StartDIBits(
218 RetainPtr<const CFX_DIBBase> bitmap,
219 float alpha,
220 uint32_t color,
221 const CFX_Matrix& matrix,
222 const FXDIB_ResampleOptions& options,
223 BlendMode blend_type) {
224 return {Result::kNotSupported, nullptr};
225 }
226