1936 QVERIFY(guard3.context() == 0); |
1959 QVERIFY(guard3.context() == 0); |
1937 QVERIFY(guard3.id() == 0); |
1960 QVERIFY(guard3.id() == 0); |
1938 #endif |
1961 #endif |
1939 } |
1962 } |
1940 |
1963 |
|
1964 // Tests QGLContext::bindTexture with default options |
|
1965 void tst_QGL::qglContextDefaultBindTexture() |
|
1966 { |
|
1967 #ifdef QT_BUILD_INTERNAL |
|
1968 QGLWidget w; |
|
1969 w.makeCurrent(); |
|
1970 QGLContext *ctx = const_cast<QGLContext*>(w.context()); |
|
1971 |
|
1972 QImage *boundImage = new QImage(256, 256, QImage::Format_RGB32); |
|
1973 boundImage->fill(0xFFFFFFFF); |
|
1974 QPixmap *boundPixmap = new QPixmap(256, 256); |
|
1975 boundPixmap->fill(Qt::red); |
|
1976 |
|
1977 int startCacheItemCount = QGLTextureCache::instance()->size(); |
|
1978 |
|
1979 GLuint boundImageTextureId = ctx->bindTexture(*boundImage); |
|
1980 GLuint boundPixmapTextureId = ctx->bindTexture(*boundPixmap); |
|
1981 |
|
1982 // Make sure the image & pixmap have been added to the cache: |
|
1983 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2); |
|
1984 |
|
1985 // Make sure the image & pixmap have the is_cached flag set: |
|
1986 QVERIFY(QImagePixmapCleanupHooks::isImageCached(*boundImage)); |
|
1987 QVERIFY(QImagePixmapCleanupHooks::isPixmapCached(*boundPixmap)); |
|
1988 |
|
1989 // Make sure the texture IDs returned are valid: |
|
1990 QCOMPARE((bool)glIsTexture(boundImageTextureId), GL_TRUE); |
|
1991 QCOMPARE((bool)glIsTexture(boundPixmapTextureId), GL_TRUE); |
|
1992 |
|
1993 // Make sure the textures are still valid after we delete the image/pixmap: |
|
1994 // Also check that although the textures are left intact, the cache entries are removed: |
|
1995 delete boundImage; |
|
1996 boundImage = 0; |
|
1997 QCOMPARE((bool)glIsTexture(boundImageTextureId), GL_TRUE); |
|
1998 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1); |
|
1999 delete boundPixmap; |
|
2000 boundPixmap = 0; |
|
2001 QCOMPARE((bool)glIsTexture(boundPixmapTextureId), GL_TRUE); |
|
2002 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount); |
|
2003 |
|
2004 // Finally, make sure QGLContext::deleteTexture deletes the texture IDs: |
|
2005 ctx->deleteTexture(boundImageTextureId); |
|
2006 ctx->deleteTexture(boundPixmapTextureId); |
|
2007 QCOMPARE((bool)glIsTexture(boundImageTextureId), GL_FALSE); |
|
2008 QCOMPARE((bool)glIsTexture(boundPixmapTextureId), GL_FALSE); |
|
2009 #endif |
|
2010 } |
|
2011 |
|
2012 void tst_QGL::textureCleanup() |
|
2013 { |
|
2014 #ifdef QT_BUILD_INTERNAL |
|
2015 QGLWidget w; |
|
2016 w.resize(200,200); |
|
2017 w.show(); |
|
2018 w.makeCurrent(); |
|
2019 |
|
2020 // Test pixmaps which have been loaded via QPixmapCache are removed from the texture cache |
|
2021 // when the pixmap cache is cleared |
|
2022 { |
|
2023 int startCacheItemCount = QGLTextureCache::instance()->size(); |
|
2024 QPainter p(&w); |
|
2025 |
|
2026 QPixmap boundPixmap(":designer.png"); |
|
2027 |
|
2028 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount); |
|
2029 |
|
2030 p.drawPixmap(0, 0, boundPixmap); |
|
2031 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1); |
|
2032 |
|
2033 // Need to call end for the GL2 paint engine to release references to pixmap if using tfp |
|
2034 p.end(); |
|
2035 |
|
2036 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1); |
|
2037 |
|
2038 // Check that the texture doesn't get removed from the cache when the pixmap is cleared |
|
2039 // as it should still be in the cache: |
|
2040 boundPixmap = QPixmap(); |
|
2041 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1); |
|
2042 |
|
2043 QPixmapCache::clear(); |
|
2044 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount); |
|
2045 } |
|
2046 |
|
2047 // Test pixmaps which have been loaded via QPixmapCache are removed from the texture cache |
|
2048 // when they are explicitly removed from the pixmap cache |
|
2049 { |
|
2050 int startCacheItemCount = QGLTextureCache::instance()->size(); |
|
2051 QPainter p(&w); |
|
2052 |
|
2053 QPixmap boundPixmap(128, 128); |
|
2054 QString cacheKey = QString::fromLatin1("myPixmap"); |
|
2055 QPixmapCache::insert(cacheKey, boundPixmap); |
|
2056 |
|
2057 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount); |
|
2058 |
|
2059 p.drawPixmap(0, 0, boundPixmap); |
|
2060 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1); |
|
2061 |
|
2062 // Need to call end for the GL2 paint engine to release references to pixmap if using tfp |
|
2063 p.end(); |
|
2064 |
|
2065 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1); |
|
2066 |
|
2067 // Check that the texture doesn't get removed from the cache when the pixmap is cleared |
|
2068 // as it should still be in the cache: |
|
2069 boundPixmap = QPixmap(); |
|
2070 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1); |
|
2071 |
|
2072 // Finally, we check that the texture cache entry is removed when we remove the |
|
2073 // pixmap cache entry, which should hold the last reference: |
|
2074 QPixmapCache::remove(cacheKey); |
|
2075 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount); |
|
2076 } |
|
2077 |
|
2078 // Check images & pixmaps are removed from the cache when they are deleted |
|
2079 { |
|
2080 int startCacheItemCount = QGLTextureCache::instance()->size(); |
|
2081 QPainter p(&w); |
|
2082 |
|
2083 QImage *boundImage = new QImage(256, 256, QImage::Format_RGB32); |
|
2084 boundImage->fill(0xFFFFFFFF); |
|
2085 QPixmap *boundPixmap = new QPixmap(256, 256); |
|
2086 boundPixmap->fill(Qt::red); |
|
2087 |
|
2088 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount); |
|
2089 |
|
2090 p.drawImage(0, 0, *boundImage); |
|
2091 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1); |
|
2092 |
|
2093 p.drawPixmap(0, 0, *boundPixmap); |
|
2094 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2); |
|
2095 |
|
2096 // Need to call end for the GL2 paint engine to release references to pixmap if using tfp |
|
2097 p.end(); |
|
2098 |
|
2099 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2); |
|
2100 |
|
2101 delete boundImage; |
|
2102 boundImage = 0; |
|
2103 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1); |
|
2104 |
|
2105 delete boundPixmap; |
|
2106 boundPixmap = 0; |
|
2107 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount); |
|
2108 } |
|
2109 |
|
2110 // Check images & pixmaps are removed from the cache when they are assigned to |
|
2111 { |
|
2112 int startCacheItemCount = QGLTextureCache::instance()->size(); |
|
2113 QPainter p(&w); |
|
2114 |
|
2115 QImage boundImage(256, 256, QImage::Format_RGB32); |
|
2116 boundImage.fill(0xFFFFFFFF); |
|
2117 QPixmap boundPixmap(256, 256); |
|
2118 boundPixmap.fill(Qt::red); |
|
2119 |
|
2120 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount); |
|
2121 |
|
2122 p.drawImage(0, 0, boundImage); |
|
2123 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1); |
|
2124 |
|
2125 p.drawPixmap(0, 0, boundPixmap); |
|
2126 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2); |
|
2127 |
|
2128 // Need to call end for the GL2 paint engine to release references to pixmap if using tfp |
|
2129 p.end(); |
|
2130 |
|
2131 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2); |
|
2132 |
|
2133 boundImage = QImage(64, 64, QImage::Format_RGB32); |
|
2134 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1); |
|
2135 |
|
2136 boundPixmap = QPixmap(64, 64); |
|
2137 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount); |
|
2138 } |
|
2139 |
|
2140 // Check images & pixmaps are removed from the cache when they are modified (detached) |
|
2141 { |
|
2142 int startCacheItemCount = QGLTextureCache::instance()->size(); |
|
2143 QPainter p(&w); |
|
2144 |
|
2145 QImage boundImage(256, 256, QImage::Format_RGB32); |
|
2146 boundImage.fill(0xFFFFFFFF); |
|
2147 QPixmap boundPixmap(256, 256); |
|
2148 boundPixmap.fill(Qt::red); |
|
2149 |
|
2150 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount); |
|
2151 |
|
2152 p.drawImage(0, 0, boundImage); |
|
2153 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1); |
|
2154 |
|
2155 p.drawPixmap(0, 0, boundPixmap); |
|
2156 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2); |
|
2157 |
|
2158 // Need to call end for the GL2 paint engine to release references to pixmap if using tfp |
|
2159 p.end(); |
|
2160 |
|
2161 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2); |
|
2162 |
|
2163 boundImage.fill(0x00000000); |
|
2164 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1); |
|
2165 |
|
2166 boundPixmap.fill(Qt::blue); |
|
2167 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount); |
|
2168 } |
|
2169 |
|
2170 // Check that images/pixmaps aren't removed from the cache if a shallow copy has been made |
|
2171 QImage copyOfImage; |
|
2172 QPixmap copyOfPixmap; |
|
2173 int startCacheItemCount = QGLTextureCache::instance()->size(); |
|
2174 { |
|
2175 QPainter p(&w); |
|
2176 |
|
2177 QImage boundImage(256, 256, QImage::Format_RGB32); |
|
2178 boundImage.fill(0xFFFFFFFF); |
|
2179 QPixmap boundPixmap(256, 256); |
|
2180 boundPixmap.fill(Qt::red); |
|
2181 |
|
2182 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount); |
|
2183 |
|
2184 p.drawImage(0, 0, boundImage); |
|
2185 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1); |
|
2186 |
|
2187 p.drawPixmap(0, 0, boundPixmap); |
|
2188 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2); |
|
2189 |
|
2190 // Need to call end for the GL2 paint engine to release references to pixmap if using tfp |
|
2191 p.end(); |
|
2192 |
|
2193 copyOfImage = boundImage; |
|
2194 copyOfPixmap = boundPixmap; |
|
2195 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2); |
|
2196 } // boundImage & boundPixmap would have been deleted when they went out of scope |
|
2197 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+2); |
|
2198 |
|
2199 copyOfImage = QImage(); |
|
2200 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount+1); |
|
2201 |
|
2202 copyOfPixmap = QPixmap(); |
|
2203 QCOMPARE(QGLTextureCache::instance()->size(), startCacheItemCount); |
|
2204 #endif |
|
2205 } |
|
2206 |
1941 QTEST_MAIN(tst_QGL) |
2207 QTEST_MAIN(tst_QGL) |
1942 #include "tst_qgl.moc" |
2208 #include "tst_qgl.moc" |