Bug 818060 - Add a memory reporter for graphics textures. r=njn,BenWa

This commit is contained in:
Kartikaya Gupta 2012-12-13 11:38:37 -05:00
parent e99b00147f
commit 555f703aa1
6 changed files with 120 additions and 3 deletions

View File

@ -3108,6 +3108,8 @@ protected:
}
};
uint32_t GetBitsPerTexel(GLenum format, GLenum type);
} /* namespace gl */
} /* namespace mozilla */

View File

@ -419,6 +419,63 @@ GLContext::BlitTextureToTexture(GLuint srcTex, GLuint destTex,
srcSize, destSize);
}
uint32_t GetBitsPerTexel(GLenum format, GLenum type)
{
// If there is no defined format or type, we're not taking up any memory
if (!format || !type) {
return 0;
}
if (format == LOCAL_GL_DEPTH_COMPONENT) {
if (type == LOCAL_GL_UNSIGNED_SHORT)
return 2;
else if (type == LOCAL_GL_UNSIGNED_INT)
return 4;
} else if (format == LOCAL_GL_DEPTH_STENCIL) {
if (type == LOCAL_GL_UNSIGNED_INT_24_8_EXT)
return 4;
}
if (type == LOCAL_GL_UNSIGNED_BYTE || type == LOCAL_GL_FLOAT) {
int multiplier = type == LOCAL_GL_FLOAT ? 32 : 8;
switch (format) {
case LOCAL_GL_ALPHA:
case LOCAL_GL_LUMINANCE:
return 1 * multiplier;
case LOCAL_GL_LUMINANCE_ALPHA:
return 2 * multiplier;
case LOCAL_GL_RGB:
return 3 * multiplier;
case LOCAL_GL_RGBA:
return 4 * multiplier;
case LOCAL_GL_COMPRESSED_RGB_PVRTC_2BPPV1:
case LOCAL_GL_COMPRESSED_RGBA_PVRTC_2BPPV1:
return 2;
case LOCAL_GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
case LOCAL_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
case LOCAL_GL_ATC_RGB:
case LOCAL_GL_COMPRESSED_RGB_PVRTC_4BPPV1:
case LOCAL_GL_COMPRESSED_RGBA_PVRTC_4BPPV1:
return 4;
case LOCAL_GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
case LOCAL_GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
case LOCAL_GL_ATC_RGBA_EXPLICIT_ALPHA:
case LOCAL_GL_ATC_RGBA_INTERPOLATED_ALPHA:
return 8;
default:
break;
}
} else if (type == LOCAL_GL_UNSIGNED_SHORT_4_4_4_4 ||
type == LOCAL_GL_UNSIGNED_SHORT_5_5_5_1 ||
type == LOCAL_GL_UNSIGNED_SHORT_5_6_5)
{
return 16;
}
MOZ_ASSERT(false);
return 0;
}
} /* namespace gl */
} /* namespace mozilla */

View File

@ -21,9 +21,7 @@ TiledLayerBufferOGL::~TiledLayerBufferOGL()
mContext->MakeCurrent();
for (size_t i = 0; i < mRetainedTiles.Length(); i++) {
if (mRetainedTiles[i] == GetPlaceholderTile())
continue;
mContext->fDeleteTextures(1, &mRetainedTiles[i].mTextureHandle);
ReleaseTile(mRetainedTiles[i]);
}
}
@ -34,6 +32,8 @@ TiledLayerBufferOGL::ReleaseTile(TiledTexture aTile)
if (aTile == GetPlaceholderTile())
return;
mContext->fDeleteTextures(1, &aTile.mTextureHandle);
gfxPlatform::UpdateTiledThebesLayerTextureUsage(eMemoryUse_free, aTile.mFormat, GetTileType(aTile), GetTileLength());
}
void
@ -59,6 +59,13 @@ TiledLayerBufferOGL::Upload(const BasicTiledLayerBuffer* aMainMemoryTiledBuffer,
#endif
}
GLenum
TiledLayerBufferOGL::GetTileType(TiledTexture aTile)
{
// Deduce the type that was assigned in GetFormatAndTileForImageFormat
return aTile.mFormat == LOCAL_GL_RGB ? LOCAL_GL_UNSIGNED_SHORT_5_6_5 : LOCAL_GL_UNSIGNED_BYTE;
}
void
TiledLayerBufferOGL::GetFormatAndTileForImageFormat(gfxASurface::gfxImageFormat aFormat,
GLenum& aOutFormat,
@ -91,6 +98,9 @@ TiledLayerBufferOGL::ValidateTile(TiledTexture aTile,
mContext->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_T, LOCAL_GL_CLAMP_TO_EDGE);
} else {
mContext->fBindTexture(LOCAL_GL_TEXTURE_2D, aTile.mTextureHandle);
// We're re-using a texture, but the format may change. Update the memory
// reporter with a free and alloc (below) using the old and new formats.
gfxPlatform::UpdateTiledThebesLayerTextureUsage(eMemoryUse_free, aTile.mFormat, GetTileType(aTile), GetTileLength());
}
nsRefPtr<gfxReusableSurfaceWrapper> reusableSurface = mMainMemoryTiledBuffer->GetTile(aTileOrigin).mSurface.get();
@ -102,6 +112,8 @@ TiledLayerBufferOGL::ValidateTile(TiledTexture aTile,
GetTileLength(), GetTileLength(), 0,
format, type, buf);
gfxPlatform::UpdateTiledThebesLayerTextureUsage(eMemoryUse_alloc, format, type, GetTileLength());
aTile.mFormat = format;
#ifdef GFX_TILEDLAYER_PREF_WARNINGS

View File

@ -100,6 +100,7 @@ private:
const BasicTiledLayerBuffer* mMainMemoryTiledBuffer;
gfxSize mFrameResolution;
GLenum GetTileType(TiledTexture aTile);
void GetFormatAndTileForImageFormat(gfxASurface::gfxImageFormat aFormat,
GLenum& aOutFormat,
GLenum& aOutType);

View File

@ -70,6 +70,7 @@
#include "mozilla/Attributes.h"
#include "nsIGfxInfo.h"
#include "nsIMemoryReporter.h"
using namespace mozilla;
using namespace mozilla::layers;
@ -213,6 +214,33 @@ static const char *gPrefLangNames[] = {
"x-user-def"
};
static int64_t sTiledThebesLayerTextureUsage = 0;
static int64_t
GetTiledThebesLayerTextureUsage()
{
return sTiledThebesLayerTextureUsage;
}
void
gfxPlatform::UpdateTiledThebesLayerTextureUsage(eMemoryUse action, GLenum format, GLenum type, uint16_t tileSize)
{
uint32_t bytesPerTexel = mozilla::gl::GetBitsPerTexel(format, type) / 8;
int64_t bytes = (int64_t)(tileSize * tileSize * bytesPerTexel);
if (action == eMemoryUse_free) {
sTiledThebesLayerTextureUsage -= bytes;
} else {
sTiledThebesLayerTextureUsage += bytes;
}
}
NS_MEMORY_REPORTER_IMPLEMENT(TiledThebesLayer,
"gfx-tiled-thebes-layer-textures",
KIND_OTHER,
UNITS_BYTES,
GetTiledThebesLayerTextureUsage,
"Texture memory used by TiledThebesLayer.");
gfxPlatform::gfxPlatform()
: mAzureCanvasBackendCollector(this, &gfxPlatform::GetAzureBackendInfo)
{
@ -229,6 +257,8 @@ gfxPlatform::gfxPlatform()
uint32_t canvasMask = (1 << BACKEND_CAIRO) | (1 << BACKEND_SKIA);
uint32_t contentMask = 0;
InitBackendPrefs(canvasMask, contentMask);
NS_RegisterMemoryReporter(new NS_MEMORY_REPORTER_NAME(TiledThebesLayer));
}
gfxPlatform*

View File

@ -20,6 +20,7 @@
#include "gfx2DGlue.h"
#include "mozilla/RefPtr.h"
#include "GfxInfoCollector.h"
#include "GLContext.h"
#ifdef XP_OS2
#undef OS2EMX_PLAIN_CHAR
@ -104,6 +105,13 @@ enum eGfxLog {
eGfxLog_cmapdata = 4
};
enum eMemoryUse {
// when memory being allocated is reported to a memory reporter
eMemoryUse_alloc = 0,
// when memory being freed is reported to a memory reporter
eMemoryUse_free = 1
};
// when searching through pref langs, max number of pref langs
const uint32_t kMaxLenPrefLangList = 32;
@ -416,6 +424,13 @@ public:
// excludes previously valid tiles.
static bool UseReusableTileStore();
// When memory is used/freed for tile textures, call this method
// to update the value reported by the memory reporter.
static void UpdateTiledThebesLayerTextureUsage(eMemoryUse action,
GLenum format,
GLenum type,
uint16_t tileSize);
static bool OffMainThreadCompositingEnabled();
/**