kis_painter_benchmark.cpp 7.43 KB
Newer Older
Lukáš Tvrdý's avatar
Lukáš Tvrdý committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  Copyright (c) 2010 Lukáš Tvrdý lukast.dev@gmail.com
 *
 *  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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

19 20 21 22 23 24 25 26 27
#if defined(_WIN32) || defined(_WIN64)
#include <stdlib.h>
#define srand48 srand
inline double drand48()
{
    return double(rand()) / RAND_MAX;
}
#endif

Lukáš Tvrdý's avatar
Lukáš Tvrdý committed
28 29
#include <qtest_kde.h>

30
#include <QImage>
31
#include <QDebug>
32

Lukáš Tvrdý's avatar
Lukáš Tvrdý committed
33 34 35 36
#include "kis_painter_benchmark.h"
#include "kis_benchmark_values.h"

#include "kis_paint_device.h"
37 38 39 40
#include "kis_fixed_paint_device.h"

#include "kis_selection.h"
#include "kis_pixel_selection.h"
Lukáš Tvrdý's avatar
Lukáš Tvrdý committed
41 42 43

#include <KoColorSpace.h>
#include <KoColorSpaceRegistry.h>
44
#include <KoCompositeOp.h>
Lukáš Tvrdý's avatar
Lukáš Tvrdý committed
45 46 47 48
#include <KoColor.h>

#include <kis_image.h>
#include <kis_painter.h>
49
#include <kis_types.h>
Lukáš Tvrdý's avatar
Lukáš Tvrdý committed
50

51
#define SAVE_OUTPUT
52 53

#define CYCLES 20
54
static const int LINE_COUNT = 100;
55
static const int LINE_WIDTH = 1;
Lukáš Tvrdý's avatar
Lukáš Tvrdý committed
56 57 58 59 60

void KisPainterBenchmark::initTestCase()
{
    m_colorSpace = KoColorSpaceRegistry::instance()->rgb8();    
    
61 62
    m_color = KoColor(m_colorSpace);
    m_color.fromQColor(Qt::red);
63 64 65 66 67 68 69
    
    
    srand48(0);
    for (int i = 0; i < LINE_COUNT ;i++){
        m_points.append( QPointF(drand48() * TEST_IMAGE_WIDTH, drand48() * TEST_IMAGE_HEIGHT) );
        m_points.append( QPointF(drand48() * TEST_IMAGE_WIDTH, drand48() * TEST_IMAGE_HEIGHT) );
    }
Lukáš Tvrdý's avatar
Lukáš Tvrdý committed
70 71 72 73 74 75 76 77 78 79
}

void KisPainterBenchmark::cleanupTestCase()
{
}

void KisPainterBenchmark::benchmarkBitBlt()
{
    KisPaintDeviceSP src = new KisPaintDevice(m_colorSpace);
    KisPaintDeviceSP dst = new KisPaintDevice(m_colorSpace);
80 81 82 83 84 85
    src->fill(0,0,TEST_IMAGE_WIDTH, TEST_IMAGE_HEIGHT, m_color.data());
    dst->fill(0,0,TEST_IMAGE_WIDTH, TEST_IMAGE_HEIGHT, m_color.data());
    
    KisPainter gc(dst);
    
    QPoint pos(0,0);
86
    QRect rc(0,0,TEST_IMAGE_WIDTH, TEST_IMAGE_HEIGHT);
87 88 89 90 91 92 93 94 95
    
    QBENCHMARK{
        for (int i = 0; i < CYCLES ; i++){
            gc.bitBlt(pos,src,rc);
        }
    }

}

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
void KisPainterBenchmark::benchmarkFastBitBlt()
{
    KisPaintDeviceSP src = new KisPaintDevice(m_colorSpace);
    KisPaintDeviceSP dst = new KisPaintDevice(m_colorSpace);
    src->fill(0,0,TEST_IMAGE_WIDTH, TEST_IMAGE_HEIGHT, m_color.data());
    dst->fill(0,0,TEST_IMAGE_WIDTH, TEST_IMAGE_HEIGHT, m_color.data());

    KisPainter gc(dst);
    gc.setCompositeOp(m_colorSpace->compositeOp(COMPOSITE_COPY));

    QPoint pos(0,0);
    QRect rc(0,0,TEST_IMAGE_WIDTH, TEST_IMAGE_HEIGHT);

    QBENCHMARK{
        for (int i = 0; i < CYCLES ; i++){
            gc.bitBlt(pos,src,rc);
        }
    }

}
116 117 118 119 120 121 122 123 124 125 126 127

void KisPainterBenchmark::benchmarkBitBltSelection()
{
    KisPaintDeviceSP src = new KisPaintDevice(m_colorSpace);
    KisPaintDeviceSP dst = new KisPaintDevice(m_colorSpace);
    src->fill(0,0,TEST_IMAGE_WIDTH, TEST_IMAGE_HEIGHT, m_color.data());
    dst->fill(0,0,TEST_IMAGE_WIDTH, TEST_IMAGE_HEIGHT, m_color.data());

    KisSelectionSP selection = new KisSelection();
    selection->getOrCreatePixelSelection()->select(QRect(0, 0, TEST_IMAGE_WIDTH, TEST_IMAGE_HEIGHT));
    selection->updateProjection();

Lukáš Tvrdý's avatar
Lukáš Tvrdý committed
128
    
129 130
    KisPainter gc(dst);
    gc.setSelection(selection);
Lukáš Tvrdý's avatar
Lukáš Tvrdý committed
131 132
    
    QPoint pos(0,0);
133
    QRect rc(0,0,TEST_IMAGE_WIDTH, TEST_IMAGE_HEIGHT);
134 135 136 137 138 139 140
    
    QBENCHMARK{
        for (int i = 0; i < CYCLES ; i++){
            gc.bitBlt(pos,src,rc);
        }
    }

Lukáš Tvrdý's avatar
Lukáš Tvrdý committed
141
    
142 143 144 145 146 147 148 149 150
}


void KisPainterBenchmark::benchmarkFixedBitBlt()
{
    QImage img(TEST_IMAGE_WIDTH,TEST_IMAGE_HEIGHT,QImage::Format_ARGB32);
    img.fill(255);

    KisFixedPaintDeviceSP fdev = new KisFixedPaintDevice(m_colorSpace);
151
    fdev->convertFromQImage(img, 0);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

    KisPaintDeviceSP dst = new KisPaintDevice(m_colorSpace);
    KisPainter gc(dst);
    QPoint pos(0, 0);
    QRect rc = img.rect();

    QBENCHMARK{
        for (int i = 0; i < CYCLES ; i++){
            gc.bltFixed(pos,fdev,rc);
        }
    }
}


void KisPainterBenchmark::benchmarkFixedBitBltSelection()
{
    QImage img(TEST_IMAGE_WIDTH,TEST_IMAGE_HEIGHT,QImage::Format_ARGB32);
    img.fill(128);

    KisFixedPaintDeviceSP fdev = new KisFixedPaintDevice(m_colorSpace);
172
    fdev->convertFromQImage(img, 0);
173 174 175 176 177 178 179 180 181 182 183 184 185

    KisPaintDeviceSP dst = new KisPaintDevice(m_colorSpace);

    KisSelectionSP selection = new KisSelection();
    selection->getOrCreatePixelSelection()->select(QRect(0, 0, TEST_IMAGE_WIDTH , TEST_IMAGE_HEIGHT));
    selection->updateProjection();

    KisPainter gc(dst);
    gc.setSelection(selection);

    QPoint pos(0, 0);
    QRect rc = img.rect();

186
    QBENCHMARK{
187 188 189
        for (int i = 0; i < CYCLES ; i++){
            gc.bltFixed(pos,fdev,rc);
        }
Lukáš Tvrdý's avatar
Lukáš Tvrdý committed
190 191 192 193
    }

}

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
void KisPainterBenchmark::benchmarkDrawThickLine()
{
    KisPaintDeviceSP dev = new KisPaintDevice(m_colorSpace);
    KoColor color(m_colorSpace);
    color.fromQColor(Qt::white);
    
    dev->clear();
    dev->fill(0,0,TEST_IMAGE_WIDTH,TEST_IMAGE_HEIGHT,color.data());
    
    color.fromQColor(Qt::black);
    
    KisPainter painter(dev);
    painter.setPaintColor(color);
    
    QBENCHMARK{
        for (int i = 0; i < LINE_COUNT; i++){
210
            painter.drawThickLine(m_points[i*2],m_points[i*2+1],LINE_WIDTH,LINE_WIDTH);
211 212
        }
    }
213 214 215
#ifdef SAVE_OUTPUT    
    dev->convertToQImage(m_colorSpace->profile()).save("drawThickLine.png");
#endif
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
}


void KisPainterBenchmark::benchmarkDrawQtLine()
{
    KisPaintDeviceSP dev = new KisPaintDevice(m_colorSpace);
    KoColor color(m_colorSpace);
    color.fromQColor(Qt::white);
    
    dev->clear();
    dev->fill(0,0,TEST_IMAGE_WIDTH,TEST_IMAGE_HEIGHT,color.data());
    
    color.fromQColor(Qt::black);
    
    KisPainter painter(dev);
    painter.setPaintColor(color);
    painter.setFillStyle(KisPainter::FillStyleForegroundColor);
    
    QPen pen;
235
    pen.setWidth(LINE_WIDTH);
236 237 238 239 240 241 242 243 244 245 246
    pen.setColor(Qt::white);
    pen.setCapStyle(Qt::RoundCap);
    
    QBENCHMARK{
        for (int i = 0; i < LINE_COUNT; i++){
            QPainterPath path;
            path.moveTo(m_points[i*2]);
            path.lineTo(m_points[i*2 + 1]);
            painter.drawPainterPath(path, pen);
        }
    }
247 248 249
#ifdef SAVE_OUTPUT        
    dev->convertToQImage(m_colorSpace->profile(),0,0,TEST_IMAGE_WIDTH,TEST_IMAGE_HEIGHT).save("drawQtLine.png");
#endif
250
}
Lukáš Tvrdý's avatar
Lukáš Tvrdý committed
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 276
void KisPainterBenchmark::benchmarkDrawScanLine()
{
    KisPaintDeviceSP dev = new KisPaintDevice(m_colorSpace);
    KoColor color(m_colorSpace);
    color.fromQColor(Qt::white);
    
    dev->clear();
    dev->fill(0,0,TEST_IMAGE_WIDTH,TEST_IMAGE_HEIGHT,color.data());
    
    color.fromQColor(Qt::black);
    
    KisPainter painter(dev);
    painter.setPaintColor(color);
    painter.setFillStyle(KisPainter::FillStyleForegroundColor);
    
    
    QBENCHMARK{
        for (int i = 0; i < LINE_COUNT; i++){
            painter.drawLine(m_points[i*2],m_points[i*2+1],LINE_WIDTH,true);
        }
    }
#ifdef SAVE_OUTPUT    
    dev->convertToQImage(m_colorSpace->profile(),0,0,TEST_IMAGE_WIDTH,TEST_IMAGE_HEIGHT).save("drawScanLine.png");
#endif
}
Lukáš Tvrdý's avatar
Lukáš Tvrdý committed
277 278 279

QTEST_KDEMAIN(KisPainterBenchmark, NoGUI)
#include "kis_painter_benchmark.moc"