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 "public/fpdf_progressive.h"
8
9 #include <memory>
10 #include <utility>
11
12 #include "core/fpdfapi/page/cpdf_page.h"
13 #include "core/fpdfapi/render/cpdf_pagerendercontext.h"
14 #include "core/fpdfapi/render/cpdf_progressiverenderer.h"
15 #include "core/fxge/cfx_defaultrenderdevice.h"
16 #include "core/fxge/dib/cfx_dibitmap.h"
17 #include "fpdfsdk/cpdfsdk_helpers.h"
18 #include "fpdfsdk/cpdfsdk_pauseadapter.h"
19 #include "fpdfsdk/cpdfsdk_renderpage.h"
20 #include "public/fpdfview.h"
21
22 // These checks are here because core/ and public/ cannot depend on each other.
23 static_assert(CPDF_ProgressiveRenderer::kReady == FPDF_RENDER_READY,
24 "CPDF_ProgressiveRenderer::kReady value mismatch");
25 static_assert(CPDF_ProgressiveRenderer::kToBeContinued ==
26 FPDF_RENDER_TOBECONTINUED,
27 "CPDF_ProgressiveRenderer::kToBeContinued value mismatch");
28 static_assert(CPDF_ProgressiveRenderer::kDone == FPDF_RENDER_DONE,
29 "CPDF_ProgressiveRenderer::kDone value mismatch");
30 static_assert(CPDF_ProgressiveRenderer::kFailed == FPDF_RENDER_FAILED,
31 "CPDF_ProgressiveRenderer::kFailed value mismatch");
32
33 namespace {
34
ToFPDFStatus(CPDF_ProgressiveRenderer::Status status)35 int ToFPDFStatus(CPDF_ProgressiveRenderer::Status status) {
36 return static_cast<int>(status);
37 }
38
39 } // namespace
40
41 FPDF_EXPORT int FPDF_CALLCONV
FPDF_RenderPageBitmapWithColorScheme_Start(FPDF_BITMAP bitmap,FPDF_PAGE page,int start_x,int start_y,int size_x,int size_y,int rotate,int flags,const FPDF_COLORSCHEME * color_scheme,IFSDK_PAUSE * pause)42 FPDF_RenderPageBitmapWithColorScheme_Start(FPDF_BITMAP bitmap,
43 FPDF_PAGE page,
44 int start_x,
45 int start_y,
46 int size_x,
47 int size_y,
48 int rotate,
49 int flags,
50 const FPDF_COLORSCHEME* color_scheme,
51 IFSDK_PAUSE* pause) {
52 if (!pause || pause->version != 1) {
53 return FPDF_RENDER_FAILED;
54 }
55
56 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
57 if (!pPage) {
58 return FPDF_RENDER_FAILED;
59 }
60
61 RetainPtr<CFX_DIBitmap> pBitmap(CFXDIBitmapFromFPDFBitmap(bitmap));
62 if (!pBitmap) {
63 return FPDF_RENDER_FAILED;
64 }
65 CHECK(!pBitmap->IsPremultiplied());
66
67 auto owned_context = std::make_unique<CPDF_PageRenderContext>();
68 CPDF_PageRenderContext* context = owned_context.get();
69 pPage->SetRenderContext(std::move(owned_context));
70
71 #if defined(PDF_USE_SKIA)
72 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
73 pBitmap->PreMultiply();
74 }
75 #endif
76
77 auto device = std::make_unique<CFX_DefaultRenderDevice>();
78 device->AttachWithRgbByteOrder(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER));
79 context->m_pDevice = std::move(device);
80
81 CPDFSDK_PauseAdapter pause_adapter(pause);
82 CPDFSDK_RenderPageWithContext(context, pPage, start_x, start_y, size_x,
83 size_y, rotate, flags, color_scheme,
84 /*need_to_restore=*/false, &pause_adapter);
85
86 if (!context->m_pRenderer) {
87 #if defined(PDF_USE_SKIA)
88 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
89 pBitmap->UnPreMultiply();
90 }
91 #endif // defined(PDF_USE_SKIA)
92
93 return FPDF_RENDER_FAILED;
94 }
95
96 int status = ToFPDFStatus(context->m_pRenderer->GetStatus());
97 if (status == FPDF_RENDER_TOBECONTINUED) {
98 // Note that `pBitmap` is still pre-multiplied here, as the caller is
99 // expected to pass it to FPDF_RenderPage_Continue(). Then
100 // FPDF_RenderPage_Continue() can continue rendering into it without doing
101 // another round of (un)pre-multiplication. FPDF_RenderPage_Continue() will
102 // call UnPreMultiply() when done.
103 //
104 // Normally, PDFium would not return a pre-multiplied bitmap to the caller,
105 // but in this case, the bitmap is in an indeterminate state while it is
106 // being progressively rendered. So many an exception here, as it can
107 // greatly improve performance.
108 return FPDF_RENDER_TOBECONTINUED;
109 }
110
111 #if defined(PDF_USE_SKIA)
112 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
113 pBitmap->UnPreMultiply();
114 }
115 #endif // defined(PDF_USE_SKIA)
116 return status;
117 }
118
FPDF_RenderPageBitmap_Start(FPDF_BITMAP bitmap,FPDF_PAGE page,int start_x,int start_y,int size_x,int size_y,int rotate,int flags,IFSDK_PAUSE * pause)119 FPDF_EXPORT int FPDF_CALLCONV FPDF_RenderPageBitmap_Start(FPDF_BITMAP bitmap,
120 FPDF_PAGE page,
121 int start_x,
122 int start_y,
123 int size_x,
124 int size_y,
125 int rotate,
126 int flags,
127 IFSDK_PAUSE* pause) {
128 return FPDF_RenderPageBitmapWithColorScheme_Start(
129 bitmap, page, start_x, start_y, size_x, size_y, rotate, flags,
130 /*color_scheme=*/nullptr, pause);
131 }
132
FPDF_RenderPage_Continue(FPDF_PAGE page,IFSDK_PAUSE * pause)133 FPDF_EXPORT int FPDF_CALLCONV FPDF_RenderPage_Continue(FPDF_PAGE page,
134 IFSDK_PAUSE* pause) {
135 if (!pause || pause->version != 1)
136 return FPDF_RENDER_FAILED;
137
138 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
139 if (!pPage)
140 return FPDF_RENDER_FAILED;
141
142 auto* pContext =
143 static_cast<CPDF_PageRenderContext*>(pPage->GetRenderContext());
144 if (!pContext || !pContext->m_pRenderer)
145 return FPDF_RENDER_FAILED;
146
147 CPDFSDK_PauseAdapter pause_adapter(pause);
148 pContext->m_pRenderer->Continue(&pause_adapter);
149
150 int status = ToFPDFStatus(pContext->m_pRenderer->GetStatus());
151 if (status == FPDF_RENDER_TOBECONTINUED) {
152 return FPDF_RENDER_TOBECONTINUED;
153 }
154
155 #if defined(PDF_USE_SKIA)
156 if (CFX_DefaultRenderDevice::UseSkiaRenderer()) {
157 pContext->m_pDevice->GetBitmap()->UnPreMultiply();
158 }
159 #endif // defined(PDF_USE_SKIA)
160 return status;
161 }
162
FPDF_RenderPage_Close(FPDF_PAGE page)163 FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPage_Close(FPDF_PAGE page) {
164 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
165 if (pPage)
166 pPage->ClearRenderContext();
167 }
168