blob: 730ba48162b95e58ff4e867739394d421151c99b [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdf_progressive.h"
8
Tom Sepezf0799fe2017-03-28 09:31:32 -07009#include <utility>
10
dsinclair39c62fd2016-09-29 12:49:17 -070011#include "core/fpdfapi/cpdf_pagerendercontext.h"
dsinclair41872fa2016-10-04 11:29:35 -070012#include "core/fpdfapi/page/cpdf_page.h"
dsinclair69d9c682016-10-04 12:18:35 -070013#include "core/fpdfapi/render/cpdf_progressiverenderer.h"
dsinclaira52ab742016-09-29 13:59:29 -070014#include "core/fxcrt/fx_memory.h"
Dan Sinclaira5085d42017-05-11 16:26:50 -040015#include "core/fxge/cfx_defaultrenderdevice.h"
dsinclair74a34fc2016-09-29 16:41:42 -070016#include "core/fxge/cfx_renderdevice.h"
dsinclair114e46a2016-09-29 17:18:21 -070017#include "fpdfsdk/fsdk_define.h"
18#include "fpdfsdk/fsdk_pauseadapter.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080019#include "public/fpdfview.h"
tsepez36eb4bd2016-10-03 15:24:27 -070020#include "third_party/base/ptr_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070021
Lei Zhang8fec3e42015-11-04 15:32:02 -080022// These checks are here because core/ and public/ cannot depend on each other.
Dan Sinclair72cc6c62017-07-24 09:16:19 -040023static_assert(CPDF_ProgressiveRenderer::Ready == FPDF_RENDER_READY,
Lei Zhang8fec3e42015-11-04 15:32:02 -080024 "CPDF_ProgressiveRenderer::Ready value mismatch");
25static_assert(CPDF_ProgressiveRenderer::ToBeContinued ==
Dan Sinclair56725242017-07-18 12:05:33 -040026 FPDF_RENDER_TOBECONTINUED,
Lei Zhang8fec3e42015-11-04 15:32:02 -080027 "CPDF_ProgressiveRenderer::ToBeContinued value mismatch");
28static_assert(CPDF_ProgressiveRenderer::Done == FPDF_RENDER_DONE,
29 "CPDF_ProgressiveRenderer::Done value mismatch");
30static_assert(CPDF_ProgressiveRenderer::Failed == FPDF_RENDER_FAILED,
31 "CPDF_ProgressiveRenderer::Failed value mismatch");
32
Dan Sinclair00d2ad12017-08-10 14:13:02 -040033FPDF_EXPORT int FPDF_CALLCONV FPDF_RenderPageBitmap_Start(FPDF_BITMAP bitmap,
34 FPDF_PAGE page,
35 int start_x,
36 int start_y,
37 int size_x,
38 int size_y,
39 int rotate,
40 int flags,
41 IFSDK_PAUSE* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -070042 if (!bitmap || !pause || pause->version != 1)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Tom Sepezdb0be962015-10-16 14:00:21 -070045 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 if (!pPage)
47 return FPDF_RENDER_FAILED;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070048
Tom Sepezf0799fe2017-03-28 09:31:32 -070049 auto pOwnedContext = pdfium::MakeUnique<CPDF_PageRenderContext>();
50 CPDF_PageRenderContext* pContext = pOwnedContext.get();
51 pPage->SetRenderContext(std::move(pOwnedContext));
52
Dan Sinclair0b950422017-09-21 15:49:49 -040053 RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap));
Dan Sinclaira5085d42017-05-11 16:26:50 -040054 auto pOwnedDevice = pdfium::MakeUnique<CFX_DefaultRenderDevice>();
55 CFX_DefaultRenderDevice* pDevice = pOwnedDevice.get();
Tom Sepezf0799fe2017-03-28 09:31:32 -070056 pContext->m_pDevice = std::move(pOwnedDevice);
thestigbefa4502016-05-26 20:15:19 -070057 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070058
tsepez1e2c5572016-05-25 14:58:09 -070059 IFSDK_PAUSE_Adapter IPauseAdapter(pause);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -070061 rotate, flags, false, &IPauseAdapter);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062
Cary Clark88f47432017-07-19 13:07:35 -040063#ifdef _SKIA_SUPPORT_PATHS_
Cary Clark364d18b2017-07-25 10:39:10 -040064 pDevice->Flush(false);
Cary Clark88f47432017-07-19 13:07:35 -040065 pBitmap->UnPreMultiply();
66#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 if (pContext->m_pRenderer) {
Tom Sepezb3b67622015-10-19 16:20:03 -070068 return CPDF_ProgressiveRenderer::ToFPDFStatus(
69 pContext->m_pRenderer->GetStatus());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 }
71 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070072}
73
Dan Sinclair00d2ad12017-08-10 14:13:02 -040074FPDF_EXPORT int FPDF_CALLCONV FPDF_RenderPage_Continue(FPDF_PAGE page,
75 IFSDK_PAUSE* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -070076 if (!pause || pause->version != 1)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078
Tom Sepezdb0be962015-10-16 14:00:21 -070079 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 if (!pPage)
81 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082
weili9f515bc2016-07-24 08:08:24 -070083 CPDF_PageRenderContext* pContext = pPage->GetRenderContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 if (pContext && pContext->m_pRenderer) {
85 IFSDK_PAUSE_Adapter IPauseAdapter(pause);
86 pContext->m_pRenderer->Continue(&IPauseAdapter);
Cary Clark88f47432017-07-19 13:07:35 -040087#ifdef _SKIA_SUPPORT_PATHS_
Cary Clarkd4c40112017-07-19 15:33:34 -040088 CFX_RenderDevice* pDevice = pContext->m_pDevice.get();
Cary Clark364d18b2017-07-25 10:39:10 -040089 pDevice->Flush(false);
Cary Clarkd4c40112017-07-19 15:33:34 -040090 pDevice->GetBitmap()->UnPreMultiply();
Cary Clark88f47432017-07-19 13:07:35 -040091#endif
Tom Sepezb3b67622015-10-19 16:20:03 -070092 return CPDF_ProgressiveRenderer::ToFPDFStatus(
93 pContext->m_pRenderer->GetStatus());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 }
95 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096}
97
Dan Sinclair00d2ad12017-08-10 14:13:02 -040098FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPage_Close(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -070099 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Cary Clark364d18b2017-07-25 10:39:10 -0400100 if (pPage) {
101#ifdef _SKIA_SUPPORT_PATHS_
102 CPDF_PageRenderContext* pContext = pPage->GetRenderContext();
103 if (pContext && pContext->m_pRenderer) {
104 CFX_RenderDevice* pDevice = pContext->m_pDevice.get();
105 pDevice->Flush(true);
106 pDevice->GetBitmap()->UnPreMultiply();
107 }
108#endif
Tom Sepez385d32f2017-05-26 13:08:36 -0700109 pPage->SetRenderContext(nullptr);
Cary Clark364d18b2017-07-25 10:39:10 -0400110 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700111}