Compute wxDCImpl::m_mm_to_pix_[xy] on demand

If nothing else, this avoids 2 calls to each of wxGetDisplaySize() and
wxGetDisplaySizeMM() on each and every wxGCDC construction which are
completely unnecessary as wxGCDCImpl uses its own hardcoded resolution
of 72 DPI, as do some other classes deriving from wxDCImpl.

And even for the classes which do compute these fields using the display
resolution, it may still be unnecessary to do it as they can be never
used if neither GetSizeMM() nor SetLogicalScale() are called on the
corresponding wxDC object.

Finally, this is more than an optimization as when using Cairo-based
wxGCDC without display (which is explicitly supported), calling
wxGetDisplaySize() simply doesn't work at all.
This commit is contained in:
Vadim Zeitlin
2018-12-06 03:19:42 +01:00
parent e1a702a205
commit 3dc16a7419
11 changed files with 61 additions and 30 deletions

View File

@@ -686,6 +686,16 @@ protected:
// for rendering on higher-resolution DCs such as printer ones // for rendering on higher-resolution DCs such as printer ones
static float GetFontPointSizeAdjustment(float dpi); static float GetFontPointSizeAdjustment(float dpi);
// Return the number of pixels per mm in the horizontal and vertical
// directions, respectively.
//
// If the physical size of the DC is not known, or doesn't make sense, as
// for a SVG DC, for example, a fixed value corresponding to the standard
// DPI is used.
double GetMMToPXx() const;
double GetMMToPXy() const;
// window on which the DC draws or NULL // window on which the DC draws or NULL
wxWindow *m_window; wxWindow *m_window;
@@ -715,9 +725,12 @@ protected:
double m_contentScaleFactor; // used by high resolution displays (retina) double m_contentScaleFactor; // used by high resolution displays (retina)
// what is a mm on a screen you don't know the size of? // Pixel per mm in horizontal and vertical directions.
double m_mm_to_pix_x, //
m_mm_to_pix_y; // These variables are computed on demand by GetMMToPX[xy]() functions,
// don't access them directly other than for assigning to them.
mutable double m_mm_to_pix_x,
m_mm_to_pix_y;
// bounding and clipping boxes // bounding and clipping boxes
wxCoord m_minX, m_minY, m_maxX, m_maxY; // Bounding box is stored in device units. wxCoord m_minX, m_minY, m_maxX, m_maxY; // Bounding box is stored in device units.

View File

@@ -331,6 +331,7 @@ wxDCImpl::wxDCImpl( wxDC *owner )
, m_scaleX(1.0), m_scaleY(1.0) , m_scaleX(1.0), m_scaleY(1.0)
, m_signX(1), m_signY(1) , m_signX(1), m_signY(1)
, m_contentScaleFactor(1) , m_contentScaleFactor(1)
, m_mm_to_pix_x(0.0), m_mm_to_pix_y(0.0)
, m_minX(0), m_minY(0), m_maxX(0), m_maxY(0) , m_minX(0), m_minY(0), m_maxX(0), m_maxY(0)
, m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0) , m_clipX1(0), m_clipY1(0), m_clipX2(0), m_clipY2(0)
, m_logicalFunction(wxCOPY) , m_logicalFunction(wxCOPY)
@@ -348,11 +349,6 @@ wxDCImpl::wxDCImpl( wxDC *owner )
#endif // wxUSE_PALETTE #endif // wxUSE_PALETTE
{ {
m_owner = owner; m_owner = owner;
m_mm_to_pix_x = (double)wxGetDisplaySize().GetWidth() /
(double)wxGetDisplaySizeMM().GetWidth();
m_mm_to_pix_y = (double)wxGetDisplaySize().GetHeight() /
(double)wxGetDisplaySizeMM().GetHeight();
} }
wxDCImpl::~wxDCImpl() wxDCImpl::~wxDCImpl()
@@ -517,16 +513,16 @@ void wxDCImpl::SetMapMode( wxMappingMode mode )
switch (mode) switch (mode)
{ {
case wxMM_TWIPS: case wxMM_TWIPS:
SetLogicalScale( twips2mm*m_mm_to_pix_x, twips2mm*m_mm_to_pix_y ); SetLogicalScale( twips2mm*GetMMToPXx(), twips2mm*GetMMToPXy() );
break; break;
case wxMM_POINTS: case wxMM_POINTS:
SetLogicalScale( pt2mm*m_mm_to_pix_x, pt2mm*m_mm_to_pix_y ); SetLogicalScale( pt2mm*GetMMToPXx(), pt2mm*GetMMToPXy() );
break; break;
case wxMM_METRIC: case wxMM_METRIC:
SetLogicalScale( m_mm_to_pix_x, m_mm_to_pix_y ); SetLogicalScale( GetMMToPXx(), GetMMToPXy() );
break; break;
case wxMM_LOMETRIC: case wxMM_LOMETRIC:
SetLogicalScale( m_mm_to_pix_x/10.0, m_mm_to_pix_y/10.0 ); SetLogicalScale( GetMMToPXx()/10.0, GetMMToPXy()/10.0 );
break; break;
default: default:
case wxMM_TEXT: case wxMM_TEXT:
@@ -1441,3 +1437,25 @@ float wxDCImpl::GetFontPointSizeAdjustment(float dpi)
const wxSize screenPPI = wxGetDisplayPPI(); const wxSize screenPPI = wxGetDisplayPPI();
return float(screenPPI.y) / dpi; return float(screenPPI.y) / dpi;
} }
double wxDCImpl::GetMMToPXx() const
{
if ( wxIsNullDouble(m_mm_to_pix_x) )
{
m_mm_to_pix_x = (double)wxGetDisplaySize().GetWidth() /
(double)wxGetDisplaySizeMM().GetWidth();
}
return m_mm_to_pix_x;
}
double wxDCImpl::GetMMToPXy() const
{
if ( wxIsNullDouble(m_mm_to_pix_x) )
{
m_mm_to_pix_y = (double)wxGetDisplaySize().GetHeight() /
(double)wxGetDisplaySizeMM().GetHeight();
}
return m_mm_to_pix_y;
}

View File

@@ -390,9 +390,9 @@ void wxGCDCImpl::DoGetSizeMM( int* width, int* height ) const
GetOwner()->GetSize( &w, &h ); GetOwner()->GetSize( &w, &h );
if (width) if (width)
*width = long( double(w) / (m_scaleX * m_mm_to_pix_x) ); *width = long( double(w) / (m_scaleX * GetMMToPXx()) );
if (height) if (height)
*height = long( double(h) / (m_scaleY * m_mm_to_pix_y) ); *height = long( double(h) / (m_scaleY * GetMMToPXy()) );
} }
void wxGCDCImpl::SetTextForeground( const wxColour &col ) void wxGCDCImpl::SetTextForeground( const wxColour &col )

View File

@@ -442,10 +442,10 @@ wxSVGFileDCImpl::~wxSVGFileDCImpl()
void wxSVGFileDCImpl::DoGetSizeMM(int *width, int *height) const void wxSVGFileDCImpl::DoGetSizeMM(int *width, int *height) const
{ {
if (width) if (width)
*width = wxRound( (double)m_width / m_mm_to_pix_x ); *width = wxRound( (double)m_width / GetMMToPXx() );
if (height) if (height)
*height = wxRound( (double)m_height / m_mm_to_pix_y ); *height = wxRound( (double)m_height / GetMMToPXy() );
} }
wxSize wxSVGFileDCImpl::GetPPI() const wxSize wxSVGFileDCImpl::GetPPI() const

View File

@@ -563,15 +563,15 @@ void wxDFBDCImpl::DoGetSizeMM(int *width, int *height) const
int w = 0; int w = 0;
int h = 0; int h = 0;
GetSize(&w, &h); GetSize(&w, &h);
if ( width ) *width = int(double(w) / (m_userScaleX*m_mm_to_pix_x)); if ( width ) *width = int(double(w) / (m_userScaleX*GetMMToPXx()));
if ( height ) *height = int(double(h) / (m_userScaleY*m_mm_to_pix_y)); if ( height ) *height = int(double(h) / (m_userScaleY*GetMMToPXy()));
} }
wxSize wxDFBDCImpl::GetPPI() const wxSize wxDFBDCImpl::GetPPI() const
{ {
#warning "move this to common code?" #warning "move this to common code?"
return wxSize(int(double(m_mm_to_pix_x) * inches2mm), return wxSize(int(double(GetMMToPXx()) * inches2mm),
int(double(m_mm_to_pix_y) * inches2mm)); int(double(GetMMToPXy()) * inches2mm));
} }

View File

@@ -506,8 +506,8 @@ void wxGTKDCImpl::DoGetSizeMM( int* width, int* height ) const
int w = 0; int w = 0;
int h = 0; int h = 0;
GetOwner()->GetSize( &w, &h ); GetOwner()->GetSize( &w, &h );
if (width) *width = int( double(w) / (m_userScaleX*m_mm_to_pix_x) ); if (width) *width = int( double(w) / (m_userScaleX*GetMMToPXx()) );
if (height) *height = int( double(h) / (m_userScaleY*m_mm_to_pix_y) ); if (height) *height = int( double(h) / (m_userScaleY*GetMMToPXy()) );
} }
// Resolution in pixels per logical inch // Resolution in pixels per logical inch

View File

@@ -2072,7 +2072,7 @@ void wxWindowDCImpl::ComputeScaleAndOrigin()
// Resolution in pixels per logical inch // Resolution in pixels per logical inch
wxSize wxWindowDCImpl::GetPPI() const wxSize wxWindowDCImpl::GetPPI() const
{ {
return wxSize( (int) (m_mm_to_pix_x * 25.4 + 0.5), (int) (m_mm_to_pix_y * 25.4 + 0.5)); return wxSize( (int) (GetMMToPXx() * 25.4 + 0.5), (int) (GetMMToPXy() * 25.4 + 0.5));
} }
int wxWindowDCImpl::GetDepth() const int wxWindowDCImpl::GetDepth() const

View File

@@ -50,8 +50,8 @@ void wxGTKDCImpl::DoGetSizeMM( int* width, int* height ) const
int w = 0; int w = 0;
int h = 0; int h = 0;
GetSize( &w, &h ); GetSize( &w, &h );
if (width) *width = int( double(w) / (m_userScaleX*m_mm_to_pix_x) ); if (width) *width = int( double(w) / (m_userScaleX*GetMMToPXx()) );
if (height) *height = int( double(h) / (m_userScaleY*m_mm_to_pix_y) ); if (height) *height = int( double(h) / (m_userScaleY*GetMMToPXy()) );
} }
// Resolution in pixels per logical inch // Resolution in pixels per logical inch

View File

@@ -2113,7 +2113,7 @@ void wxWindowDCImpl::ComputeScaleAndOrigin()
// Resolution in pixels per logical inch // Resolution in pixels per logical inch
wxSize wxWindowDCImpl::GetPPI() const wxSize wxWindowDCImpl::GetPPI() const
{ {
return wxSize( (int) (m_mm_to_pix_x * 25.4 + 0.5), (int) (m_mm_to_pix_y * 25.4 + 0.5)); return wxSize( (int) (GetMMToPXx() * 25.4 + 0.5), (int) (GetMMToPXy() * 25.4 + 0.5));
} }
int wxWindowDCImpl::GetDepth() const int wxWindowDCImpl::GetDepth() const

View File

@@ -87,9 +87,9 @@ void wxMotifDCImpl::DoGetSizeMM( int* width, int* height ) const
GetSize( &w, &h ); GetSize( &w, &h );
if ( width ) if ( width )
*width = int( double(w) / (m_scaleX*m_mm_to_pix_x) ); *width = int( double(w) / (m_scaleX*GetMMToPXx()) );
if ( height ) if ( height )
*height = int( double(h) / (m_scaleY*m_mm_to_pix_y) ); *height = int( double(h) / (m_scaleY*GetMMToPXy()) );
} }
// Resolution in pixels per logical inch // Resolution in pixels per logical inch

View File

@@ -51,9 +51,9 @@ void wxX11DCImpl::DoGetSizeMM( int* width, int* height ) const
DoGetSize( &w, &h ); DoGetSize( &w, &h );
if ( width ) if ( width )
*width = int( double(w) / (m_scaleX*m_mm_to_pix_x) ); *width = int( double(w) / (m_scaleX*GetMMToPXx()) );
if ( height ) if ( height )
*height = int( double(h) / (m_scaleY*m_mm_to_pix_y) ); *height = int( double(h) / (m_scaleY*GetMMToPXy()) );
} }
// Resolution in pixels per logical inch // Resolution in pixels per logical inch