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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef CORE_FXGE_CFX_RENDERDEVICE_H_ 8 #define CORE_FXGE_CFX_RENDERDEVICE_H_ 9 10 #include <memory> 11 #include <vector> 12 13 #include "build/build_config.h" 14 #include "core/fxcrt/fx_coordinates.h" 15 #include "core/fxcrt/retain_ptr.h" 16 #include "core/fxcrt/span.h" 17 #include "core/fxcrt/unowned_ptr.h" 18 #include "core/fxge/cfx_path.h" 19 #include "core/fxge/dib/fx_dib.h" 20 #include "core/fxge/render_defines.h" 21 #include "core/fxge/renderdevicedriver_iface.h" 22 23 class CFX_AggImageRenderer; 24 class CFX_DIBBase; 25 class CFX_DIBitmap; 26 class CFX_Font; 27 class CFX_GraphStateData; 28 class PauseIndicatorIface; 29 class TextCharPos; 30 struct CFX_Color; 31 struct CFX_FillRenderOptions; 32 struct CFX_TextRenderOptions; 33 34 enum class BorderStyle { kSolid, kDash, kBeveled, kInset, kUnderline }; 35 36 // Base class for all render devices. Derived classes must call 37 // SetDeviceDriver() to fully initialize the class. Until then, class methods 38 // are not safe to call, or may return invalid results. 39 class CFX_RenderDevice { 40 public: 41 class StateRestorer { 42 public: 43 explicit StateRestorer(CFX_RenderDevice* pDevice); 44 ~StateRestorer(); 45 46 private: 47 UnownedPtr<CFX_RenderDevice> m_pDevice; 48 }; 49 50 virtual ~CFX_RenderDevice(); 51 52 static CFX_Matrix GetFlipMatrix(float width, 53 float height, 54 float left, 55 float top); 56 57 void SaveState(); 58 void RestoreState(bool bKeepSaved); 59 GetWidth()60 int GetWidth() const { return m_Width; } GetHeight()61 int GetHeight() const { return m_Height; } GetDeviceType()62 DeviceType GetDeviceType() const { return m_DeviceType; } GetRenderCaps()63 int GetRenderCaps() const { return m_RenderCaps; } 64 int GetDeviceCaps(int id) const; 65 RetainPtr<CFX_DIBitmap> GetBitmap(); 66 RetainPtr<const CFX_DIBitmap> GetBitmap() const; 67 [[nodiscard]] bool CreateCompatibleBitmap(const RetainPtr<CFX_DIBitmap>& pDIB, 68 int width, 69 int height) const; GetClipBox()70 const FX_RECT& GetClipBox() const { return m_ClipBox; } 71 void SetBaseClip(const FX_RECT& rect); 72 bool SetClip_PathFill(const CFX_Path& path, 73 const CFX_Matrix* pObject2Device, 74 const CFX_FillRenderOptions& fill_options); 75 bool SetClip_PathStroke(const CFX_Path& path, 76 const CFX_Matrix* pObject2Device, 77 const CFX_GraphStateData* pGraphState); 78 bool SetClip_Rect(const FX_RECT& pRect); 79 bool DrawPath(const CFX_Path& path, 80 const CFX_Matrix* pObject2Device, 81 const CFX_GraphStateData* pGraphState, 82 uint32_t fill_color, 83 uint32_t stroke_color, 84 const CFX_FillRenderOptions& fill_options); 85 bool FillRect(const FX_RECT& rect, uint32_t color); 86 87 RetainPtr<const CFX_DIBitmap> GetBackDrop() const; 88 bool GetDIBits(RetainPtr<CFX_DIBitmap> bitmap, int left, int top) const; 89 bool SetDIBits(RetainPtr<const CFX_DIBBase> bitmap, int left, int top); 90 bool SetDIBitsWithBlend(RetainPtr<const CFX_DIBBase> bitmap, 91 int left, 92 int top, 93 BlendMode blend_mode); 94 bool StretchDIBits(RetainPtr<const CFX_DIBBase> bitmap, 95 int left, 96 int top, 97 int dest_width, 98 int dest_height); 99 bool StretchDIBitsWithFlagsAndBlend(RetainPtr<const CFX_DIBBase> bitmap, 100 int left, 101 int top, 102 int dest_width, 103 int dest_height, 104 const FXDIB_ResampleOptions& options, 105 BlendMode blend_mode); 106 bool SetBitMask(RetainPtr<const CFX_DIBBase> bitmap, 107 int left, 108 int top, 109 uint32_t argb); 110 bool StretchBitMask(RetainPtr<CFX_DIBBase> bitmap, 111 int left, 112 int top, 113 int dest_width, 114 int dest_height, 115 uint32_t color); 116 bool StretchBitMaskWithFlags(RetainPtr<CFX_DIBBase> bitmap, 117 int left, 118 int top, 119 int dest_width, 120 int dest_height, 121 uint32_t argb, 122 const FXDIB_ResampleOptions& options); 123 RenderDeviceDriverIface::StartResult StartDIBits( 124 RetainPtr<const CFX_DIBBase> bitmap, 125 float alpha, 126 uint32_t argb, 127 const CFX_Matrix& matrix, 128 const FXDIB_ResampleOptions& options); 129 RenderDeviceDriverIface::StartResult StartDIBitsWithBlend( 130 RetainPtr<const CFX_DIBBase> bitmap, 131 float alpha, 132 uint32_t argb, 133 const CFX_Matrix& matrix, 134 const FXDIB_ResampleOptions& options, 135 BlendMode blend_mode); 136 bool ContinueDIBits(CFX_AggImageRenderer* handle, 137 PauseIndicatorIface* pPause); 138 139 bool DrawNormalText(pdfium::span<const TextCharPos> pCharPos, 140 CFX_Font* pFont, 141 float font_size, 142 const CFX_Matrix& mtText2Device, 143 uint32_t fill_color, 144 const CFX_TextRenderOptions& options); 145 bool DrawTextPath(pdfium::span<const TextCharPos> pCharPos, 146 CFX_Font* pFont, 147 float font_size, 148 const CFX_Matrix& mtText2User, 149 const CFX_Matrix* pUser2Device, 150 const CFX_GraphStateData* pGraphState, 151 uint32_t fill_color, 152 uint32_t stroke_color, 153 CFX_Path* pClippingPath, 154 const CFX_FillRenderOptions& fill_options); 155 156 void DrawFillRect(const CFX_Matrix* pUser2Device, 157 const CFX_FloatRect& rect, 158 const CFX_Color& color, 159 int32_t nTransparency); 160 void DrawFillRect(const CFX_Matrix* pUser2Device, 161 const CFX_FloatRect& rect, 162 const FX_COLORREF& color); 163 void DrawStrokeRect(const CFX_Matrix& mtUser2Device, 164 const CFX_FloatRect& rect, 165 const FX_COLORREF& color, 166 float fWidth); 167 void DrawStrokeLine(const CFX_Matrix* pUser2Device, 168 const CFX_PointF& ptMoveTo, 169 const CFX_PointF& ptLineTo, 170 const FX_COLORREF& color, 171 float fWidth); 172 void DrawBorder(const CFX_Matrix* pUser2Device, 173 const CFX_FloatRect& rect, 174 float fWidth, 175 const CFX_Color& color, 176 const CFX_Color& crLeftTop, 177 const CFX_Color& crRightBottom, 178 BorderStyle nStyle, 179 int32_t nTransparency); 180 void DrawFillArea(const CFX_Matrix& mtUser2Device, 181 const std::vector<CFX_PointF>& points, 182 const FX_COLORREF& color); 183 void DrawShadow(const CFX_Matrix& mtUser2Device, 184 const CFX_FloatRect& rect, 185 int32_t nTransparency, 186 int32_t nStartGray, 187 int32_t nEndGray); 188 189 // See RenderDeviceDriverIface methods of the same name. 190 bool MultiplyAlpha(float alpha); 191 bool MultiplyAlphaMask(RetainPtr<const CFX_DIBitmap> mask); 192 193 #if defined(PDF_USE_SKIA) 194 bool DrawShading(const CPDF_ShadingPattern& pattern, 195 const CFX_Matrix& matrix, 196 const FX_RECT& clip_rect, 197 int alpha); 198 bool SetBitsWithMask(RetainPtr<const CFX_DIBBase> bitmap, 199 RetainPtr<const CFX_DIBBase> mask, 200 int left, 201 int top, 202 float alpha, 203 BlendMode blend_type); 204 void SyncInternalBitmaps(); 205 #endif // defined(PDF_USE_SKIA) 206 207 protected: 208 CFX_RenderDevice(); 209 210 void SetBitmap(RetainPtr<CFX_DIBitmap> bitmap); 211 212 void SetDeviceDriver(std::unique_ptr<RenderDeviceDriverIface> pDriver); GetDeviceDriver()213 RenderDeviceDriverIface* GetDeviceDriver() const { 214 return m_pDeviceDriver.get(); 215 } 216 217 private: 218 void InitDeviceInfo(); 219 void UpdateClipBox(); 220 bool DrawFillStrokePath(const CFX_Path& path, 221 const CFX_Matrix* pObject2Device, 222 const CFX_GraphStateData* pGraphState, 223 uint32_t fill_color, 224 uint32_t stroke_color, 225 const CFX_FillRenderOptions& fill_options); 226 bool DrawCosmeticLine(const CFX_PointF& ptMoveTo, 227 const CFX_PointF& ptLineTo, 228 uint32_t color, 229 const CFX_FillRenderOptions& fill_options); 230 void DrawZeroAreaPath(const std::vector<CFX_Path::Point>& path, 231 const CFX_Matrix* matrix, 232 bool adjust, 233 bool aliased_path, 234 uint32_t fill_color, 235 uint8_t fill_alpha); 236 237 RetainPtr<CFX_DIBitmap> m_pBitmap; 238 int m_Width = 0; 239 int m_Height = 0; 240 int m_bpp = 0; 241 int m_RenderCaps = 0; 242 DeviceType m_DeviceType = DeviceType::kDisplay; 243 FX_RECT m_ClipBox; 244 std::unique_ptr<RenderDeviceDriverIface> m_pDeviceDriver; 245 }; 246 247 #endif // CORE_FXGE_CFX_RENDERDEVICE_H_ 248