summaryrefslogtreecommitdiffstats
path: root/src/declarative/fx
diff options
context:
space:
mode:
authorAaron Kennedy <aaron.kennedy@nokia.com>2009-06-04 03:35:15 (GMT)
committerAaron Kennedy <aaron.kennedy@nokia.com>2009-06-04 03:35:15 (GMT)
commita276e872e83e0c2f04a367299580ef787b6efc51 (patch)
tree3bb71c20fb6d18c180b6c07fa8bc0326a68acb23 /src/declarative/fx
parentf7cf2cdc0b6c83a639d155987f871f7932c1d845 (diff)
downloadQt-a276e872e83e0c2f04a367299580ef787b6efc51.zip
Qt-a276e872e83e0c2f04a367299580ef787b6efc51.tar.gz
Qt-a276e872e83e0c2f04a367299580ef787b6efc51.tar.bz2
Share GL textures where possible
Diffstat (limited to 'src/declarative/fx')
-rw-r--r--src/declarative/fx/qfximage.cpp57
-rw-r--r--src/declarative/fx/qfximage_p.h4
-rw-r--r--src/declarative/fx/qfxrect.cpp126
-rw-r--r--src/declarative/fx/qfxrect_p.h4
4 files changed, 112 insertions, 79 deletions
diff --git a/src/declarative/fx/qfximage.cpp b/src/declarative/fx/qfximage.cpp
index 509323a..03a674d 100644
--- a/src/declarative/fx/qfximage.cpp
+++ b/src/declarative/fx/qfximage.cpp
@@ -124,9 +124,13 @@ QFxImage::QFxImage(QFxImagePrivate &dd, QFxItem *parent)
QFxImage::~QFxImage()
{
- Q_D(const QFxImage);
+ Q_D(QFxImage);
if (d->sciReply)
d->sciReply->deleteLater();
+ if (d->tex) {
+ d->tex->release();
+ d->tex = 0;
+ }
}
/*!
@@ -156,7 +160,10 @@ void QFxImage::setPixmap(const QPixmap &pix)
#if defined(QFX_RENDER_OPENGL)
d->texDirty = true;
- d->tex.clear();
+ if (d->tex) {
+ d->tex->release();
+ d->tex = 0;
+ }
#endif
update();
}
@@ -440,7 +447,7 @@ uint QFxImage::glSimpleItemData(float *vertices, float *texVertices,
vertices[4] = 0; vertices[5] = 0;
vertices[6] = widthV; vertices[7] = 0;
- *texture = &d->tex;
+ *texture = d->tex;
if (d->tiled) {
float tileWidth = widthV / d->pix.width();
@@ -451,9 +458,9 @@ uint QFxImage::glSimpleItemData(float *vertices, float *texVertices,
texVertices[6] = tileWidth; texVertices[7] = tileHeight;
} else {
texVertices[0] = 0; texVertices[1] = 0;
- texVertices[2] = d->tex.glWidth(); texVertices[3] = 0;
- texVertices[4] = 0; texVertices[5] = d->tex.glHeight();
- texVertices[6] = d->tex.glWidth(); texVertices[7] = d->tex.glHeight();
+ texVertices[2] = d->tex->glWidth(); texVertices[3] = 0;
+ texVertices[4] = 0; texVertices[5] = d->tex->glHeight();
+ texVertices[6] = d->tex->glWidth(); texVertices[7] = d->tex->glHeight();
}
return 8;
@@ -461,11 +468,9 @@ uint QFxImage::glSimpleItemData(float *vertices, float *texVertices,
void QFxImagePrivate::checkDirty()
{
- if (texDirty && !pix.isNull()) {
- tex.setImage(pix.toImage(), GLTexture::PowerOfTwo);
- tex.setHorizontalWrap(GLTexture::Repeat);
- tex.setVerticalWrap(GLTexture::Repeat);
- }
+ Q_Q(QFxImage);
+ if (texDirty && !pix.isNull())
+ tex = q->cachedTexture(url.toString(), pix);
texDirty = false;
}
@@ -492,8 +497,8 @@ void QFxImage::paintGLContents(GLPainter &p)
float widthV = width();
float heightV = height();
- float glWidth = d->tex.glWidth();
- float glHeight = d->tex.glHeight();
+ float glWidth = d->tex->glWidth();
+ float glHeight = d->tex->glHeight();
float vert[] = {
0, heightV,
@@ -516,7 +521,7 @@ void QFxImage::paintGLContents(GLPainter &p)
shader->setAttributeArray(SingleTextureShader::Vertices, vert, 2);
shader->setAttributeArray(SingleTextureShader::TextureCoords, tex, 2);
- glBindTexture(GL_TEXTURE_2D, d->tex.texture());
+ glBindTexture(GL_TEXTURE_2D, d->tex->texture());
glDrawArrays(GL_TRIANGLES, 0, 6);
} else {
@@ -548,8 +553,8 @@ void QFxImage::paintGLContents(GLPainter &p)
float heightV = height();
float texleft = 0;
- float texright = d->tex.glWidth();
- float textop = d->tex.glHeight();
+ float texright = d->tex->glWidth();
+ float textop = d->tex->glHeight();
float texbottom = 0;
float imgleft = 0;
float imgright = widthV;
@@ -562,19 +567,19 @@ void QFxImage::paintGLContents(GLPainter &p)
const int sgb = d->scaleGrid->bottom();
if (sgl) {
- texleft = d->tex.glWidth() * float(sgl) / imgWidth;
+ texleft = d->tex->glWidth() * float(sgl) / imgWidth;
imgleft = sgl;
}
if (sgr) {
- texright = d->tex.glWidth() - float(sgr) / imgWidth;
+ texright = d->tex->glWidth() - float(sgr) / imgWidth;
imgright = widthV - sgr;
}
if (sgt) {
- textop = d->tex.glHeight() - float(sgb) / imgHeight;
+ textop = d->tex->glHeight() - float(sgb) / imgHeight;
imgtop = sgt;
}
if (sgb) {
- texbottom = d->tex.glHeight() * float(sgt) / imgHeight;
+ texbottom = d->tex->glHeight() * float(sgt) / imgHeight;
imgbottom = heightV - sgb;
}
@@ -722,7 +727,7 @@ void QFxImage::paintGLContents(GLPainter &p)
1, texbottom,
1, 0 };
- glBindTexture(GL_TEXTURE_2D, d->tex.texture());
+ glBindTexture(GL_TEXTURE_2D, d->tex->texture());
shader->setAttributeArray(SingleTextureShader::Vertices, vert1, 2);
shader->setAttributeArray(SingleTextureShader::TextureCoords, tex1, 2);
@@ -847,7 +852,10 @@ void QFxImage::setSource(const QString &url)
setImplicitHeight(0);
#if defined(QFX_RENDER_OPENGL)
d->texDirty = true;
- d->tex.clear();
+ if (d->tex) {
+ d->tex->release();
+ d->tex = 0;
+ }
#endif
emit statusChanged(d->status);
emit sourceChanged(d->source);
@@ -908,7 +916,10 @@ void QFxImage::requestFinished()
d->progress = 1.0;
#if defined(QFX_RENDER_OPENGL)
d->texDirty = true;
- d->tex.clear();
+ if (d->tex) {
+ d->tex->release();
+ d->tex = 0;
+ }
#endif
emit statusChanged(d->status);
emit sourceChanged(d->source);
diff --git a/src/declarative/fx/qfximage_p.h b/src/declarative/fx/qfximage_p.h
index b5c6055..cd07a37 100644
--- a/src/declarative/fx/qfximage_p.h
+++ b/src/declarative/fx/qfximage_p.h
@@ -74,7 +74,7 @@ public:
QFxImagePrivate()
: scaleGrid(0), tiled(false), smooth(false), opaque(false),
#if defined(QFX_RENDER_OPENGL)
- texDirty(true),
+ texDirty(true), tex(0),
#endif
status(QFxImage::Idle), sciReply(0), progress(0.0)
{
@@ -102,7 +102,7 @@ public:
#if defined(QFX_RENDER_OPENGL)
bool texDirty : 1;
void checkDirty();
- GLTexture tex;
+ QSimpleCanvasItem::CachedTexture *tex;
#endif
QFxImage::Status status;
diff --git a/src/declarative/fx/qfxrect.cpp b/src/declarative/fx/qfxrect.cpp
index d52e31c..eacc85e 100644
--- a/src/declarative/fx/qfxrect.cpp
+++ b/src/declarative/fx/qfxrect.cpp
@@ -257,7 +257,10 @@ void QFxRect::doUpdate()
#endif
#if defined(QFX_RENDER_OPENGL)
Q_D(QFxRect);
- d->rectTexture.clear();
+ if (d->rectTexture) {
+ d->rectTexture->release();
+ d->rectTexture = 0;
+ }
#endif
const int pw = d->pen && d->pen->isValid() ? d->pen->width() : 0;
setPaintMargin((pw+1)/2);
@@ -361,7 +364,10 @@ void QFxRect::setRadius(qreal radius)
#if defined(QFX_RENDER_QPAINTER)
d->rectImage = QPixmap();
#elif defined(QFX_RENDER_OPENGL)
- d->rectTexture.clear();
+ if (d->rectTexture) {
+ d->rectTexture->release();
+ d->rectTexture = 0;
+ }
#endif
update();
}
@@ -408,7 +414,10 @@ void QFxRect::setColor(const QColor &c)
d->rectImage = QPixmap();
#endif
#if defined(QFX_RENDER_OPENGL)
- d->rectTexture.clear();
+ if (d->rectTexture) {
+ d->rectTexture->release();
+ d->rectTexture = 0;
+ }
#endif
update();
}
@@ -521,42 +530,55 @@ void QFxRect::generateBorderedRect()
void QFxRect::generateRoundedRect()
{
Q_D(QFxRect);
- if (d->rectTexture.isNull()) {
+ if (!d->rectTexture) {
const int pw = d->pen && d->pen->isValid() ? d->pen->width() : 0;
- QImage roundRect(d->radius*2 + 4 + pw*2, d->radius*2 + 4 + pw*2, QImage::Format_ARGB32_Premultiplied);
- roundRect.fill(0);
- QPainter p(&roundRect);
- p.setRenderHint(QPainter::Antialiasing);
- if (d->pen && d->pen->isValid()) {
- QPen pn(QColor(pen()->color()), pen()->width());
- p.setPen(pn);
- } else {
- p.setPen(Qt::NoPen);
+ QString key = QString("QFxRect://r_%1_%2_%3_%4").arg(pw).arg(d->radius).arg((d->pen && d->pen->isValid())?d->pen->color().name():QString()).arg(d->color.name());
+
+ d->rectTexture = cachedTexture(key);
+
+ if (!d->rectTexture) {
+ QPixmap roundRect(d->radius*2 + 4 + pw*2, d->radius*2 + 4 + pw*2);
+ roundRect.fill(Qt::transparent);
+ QPainter p(&roundRect);
+ p.setRenderHint(QPainter::Antialiasing);
+ if (d->pen && d->pen->isValid()) {
+ QPen pn(QColor(pen()->color()), pen()->width());
+ p.setPen(pn);
+ } else {
+ p.setPen(Qt::NoPen);
+ }
+ p.setBrush(d->color);
+ p.drawRoundedRect((pw+1)/2, (pw+1)/2, roundRect.width()-(pw+1)/2*2, roundRect.height()-(pw+1)/2*2, d->radius, d->radius);
+
+ d->rectTexture = cachedTexture(key, roundRect);
}
- p.setBrush(d->color);
- p.drawRoundedRect((pw+1)/2, (pw+1)/2, roundRect.width()-(pw+1)/2*2, roundRect.height()-(pw+1)/2*2, d->radius, d->radius);
- d->rectTexture.setImage(roundRect, GLTexture::PowerOfTwo);
}
}
void QFxRect::generateBorderedRect()
{
Q_D(QFxRect);
- if (d->rectTexture.isNull()) {
+ if (!d->rectTexture) {
const int pw = d->pen && d->pen->isValid() ? d->pen->width() : 0;
- QImage borderedRect(pw*2 + 4, pw*2 + 4, QImage::Format_ARGB32_Premultiplied);
- borderedRect.fill(0);
- QPainter p(&(borderedRect));
- p.setRenderHint(QPainter::Antialiasing);
- if (d->pen && d->pen->isValid()) {
- QPen pn(QColor(pen()->color()), pen()->width());
- p.setPen(pn);
- } else {
- p.setPen(Qt::NoPen);
+ QString key = QString("QFxRect://b_%1_%2_%3_%4").arg(pw).arg(d->radius).arg((d->pen && d->pen->isValid())?d->pen->color().name():QString()).arg(d->color.name());
+
+ d->rectTexture = cachedTexture(key);
+
+ if (!d->rectTexture) {
+ QPixmap borderedRect(pw*2 + 4, pw*2 + 4);
+ borderedRect.fill(Qt::transparent);
+ QPainter p(&(borderedRect));
+ p.setRenderHint(QPainter::Antialiasing);
+ if (d->pen && d->pen->isValid()) {
+ QPen pn(QColor(pen()->color()), pen()->width());
+ p.setPen(pn);
+ } else {
+ p.setPen(Qt::NoPen);
+ }
+ p.setBrush(d->color);
+ p.drawRect(qreal(pw+1)/2, qreal(pw+1)/2, borderedRect.width()-(pw+1)/2*2, borderedRect.height()-(pw+1)/2*2);
+ d->rectTexture = cachedTexture(key, borderedRect);
}
- p.setBrush(d->color);
- p.drawRect(qreal(pw+1)/2, qreal(pw+1)/2, borderedRect.width()-(pw+1)/2*2, borderedRect.height()-(pw+1)/2*2);
- d->rectTexture.setImage(borderedRect, GLTexture::PowerOfTwo);
}
}
#endif
@@ -721,8 +743,8 @@ void QFxRect::paintGLContents(GLPainter &p)
QGLShaderProgram *shader = p.useTextureShader();
- float texWidth = d->rectTexture.width();
- float texHeight = d->rectTexture.height();
+ float texWidth = d->rectTexture->width();
+ float texHeight = d->rectTexture->height();
if (!texWidth || !texHeight)
return;
@@ -756,10 +778,10 @@ void QFxRect::paintGLContents(GLPainter &p)
if (offset==1)
texleft=texright=textop=texbottom=0.5;
- texleft *= d->rectTexture.glWidth();
- texright *= d->rectTexture.glWidth();
- textop *= d->rectTexture.glHeight();
- texbottom *= d->rectTexture.glHeight();
+ texleft *= d->rectTexture->glWidth();
+ texright *= d->rectTexture->glWidth();
+ textop *= d->rectTexture->glHeight();
+ texbottom *= d->rectTexture->glHeight();
float vert1[] = { -pw/2, -pw/2,
-pw/2, imgtop,
@@ -852,35 +874,35 @@ void QFxRect::paintGLContents(GLPainter &p)
texright, 0,
texright, textop,
- d->rectTexture.glWidth(), 0,
+ d->rectTexture->glWidth(), 0,
texright, textop,
- d->rectTexture.glWidth(), 0,
- d->rectTexture.glWidth(), textop,
+ d->rectTexture->glWidth(), 0,
+ d->rectTexture->glWidth(), textop,
- 0, d->rectTexture.glHeight(),
+ 0, d->rectTexture->glHeight(),
0, texbottom,
- texleft, d->rectTexture.glHeight(),
+ texleft, d->rectTexture->glHeight(),
0, texbottom,
- texleft, d->rectTexture.glHeight(),
+ texleft, d->rectTexture->glHeight(),
texleft, texbottom,
- texleft, d->rectTexture.glHeight(),
+ texleft, d->rectTexture->glHeight(),
texleft, texbottom,
- texright, d->rectTexture.glHeight(),
+ texright, d->rectTexture->glHeight(),
texleft, texbottom,
- texright, d->rectTexture.glHeight(),
+ texright, d->rectTexture->glHeight(),
texright, texbottom,
- texright, d->rectTexture.glHeight(),
+ texright, d->rectTexture->glHeight(),
texright, texbottom,
- d->rectTexture.glWidth(), d->rectTexture.glHeight(),
+ d->rectTexture->glWidth(), d->rectTexture->glHeight(),
texright, texbottom,
- d->rectTexture.glWidth(), d->rectTexture.glHeight(),
- d->rectTexture.glWidth(), texbottom,
+ d->rectTexture->glWidth(), d->rectTexture->glHeight(),
+ d->rectTexture->glWidth(), texbottom,
0, textop,
0, texbottom,
@@ -900,15 +922,15 @@ void QFxRect::paintGLContents(GLPainter &p)
texright, textop,
texright, texbottom,
- d->rectTexture.glWidth(), textop,
+ d->rectTexture->glWidth(), textop,
texright, texbottom,
- d->rectTexture.glWidth(), textop,
- d->rectTexture.glWidth(), texbottom };
+ d->rectTexture->glWidth(), textop,
+ d->rectTexture->glWidth(), texbottom };
- glBindTexture(GL_TEXTURE_2D, d->rectTexture.texture());
+ glBindTexture(GL_TEXTURE_2D, d->rectTexture->texture());
shader->setAttributeArray(SingleTextureShader::Vertices, vert1, 2);
shader->setAttributeArray(SingleTextureShader::TextureCoords, tex1, 2);
diff --git a/src/declarative/fx/qfxrect_p.h b/src/declarative/fx/qfxrect_p.h
index 6d11917..d68eb4e 100644
--- a/src/declarative/fx/qfxrect_p.h
+++ b/src/declarative/fx/qfxrect_p.h
@@ -69,7 +69,7 @@ class QFxRectPrivate : public QFxItemPrivate
public:
QFxRectPrivate()
- : gradient(0), pen(0), radius(0)
+ : rectTexture(0), gradient(0), pen(0), radius(0)
{
}
@@ -83,7 +83,7 @@ public:
}
#if defined(QFX_RENDER_OPENGL)
- GLTexture rectTexture;
+ QSimpleCanvasItem::CachedTexture *rectTexture;
#endif
QColor getColor();
QColor color;