Speed-up for wxCanvasImage.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@8858 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robert Roebling
2000-11-28 21:01:20 +00:00
parent bd3e41f600
commit 880d870ea4
4 changed files with 79 additions and 42 deletions

View File

@@ -490,7 +490,7 @@ public:
double GetPosX() { return m_x; } double GetPosX() { return m_x; }
double GetPosY() { return m_y; } double GetPosY() { return m_y; }
void SetPosXY( double x, double y) {m_x=x; m_y=y;CalcBoundingBox(); }; void SetPosXY( double x, double y);
void TransLate( double x, double y ); void TransLate( double x, double y );
@@ -507,7 +507,13 @@ private:
wxImage m_image; wxImage m_image;
int m_orgw,m_orgh; int m_orgw,m_orgh;
wxImage m_tmp;
// cache
wxBitmap m_cBitmap;
wxImage m_cImage;
int m_cW;
int m_cH;
double m_cR;
}; };
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------

View File

@@ -38,6 +38,7 @@
BEGIN_EVENT_TABLE(MyFrame,wxFrame) BEGIN_EVENT_TABLE(MyFrame,wxFrame)
EVT_MENU(ID_QUIT, MyFrame::OnQuit) EVT_MENU(ID_QUIT, MyFrame::OnQuit)
EVT_CLOSE(MyFrame::OnCloseWindow) EVT_CLOSE(MyFrame::OnCloseWindow)
EVT_TIMER(-1, MyFrame::OnTimer)
END_EVENT_TABLE() END_EVENT_TABLE()
MyFrame::MyFrame( wxWindow *parent, wxWindowID id, const wxString &title, MyFrame::MyFrame( wxWindow *parent, wxWindowID id, const wxString &title,
@@ -75,11 +76,10 @@ MyFrame::MyFrame( wxWindow *parent, wxWindowID id, const wxString &title,
m_smile1 = new wxCanvasImage( image, 0,70,32,32 ); m_smile1 = new wxCanvasImage( image, 0,70,32,32 );
root->Append( m_smile1 ); root->Append( m_smile1 );
wxCanvasRect *rect = new wxCanvasRect( 20,20,100,100 ); wxCanvasCircle *circ = new wxCanvasCircle( 170,70,50 );
rect->SetBrush( *wxRED_BRUSH ); circ->SetBrush( *wxBLUE_BRUSH );
root->Append( rect ); root->Append( circ );
/*
int i; int i;
for (i = 10; i < 300; i+=10) for (i = 10; i < 300; i+=10)
{ {
@@ -87,19 +87,16 @@ MyFrame::MyFrame( wxWindow *parent, wxWindowID id, const wxString &title,
r->SetBrush( *wxRED_BRUSH ); r->SetBrush( *wxRED_BRUSH );
root->Append( r ); root->Append( r );
} }
*/
m_smile2 = new wxCanvasImage( image, 0,110,32,32 ); m_smile2 = new wxCanvasImage( image, 0,110,32,32 );
root->Append( m_smile2 ); root->Append( m_smile2 );
/*
for (i = 15; i < 300; i+=10) for (i = 15; i < 300; i+=10)
{ {
wxCanvasRect *r = new wxCanvasRect( i,50,3,140 ); wxCanvasRect *r = new wxCanvasRect( i,50,3,140 );
r->SetBrush( *wxRED_BRUSH ); r->SetBrush( *wxRED_BRUSH );
root->Append( r ); root->Append( r );
} }
*/
// This will call all object and children recursivly so // This will call all object and children recursivly so
// all know what their wxCanvasAdmin is. Call at the end. // all know what their wxCanvasAdmin is. Call at the end.
@@ -107,6 +104,14 @@ MyFrame::MyFrame( wxWindow *parent, wxWindowID id, const wxString &title,
// One object group is the root object. // One object group is the root object.
canvas->SetRoot( root ); canvas->SetRoot( root );
m_timer = new wxTimer( this );
m_timer->Start( 80, FALSE );
}
MyFrame::~MyFrame()
{
delete m_timer;
} }
void MyFrame::CreateMyMenuBar() void MyFrame::CreateMyMenuBar()
@@ -134,6 +139,14 @@ void MyFrame::OnCloseWindow( wxCloseEvent &event )
Destroy(); Destroy();
} }
void MyFrame::OnTimer( wxTimerEvent &event )
{
m_smile1->MoveRelative( 1, 0);
m_smile2->MoveRelative( 1, 0);
wxWakeUpIdle();
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// MyApp // MyApp
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@@ -19,6 +19,7 @@
#endif #endif
#include "wx/canvas/canvas.h" #include "wx/canvas/canvas.h"
#include "wx/timer.h"
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
// constants // constants
@@ -40,6 +41,7 @@ public:
const wxPoint& pos = wxDefaultPosition, const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize, const wxSize& size = wxDefaultSize,
long style = wxDEFAULT_FRAME_STYLE ); long style = wxDEFAULT_FRAME_STYLE );
~MyFrame();
private: private:
// WDR: method declarations for MyFrame // WDR: method declarations for MyFrame
@@ -50,11 +52,13 @@ private:
wxCanvasImage *m_smile1; wxCanvasImage *m_smile1;
wxCanvasImage *m_smile2; wxCanvasImage *m_smile2;
wxCanvasAdmin *m_admin; wxCanvasAdmin *m_admin;
wxTimer *m_timer;
private: private:
// WDR: handler declarations for MyFrame // WDR: handler declarations for MyFrame
void OnQuit( wxCommandEvent &event ); void OnQuit( wxCommandEvent &event );
void OnCloseWindow( wxCloseEvent &event ); void OnCloseWindow( wxCloseEvent &event );
void OnTimer( wxTimerEvent &event );
private: private:
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()

View File

@@ -676,7 +676,7 @@ wxCanvasObject* wxCanvasObjectGroup::IsHitWorld( double x, double y, double marg
if (!obj->IsControl() ) if (!obj->IsControl() )
{ {
if (obj->IsHitWorld(x,y,margin)) if (obj->IsHitWorld(xh,yh,margin))
{ {
return obj; return obj;
} }
@@ -1321,6 +1321,13 @@ wxCanvasImage::wxCanvasImage( const wxImage &image, double x, double y, double w
CalcBoundingBox(); CalcBoundingBox();
} }
void wxCanvasImage::SetPosXY( double x, double y)
{
m_x = x;
m_y = y;
CalcBoundingBox();
}
void wxCanvasImage::TransLate( double x, double y ) void wxCanvasImage::TransLate( double x, double y )
{ {
m_x += x; m_x += x;
@@ -1352,6 +1359,7 @@ void wxCanvasImage::Render(wxTransformMatrix* cworld, int clip_x, int clip_y, in
y = m_admin->LogicalToDeviceY(y); y = m_admin->LogicalToDeviceY(y);
// What is this???
if ( m_orgw*5 < m_admin->LogicalToDeviceXRel( m_bbox.GetWidth() ) || if ( m_orgw*5 < m_admin->LogicalToDeviceXRel( m_bbox.GetWidth() ) ||
m_orgw/5 > m_admin->LogicalToDeviceXRel( m_bbox.GetWidth() ) || m_orgw/5 > m_admin->LogicalToDeviceXRel( m_bbox.GetWidth() ) ||
m_orgh*5 < m_admin->LogicalToDeviceYRel( m_bbox.GetHeight() ) || m_orgh*5 < m_admin->LogicalToDeviceYRel( m_bbox.GetHeight() ) ||
@@ -1376,28 +1384,53 @@ void wxCanvasImage::Render(wxTransformMatrix* cworld, int clip_x, int clip_y, in
return; return;
} }
wxImage tmp;
bool is_cashed = FALSE;
if (m_cImage.Ok() && (m_cW == m_bbox.GetWidth()) && (m_cH == m_bbox.GetHeight()))
{
// use cached image
tmp = m_cImage;
is_cashed = TRUE;
}
else
{
if ((m_admin->LogicalToDeviceXRel( m_bbox.GetWidth() ) == m_image.GetWidth()) && if ((m_admin->LogicalToDeviceXRel( m_bbox.GetWidth() ) == m_image.GetWidth()) &&
(m_admin->LogicalToDeviceYRel( m_bbox.GetHeight() ) == m_image.GetHeight())) (m_admin->LogicalToDeviceYRel( m_bbox.GetHeight() ) == m_image.GetHeight()))
{ {
m_tmp = m_image; tmp = m_image;
} }
else else
{ {
m_tmp = m_image.Scale( m_admin->LogicalToDeviceXRel( m_bbox.GetWidth()), tmp = m_image.Scale( m_admin->LogicalToDeviceXRel( m_bbox.GetWidth()),
m_admin->LogicalToDeviceYRel( m_bbox.GetHeight()) ); m_admin->LogicalToDeviceYRel( m_bbox.GetHeight()) );
} }
wxBitmap bmp; // create cached image
m_cImage = tmp;
m_cW = tmp.GetWidth();
m_cH = tmp.GetHeight();
}
// wxPoint centr(m_admin->LogicalToDeviceX(m_x),m_admin->LogicalToDeviceY(m_y)); // wxPoint centr(m_admin->LogicalToDeviceX(m_x),m_admin->LogicalToDeviceY(m_y));
wxPoint centr(0,0); wxPoint centr(0,0);
if (cworld->GetRotation()) wxBitmap bmp;
if (m_cBitmap.Ok() && is_cashed && (m_cR == cworld->GetRotation()))
{ {
bmp=m_tmp.Rotate(-cworld->GetRotation()/180.0 * pi,centr, TRUE, NULL).ConvertToBitmap(); bmp = m_cBitmap;
} }
else else
{ {
bmp = m_tmp.ConvertToBitmap(); if (cworld->GetRotation())
tmp = tmp.Rotate(-cworld->GetRotation()/180.0 * pi, centr, TRUE, NULL );
bmp = tmp.ConvertToBitmap();
// create cached bitmap
m_cBitmap = bmp;
m_cR = cworld->GetRotation();
} }
wxDC *dc = m_admin->GetActive()->GetDC(); wxDC *dc = m_admin->GetActive()->GetDC();
@@ -1423,28 +1456,9 @@ void wxCanvasImage::Render(wxTransformMatrix* cworld, int clip_x, int clip_y, in
} }
else else
{ {
//TODO clipping not right dc->SetClippingRegion( clip_x, clip_y, clip_width, clip_height );
// dc->DrawPoint(centr2); dc->DrawBitmap( bmp, x, y, TRUE );
// dc->DrawPoint(x,y); dc->DestroyClippingRegion();
if ((clip_x == x) &&
(clip_y == y) &&
(clip_width == tmparea.width) &&
(clip_height == tmparea.height))
{
dc->DrawBitmap( m_tmp, clip_x, clip_y, TRUE );
}
else
{
int start_x = clip_x - (int)x;
int start_y = clip_y - (int)y;
//dc->DrawBitmap( bmp, x, y, TRUE );
wxMemoryDC dcm;
dcm.SelectObject(bmp);
dc->Blit(clip_x, clip_y,clip_width, clip_height,&dcm,start_x,start_y,wxCOPY,TRUE);
dcm.SelectObject(wxNullBitmap);
}
} }
} }