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_skia_device_embeddertest.cpp
Go to the documentation of this file.
1// Copyright 2016 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#include "core/fxge/skia/fx_skia_device.h"
6
7#include <memory>
8#include <set>
9#include <utility>
10
11#include "core/fpdfapi/page/cpdf_page.h"
12#include "core/fpdfapi/render/cpdf_pagerendercontext.h"
13#include "core/fxcrt/fx_codepage.h"
14#include "core/fxge/cfx_defaultrenderdevice.h"
15#include "core/fxge/cfx_fillrenderoptions.h"
16#include "core/fxge/cfx_font.h"
17#include "core/fxge/cfx_graphstatedata.h"
18#include "core/fxge/cfx_path.h"
19#include "core/fxge/cfx_renderdevice.h"
20#include "core/fxge/cfx_textrenderoptions.h"
21#include "core/fxge/dib/cfx_dibitmap.h"
22#include "core/fxge/text_char_pos.h"
23#include "fpdfsdk/cpdfsdk_helpers.h"
24#include "fpdfsdk/cpdfsdk_renderpage.h"
25#include "public/cpp/fpdf_scopers.h"
26#include "public/fpdfview.h"
27#include "testing/embedder_test.h"
28#include "testing/gmock/include/gmock/gmock.h"
29#include "testing/gtest/include/gtest/gtest.h"
30#include "third_party/skia/include/core/SkCanvas.h"
31#include "third_party/skia/include/core/SkImage.h"
32#include "third_party/skia/include/core/SkSize.h"
33#include "third_party/skia/include/utils/SkNoDrawCanvas.h"
34
35namespace {
36
37using ::testing::NiceMock;
38using ::testing::SizeIs;
39using ::testing::WithArg;
40
41struct State {
42 enum class Change { kNo, kYes };
43 enum class Save { kNo, kYes };
44 enum class Clip { kNo, kSame, kDifferentPath, kDifferentMatrix };
45 enum class Graphic { kNone, kPath, kText };
46
47 Change m_change;
48 Save m_save;
49 Clip m_clip;
50 Graphic m_graphic;
51 uint32_t m_pixel;
52};
53
54void EmptyTest(CFX_SkiaDeviceDriver* driver, const State&) {
55 driver->SaveState();
56 driver->RestoreState(true);
57 driver->RestoreState(false);
58}
59
60void CommonTest(CFX_SkiaDeviceDriver* driver, const State& state) {
61 TextCharPos charPos[1];
62 charPos[0].m_Origin = CFX_PointF(0, 1);
63 charPos[0].m_GlyphIndex = 0;
64 charPos[0].m_FontCharWidth = 4;
65
66 CFX_Font font;
67 font.LoadSubst("Courier", /*bTrueType=*/true, /*flags=*/0,
68 /*weight=*/400, /*italic_angle=*/0, FX_CodePage::kShiftJIS,
69 /*bVertical=*/false);
70 float fontSize = 20;
71 CFX_Path clipPath;
72 CFX_Path clipPath2;
73 clipPath.AppendRect(0, 0, 3, 1);
74 clipPath2.AppendRect(0, 0, 2, 1);
75 CFX_Matrix clipMatrix;
76 CFX_Matrix clipMatrix2(1, 0, 0, 1, 0, 1);
77 driver->SaveState();
78 CFX_Path path1;
79 path1.AppendRect(0, 0, 1, 2);
80
81 CFX_Matrix matrix;
82 CFX_Matrix matrix2;
83 matrix2.Translate(1, 0);
84 CFX_GraphStateData graphState;
85 // Turn off anti-aliasing so that pixels with transitional colors can be
86 // avoided.
87 static constexpr CFX_TextRenderOptions kTextOptions(
89 if (state.m_save == State::Save::kYes)
90 driver->SaveState();
91 if (state.m_clip != State::Clip::kNo)
92 driver->SetClip_PathFill(clipPath, &clipMatrix, CFX_FillRenderOptions());
93 if (state.m_graphic == State::Graphic::kPath) {
94 driver->DrawPath(path1, &matrix, &graphState, 0xFF112233, 0,
97 } else if (state.m_graphic == State::Graphic::kText) {
98 driver->DrawDeviceText(charPos, &font, matrix, fontSize, 0xFF445566,
99 kTextOptions);
100 }
101 if (state.m_save == State::Save::kYes)
102 driver->RestoreState(true);
103 CFX_Path path2;
104 path2.AppendRect(0, 0, 2, 2);
105 if (state.m_change == State::Change::kYes) {
106 if (state.m_graphic == State::Graphic::kPath)
108 else if (state.m_graphic == State::Graphic::kText)
109 fontSize = 2;
110 }
111 if (state.m_clip == State::Clip::kSame)
112 driver->SetClip_PathFill(clipPath, &clipMatrix, CFX_FillRenderOptions());
113 else if (state.m_clip == State::Clip::kDifferentPath)
114 driver->SetClip_PathFill(clipPath2, &clipMatrix, CFX_FillRenderOptions());
115 else if (state.m_clip == State::Clip::kDifferentMatrix)
116 driver->SetClip_PathFill(clipPath, &clipMatrix2, CFX_FillRenderOptions());
117 if (state.m_graphic == State::Graphic::kPath) {
118 driver->DrawPath(path2, &matrix2, &graphState, 0xFF112233, 0,
121 } else if (state.m_graphic == State::Graphic::kText) {
122 driver->DrawDeviceText(charPos, &font, matrix2, fontSize, 0xFF445566,
123 kTextOptions);
124 }
125 if (state.m_save == State::Save::kYes)
126 driver->RestoreState(false);
127 driver->RestoreState(false);
128}
129
130void OutOfSequenceClipTest(CFX_SkiaDeviceDriver* driver, const State&) {
131 CFX_Path clipPath;
132 clipPath.AppendRect(1, 0, 3, 1);
133 CFX_Matrix clipMatrix;
134 driver->SaveState();
135 driver->SetClip_PathFill(clipPath, &clipMatrix, CFX_FillRenderOptions());
136 driver->RestoreState(true);
137 driver->SaveState();
138 driver->SetClip_PathFill(clipPath, &clipMatrix, CFX_FillRenderOptions());
139 driver->RestoreState(false);
140 driver->RestoreState(false);
141
142 driver->SaveState();
143 driver->SaveState();
144 driver->SetClip_PathFill(clipPath, &clipMatrix, CFX_FillRenderOptions());
145 driver->RestoreState(true);
146 driver->SetClip_PathFill(clipPath, &clipMatrix, CFX_FillRenderOptions());
147 driver->RestoreState(false);
148 driver->RestoreState(false);
149}
150
151void Harness(void (*Test)(CFX_SkiaDeviceDriver*, const State&),
152 const State& state) {
153 constexpr int kWidth = 4;
154 constexpr int kHeight = 1;
155 ScopedFPDFBitmap bitmap(FPDFBitmap_Create(kWidth, kHeight, 1));
156 ASSERT_TRUE(bitmap);
157 FPDFBitmap_FillRect(bitmap.get(), 0, 0, kWidth, kHeight, 0x00000000);
158 RetainPtr<CFX_DIBitmap> pBitmap(CFXDIBitmapFromFPDFBitmap(bitmap.get()));
159 auto driver = CFX_SkiaDeviceDriver::Create(pBitmap, false, nullptr, false);
160 ASSERT_TRUE(driver);
161 (*Test)(driver.get(), state);
162 uint32_t pixel = pBitmap->GetPixelForTesting(0, 0);
163 EXPECT_EQ(state.m_pixel, pixel);
164}
165
166void RenderPageToSkCanvas(FPDF_PAGE page,
167 int start_x,
168 int start_y,
169 int size_x,
170 int size_y,
171 SkCanvas* canvas) {
172 CPDF_Page* cpdf_page = CPDFPageFromFPDFPage(page);
173
174 auto context = std::make_unique<CPDF_PageRenderContext>();
175 CPDF_PageRenderContext* unowned_context = context.get();
176
177 CPDF_Page::RenderContextClearer clearer(cpdf_page);
178 cpdf_page->SetRenderContext(std::move(context));
179
180 auto default_device = std::make_unique<CFX_DefaultRenderDevice>();
181 default_device->AttachCanvas(canvas);
182 unowned_context->m_pDevice = std::move(default_device);
183
184 CPDFSDK_RenderPageWithContext(unowned_context, cpdf_page, start_x, start_y,
185 size_x, size_y, /*rotate=*/0, /*flags=*/0,
186 /*color_scheme=*/nullptr,
187 /*need_to_restore=*/true, /*pause=*/nullptr);
188}
189
190class MockCanvas : public SkNoDrawCanvas {
191 public:
192 MockCanvas(int width, int height) : SkNoDrawCanvas(width, height) {}
193
194 MOCK_METHOD(void,
195 onDrawImageRect2,
196 (const SkImage*,
197 const SkRect&,
198 const SkRect&,
199 const SkSamplingOptions&,
200 const SkPaint*,
201 SrcRectConstraint),
202 (override));
203};
204
205using FxgeSkiaEmbedderTest = EmbedderTest;
206
207} // namespace
208
209TEST(fxge, SkiaStateEmpty) {
210 if (!CFX_DefaultRenderDevice::UseSkiaRenderer()) {
211 return;
212 }
213 Harness(&EmptyTest, {});
214}
215
216TEST(fxge, SkiaStatePath) {
217 if (!CFX_DefaultRenderDevice::UseSkiaRenderer()) {
218 return;
219 }
220 Harness(&CommonTest, {State::Change::kNo, State::Save::kYes,
221 State::Clip::kSame, State::Graphic::kPath, 0xFF112233});
222 Harness(&CommonTest,
223 {State::Change::kNo, State::Save::kYes, State::Clip::kDifferentPath,
224 State::Graphic::kPath, 0xFF112233});
225 Harness(&CommonTest, {State::Change::kNo, State::Save::kYes, State::Clip::kNo,
226 State::Graphic::kPath, 0xFF112233});
227 Harness(&CommonTest, {State::Change::kYes, State::Save::kNo, State::Clip::kNo,
228 State::Graphic::kPath, 0xFF112233});
229 Harness(&CommonTest, {State::Change::kNo, State::Save::kNo, State::Clip::kNo,
230 State::Graphic::kPath, 0xFF112233});
231}
232
233TEST(fxge, SkiaStateText) {
234 if (!CFX_DefaultRenderDevice::UseSkiaRenderer()) {
235 return;
236 }
237
238 Harness(&CommonTest,
239 {State::Change::kNo, State::Save::kYes, State::Clip::kDifferentMatrix,
240 State::Graphic::kText, 0xFF445566});
241 Harness(&CommonTest, {State::Change::kNo, State::Save::kYes,
242 State::Clip::kSame, State::Graphic::kText, 0xFF445566});
243}
244
245TEST(fxge, SkiaStateOOSClip) {
246 if (!CFX_DefaultRenderDevice::UseSkiaRenderer()) {
247 return;
248 }
249 Harness(&OutOfSequenceClipTest, {});
250}
251
252TEST_F(FxgeSkiaEmbedderTest, RenderBigImageTwice) {
253 static constexpr int kImageWidth = 5100;
254 static constexpr int kImageHeight = 6600;
255
256 // Page size that renders 20 image pixels per output pixel. This value evenly
257 // divides both the image width and half the image height.
258 static constexpr int kPageToImageFactor = 20;
259 static constexpr int kPageWidth = kImageWidth / kPageToImageFactor;
260 static constexpr int kPageHeight = kImageHeight / kPageToImageFactor;
261
262 if (!CFX_DefaultRenderDevice::UseSkiaRenderer()) {
263 GTEST_SKIP() << "Skia is not the default renderer";
264 }
265
266 ASSERT_TRUE(OpenDocument("bug_2034.pdf"));
267 FPDF_PAGE page = LoadPage(0);
268 ASSERT_TRUE(page);
269
270 std::set<int> image_ids;
271 NiceMock<MockCanvas> canvas(kPageWidth, kPageHeight / 2);
272 EXPECT_CALL(canvas, onDrawImageRect2)
273 .WillRepeatedly(WithArg<0>([&image_ids](const SkImage* image) {
274 ASSERT_TRUE(image);
275 image_ids.insert(image->uniqueID());
276
277 // TODO(crbug.com/pdfium/2026): Image dimensions should be clipped to
278 // 5100x3320. The extra `kPageToImageFactor` accounts for anti-aliasing.
279 EXPECT_EQ(SkISize::Make(kImageWidth, kImageHeight), image->dimensions())
280 << "Actual image dimensions: " << image->width() << "x"
281 << image->height();
282 }));
283
284 // Render top half.
285 RenderPageToSkCanvas(page, /*start_x=*/0, /*start_y=*/0,
286 /*size_x=*/kPageWidth, /*size_y=*/kPageHeight, &canvas);
287
288 // Render bottom half.
289 RenderPageToSkCanvas(page, /*start_x=*/0, /*start_y=*/-kPageHeight / 2,
290 /*size_x=*/kPageWidth, /*size_y=*/kPageHeight, &canvas);
291
292 EXPECT_THAT(image_ids, SizeIs(1));
293
294 UnloadPage(page);
295}
void LoadSubst(const ByteString &face_name, bool bTrueType, uint32_t flags, int weight, int italic_angle, FX_CodePage code_page, bool bVertical)
Definition cfx_font.cpp:220
CFX_Matrix(float a1, float b1, float c1, float d1, float e1, float f1)
void Translate(int32_t x, int32_t y)
void AppendRect(float left, float bottom, float right, float top)
Definition cfx_path.cpp:309
bool DrawDeviceText(pdfium::span< const TextCharPos > pCharPos, CFX_Font *pFont, const CFX_Matrix &mtObject2Device, float font_size, uint32_t color, const CFX_TextRenderOptions &options) override
void SaveState() override
bool SetClip_PathFill(const CFX_Path &path, const CFX_Matrix *pObject2Device, const CFX_FillRenderOptions &fill_options) override
void RestoreState(bool bKeepSaved) 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
RenderContextClearer(CPDF_Page *pPage)
uint32_t m_GlyphIndex
CPDF_Page * CPDFPageFromFPDFPage(FPDF_PAGE page)
void CPDFSDK_RenderPageWithContext(CPDF_PageRenderContext *pContext, CPDF_Page *pPage, int start_x, int start_y, int size_x, int size_y, int rotate, int flags, const FPDF_COLORSCHEME *color_scheme, bool need_to_restore, CPDFSDK_PauseAdapter *pause)
TEST_F(FPDFParserDecodeEmbedderTest, Bug552046)
FX_CodePage
Definition fx_codepage.h:18
TEST(FXCRYPT, CryptToBase16)
BlendMode
Definition fx_dib.h:49
static constexpr CFX_FillRenderOptions WindingOptions()
constexpr CFX_TextRenderOptions(AliasingType type)