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
fx_agg_driver.cpp
Go to the documentation of this file.
1// Copyright 2014 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/agg/fx_agg_driver.h"
8
9#include <math.h>
10#include <stdint.h>
11
12#include <algorithm>
13#include <utility>
14
15#include "build/build_config.h"
16#include "core/fxcrt/fx_2d_size.h"
17#include "core/fxcrt/fx_safe_types.h"
18#include "core/fxcrt/unowned_ptr_exclusion.h"
19#include "core/fxge/cfx_cliprgn.h"
20#include "core/fxge/cfx_defaultrenderdevice.h"
21#include "core/fxge/cfx_graphstatedata.h"
22#include "core/fxge/cfx_path.h"
23#include "core/fxge/dib/cfx_dibitmap.h"
24#include "core/fxge/dib/cfx_imagerenderer.h"
25#include "core/fxge/dib/cfx_imagestretcher.h"
26#include "third_party/base/check.h"
27#include "third_party/base/check_op.h"
28#include "third_party/base/containers/span.h"
29#include "third_party/base/notreached.h"
30
31// Ignore fallthrough warnings in agg23 headers.
32#if defined(__clang__)
33#pragma GCC diagnostic push
34#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
35#endif
36#include "third_party/agg23/agg_clip_liang_barsky.h"
37#include "third_party/agg23/agg_conv_dash.h"
38#include "third_party/agg23/agg_conv_stroke.h"
39#include "third_party/agg23/agg_curves.h"
40#include "third_party/agg23/agg_path_storage.h"
41#include "third_party/agg23/agg_pixfmt_gray.h"
42#include "third_party/agg23/agg_rasterizer_scanline_aa.h"
43#include "third_party/agg23/agg_renderer_scanline.h"
44#include "third_party/agg23/agg_scanline_u.h"
45#if defined(__clang__)
46#pragma GCC diagnostic pop
47#endif
48
49namespace pdfium {
50namespace {
51
52const float kMaxPos = 32000.0f;
53
54CFX_PointF HardClip(const CFX_PointF& pos) {
55 return CFX_PointF(std::clamp(pos.x, -kMaxPos, kMaxPos),
56 std::clamp(pos.y, -kMaxPos, kMaxPos));
57}
58
59void RgbByteOrderCompositeRect(const RetainPtr<CFX_DIBitmap>& pBitmap,
60 int left,
61 int top,
62 int width,
63 int height,
64 FX_ARGB argb) {
65 int src_alpha = FXARGB_A(argb);
66 if (src_alpha == 0)
67 return;
68
69 FX_RECT rect(left, top, left + width, top + height);
70 rect.Intersect(0, 0, pBitmap->GetWidth(), pBitmap->GetHeight());
71 width = rect.Width();
72 int src_r = FXARGB_R(argb);
73 int src_g = FXARGB_G(argb);
74 int src_b = FXARGB_B(argb);
75 int Bpp = pBitmap->GetBPP() / 8;
76 int dib_argb = FXARGB_TOBGRORDERDIB(argb);
77 pdfium::span<uint8_t> pBuffer = pBitmap->GetWritableBuffer();
78 if (src_alpha == 255) {
79 for (int row = rect.top; row < rect.bottom; row++) {
80 uint8_t* dest_scan =
81 pBuffer.subspan(row * pBitmap->GetPitch() + rect.left * Bpp).data();
82 if (Bpp == 4) {
83 std::fill_n(reinterpret_cast<uint32_t*>(dest_scan), width, dib_argb);
84 } else {
85 for (int col = 0; col < width; col++) {
86 *dest_scan++ = src_r;
87 *dest_scan++ = src_g;
88 *dest_scan++ = src_b;
89 }
90 }
91 }
92 return;
93 }
94 bool bAlpha = pBitmap->IsAlphaFormat();
95 for (int row = rect.top; row < rect.bottom; row++) {
96 uint8_t* dest_scan =
97 pBuffer.subspan(row * pBitmap->GetPitch() + rect.left * Bpp).data();
98 if (bAlpha) {
99 for (int col = 0; col < width; col++) {
100 uint8_t back_alpha = dest_scan[3];
101 if (back_alpha == 0) {
102 FXARGB_SETRGBORDERDIB(dest_scan, argb);
103 dest_scan += 4;
104 continue;
105 }
106 uint8_t dest_alpha =
107 back_alpha + src_alpha - back_alpha * src_alpha / 255;
108 dest_scan[3] = dest_alpha;
109 int alpha_ratio = src_alpha * 255 / dest_alpha;
110 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, src_r, alpha_ratio);
111 dest_scan++;
112 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, src_g, alpha_ratio);
113 dest_scan++;
114 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, src_b, alpha_ratio);
115 dest_scan += 2;
116 }
117 continue;
118 }
119 for (int col = 0; col < width; col++) {
120 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, src_r, src_alpha);
121 dest_scan++;
122 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, src_g, src_alpha);
123 dest_scan++;
124 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, src_b, src_alpha);
125 dest_scan++;
126 if (Bpp == 4)
127 dest_scan++;
128 }
129 }
130}
131
132void RgbByteOrderTransferBitmap(RetainPtr<CFX_DIBitmap> pBitmap,
133 int width,
134 int height,
135 RetainPtr<const CFX_DIBBase> pSrcBitmap,
136 int src_left,
137 int src_top) {
138 int dest_left = 0;
139 int dest_top = 0;
140 if (!pBitmap->GetOverlapRect(dest_left, dest_top, width, height,
141 pSrcBitmap->GetWidth(), pSrcBitmap->GetHeight(),
142 src_left, src_top, nullptr)) {
143 return;
144 }
145
146 const int Bpp = pBitmap->GetBPP() / 8;
147 const FXDIB_Format dest_format = pBitmap->GetFormat();
148 const FXDIB_Format src_format = pSrcBitmap->GetFormat();
149 const int dest_pitch = pBitmap->GetPitch();
150
151 const size_t dest_x_offset = Fx2DSizeOrDie(dest_left, Bpp);
152 const size_t dest_y_offset = Fx2DSizeOrDie(dest_top, dest_pitch);
153
154 pdfium::span<uint8_t> dest_span = pBitmap->GetWritableBuffer()
155 .subspan(dest_y_offset)
156 .subspan(dest_x_offset);
157 if (dest_format == src_format) {
158 const size_t src_x_offset = Fx2DSizeOrDie(src_left, Bpp);
159 for (int row = 0; row < height; row++) {
160 uint8_t* dest_scan = dest_span.data();
161 const uint8_t* src_scan =
162 pSrcBitmap->GetScanline(src_top + row).subspan(src_x_offset).data();
163 if (Bpp == 4) {
164 for (int col = 0; col < width; col++) {
165 FXARGB_SETRGBORDERDIB(dest_scan,
166 *reinterpret_cast<const uint32_t*>(src_scan));
167 dest_scan += 4;
168 src_scan += 4;
169 }
170 } else {
171 for (int col = 0; col < width; col++) {
172 *dest_scan++ = src_scan[2];
173 *dest_scan++ = src_scan[1];
174 *dest_scan++ = src_scan[0];
175 src_scan += 3;
176 }
177 }
178 dest_span = dest_span.subspan(dest_pitch);
179 }
180 return;
181 }
182
183 if (dest_format == FXDIB_Format::kRgb) {
184 DCHECK_EQ(src_format, FXDIB_Format::kRgb32);
185 const size_t src_x_offset = Fx2DSizeOrDie(src_left, 4);
186 for (int row = 0; row < height; row++) {
187 uint8_t* dest_scan = dest_span.data();
188 const uint8_t* src_scan =
189 pSrcBitmap->GetScanline(src_top + row).subspan(src_x_offset).data();
190 for (int col = 0; col < width; col++) {
191 *dest_scan++ = src_scan[2];
192 *dest_scan++ = src_scan[1];
193 *dest_scan++ = src_scan[0];
194 src_scan += 4;
195 }
196 if (row < height - 1) {
197 // Since `dest_scan` was initialized in a way that takes `dest_x_offset`
198 // and `dest_y_offset` into account, it may go past the end of the span
199 // after processing the last row.
200 dest_span = dest_span.subspan(dest_pitch);
201 }
202 }
203 return;
204 }
205
206 DCHECK(dest_format == FXDIB_Format::kArgb ||
207 dest_format == FXDIB_Format::kRgb32);
208 if (src_format == FXDIB_Format::kRgb) {
209 const size_t src_x_offset = Fx2DSizeOrDie(src_left, 3);
210 for (int row = 0; row < height; row++) {
211 uint8_t* dest_scan = dest_span.data();
212 const uint8_t* src_scan =
213 pSrcBitmap->GetScanline(src_top + row).subspan(src_x_offset).data();
214 for (int col = 0; col < width; col++) {
215 FXARGB_SETDIB(dest_scan,
216 ArgbEncode(0xff, src_scan[0], src_scan[1], src_scan[2]));
217 dest_scan += 4;
218 src_scan += 3;
219 }
220 dest_span = dest_span.subspan(dest_pitch);
221 }
222 return;
223 }
224 if (src_format != FXDIB_Format::kRgb32)
225 return;
226 DCHECK_EQ(dest_format, FXDIB_Format::kArgb);
227 const size_t src_x_offset = Fx2DSizeOrDie(src_left, 4);
228 for (int row = 0; row < height; row++) {
229 uint8_t* dest_scan = dest_span.data();
230 const uint8_t* src_scan =
231 pSrcBitmap->GetScanline(src_top + row).subspan(src_x_offset).data();
232 for (int col = 0; col < width; col++) {
233 FXARGB_SETDIB(dest_scan,
234 ArgbEncode(0xff, src_scan[0], src_scan[1], src_scan[2]));
235 src_scan += 4;
236 dest_scan += 4;
237 }
238 dest_span = dest_span.subspan(dest_pitch);
239 }
240}
241
242void RasterizeStroke(agg::rasterizer_scanline_aa* rasterizer,
243 agg::path_storage* path_data,
244 const CFX_Matrix* pObject2Device,
245 const CFX_GraphStateData* pGraphState,
246 float scale,
247 bool bTextMode) {
248 agg::line_cap_e cap;
249 switch (pGraphState->m_LineCap) {
251 cap = agg::round_cap;
252 break;
254 cap = agg::square_cap;
255 break;
256 default:
257 cap = agg::butt_cap;
258 break;
259 }
260 agg::line_join_e join;
261 switch (pGraphState->m_LineJoin) {
263 join = agg::round_join;
264 break;
266 join = agg::bevel_join;
267 break;
268 default:
269 join = agg::miter_join_revert;
270 break;
271 }
272 float width = pGraphState->m_LineWidth * scale;
273 float unit = 1.0f;
274 if (pObject2Device) {
275 unit =
276 1.0f / ((pObject2Device->GetXUnit() + pObject2Device->GetYUnit()) / 2);
277 }
278 width = std::max(width, unit);
279 if (!pGraphState->m_DashArray.empty()) {
280 using DashConverter = agg::conv_dash<agg::path_storage>;
281 DashConverter dash(*path_data);
282 for (size_t i = 0; i < (pGraphState->m_DashArray.size() + 1) / 2; i++) {
283 float on = pGraphState->m_DashArray[i * 2];
284 if (on <= 0.000001f)
285 on = 0.1f;
286 float off = i * 2 + 1 == pGraphState->m_DashArray.size()
287 ? on
288 : pGraphState->m_DashArray[i * 2 + 1];
289 off = std::max(off, 0.0f);
290 dash.add_dash(on * scale, off * scale);
291 }
292 dash.dash_start(pGraphState->m_DashPhase * scale);
293 using DashStroke = agg::conv_stroke<DashConverter>;
294 DashStroke stroke(dash);
295 stroke.line_join(join);
296 stroke.line_cap(cap);
297 stroke.miter_limit(pGraphState->m_MiterLimit);
298 stroke.width(width);
299 rasterizer->add_path_transformed(stroke, pObject2Device);
300 return;
301 }
302 agg::conv_stroke<agg::path_storage> stroke(*path_data);
303 stroke.line_join(join);
304 stroke.line_cap(cap);
305 stroke.miter_limit(pGraphState->m_MiterLimit);
306 stroke.width(width);
307 rasterizer->add_path_transformed(stroke, pObject2Device);
308}
309
310agg::filling_rule_e GetAlternateOrWindingFillType(
311 const CFX_FillRenderOptions& fill_options) {
313 ? agg::fill_non_zero
314 : agg::fill_even_odd;
315}
316
317RetainPtr<CFX_DIBitmap> GetClipMaskFromRegion(const CFX_ClipRgn* r) {
318 return (r && r->GetType() == CFX_ClipRgn::kMaskF) ? r->GetMask() : nullptr;
319}
320
321FX_RECT GetClipBoxFromRegion(const RetainPtr<CFX_DIBitmap>& device,
322 const CFX_ClipRgn* region) {
323 if (region)
324 return region->GetBox();
325 return FX_RECT(0, 0, device->GetWidth(), device->GetHeight());
326}
327
328class CFX_Renderer {
329 public:
330 CFX_Renderer(const RetainPtr<CFX_DIBitmap>& pDevice,
331 const RetainPtr<CFX_DIBitmap>& pBackdropDevice,
332 const CFX_ClipRgn* pClipRgn,
333 uint32_t color,
334 bool bFullCover,
335 bool bRgbByteOrder);
336
337 // Needed for agg caller
338 void prepare(unsigned) {}
339
340 template <class Scanline>
341 void render(const Scanline& sl);
342
343 private:
344 using CompositeSpanFunc = void (CFX_Renderer::*)(uint8_t*,
345 int,
346 int,
347 int,
348 const uint8_t*,
349 int,
350 int,
351 const uint8_t*);
352
353 void CompositeSpan(uint8_t* dest_scan,
354 const uint8_t* backdrop_scan,
355 int Bpp,
356 bool bDestAlpha,
357 int span_left,
358 int span_len,
359 const uint8_t* cover_scan,
360 int clip_left,
361 int clip_right,
362 const uint8_t* clip_scan);
363
364 void CompositeSpan1bpp(uint8_t* dest_scan,
365 int Bpp,
366 int span_left,
367 int span_len,
368 const uint8_t* cover_scan,
369 int clip_left,
370 int clip_right,
371 const uint8_t* clip_scan);
372
373 void CompositeSpanGray(uint8_t* dest_scan,
374 int Bpp,
375 int span_left,
376 int span_len,
377 const uint8_t* cover_scan,
378 int clip_left,
379 int clip_right,
380 const uint8_t* clip_scan);
381
382 void CompositeSpanARGB(uint8_t* dest_scan,
383 int Bpp,
384 int span_left,
385 int span_len,
386 const uint8_t* cover_scan,
387 int clip_left,
388 int clip_right,
389 const uint8_t* clip_scan);
390
391 void CompositeSpanRGB(uint8_t* dest_scan,
392 int Bpp,
393 int span_left,
394 int span_len,
395 const uint8_t* cover_scan,
396 int clip_left,
397 int clip_right,
398 const uint8_t* clip_scan);
399
400 void CompositeSpan1bppHelper(uint8_t* dest_scan,
401 int col_start,
402 int col_end,
403 const uint8_t* cover_scan,
404 const uint8_t* clip_scan,
405 int span_left);
406
407 static CompositeSpanFunc GetCompositeSpanFunc(
408 const RetainPtr<CFX_DIBitmap>& device) {
409 if (device->GetBPP() == 1)
410 return &CFX_Renderer::CompositeSpan1bpp;
411 if (device->GetBPP() == 8)
412 return &CFX_Renderer::CompositeSpanGray;
413 if (device->GetFormat() == FXDIB_Format::kArgb)
414 return &CFX_Renderer::CompositeSpanARGB;
415 return &CFX_Renderer::CompositeSpanRGB;
416 }
417
418 inline int GetSrcAlpha(const uint8_t* clip_scan, int col) const {
419 return clip_scan ? m_Alpha * clip_scan[col] / 255 : m_Alpha;
420 }
421
422 inline int GetSourceAlpha(const uint8_t* cover_scan,
423 const uint8_t* clip_scan,
424 int col) const {
425 return clip_scan ? m_Alpha * cover_scan[col] * clip_scan[col] / 255 / 255
426 : m_Alpha * cover_scan[col] / 255;
427 }
428
429 inline int GetColStart(int span_left, int clip_left) const {
430 return span_left < clip_left ? clip_left - span_left : 0;
431 }
432
433 inline int GetColEnd(int span_left, int span_len, int clip_right) const {
434 return span_left + span_len < clip_right ? span_len
435 : clip_right - span_left;
436 }
437
438 int m_Alpha;
439 int m_Red;
440 int m_Green;
441 int m_Blue;
442 int m_Gray;
443 const uint32_t m_Color;
444 const bool m_bFullCover;
445 const bool m_bRgbByteOrder;
446 const FX_RECT m_ClipBox;
447 RetainPtr<CFX_DIBitmap> const m_pBackdropDevice;
448 RetainPtr<CFX_DIBitmap> const m_pClipMask;
449 RetainPtr<CFX_DIBitmap> const m_pDevice;
450 UnownedPtr<const CFX_ClipRgn> m_pClipRgn;
451 const CompositeSpanFunc m_CompositeSpanFunc;
452};
453
454void CFX_Renderer::CompositeSpan(uint8_t* dest_scan,
455 const uint8_t* backdrop_scan,
456 int Bpp,
457 bool bDestAlpha,
458 int span_left,
459 int span_len,
460 const uint8_t* cover_scan,
461 int clip_left,
462 int clip_right,
463 const uint8_t* clip_scan) {
464 int col_start = GetColStart(span_left, clip_left);
465 int col_end = GetColEnd(span_left, span_len, clip_right);
466 if (Bpp) {
467 dest_scan += col_start * Bpp;
468 backdrop_scan += col_start * Bpp;
469 } else {
470 dest_scan += col_start / 8;
471 backdrop_scan += col_start / 8;
472 }
473 if (m_bRgbByteOrder) {
474 if (Bpp == 4 && bDestAlpha) {
475 for (int col = col_start; col < col_end; col++) {
476 int src_alpha = GetSrcAlpha(clip_scan, col);
477 uint8_t dest_alpha =
478 backdrop_scan[3] + src_alpha - backdrop_scan[3] * src_alpha / 255;
479 dest_scan[3] = dest_alpha;
480 int alpha_ratio = src_alpha * 255 / dest_alpha;
481 if (m_bFullCover) {
482 *dest_scan++ =
483 FXDIB_ALPHA_MERGE(*backdrop_scan++, m_Red, alpha_ratio);
484 *dest_scan++ =
485 FXDIB_ALPHA_MERGE(*backdrop_scan++, m_Green, alpha_ratio);
486 *dest_scan++ =
487 FXDIB_ALPHA_MERGE(*backdrop_scan++, m_Blue, alpha_ratio);
488 dest_scan++;
489 backdrop_scan++;
490 } else {
491 int r = FXDIB_ALPHA_MERGE(*backdrop_scan++, m_Red, alpha_ratio);
492 int g = FXDIB_ALPHA_MERGE(*backdrop_scan++, m_Green, alpha_ratio);
493 int b = FXDIB_ALPHA_MERGE(*backdrop_scan++, m_Blue, alpha_ratio);
494 backdrop_scan++;
495 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, r, cover_scan[col]);
496 dest_scan++;
497 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, g, cover_scan[col]);
498 dest_scan++;
499 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, b, cover_scan[col]);
500 dest_scan += 2;
501 }
502 }
503 return;
504 }
505 if (Bpp == 3 || Bpp == 4) {
506 for (int col = col_start; col < col_end; col++) {
507 int src_alpha = GetSrcAlpha(clip_scan, col);
508 int r = FXDIB_ALPHA_MERGE(*backdrop_scan++, m_Red, src_alpha);
509 int g = FXDIB_ALPHA_MERGE(*backdrop_scan++, m_Green, src_alpha);
510 int b = FXDIB_ALPHA_MERGE(*backdrop_scan, m_Blue, src_alpha);
511 backdrop_scan += Bpp - 2;
512 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, r, cover_scan[col]);
513 dest_scan++;
514 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, g, cover_scan[col]);
515 dest_scan++;
516 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, b, cover_scan[col]);
517 dest_scan += Bpp - 2;
518 }
519 }
520 return;
521 }
522 if (Bpp == 4 && bDestAlpha) {
523 for (int col = col_start; col < col_end; col++) {
524 int src_alpha = GetSrcAlpha(clip_scan, col);
525 int src_alpha_covered = src_alpha * cover_scan[col] / 255;
526 if (src_alpha_covered == 0) {
527 dest_scan += 4;
528 continue;
529 }
530 if (cover_scan[col] == 255) {
531 dest_scan[3] = src_alpha_covered;
532 *dest_scan++ = m_Blue;
533 *dest_scan++ = m_Green;
534 *dest_scan = m_Red;
535 dest_scan += 2;
536 continue;
537 }
538 if (dest_scan[3] == 0) {
539 dest_scan[3] = src_alpha_covered;
540 *dest_scan++ = m_Blue;
541 *dest_scan++ = m_Green;
542 *dest_scan = m_Red;
543 dest_scan += 2;
544 continue;
545 }
546 uint8_t cover = cover_scan[col];
547 dest_scan[3] = FXDIB_ALPHA_MERGE(dest_scan[3], src_alpha, cover);
548 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Blue, cover);
549 dest_scan++;
550 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Green, cover);
551 dest_scan++;
552 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Red, cover);
553 dest_scan += 2;
554 }
555 return;
556 }
557 if (Bpp == 3 || Bpp == 4) {
558 for (int col = col_start; col < col_end; col++) {
559 int src_alpha = GetSrcAlpha(clip_scan, col);
560 if (m_bFullCover) {
561 *dest_scan++ = FXDIB_ALPHA_MERGE(*backdrop_scan++, m_Blue, src_alpha);
562 *dest_scan++ = FXDIB_ALPHA_MERGE(*backdrop_scan++, m_Green, src_alpha);
563 *dest_scan = FXDIB_ALPHA_MERGE(*backdrop_scan, m_Red, src_alpha);
564 dest_scan += Bpp - 2;
565 backdrop_scan += Bpp - 2;
566 continue;
567 }
568 int b = FXDIB_ALPHA_MERGE(*backdrop_scan++, m_Blue, src_alpha);
569 int g = FXDIB_ALPHA_MERGE(*backdrop_scan++, m_Green, src_alpha);
570 int r = FXDIB_ALPHA_MERGE(*backdrop_scan, m_Red, src_alpha);
571 backdrop_scan += Bpp - 2;
572 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, b, cover_scan[col]);
573 dest_scan++;
574 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, g, cover_scan[col]);
575 dest_scan++;
576 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, r, cover_scan[col]);
577 dest_scan += Bpp - 2;
578 }
579 return;
580 }
581 if (Bpp == 1) {
582 for (int col = col_start; col < col_end; col++) {
583 int src_alpha = GetSrcAlpha(clip_scan, col);
584 if (m_bFullCover) {
585 *dest_scan = FXDIB_ALPHA_MERGE(*backdrop_scan++, m_Gray, src_alpha);
586 continue;
587 }
588 int gray = FXDIB_ALPHA_MERGE(*backdrop_scan++, m_Gray, src_alpha);
589 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, gray, cover_scan[col]);
590 dest_scan++;
591 }
592 return;
593 }
594 CompositeSpan1bppHelper(dest_scan, col_start, col_end, cover_scan, clip_scan,
595 span_left);
596}
597
598void CFX_Renderer::CompositeSpan1bpp(uint8_t* dest_scan,
599 int Bpp,
600 int span_left,
601 int span_len,
602 const uint8_t* cover_scan,
603 int clip_left,
604 int clip_right,
605 const uint8_t* clip_scan) {
606 DCHECK(!m_bRgbByteOrder);
607 int col_start = GetColStart(span_left, clip_left);
608 int col_end = GetColEnd(span_left, span_len, clip_right);
609 dest_scan += col_start / 8;
610 CompositeSpan1bppHelper(dest_scan, col_start, col_end, cover_scan, clip_scan,
611 span_left);
612}
613
614void CFX_Renderer::CompositeSpanGray(uint8_t* dest_scan,
615 int Bpp,
616 int span_left,
617 int span_len,
618 const uint8_t* cover_scan,
619 int clip_left,
620 int clip_right,
621 const uint8_t* clip_scan) {
622 DCHECK(!m_bRgbByteOrder);
623 int col_start = GetColStart(span_left, clip_left);
624 int col_end = GetColEnd(span_left, span_len, clip_right);
625 dest_scan += col_start;
626 for (int col = col_start; col < col_end; col++) {
627 int src_alpha = GetSourceAlpha(cover_scan, clip_scan, col);
628 if (src_alpha) {
629 if (src_alpha == 255)
630 *dest_scan = m_Gray;
631 else
632 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Gray, src_alpha);
633 }
634 dest_scan++;
635 }
636}
637
638void CFX_Renderer::CompositeSpanARGB(uint8_t* dest_scan,
639 int Bpp,
640 int span_left,
641 int span_len,
642 const uint8_t* cover_scan,
643 int clip_left,
644 int clip_right,
645 const uint8_t* clip_scan) {
646 int col_start = GetColStart(span_left, clip_left);
647 int col_end = GetColEnd(span_left, span_len, clip_right);
648 dest_scan += col_start * Bpp;
649 if (m_bRgbByteOrder) {
650 for (int col = col_start; col < col_end; col++) {
651 int src_alpha = m_bFullCover ? GetSrcAlpha(clip_scan, col)
652 : GetSourceAlpha(cover_scan, clip_scan, col);
653 if (src_alpha) {
654 if (src_alpha == 255) {
655 *(reinterpret_cast<uint32_t*>(dest_scan)) = m_Color;
656 } else {
657 uint8_t dest_alpha =
658 dest_scan[3] + src_alpha - dest_scan[3] * src_alpha / 255;
659 dest_scan[3] = dest_alpha;
660 int alpha_ratio = src_alpha * 255 / dest_alpha;
661 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Red, alpha_ratio);
662 dest_scan++;
663 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Green, alpha_ratio);
664 dest_scan++;
665 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Blue, alpha_ratio);
666 dest_scan += 2;
667 continue;
668 }
669 }
670 dest_scan += 4;
671 }
672 return;
673 }
674 for (int col = col_start; col < col_end; col++) {
675 int src_alpha = m_bFullCover ? GetSrcAlpha(clip_scan, col)
676 : GetSourceAlpha(cover_scan, clip_scan, col);
677 if (src_alpha) {
678 if (src_alpha == 255) {
679 *(reinterpret_cast<uint32_t*>(dest_scan)) = m_Color;
680 } else {
681 if (dest_scan[3] == 0) {
682 dest_scan[3] = src_alpha;
683 *dest_scan++ = m_Blue;
684 *dest_scan++ = m_Green;
685 *dest_scan = m_Red;
686 dest_scan += 2;
687 continue;
688 }
689 uint8_t dest_alpha =
690 dest_scan[3] + src_alpha - dest_scan[3] * src_alpha / 255;
691 dest_scan[3] = dest_alpha;
692 int alpha_ratio = src_alpha * 255 / dest_alpha;
693 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Blue, alpha_ratio);
694 dest_scan++;
695 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Green, alpha_ratio);
696 dest_scan++;
697 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Red, alpha_ratio);
698 dest_scan += 2;
699 continue;
700 }
701 }
702 dest_scan += Bpp;
703 }
704}
705
706void CFX_Renderer::CompositeSpanRGB(uint8_t* dest_scan,
707 int Bpp,
708 int span_left,
709 int span_len,
710 const uint8_t* cover_scan,
711 int clip_left,
712 int clip_right,
713 const uint8_t* clip_scan) {
714 int col_start = GetColStart(span_left, clip_left);
715 int col_end = GetColEnd(span_left, span_len, clip_right);
716 dest_scan += col_start * Bpp;
717 if (m_bRgbByteOrder) {
718 for (int col = col_start; col < col_end; col++) {
719 int src_alpha = GetSourceAlpha(cover_scan, clip_scan, col);
720 if (src_alpha) {
721 if (src_alpha == 255) {
722 if (Bpp == 4) {
723 *(uint32_t*)dest_scan = m_Color;
724 } else if (Bpp == 3) {
725 *dest_scan++ = m_Red;
726 *dest_scan++ = m_Green;
727 *dest_scan++ = m_Blue;
728 continue;
729 }
730 } else {
731 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Red, src_alpha);
732 dest_scan++;
733 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Green, src_alpha);
734 dest_scan++;
735 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Blue, src_alpha);
736 dest_scan += Bpp - 2;
737 continue;
738 }
739 }
740 dest_scan += Bpp;
741 }
742 return;
743 }
744 for (int col = col_start; col < col_end; col++) {
745 int src_alpha = m_bFullCover ? GetSrcAlpha(clip_scan, col)
746 : GetSourceAlpha(cover_scan, clip_scan, col);
747 if (src_alpha) {
748 if (src_alpha == 255) {
749 if (Bpp == 4) {
750 *(uint32_t*)dest_scan = m_Color;
751 } else if (Bpp == 3) {
752 *dest_scan++ = m_Blue;
753 *dest_scan++ = m_Green;
754 *dest_scan++ = m_Red;
755 continue;
756 }
757 } else {
758 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Blue, src_alpha);
759 dest_scan++;
760 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Green, src_alpha);
761 dest_scan++;
762 *dest_scan = FXDIB_ALPHA_MERGE(*dest_scan, m_Red, src_alpha);
763 dest_scan += Bpp - 2;
764 continue;
765 }
766 }
767 dest_scan += Bpp;
768 }
769}
770
771CFX_Renderer::CFX_Renderer(const RetainPtr<CFX_DIBitmap>& pDevice,
772 const RetainPtr<CFX_DIBitmap>& pBackdropDevice,
773 const CFX_ClipRgn* pClipRgn,
774 uint32_t color,
775 bool bFullCover,
776 bool bRgbByteOrder)
777 : m_Alpha(FXARGB_A(color)),
778 m_Color(bRgbByteOrder ? FXARGB_TOBGRORDERDIB(color) : color),
779 m_bFullCover(bFullCover),
780 m_bRgbByteOrder(bRgbByteOrder),
781 m_ClipBox(GetClipBoxFromRegion(pDevice, pClipRgn)),
782 m_pBackdropDevice(pBackdropDevice),
783 m_pClipMask(GetClipMaskFromRegion(pClipRgn)),
784 m_pDevice(pDevice),
785 m_pClipRgn(pClipRgn),
786 m_CompositeSpanFunc(GetCompositeSpanFunc(m_pDevice)) {
787 if (m_pDevice->GetBPP() == 8) {
788 DCHECK(!m_bRgbByteOrder);
789 if (m_pDevice->IsMaskFormat())
790 m_Gray = 255;
791 else
792 m_Gray = FXRGB2GRAY(FXARGB_R(color), FXARGB_G(color), FXARGB_B(color));
793 return;
794 }
795
796 std::tie(m_Alpha, m_Red, m_Green, m_Blue) = ArgbDecode(color);
797}
798
799template <class Scanline>
800void CFX_Renderer::render(const Scanline& sl) {
801 int y = sl.y();
802 if (y < m_ClipBox.top || y >= m_ClipBox.bottom)
803 return;
804
805 uint8_t* dest_scan =
806 m_pDevice->GetWritableBuffer().subspan(m_pDevice->GetPitch() * y).data();
807 const uint8_t* backdrop_scan = nullptr;
808 if (m_pBackdropDevice) {
809 backdrop_scan = m_pBackdropDevice->GetBuffer()
810 .subspan(m_pBackdropDevice->GetPitch() * y)
811 .data();
812 }
813 int Bpp = m_pDevice->GetBPP() / 8;
814 bool bDestAlpha = m_pDevice->IsAlphaFormat() || m_pDevice->IsMaskFormat();
815 unsigned num_spans = sl.num_spans();
816 typename Scanline::const_iterator span = sl.begin();
817 while (true) {
818 if (span->len <= 0)
819 break;
820
821 int x = span->x;
822 uint8_t* dest_pos = nullptr;
823 const uint8_t* backdrop_pos = nullptr;
824 if (Bpp) {
825 backdrop_pos = backdrop_scan ? backdrop_scan + x * Bpp : nullptr;
826 dest_pos = dest_scan + x * Bpp;
827 } else {
828 dest_pos = dest_scan + x / 8;
829 backdrop_pos = backdrop_scan ? backdrop_scan + x / 8 : nullptr;
830 }
831 const uint8_t* clip_pos = nullptr;
832 if (m_pClipMask) {
833 // TODO(crbug.com/1382604): use subspan arithmetic.
834 clip_pos = m_pClipMask->GetBuffer().data() +
835 (y - m_ClipBox.top) * m_pClipMask->GetPitch() + x -
836 m_ClipBox.left;
837 }
838 if (backdrop_pos) {
839 CompositeSpan(dest_pos, backdrop_pos, Bpp, bDestAlpha, x, span->len,
840 span->covers, m_ClipBox.left, m_ClipBox.right, clip_pos);
841 } else {
842 (this->*m_CompositeSpanFunc)(dest_pos, Bpp, x, span->len, span->covers,
843 m_ClipBox.left, m_ClipBox.right, clip_pos);
844 }
845 if (--num_spans == 0)
846 break;
847
848 ++span;
849 }
850}
851
852void CFX_Renderer::CompositeSpan1bppHelper(uint8_t* dest_scan,
853 int col_start,
854 int col_end,
855 const uint8_t* cover_scan,
856 const uint8_t* clip_scan,
857 int span_left) {
858 int index = 0;
859 if (m_pDevice->HasPalette()) {
860 for (int i = 0; i < 2; i++) {
861 if (m_pDevice->GetPaletteSpan()[i] == m_Color)
862 index = i;
863 }
864 } else {
865 index = (static_cast<uint8_t>(m_Color) == 0xff) ? 1 : 0;
866 }
867 uint8_t* dest_scan1 = dest_scan;
868 for (int col = col_start; col < col_end; col++) {
869 int src_alpha = GetSourceAlpha(cover_scan, clip_scan, col);
870 if (src_alpha) {
871 if (!index)
872 *dest_scan1 &= ~(1 << (7 - (col + span_left) % 8));
873 else
874 *dest_scan1 |= 1 << (7 - (col + span_left) % 8);
875 }
876 dest_scan1 = dest_scan + (span_left % 8 + col - col_start + 1) / 8;
877 }
878}
879
880template <class BaseRenderer>
881class RendererScanLineAaOffset {
882 public:
883 typedef BaseRenderer base_ren_type;
884 typedef typename base_ren_type::color_type color_type;
885 RendererScanLineAaOffset(base_ren_type& ren, unsigned left, unsigned top)
886 : m_ren(&ren), m_left(left), m_top(top) {}
887 void color(const color_type& c) { m_color = c; }
888 const color_type& color() const { return m_color; }
889 void prepare(unsigned) {}
890 template <class Scanline>
891 void render(const Scanline& sl) {
892 int y = sl.y();
893 unsigned num_spans = sl.num_spans();
894 typename Scanline::const_iterator span = sl.begin();
895 while (true) {
896 int x = span->x;
897 if (span->len > 0) {
898 m_ren->blend_solid_hspan(x - m_left, y - m_top, (unsigned)span->len,
899 m_color, span->covers);
900 } else {
901 m_ren->blend_hline(x - m_left, y - m_top, (unsigned)(x - span->len - 1),
902 m_color, *(span->covers));
903 }
904 if (--num_spans == 0)
905 break;
906
907 ++span;
908 }
909 }
910
911 private:
912 UNOWNED_PTR_EXCLUSION base_ren_type* m_ren;
913 color_type m_color;
914 unsigned m_left;
915 unsigned m_top;
916};
917
918agg::path_storage BuildAggPath(const CFX_Path& path,
919 const CFX_Matrix* pObject2Device) {
920 agg::path_storage agg_path;
921 pdfium::span<const CFX_Path::Point> points = path.GetPoints();
922 for (size_t i = 0; i < points.size(); ++i) {
923 CFX_PointF pos = points[i].m_Point;
924 if (pObject2Device)
925 pos = pObject2Device->Transform(pos);
926
927 pos = HardClip(pos);
928 CFX_Path::Point::Type point_type = points[i].m_Type;
929 if (point_type == CFX_Path::Point::Type::kMove) {
930 agg_path.move_to(pos.x, pos.y);
931 } else if (point_type == CFX_Path::Point::Type::kLine) {
932 if (i > 0 && points[i - 1].IsTypeAndOpen(CFX_Path::Point::Type::kMove) &&
933 (i + 1 == points.size() ||
934 points[i + 1].IsTypeAndOpen(CFX_Path::Point::Type::kMove)) &&
935 points[i].m_Point == points[i - 1].m_Point) {
936 pos.x += 1;
937 }
938 agg_path.line_to(pos.x, pos.y);
939 } else if (point_type == CFX_Path::Point::Type::kBezier) {
940 if (i > 0 && i + 2 < points.size()) {
941 CFX_PointF pos0 = points[i - 1].m_Point;
942 CFX_PointF pos2 = points[i + 1].m_Point;
943 CFX_PointF pos3 = points[i + 2].m_Point;
944 if (pObject2Device) {
945 pos0 = pObject2Device->Transform(pos0);
946 pos2 = pObject2Device->Transform(pos2);
947 pos3 = pObject2Device->Transform(pos3);
948 }
949 pos0 = HardClip(pos0);
950 pos2 = HardClip(pos2);
951 pos3 = HardClip(pos3);
952 agg::curve4 curve(pos0.x, pos0.y, pos.x, pos.y, pos2.x, pos2.y, pos3.x,
953 pos3.y);
954 i += 2;
955 agg_path.add_path(curve);
956 }
957 }
958 if (points[i].m_CloseFigure)
959 agg_path.end_poly();
960 }
961 return agg_path;
962}
963
964} // namespace
965
966CFX_AggDeviceDriver::CFX_AggDeviceDriver(
967 RetainPtr<CFX_DIBitmap> pBitmap,
968 bool bRgbByteOrder,
969 RetainPtr<CFX_DIBitmap> pBackdropBitmap,
970 bool bGroupKnockout)
972 m_bRgbByteOrder(bRgbByteOrder),
973 m_bGroupKnockout(bGroupKnockout),
975 DCHECK(m_pBitmap);
976 DCHECK_NE(m_pBitmap->GetFormat(), FXDIB_Format::k1bppMask);
977 DCHECK_NE(m_pBitmap->GetFormat(), FXDIB_Format::k1bppRgb);
979}
980
981CFX_AggDeviceDriver::~CFX_AggDeviceDriver() {
983}
984
985#if !BUILDFLAG(IS_APPLE)
986void CFX_AggDeviceDriver::InitPlatform() {}
987
988void CFX_AggDeviceDriver::DestroyPlatform() {}
989
990bool CFX_AggDeviceDriver::DrawDeviceText(
991 pdfium::span<const TextCharPos> pCharPos,
992 CFX_Font* pFont,
993 const CFX_Matrix& mtObject2Device,
994 float font_size,
995 uint32_t color,
996 const CFX_TextRenderOptions& options) {
997 return false;
998}
999#endif // !BUILDFLAG(IS_APPLE)
1000
1001DeviceType CFX_AggDeviceDriver::GetDeviceType() const {
1002 return DeviceType::kDisplay;
1003}
1004
1005int CFX_AggDeviceDriver::GetDeviceCaps(int caps_id) const {
1006 switch (caps_id) {
1007 case FXDC_PIXEL_WIDTH:
1008 return m_pBitmap->GetWidth();
1009 case FXDC_PIXEL_HEIGHT:
1010 return m_pBitmap->GetHeight();
1011 case FXDC_BITS_PIXEL:
1012 return m_pBitmap->GetBPP();
1013 case FXDC_HORZ_SIZE:
1014 case FXDC_VERT_SIZE:
1015 return 0;
1016 case FXDC_RENDER_CAPS: {
1019 if (m_pBitmap->IsAlphaFormat()) {
1020 flags |= FXRC_ALPHA_OUTPUT;
1021 } else if (m_pBitmap->IsMaskFormat()) {
1022 CHECK_NE(m_pBitmap->GetBPP(), 1); // Matches format CHECKs in the ctor.
1023 flags |= FXRC_BYTEMASK_OUTPUT;
1024 }
1025 return flags;
1026 }
1027 default:
1028 NOTREACHED_NORETURN();
1029 }
1030}
1031
1032void CFX_AggDeviceDriver::SaveState() {
1033 std::unique_ptr<CFX_ClipRgn> pClip;
1034 if (m_pClipRgn)
1035 pClip = std::make_unique<CFX_ClipRgn>(*m_pClipRgn);
1036 m_StateStack.push_back(std::move(pClip));
1037}
1038
1039void CFX_AggDeviceDriver::RestoreState(bool bKeepSaved) {
1040 m_pClipRgn.reset();
1041
1042 if (m_StateStack.empty())
1043 return;
1044
1045 if (bKeepSaved) {
1046 if (m_StateStack.back())
1047 m_pClipRgn = std::make_unique<CFX_ClipRgn>(*m_StateStack.back());
1048 } else {
1049 m_pClipRgn = std::move(m_StateStack.back());
1050 m_StateStack.pop_back();
1051 }
1052}
1053
1054void CFX_AggDeviceDriver::SetClipMask(agg::rasterizer_scanline_aa& rasterizer) {
1055 FX_RECT path_rect(rasterizer.min_x(), rasterizer.min_y(),
1056 rasterizer.max_x() + 1, rasterizer.max_y() + 1);
1057 path_rect.Intersect(m_pClipRgn->GetBox());
1058 auto pThisLayer = pdfium::MakeRetain<CFX_DIBitmap>();
1059 pThisLayer->Create(path_rect.Width(), path_rect.Height(),
1061 agg::rendering_buffer raw_buf(pThisLayer->GetWritableBuffer().data(),
1062 pThisLayer->GetWidth(), pThisLayer->GetHeight(),
1063 pThisLayer->GetPitch());
1064 agg::pixfmt_gray8 pixel_buf(raw_buf);
1065 agg::renderer_base<agg::pixfmt_gray8> base_buf(pixel_buf);
1066 RendererScanLineAaOffset<agg::renderer_base<agg::pixfmt_gray8>> final_render(
1067 base_buf, path_rect.left, path_rect.top);
1068 final_render.color(agg::gray8(255));
1069 agg::scanline_u8 scanline;
1070 agg::render_scanlines(rasterizer, scanline, final_render,
1071 m_FillOptions.aliased_path);
1072 m_pClipRgn->IntersectMaskF(path_rect.left, path_rect.top,
1073 std::move(pThisLayer));
1074}
1075
1076bool CFX_AggDeviceDriver::SetClip_PathFill(
1077 const CFX_Path& path,
1078 const CFX_Matrix* pObject2Device,
1079 const CFX_FillRenderOptions& fill_options) {
1080 DCHECK(fill_options.fill_type != CFX_FillRenderOptions::FillType::kNoFill);
1081
1082 m_FillOptions = fill_options;
1083 if (!m_pClipRgn) {
1084 m_pClipRgn = std::make_unique<CFX_ClipRgn>(
1085 GetDeviceCaps(FXDC_PIXEL_WIDTH), GetDeviceCaps(FXDC_PIXEL_HEIGHT));
1086 }
1087 absl::optional<CFX_FloatRect> maybe_rectf = path.GetRect(pObject2Device);
1088 if (maybe_rectf.has_value()) {
1089 CFX_FloatRect& rectf = maybe_rectf.value();
1090 rectf.Intersect(
1092 static_cast<float>(GetDeviceCaps(FXDC_PIXEL_HEIGHT))));
1093 FX_RECT rect = rectf.GetOuterRect();
1094 m_pClipRgn->IntersectRect(rect);
1095 return true;
1096 }
1097 agg::path_storage path_data = BuildAggPath(path, pObject2Device);
1098 path_data.end_poly();
1099 agg::rasterizer_scanline_aa rasterizer;
1100 rasterizer.clip_box(0.0f, 0.0f,
1101 static_cast<float>(GetDeviceCaps(FXDC_PIXEL_WIDTH)),
1102 static_cast<float>(GetDeviceCaps(FXDC_PIXEL_HEIGHT)));
1103 rasterizer.add_path(path_data);
1104 rasterizer.filling_rule(GetAlternateOrWindingFillType(fill_options));
1105 SetClipMask(rasterizer);
1106 return true;
1107}
1108
1109bool CFX_AggDeviceDriver::SetClip_PathStroke(
1110 const CFX_Path& path,
1111 const CFX_Matrix* pObject2Device,
1112 const CFX_GraphStateData* pGraphState) {
1113 if (!m_pClipRgn) {
1114 m_pClipRgn = std::make_unique<CFX_ClipRgn>(
1115 GetDeviceCaps(FXDC_PIXEL_WIDTH), GetDeviceCaps(FXDC_PIXEL_HEIGHT));
1116 }
1117 agg::path_storage path_data = BuildAggPath(path, nullptr);
1118 agg::rasterizer_scanline_aa rasterizer;
1119 rasterizer.clip_box(0.0f, 0.0f,
1120 static_cast<float>(GetDeviceCaps(FXDC_PIXEL_WIDTH)),
1121 static_cast<float>(GetDeviceCaps(FXDC_PIXEL_HEIGHT)));
1122 RasterizeStroke(&rasterizer, &path_data, pObject2Device, pGraphState, 1.0f,
1123 false);
1124 rasterizer.filling_rule(agg::fill_non_zero);
1125 SetClipMask(rasterizer);
1126 return true;
1127}
1128
1129int CFX_AggDeviceDriver::GetDriverType() const {
1130 return 1;
1131}
1132
1133bool CFX_AggDeviceDriver::MultiplyAlpha(float alpha) {
1134 return m_pBitmap->MultiplyAlpha(alpha);
1135}
1136
1137bool CFX_AggDeviceDriver::MultiplyAlphaMask(
1138 const RetainPtr<const CFX_DIBBase>& mask) {
1139 return m_pBitmap->MultiplyAlphaMask(std::move(mask));
1140}
1141
1142void CFX_AggDeviceDriver::Clear(uint32_t color) {
1143 m_pBitmap->Clear(color);
1144}
1145
1146void CFX_AggDeviceDriver::RenderRasterizer(
1147 agg::rasterizer_scanline_aa& rasterizer,
1148 uint32_t color,
1149 bool bFullCover,
1150 bool bGroupKnockout) {
1151 RetainPtr<CFX_DIBitmap> pt = bGroupKnockout ? m_pBackdropBitmap : nullptr;
1152 CFX_Renderer render(m_pBitmap, pt, m_pClipRgn.get(), color, bFullCover,
1153 m_bRgbByteOrder);
1154 agg::scanline_u8 scanline;
1155 agg::render_scanlines(rasterizer, scanline, render,
1156 m_FillOptions.aliased_path);
1157}
1158
1159bool CFX_AggDeviceDriver::DrawPath(const CFX_Path& path,
1160 const CFX_Matrix* pObject2Device,
1161 const CFX_GraphStateData* pGraphState,
1162 uint32_t fill_color,
1163 uint32_t stroke_color,
1164 const CFX_FillRenderOptions& fill_options,
1165 BlendMode blend_type) {
1166 if (blend_type != BlendMode::kNormal)
1167 return false;
1168
1169 if (m_pBitmap->GetBuffer().empty())
1170 return true;
1171
1172 m_FillOptions = fill_options;
1174 fill_color) {
1175 agg::path_storage path_data = BuildAggPath(path, pObject2Device);
1176 agg::rasterizer_scanline_aa rasterizer;
1177 rasterizer.clip_box(0.0f, 0.0f,
1178 static_cast<float>(GetDeviceCaps(FXDC_PIXEL_WIDTH)),
1179 static_cast<float>(GetDeviceCaps(FXDC_PIXEL_HEIGHT)));
1180 rasterizer.add_path(path_data);
1181 rasterizer.filling_rule(GetAlternateOrWindingFillType(fill_options));
1182 RenderRasterizer(rasterizer, fill_color, fill_options.full_cover,
1183 /*bGroupKnockout=*/false);
1184 }
1185 int stroke_alpha = FXARGB_A(stroke_color);
1186 if (!pGraphState || !stroke_alpha)
1187 return true;
1188
1189 if (fill_options.zero_area) {
1190 agg::path_storage path_data = BuildAggPath(path, pObject2Device);
1191 agg::rasterizer_scanline_aa rasterizer;
1192 rasterizer.clip_box(0.0f, 0.0f,
1193 static_cast<float>(GetDeviceCaps(FXDC_PIXEL_WIDTH)),
1194 static_cast<float>(GetDeviceCaps(FXDC_PIXEL_HEIGHT)));
1195 RasterizeStroke(&rasterizer, &path_data, nullptr, pGraphState, 1,
1196 fill_options.stroke_text_mode);
1197 RenderRasterizer(rasterizer, stroke_color, fill_options.full_cover,
1198 m_bGroupKnockout);
1199 return true;
1200 }
1201 CFX_Matrix matrix1;
1202 CFX_Matrix matrix2;
1203 if (pObject2Device) {
1204 matrix1.a = std::max(fabs(pObject2Device->a), fabs(pObject2Device->b));
1205 matrix1.d = matrix1.a;
1206 matrix2 = CFX_Matrix(
1207 pObject2Device->a / matrix1.a, pObject2Device->b / matrix1.a,
1208 pObject2Device->c / matrix1.d, pObject2Device->d / matrix1.d, 0, 0);
1209
1210 matrix1 = *pObject2Device * matrix2.GetInverse();
1211 }
1212
1213 agg::path_storage path_data = BuildAggPath(path, &matrix1);
1214 agg::rasterizer_scanline_aa rasterizer;
1215 rasterizer.clip_box(0.0f, 0.0f,
1216 static_cast<float>(GetDeviceCaps(FXDC_PIXEL_WIDTH)),
1217 static_cast<float>(GetDeviceCaps(FXDC_PIXEL_HEIGHT)));
1218 RasterizeStroke(&rasterizer, &path_data, &matrix2, pGraphState, matrix1.a,
1219 fill_options.stroke_text_mode);
1220 RenderRasterizer(rasterizer, stroke_color, fill_options.full_cover,
1221 m_bGroupKnockout);
1222 return true;
1223}
1224
1225bool CFX_AggDeviceDriver::FillRectWithBlend(const FX_RECT& rect,
1226 uint32_t fill_color,
1227 BlendMode blend_type) {
1228 if (blend_type != BlendMode::kNormal)
1229 return false;
1230
1231 if (m_pBitmap->GetBuffer().empty())
1232 return true;
1233
1234 FX_RECT clip_rect;
1235 GetClipBox(&clip_rect);
1236 FX_RECT draw_rect = clip_rect;
1237 draw_rect.Intersect(rect);
1238 if (draw_rect.IsEmpty())
1239 return true;
1240
1241 if (!m_pClipRgn || m_pClipRgn->GetType() == CFX_ClipRgn::kRectI) {
1242 if (m_bRgbByteOrder) {
1243 RgbByteOrderCompositeRect(m_pBitmap, draw_rect.left, draw_rect.top,
1244 draw_rect.Width(), draw_rect.Height(),
1245 fill_color);
1246 } else {
1247 m_pBitmap->CompositeRect(draw_rect.left, draw_rect.top, draw_rect.Width(),
1248 draw_rect.Height(), fill_color);
1249 }
1250 return true;
1251 }
1252 m_pBitmap->CompositeMask(draw_rect.left, draw_rect.top, draw_rect.Width(),
1253 draw_rect.Height(), m_pClipRgn->GetMask(),
1254 fill_color, draw_rect.left - clip_rect.left,
1255 draw_rect.top - clip_rect.top, BlendMode::kNormal,
1256 nullptr, m_bRgbByteOrder);
1257 return true;
1258}
1259
1260bool CFX_AggDeviceDriver::GetClipBox(FX_RECT* pRect) {
1261 if (!m_pClipRgn) {
1262 pRect->left = pRect->top = 0;
1265 return true;
1266 }
1267 *pRect = m_pClipRgn->GetBox();
1268 return true;
1269}
1270
1271bool CFX_AggDeviceDriver::GetDIBits(const RetainPtr<CFX_DIBitmap>& pBitmap,
1272 int left,
1273 int top) {
1274 if (m_pBitmap->GetBuffer().empty())
1275 return true;
1276
1277 FX_RECT rect(left, top, left + pBitmap->GetWidth(),
1278 top + pBitmap->GetHeight());
1279 RetainPtr<CFX_DIBitmap> pBack;
1280 if (m_pBackdropBitmap) {
1281 pBack = m_pBackdropBitmap->ClipTo(rect);
1282 if (!pBack)
1283 return true;
1284
1285 pBack->CompositeBitmap(0, 0, pBack->GetWidth(), pBack->GetHeight(),
1286 m_pBitmap, 0, 0, BlendMode::kNormal, nullptr, false);
1287 } else {
1288 pBack = m_pBitmap->ClipTo(rect);
1289 if (!pBack)
1290 return true;
1291 }
1292
1293 left = std::min(left, 0);
1294 top = std::min(top, 0);
1295 if (m_bRgbByteOrder) {
1296 RgbByteOrderTransferBitmap(std::move(pBitmap), rect.Width(), rect.Height(),
1297 std::move(pBack), left, top);
1298 return true;
1299 }
1300 return pBitmap->TransferBitmap(0, 0, rect.Width(), rect.Height(),
1301 std::move(pBack), left, top);
1302}
1303
1304RetainPtr<CFX_DIBitmap> CFX_AggDeviceDriver::GetBackDrop() {
1305 return m_pBackdropBitmap;
1306}
1307
1308bool CFX_AggDeviceDriver::SetDIBits(const RetainPtr<const CFX_DIBBase>& pBitmap,
1309 uint32_t argb,
1310 const FX_RECT& src_rect,
1311 int left,
1312 int top,
1313 BlendMode blend_type) {
1314 if (m_pBitmap->GetBuffer().empty())
1315 return true;
1316
1317 if (pBitmap->IsMaskFormat()) {
1318 return m_pBitmap->CompositeMask(left, top, src_rect.Width(),
1319 src_rect.Height(), std::move(pBitmap), argb,
1320 src_rect.left, src_rect.top, blend_type,
1321 m_pClipRgn.get(), m_bRgbByteOrder);
1322 }
1323 return m_pBitmap->CompositeBitmap(left, top, src_rect.Width(),
1324 src_rect.Height(), std::move(pBitmap),
1325 src_rect.left, src_rect.top, blend_type,
1326 m_pClipRgn.get(), m_bRgbByteOrder);
1327}
1328
1329bool CFX_AggDeviceDriver::StretchDIBits(RetainPtr<const CFX_DIBBase> bitmap,
1330 uint32_t argb,
1331 int dest_left,
1332 int dest_top,
1333 int dest_width,
1334 int dest_height,
1335 const FX_RECT* pClipRect,
1336 const FXDIB_ResampleOptions& options,
1337 BlendMode blend_type) {
1338 if (m_pBitmap->GetBuffer().empty())
1339 return true;
1340
1341 if (dest_width == bitmap->GetWidth() && dest_height == bitmap->GetHeight()) {
1342 FX_RECT rect(0, 0, dest_width, dest_height);
1343 return SetDIBits(std::move(bitmap), argb, rect, dest_left, dest_top,
1344 blend_type);
1345 }
1346 FX_RECT dest_rect(dest_left, dest_top, dest_left + dest_width,
1347 dest_top + dest_height);
1348 dest_rect.Normalize();
1349 FX_RECT dest_clip = dest_rect;
1350 dest_clip.Intersect(*pClipRect);
1351 CFX_BitmapComposer composer;
1352 composer.Compose(m_pBitmap, m_pClipRgn.get(), /*alpha=*/1.0f, argb, dest_clip,
1353 /*bVertical=*/false, /*bFlipX=*/false, /*bFlipY=*/false,
1354 m_bRgbByteOrder, blend_type);
1355 dest_clip.Offset(-dest_rect.left, -dest_rect.top);
1356 CFX_ImageStretcher stretcher(&composer, std::move(bitmap), dest_width,
1357 dest_height, dest_clip, options);
1358 if (stretcher.Start())
1359 stretcher.Continue(nullptr);
1360 return true;
1361}
1362
1363bool CFX_AggDeviceDriver::StartDIBits(
1364 RetainPtr<const CFX_DIBBase> bitmap,
1365 float alpha,
1366 uint32_t argb,
1367 const CFX_Matrix& matrix,
1368 const FXDIB_ResampleOptions& options,
1369 std::unique_ptr<CFX_ImageRenderer>* handle,
1370 BlendMode blend_type) {
1371 if (m_pBitmap->GetBuffer().empty())
1372 return true;
1373
1374 *handle = std::make_unique<CFX_ImageRenderer>(
1375 m_pBitmap, m_pClipRgn.get(), std::move(bitmap), alpha, argb, matrix,
1376 options, m_bRgbByteOrder);
1377 return true;
1378}
1379
1380bool CFX_AggDeviceDriver::ContinueDIBits(CFX_ImageRenderer* pHandle,
1381 PauseIndicatorIface* pPause) {
1382 return m_pBitmap->GetBuffer().empty() || pHandle->Continue(pPause);
1383}
1384
1385} // namespace pdfium
1386
1387bool CFX_DefaultRenderDevice::AttachAggImpl(
1388 RetainPtr<CFX_DIBitmap> pBitmap,
1389 bool bRgbByteOrder,
1390 RetainPtr<CFX_DIBitmap> pBackdropBitmap,
1391 bool bGroupKnockout) {
1392 // Unlike the Skia version, all callers pass in a non-null `pBitmap`.
1393 CHECK(pBitmap);
1394 SetBitmap(pBitmap);
1395 SetDeviceDriver(std::make_unique<pdfium::CFX_AggDeviceDriver>(
1396 std::move(pBitmap), bRgbByteOrder, std::move(pBackdropBitmap),
1397 bGroupKnockout));
1398 return true;
1399}
1400
1401bool CFX_DefaultRenderDevice::CreateAgg(
1402 int width,
1403 int height,
1404 FXDIB_Format format,
1405 RetainPtr<CFX_DIBitmap> pBackdropBitmap) {
1406 auto pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
1407 if (!pBitmap->Create(width, height, format))
1408 return false;
1409
1410 SetBitmap(pBitmap);
1411 SetDeviceDriver(std::make_unique<pdfium::CFX_AggDeviceDriver>(
1412 std::move(pBitmap), false, std::move(pBackdropBitmap), false));
1413 return true;
1414}
ClipType GetType() const
Definition cfx_cliprgn.h:23
const FX_RECT & GetBox() const
Definition cfx_cliprgn.h:24
constexpr CFX_FloatRect(float l, float b, float r, float t)
void Intersect(const CFX_FloatRect &other_rect)
FX_RECT GetOuterRect() const
bool Continue(PauseIndicatorIface *pPause)
CFX_Matrix & operator=(const CFX_Matrix &other)=default
CFX_Matrix(float a1, float b1, float c1, float d1, float e1, float f1)
CFX_Matrix operator*(const CFX_Matrix &right) const
CFX_PointF Transform(const CFX_PointF &point) const
float GetXUnit() const
CFX_Matrix GetInverse() const
float GetYUnit() const
void SetBitmap(RetainPtr< CFX_DIBitmap > bitmap)
bool FillRectWithBlend(const FX_RECT &rect, uint32_t fill_color, BlendMode blend_type) override
CFX_AggDeviceDriver(RetainPtr< CFX_DIBitmap > pBitmap, bool bRgbByteOrder, RetainPtr< CFX_DIBitmap > pBackdropBitmap, bool bGroupKnockout)
bool ContinueDIBits(CFX_ImageRenderer *handle, PauseIndicatorIface *pPause) override
void RestoreState(bool bKeepSaved) override
bool StartDIBits(RetainPtr< const CFX_DIBBase > bitmap, float alpha, uint32_t argb, const CFX_Matrix &matrix, const FXDIB_ResampleOptions &options, std::unique_ptr< CFX_ImageRenderer > *handle, BlendMode blend_type) override
bool GetClipBox(FX_RECT *pRect) override
int GetDeviceCaps(int caps_id) const override
bool GetDIBits(const RetainPtr< CFX_DIBitmap > &pBitmap, int left, int top) override
RetainPtr< CFX_DIBitmap > GetBackDrop() override
bool SetClip_PathStroke(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_GraphStateData *pGraphState) override
int GetDriverType() const override
bool MultiplyAlpha(float alpha) override
DeviceType GetDeviceType() const override
bool DrawPath(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_GraphStateData *pGraphState, uint32_t fill_color, uint32_t stroke_color, const CFX_FillRenderOptions &fill_options, BlendMode blend_type) override
bool MultiplyAlphaMask(const RetainPtr< const CFX_DIBBase > &mask) override
bool SetDIBits(const RetainPtr< const CFX_DIBBase > &pBitmap, uint32_t argb, const FX_RECT &src_rect, int left, int top, BlendMode blend_type) override
bool StretchDIBits(RetainPtr< const CFX_DIBBase > bitmap, uint32_t argb, 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 SetClip_PathFill(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_FillRenderOptions &fill_options) override
BlendMode
Definition fx_dib.h:49
#define FXRGB2GRAY(r, g, b)
Definition fx_dib.h:131
#define FXARGB_B(argb)
Definition fx_dib.h:127
#define FXARGB_G(argb)
Definition fx_dib.h:126
#define FXARGB_SETRGBORDERDIB(p, argb)
Definition fx_dib.h:142
#define FXARGB_A(argb)
Definition fx_dib.h:124
constexpr FX_ARGB ArgbEncode(uint32_t a, uint32_t r, uint32_t g, uint32_t b)
Definition fx_dib.h:118
#define FXARGB_TOBGRORDERDIB(argb)
Definition fx_dib.h:150
#define FXARGB_SETDIB(p, argb)
Definition fx_dib.h:137
#define FXARGB_R(argb)
Definition fx_dib.h:125
FXDIB_Format
Definition fx_dib.h:19
#define FXDIB_ALPHA_MERGE(backdrop, source, source_alpha)
Definition fx_dib.h:132
#define CHECK(cvref)
#define FXDC_BITS_PIXEL
#define FXDC_RENDER_CAPS
#define FXDC_PIXEL_WIDTH
#define FXRC_ALPHA_OUTPUT
#define FXDC_VERT_SIZE
#define FXDC_PIXEL_HEIGHT
#define FXDC_HORZ_SIZE
#define FXRC_ALPHA_PATH
#define FXRC_BLEND_MODE
#define FXRC_ALPHA_IMAGE
#define FXRC_GET_BITS
#define FXRC_BYTEMASK_OUTPUT
#define FXRC_SOFT_CLIP
void Offset(int dx, int dy)
int Height() const
int32_t bottom
int32_t right
int Width() const
void Normalize()
int32_t top
int32_t left
void Intersect(const FX_RECT &src)
bool IsEmpty() const
constexpr FX_RECT(int l, int t, int r, int b)
#define UNOWNED_PTR_EXCLUSION