view.cpp 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 CERN
 * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <boost/foreach.hpp>

#include <base_struct.h>
#include <layers_id_colors_and_visibility.h>

#include <view/view.h>
#include <view/view_rtree.h>
#include <gal/definitions.h>
#include <gal/graphics_abstraction_layer.h>
#include <painter.h>

#include <profile.h>

using namespace KiGfx;

40
// Static constants
41
const unsigned int VIEW::VIEW_MAX_LAYERS = 64;
42
const int          VIEW::TOP_LAYER       = -1;
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

void VIEW::AddLayer( int aLayer, bool aDisplayOnly )
{
    if( m_layers.find( aLayer ) == m_layers.end() )
    {
        m_layers[aLayer] = VIEW_LAYER();
        m_layers[aLayer].id     = aLayer;
        m_layers[aLayer].items  = new VIEW_RTREE();
        m_layers[aLayer].renderingOrder = aLayer;
        m_layers[aLayer].enabled        = true;
        m_layers[aLayer].isDirty        = false;
        m_layers[aLayer].displayOnly    = aDisplayOnly;
    }

    sortLayers();
}


void VIEW::Add( VIEW_ITEM* aItem )
{
    int layers[VIEW_MAX_LAYERS], layers_count;

    aItem->ViewGetLayers( layers, layers_count );

    for( int i = 0; i < layers_count; i++ )
    {
        VIEW_LAYER* l = &m_layers[layers[i]];
        l->items->Insert( aItem );
        l->dirtyExtents.Merge( aItem->ViewBBox() );
    }

    if( m_dynamic )
        aItem->viewAssign( this );
}


void VIEW::Remove( VIEW_ITEM* aItem )
{
    if( m_dynamic )
        aItem->m_view = NULL;

// fixme: this is so sloooow!
    for( LayerMapIter i = m_layers.begin(); i != m_layers.end(); ++i )
    {
        VIEW_LAYER* l = & ( ( *i ).second );
        l->items->Remove( aItem );
    }
}


// stupid C++... python lamda would do this in one line
template <class Container>
struct queryVisitor
{
    typedef typename Container::value_type item_type;

    queryVisitor( Container& aCont, int aLayer ) :
        m_cont( aCont ), m_layer( aLayer )
    {
    }

    void operator()( VIEW_ITEM* aItem )
    {
        if( aItem->ViewIsVisible() )
            m_cont.push_back( VIEW::LayerItemPair( aItem, m_layer ) );
    }

    Container&  m_cont;
    int         m_layer;
};


int VIEW::Query( const BOX2I& aRect, std::vector<LayerItemPair>& aResult )
{
    if( m_orderedLayers.empty() )
        return 0;

    std::vector<VIEW_LAYER*>::reverse_iterator i;

    // execute queries in reverse direction, so that items that are on the top of
    // the rendering stack are returned first.
    for( i = m_orderedLayers.rbegin(); i != m_orderedLayers.rend(); ++i )
    {
        // ignore layers that do not contain actual items (i.e. the selection box, menus, floats)
        if( ( *i )->displayOnly )
            continue;

        queryVisitor<std::vector<LayerItemPair> > visitor( aResult, ( *i )->id );
        ( *i )->items->Query( aRect, visitor );
    }

    return aResult.size();
}


VIEW::VIEW( bool aIsDynamic, bool aUseGroups ) :
139
    m_enableTopLayer( false ),
140 141 142 143 144 145
    m_scale ( 1.0 ),
    m_painter( NULL ),
    m_gal( NULL ),
    m_dynamic( aIsDynamic ),
    m_useGroups( aUseGroups )
{
146 147
    // By default there is not layer on the top
    m_topLayer.enabled = false;
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
}


VIEW::~VIEW()
{
}


VECTOR2D VIEW::ToWorld( const VECTOR2D& aCoord, bool aAbsolute ) const
{
    MATRIX3x3D matrix = m_gal->GetWorldScreenMatrix().Inverse();

    if( aAbsolute )
    {
        return VECTOR2D( matrix * aCoord );
    }
    else
    {
        return VECTOR2D( matrix.GetScale().x * aCoord.x, matrix.GetScale().y * aCoord.y );
    }
}


VECTOR2D VIEW::ToScreen( const VECTOR2D& aCoord, bool aAbsolute ) const
{
    MATRIX3x3D matrix = m_gal->GetWorldScreenMatrix();

    if( aAbsolute )
    {
        return VECTOR2D( matrix * aCoord );
    }
    else
    {
        return VECTOR2D( matrix.GetScale().x * aCoord.x, matrix.GetScale().y * aCoord.y );
    }
}


double VIEW::ToScreen( double aCoord, bool aAbsolute ) const
{
    VECTOR2D t( aCoord, 0 );

    return ToScreen( t, aAbsolute ).x;
}


void VIEW::CopySettings( const VIEW* aOtherView )
{
    // FIXME
}


void VIEW::SetGAL( GAL* aGal )
{
    m_gal = aGal;

    // force the new GAL to display the current viewport.
    SetCenter( m_center );
    SetScale( m_scale );
}


void VIEW::SetPainter( PAINTER* aPainter )
{
    m_painter = aPainter;
}


BOX2D VIEW::GetViewport() const
{
    BOX2D    rect;
    VECTOR2D screenSize = m_gal->GetScreenPixelSize();

    rect.SetOrigin( ToWorld( VECTOR2D( 0, 0 ) ) );
    rect.SetEnd( ToWorld( screenSize ) );

    return rect.Normalize();
}


void VIEW::SetViewport( const BOX2D& aViewport, bool aKeepAspect )
{
    VECTOR2D ssize  = ToWorld( m_gal->GetScreenPixelSize(), false );
    VECTOR2D centre = aViewport.Centre();
    VECTOR2D vsize  = aViewport.GetSize();
    double   zoom   = 1.0 / std::min( fabs( vsize.x / ssize.x ), fabs( vsize.y / ssize.y ) );

    SetCenter( centre );
    SetScale( GetScale() * zoom );
}


void VIEW::SetMirror( bool aMirrorX, bool aMirrorY )
{
    // FIXME
}


void VIEW::SetScale( double aScale )
{
    SetScale( aScale, m_center );
}


void VIEW::SetScale( double aScale, const VECTOR2D& aAnchor )
{
    VECTOR2D a = ToScreen( aAnchor );

    m_gal->SetZoomFactor( aScale );
    m_gal->ComputeWorldScreenMatrix();

    VECTOR2D delta = ToWorld( a ) - aAnchor;

    SetCenter( m_center - delta );
    m_scale = aScale;
}


void VIEW::SetCenter( const VECTOR2D& aCenter )
{
    m_center = aCenter;
    m_gal->SetLookAtPoint( m_center );
    m_gal->ComputeWorldScreenMatrix();
}


void VIEW::SetLayerVisible( int aLayer, bool aVisible )
{
276
    m_layers[aLayer].enabled = aVisible;
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
}


void VIEW::sortLayers()
{
    int n = 0;

    m_orderedLayers.resize( m_layers.size() );

    for( LayerMapIter i = m_layers.begin(); i != m_layers.end(); ++i )
        m_orderedLayers[n++] = &i->second;

    sort( m_orderedLayers.begin(), m_orderedLayers.end(), compareRenderingOrder );
}


void VIEW::SetLayerOrder( int aLayer, int aRenderingOrder )
{
    m_layers[aLayer].renderingOrder = aRenderingOrder;
    sortLayers();
}


300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
void VIEW::SetTopLayer( int aLayer )
{
    // Restore previous order
    if( m_topLayer.enabled )
        m_layers[m_topLayer.id].renderingOrder = m_topLayer.renderingOrder;

    if( aLayer >= 0 && aLayer < VIEW_MAX_LAYERS )
    {
        // Save settings, so it can be restored later
        m_topLayer.renderingOrder = m_layers[aLayer].renderingOrder;
        m_topLayer.id = m_layers[aLayer].id;

        // Apply new settings only if the option is enabled
        if( m_enableTopLayer )
            m_layers[aLayer].renderingOrder = TOP_LAYER;

        // Set the flag saying that settings stored in m_topLayer are valid
        m_topLayer.enabled = true;
    }
    else
    {
        // There are no valid settings in m_topLayer
        m_topLayer.enabled = false;
    }

    sortLayers();
}


void VIEW::EnableTopLayer( bool aEnable )
{
    if( aEnable == m_enableTopLayer ) return;

    // Use stored settings only if applicable
    // (topLayer.enabled == false means there are no valid settings stored)
    if( m_topLayer.enabled )
    {
        if( aEnable )
        {
            m_layers[m_topLayer.id].renderingOrder = TOP_LAYER;
        }
        else
        {
            m_layers[m_topLayer.id].renderingOrder = m_topLayer.renderingOrder;
        }
    }

    m_enableTopLayer = aEnable;
}


351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
struct VIEW::drawItem
{
    drawItem( VIEW* aView, int aCurrentLayer ) :
        count( 0 ), countCached( 0 ), currentLayer( aCurrentLayer ), time( 0 ), view( aView )
    {
    }

    void operator()( VIEW_ITEM* aItem )
    {
        BOX2I    tmp;
        uint64_t ts  = rdtsc();
        GAL*     gal = view->GetGAL();

        if( view->m_useGroups )
        {
            int group = aItem->m_cachedGroup;

            if( group >= 0 && aItem->ViewIsVisible() )
            {
                gal->DrawGroup( group );
                countCached++;
            }
            else
            {
                group = gal->BeginGroup();
                aItem->m_cachedGroup = group;
                aItem->ViewDraw( 0, gal, tmp );
                gal->EndGroup();
                gal->DrawGroup( group );
            }
        }
        else if( aItem->ViewIsVisible() )
        {
            if( !( view->m_painter
                   && view->m_painter->Draw( static_cast<EDA_ITEM*>( aItem ), currentLayer ) ) )
            {
                // Fallback, if there is no painter or painter does not know how to draw aItem
                aItem->ViewDraw( currentLayer, gal, tmp );
            }
        }

        time += rdtsc() - ts;
        count++;
    }

    int      count;
    int      countCached;
    int      currentLayer;
    uint64_t time;
    VIEW*    view;
};

403

404 405 406 407 408 409 410 411 412 413 414
void VIEW::redrawRect( const BOX2I& aRect )
{
    int          totalItems = 0, totalCached = 0;
    uint64_t     totalDrawTime = 0;
    prof_counter totalCycles, totalRealTime;

    prof_start( &totalRealTime, false );
    prof_start( &totalCycles, true );

    BOOST_FOREACH( VIEW_LAYER* l, m_orderedLayers )
    {
415 416 417
        if( l->enabled )
        {
            drawItem drawFunc( this, l->id );
418

419 420 421
            m_gal->SetLayerDepth( (double) l->renderingOrder );
            l->items->Query( aRect, drawFunc );
            l->isDirty = false;
422

423 424 425 426
            totalItems    += drawFunc.count;
            totalDrawTime += drawFunc.time;
            totalCached   += drawFunc.countCached;
        }
427 428 429 430 431
    }

    prof_end( &totalCycles );
    prof_end( &totalRealTime );

432
    wxLogDebug( wxT( "Redraw::items %d (%d cached), %.1f ms/frame (%.0f FPS), draw/geometry ratio: %.1f%%" ),
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
            totalItems, totalCached, (double) totalRealTime.value / 1000.0,
            1000000.0 / (double) totalRealTime.value,
            (double) totalDrawTime / (double) totalCycles.value * 100.0 );
}


struct VIEW::unlinkItem
{
    void operator()( VIEW_ITEM* aItem )
    {
        aItem->m_view = NULL;
    }
};


void VIEW::Clear()
{
    BOX2I r;

    r.SetMaximum();

    for( LayerMapIter i = m_layers.begin(); i != m_layers.end(); ++i )
    {
        VIEW_LAYER* l = &( ( *i ).second );
        unlinkItem v;
        if( m_dynamic )
            l->items->Query( r, v );

        l->items->RemoveAll();
462
    }
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
}


void VIEW::Redraw()
{
    VECTOR2D screenSize = m_gal->GetScreenPixelSize();
    BOX2I    rect( ToWorld( VECTOR2D( 0, 0 ) ),
                   ToWorld( screenSize ) - ToWorld( VECTOR2D( 0, 0 ) ) );

    rect.Normalize();
    redrawRect( rect );
}


VECTOR2D VIEW::GetScreenPixelSize() const
{
    return m_gal->GetScreenPixelSize();
}


void VIEW::invalidateItem( VIEW_ITEM* aItem, int aUpdateFlags )
{
    int layer_indices[VIEW_MAX_LAYERS], layer_count;

    aItem->ViewGetLayers( layer_indices, layer_count );

    for( int i = 0; i < layer_count; i++ )
    {
        VIEW_LAYER* l = &m_layers[layer_indices[i]];

        l->dirtyExtents =
            l->isDirty ? aItem->ViewBBox() : l->dirtyExtents.Merge( aItem->ViewBBox() );

        if( aUpdateFlags & VIEW_ITEM::GEOMETRY )
        {
            l->items->Remove( aItem );
            l->items->Insert( aItem );    /* reinsert */

            if( m_useGroups )
                aItem->m_cachedGroup = -1;
        }
    }

    if( m_useGroups && aItem->m_cachedGroup >= 0 )
    {
        m_gal->DeleteGroup( aItem->m_cachedGroup );
        aItem->m_cachedGroup = -1;
    }
}


struct VIEW::clearItemCache
{
    clearItemCache( VIEW* aView ) :
        view( aView )
    {
    }

    void operator()( VIEW_ITEM* aItem )
    {
        if( aItem->m_cachedGroup >= 0 )
        {
            view->GetGAL()->DeleteGroup( aItem->m_cachedGroup );
            aItem->m_cachedGroup = -1;
        }
    }

    VIEW* view;
};


void VIEW::clearGroupCache()
{
    BOX2I r;

    r.SetMaximum();

    for( LayerMapIter i = m_layers.begin(); i != m_layers.end(); ++i )
    {
        VIEW_LAYER* l = & ( ( *i ).second );
        clearItemCache visitor( this );
        l->items->Query( r, visitor );
    };
}