Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cgdi_display_driver.cpp
Go to the documentation of this file.
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/fx_coordinates.h"
12#include "core/fxcrt/fx_system.h"
13#include "core/fxge/dib/cfx_dibbase.h"
14#include "core/fxge/dib/cfx_dibitmap.h"
15#include "core/fxge/render_defines.h"
16#include "core/fxge/win32/cwin32_platform.h"
17#include "third_party/base/check.h"
18#include "third_party/base/check_op.h"
19
20CGdiDisplayDriver::CGdiDisplayDriver(HDC hDC)
22 auto* pPlatform =
23 static_cast<CWin32Platform*>(CFX_GEModule::Get()->GetPlatform());
24 if (pPlatform->m_GdiplusExt.IsAvailable()) {
26 }
27}
28
29CGdiDisplayDriver::~CGdiDisplayDriver() = default;
30
31int CGdiDisplayDriver::GetDeviceCaps(int caps_id) const {
32 if (caps_id == FXDC_HORZ_SIZE || caps_id == FXDC_VERT_SIZE)
33 return 0;
34 return CGdiDeviceDriver::GetDeviceCaps(caps_id);
35}
36
37bool CGdiDisplayDriver::GetDIBits(const RetainPtr<CFX_DIBitmap>& pBitmap,
38 int left,
39 int top) {
40 bool ret = false;
41 int width = pBitmap->GetWidth();
42 int height = pBitmap->GetHeight();
43 HBITMAP hbmp = CreateCompatibleBitmap(m_hDC, width, height);
44 HDC hDCMemory = CreateCompatibleDC(m_hDC);
45 HBITMAP holdbmp = (HBITMAP)SelectObject(hDCMemory, hbmp);
46 BitBlt(hDCMemory, 0, 0, width, height, m_hDC, left, top, SRCCOPY);
47 SelectObject(hDCMemory, holdbmp);
48 BITMAPINFO bmi;
49 memset(&bmi, 0, sizeof bmi);
50 bmi.bmiHeader.biSize = sizeof bmi.bmiHeader;
51 bmi.bmiHeader.biBitCount = pBitmap->GetBPP();
52 bmi.bmiHeader.biHeight = -height;
53 bmi.bmiHeader.biPlanes = 1;
54 bmi.bmiHeader.biWidth = width;
55 if (pBitmap->GetBPP() > 8) {
56 ret = ::GetDIBits(hDCMemory, hbmp, 0, height,
57 pBitmap->GetWritableBuffer().data(), &bmi,
58 DIB_RGB_COLORS) == height;
59 } else {
60 auto rgb_bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
61 if (rgb_bitmap->Create(width, height, FXDIB_Format::kRgb)) {
62 bmi.bmiHeader.biBitCount = 24;
63 ::GetDIBits(hDCMemory, hbmp, 0, height,
64 rgb_bitmap->GetWritableBuffer().data(), &bmi, DIB_RGB_COLORS);
65 ret = pBitmap->TransferBitmap(0, 0, width, height, std::move(rgb_bitmap),
66 0, 0);
67 } else {
68 ret = false;
69 }
70 }
71 if (ret && pBitmap->IsAlphaFormat())
72 pBitmap->SetUniformOpaqueAlpha();
73
74 DeleteObject(hbmp);
75 DeleteObject(hDCMemory);
76 return ret;
77}
78
79bool CGdiDisplayDriver::SetDIBits(const RetainPtr<const CFX_DIBBase>& pSource,
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 (pSource->IsMaskFormat()) {
87 int width = pSource->GetWidth();
88 int height = pSource->GetHeight();
89 int alpha = FXARGB_A(color);
90 if (pSource->GetBPP() != 1 || alpha != 255) {
91 auto background = pdfium::MakeRetain<CFX_DIBitmap>();
92 if (!background->Create(width, height, FXDIB_Format::kRgb32) ||
93 !GetDIBits(background, left, top) ||
94 !background->CompositeMask(0, 0, width, height, std::move(pSource),
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(background, 0, alpha_src_rect, left, top,
102 }
103 FX_RECT clip_rect(left, top, left + src_rect.Width(),
104 top + src_rect.Height());
105 return StretchDIBits(std::move(pSource), color, left - src_rect.left,
106 top - src_rect.top, width, height, &clip_rect,
108 }
109 int width = src_rect.Width();
110 int height = src_rect.Height();
111 if (pSource->IsAlphaFormat()) {
112 auto bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
113 if (!bitmap->Create(width, height, FXDIB_Format::kRgb) ||
114 !GetDIBits(bitmap, left, top) ||
115 !bitmap->CompositeBitmap(0, 0, width, height, std::move(pSource),
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(bitmap, 0, alpha_src_rect, left, top, BlendMode::kNormal);
122 }
123 return GDI_SetDIBits(std::move(pSource), src_rect, left, top);
124}
125
126bool CGdiDisplayDriver::UseFoxitStretchEngine(
127 RetainPtr<const CFX_DIBBase> bitmap,
128 uint32_t color,
129 int dest_left,
130 int dest_top,
131 int dest_width,
132 int dest_height,
133 const FX_RECT* pClipRect,
134 const FXDIB_ResampleOptions& options) {
135 FX_RECT bitmap_clip = *pClipRect;
136 if (dest_width < 0)
137 dest_left += dest_width;
138
139 if (dest_height < 0)
140 dest_top += dest_height;
141
142 bitmap_clip.Offset(-dest_left, -dest_top);
143 bitmap = bitmap->StretchTo(dest_width, dest_height, options, &bitmap_clip);
144 if (!bitmap) {
145 return true;
146 }
147
148 FX_RECT src_rect(0, 0, bitmap->GetWidth(), bitmap->GetHeight());
149 return SetDIBits(std::move(bitmap), color, src_rect, pClipRect->left,
151}
152
153bool CGdiDisplayDriver::StretchDIBits(RetainPtr<const CFX_DIBBase> bitmap,
154 uint32_t color,
155 int dest_left,
156 int dest_top,
157 int dest_width,
158 int dest_height,
159 const FX_RECT* pClipRect,
160 const FXDIB_ResampleOptions& options,
161 BlendMode blend_type) {
162 DCHECK(bitmap);
163 DCHECK(pClipRect);
164
165 if (options.HasAnyOptions() || dest_width > 10000 || dest_width < -10000 ||
166 dest_height > 10000 || dest_height < -10000) {
167 return UseFoxitStretchEngine(std::move(bitmap), color, dest_left, dest_top,
168 dest_width, dest_height, pClipRect, options);
169 }
170 if (bitmap->IsMaskFormat()) {
171 FX_RECT image_rect;
172 image_rect.left = dest_width > 0 ? dest_left : dest_left + dest_width;
173 image_rect.right = dest_width > 0 ? dest_left + dest_width : dest_left;
174 image_rect.top = dest_height > 0 ? dest_top : dest_top + dest_height;
175 image_rect.bottom = dest_height > 0 ? dest_top + dest_height : dest_top;
176 FX_RECT clip_rect = image_rect;
177 clip_rect.Intersect(*pClipRect);
178 clip_rect.Offset(-image_rect.left, -image_rect.top);
179 int clip_width = clip_rect.Width(), clip_height = clip_rect.Height();
180 bitmap = bitmap->StretchTo(dest_width, dest_height, FXDIB_ResampleOptions(),
181 &clip_rect);
182 if (!bitmap) {
183 return true;
184 }
185
186 auto background = pdfium::MakeRetain<CFX_DIBitmap>();
187 if (!background->Create(clip_width, clip_height, FXDIB_Format::kRgb32) ||
188 !GetDIBits(background, image_rect.left + clip_rect.left,
189 image_rect.top + clip_rect.top) ||
190 !background->CompositeMask(0, 0, clip_width, clip_height,
191 std::move(bitmap), color, 0, 0,
192 BlendMode::kNormal, nullptr, false)) {
193 return false;
194 }
195
196 FX_RECT src_rect(0, 0, clip_width, clip_height);
197 return SetDIBits(background, 0, src_rect, image_rect.left + clip_rect.left,
198 image_rect.top + clip_rect.top, BlendMode::kNormal);
199 }
200 if (bitmap->IsAlphaFormat()) {
201 auto* pPlatform =
202 static_cast<CWin32Platform*>(CFX_GEModule::Get()->GetPlatform());
203 if (pPlatform->m_GdiplusExt.IsAvailable()) {
204 return pPlatform->m_GdiplusExt.StretchDIBits(
205 m_hDC, std::move(bitmap), dest_left, dest_top, dest_width,
206 dest_height, pClipRect, FXDIB_ResampleOptions());
207 }
208 return UseFoxitStretchEngine(std::move(bitmap), color, dest_left, dest_top,
209 dest_width, dest_height, pClipRect,
211 }
212 return GDI_StretchDIBits(std::move(bitmap), dest_left, dest_top, dest_width,
213 dest_height, FXDIB_ResampleOptions());
214}
215
216bool CGdiDisplayDriver::StartDIBits(RetainPtr<const CFX_DIBBase> bitmap,
217 float alpha,
218 uint32_t color,
219 const CFX_Matrix& matrix,
220 const FXDIB_ResampleOptions& options,
221 std::unique_ptr<CFX_ImageRenderer>* handle,
222 BlendMode blend_type) {
223 return false;
224}
PlatformIface * GetPlatform() const
static CFX_GEModule * Get()
bool GDI_SetDIBits(const RetainPtr< const CFX_DIBBase > &source, const FX_RECT &src_rect, int left, int top)
bool GDI_StretchDIBits(RetainPtr< const CFX_DIBBase > source, int dest_left, int dest_top, int dest_width, int dest_height, const FXDIB_ResampleOptions &options)
int GetDeviceCaps(int caps_id) const override
~CGdiDisplayDriver() override
int GetDeviceCaps(int caps_id) const override
bool SetDIBits(const RetainPtr< const CFX_DIBBase > &pBitmap, uint32_t color, const FX_RECT &src_rect, int left, int top, BlendMode blend_type) override
bool 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) override
bool GetDIBits(const RetainPtr< CFX_DIBitmap > &pBitmap, int left, int top) override
bool StartDIBits(RetainPtr< const CFX_DIBBase > bitmap, float alpha, uint32_t color, const CFX_Matrix &matrix, const FXDIB_ResampleOptions &options, std::unique_ptr< CFX_ImageRenderer > *handle, BlendMode blend_type) override
BlendMode
Definition fx_dib.h:49
#define FXARGB_A(argb)
Definition fx_dib.h:124
FXDIB_Format
Definition fx_dib.h:19
#define FXDC_VERT_SIZE
#define FXDC_HORZ_SIZE
#define FXRC_ALPHA_PATH
#define FXRC_ALPHA_IMAGE
bool HasAnyOptions() const
Definition fx_dib.cpp:40
void Offset(int dx, int dy)
int Height() const
int32_t bottom
int32_t right
int Width() const
int32_t top
int32_t left
void Intersect(const FX_RECT &src)
constexpr FX_RECT(int l, int t, int r, int b)