• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 PDFium Authors. All rights reserved.
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 <utility>
10 
11 #include "core/fpdfapi/page/cpdf_page.h"
12 #include "core/fpdfapi/render/cpdf_pagerendercontext.h"
13 #include "core/fpdfapi/render/cpdf_progressiverenderer.h"
14 #include "core/fxge/cfx_defaultrenderdevice.h"
15 #include "fpdfsdk/cpdfsdk_helpers.h"
16 #include "fpdfsdk/cpdfsdk_pauseadapter.h"
17 #include "fpdfsdk/cpdfsdk_renderpage.h"
18 #include "public/fpdfview.h"
19 #include "third_party/base/ptr_util.h"
20 
21 #ifdef _SKIA_SUPPORT_PATHS_
22 #include "core/fxge/cfx_renderdevice.h"
23 #endif
24 
25 // These checks are here because core/ and public/ cannot depend on each other.
26 static_assert(CPDF_ProgressiveRenderer::kReady == FPDF_RENDER_READY,
27               "CPDF_ProgressiveRenderer::kReady value mismatch");
28 static_assert(CPDF_ProgressiveRenderer::kToBeContinued ==
29                   FPDF_RENDER_TOBECONTINUED,
30               "CPDF_ProgressiveRenderer::kToBeContinued value mismatch");
31 static_assert(CPDF_ProgressiveRenderer::kDone == FPDF_RENDER_DONE,
32               "CPDF_ProgressiveRenderer::kDone value mismatch");
33 static_assert(CPDF_ProgressiveRenderer::kFailed == FPDF_RENDER_FAILED,
34               "CPDF_ProgressiveRenderer::kFailed value mismatch");
35 
36 namespace {
37 
ToFPDFStatus(CPDF_ProgressiveRenderer::Status status)38 int ToFPDFStatus(CPDF_ProgressiveRenderer::Status status) {
39   return static_cast<int>(status);
40 }
41 
42 }  // namespace
43 
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)44 FPDF_EXPORT int FPDF_CALLCONV FPDF_RenderPageBitmap_Start(FPDF_BITMAP bitmap,
45                                                           FPDF_PAGE page,
46                                                           int start_x,
47                                                           int start_y,
48                                                           int size_x,
49                                                           int size_y,
50                                                           int rotate,
51                                                           int flags,
52                                                           IFSDK_PAUSE* pause) {
53   if (!bitmap || !pause || pause->version != 1)
54     return FPDF_RENDER_FAILED;
55 
56   CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
57   if (!pPage)
58     return FPDF_RENDER_FAILED;
59 
60   auto pOwnedContext = pdfium::MakeUnique<CPDF_PageRenderContext>();
61   CPDF_PageRenderContext* pContext = pOwnedContext.get();
62   pPage->SetRenderContext(std::move(pOwnedContext));
63 
64   RetainPtr<CFX_DIBitmap> pBitmap(CFXDIBitmapFromFPDFBitmap(bitmap));
65   auto pOwnedDevice = pdfium::MakeUnique<CFX_DefaultRenderDevice>();
66   CFX_DefaultRenderDevice* pDevice = pOwnedDevice.get();
67   pContext->m_pDevice = std::move(pOwnedDevice);
68   pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
69 
70   CPDFSDK_PauseAdapter pause_adapter(pause);
71   CPDFSDK_RenderPageWithContext(pContext, pPage, start_x, start_y, size_x,
72                                 size_y, rotate, flags,
73                                 /*need_to_restore=*/false, &pause_adapter);
74 
75 #ifdef _SKIA_SUPPORT_PATHS_
76   pDevice->Flush(false);
77   pBitmap->UnPreMultiply();
78 #endif
79 
80   if (!pContext->m_pRenderer)
81     return FPDF_RENDER_FAILED;
82 
83   return ToFPDFStatus(pContext->m_pRenderer->GetStatus());
84 }
85 
FPDF_RenderPage_Continue(FPDF_PAGE page,IFSDK_PAUSE * pause)86 FPDF_EXPORT int FPDF_CALLCONV FPDF_RenderPage_Continue(FPDF_PAGE page,
87                                                        IFSDK_PAUSE* pause) {
88   if (!pause || pause->version != 1)
89     return FPDF_RENDER_FAILED;
90 
91   CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
92   if (!pPage)
93     return FPDF_RENDER_FAILED;
94 
95   auto* pContext =
96       static_cast<CPDF_PageRenderContext*>(pPage->GetRenderContext());
97   if (!pContext || !pContext->m_pRenderer)
98     return FPDF_RENDER_FAILED;
99 
100   CPDFSDK_PauseAdapter pause_adapter(pause);
101   pContext->m_pRenderer->Continue(&pause_adapter);
102 #ifdef _SKIA_SUPPORT_PATHS_
103   CFX_RenderDevice* pDevice = pContext->m_pDevice.get();
104   pDevice->Flush(false);
105   pDevice->GetBitmap()->UnPreMultiply();
106 #endif
107   return ToFPDFStatus(pContext->m_pRenderer->GetStatus());
108 }
109 
FPDF_RenderPage_Close(FPDF_PAGE page)110 FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPage_Close(FPDF_PAGE page) {
111   CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
112   if (pPage) {
113 #ifdef _SKIA_SUPPORT_PATHS_
114     auto* pContext =
115         static_cast<CPDF_PageRenderContext*>(pPage->GetRenderContext());
116     if (pContext && pContext->m_pRenderer) {
117       CFX_RenderDevice* pDevice = pContext->m_pDevice.get();
118       pDevice->Flush(true);
119       pDevice->GetBitmap()->UnPreMultiply();
120     }
121 #endif
122     pPage->SetRenderContext(nullptr);
123   }
124 }
125