Bug 831193 (part 10) - Don't use NS_MEMORY_REPORTER_IMPLEMENT in parts of gfx/. r=bjacob.
authorNicholas Nethercote <nnethercote@mozilla.com>
Thu, 17 Jan 2013 16:45:11 -0800
changeset 144649 e704db333c96ee0c3d724d755249e7a836e17749
parent 144648 24b0e5ef846ae160051aa86a8ff10a3af2fc0781
child 144650 5680c6a6162f521c4545d14a111c8d26c29742c9
push id33045
push usernnethercote@mozilla.com
push dateWed, 28 Aug 2013 08:59:06 +0000
treeherdermozilla-inbound@eead1e72622d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbjacob
bugs831193
milestone26.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 831193 (part 10) - Don't use NS_MEMORY_REPORTER_IMPLEMENT in parts of gfx/. r=bjacob.
gfx/gl/GLContext.cpp
gfx/gl/GLContext.h
gfx/layers/ipc/ShadowLayerUtilsGralloc.cpp
gfx/layers/opengl/TextureHostOGL.cpp
--- a/gfx/gl/GLContext.cpp
+++ b/gfx/gl/GLContext.cpp
@@ -1,9 +1,10 @@
-/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* vim: set ts=8 sts=4 et sw=4 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include <algorithm>
 #include <stdio.h>
 #include <string.h>
 
@@ -108,43 +109,31 @@ static const char *sExtensionNames[] = {
     "GL_ANGLE_instanced_arrays",
     "GL_EXT_occlusion_query_boolean",
     "GL_ARB_occlusion_query2",
     "GL_EXT_transform_feedback",
     "GL_NV_transform_feedback",
     nullptr
 };
 
-static int64_t sTextureMemoryUsage = 0;
-
-static int64_t
-GetTextureMemoryUsage()
-{
-    return sTextureMemoryUsage;
-}
-
-void
-GLContext::UpdateTextureMemoryUsage(MemoryUse action, GLenum format, GLenum type, uint16_t tileSize)
+int64_t GfxTexturesReporter::sAmount = 0;
+
+/* static */ void
+GfxTexturesReporter::UpdateAmount(MemoryUse 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 == MemoryFreed) {
-        sTextureMemoryUsage -= bytes;
+        sAmount -= bytes;
     } else {
-        sTextureMemoryUsage += bytes;
+        sAmount += bytes;
     }
 }
 
-NS_MEMORY_REPORTER_IMPLEMENT(TextureMemoryUsage,
-    "gfx-textures",
-    KIND_OTHER,
-    UNITS_BYTES,
-    GetTextureMemoryUsage,
-    "Memory used for storing GL textures.")
-
 /*
  * XXX - we should really know the ARB/EXT variants of these
  * instead of only handling the symbol if it's exposed directly.
  */
 
 bool
 GLContext::InitWithPrefix(const char *prefix, bool trygl)
 {
@@ -997,17 +986,17 @@ GLContext::CanUploadSubTextures()
 
 bool GLContext::sPowerOfTwoForced = false;
 bool GLContext::sPowerOfTwoPrefCached = false;
 
 void
 GLContext::PlatformStartup()
 {
   CacheCanUploadNPOT();
-  NS_RegisterMemoryReporter(new NS_MEMORY_REPORTER_NAME(TextureMemoryUsage));
+  NS_RegisterMemoryReporter(new GfxTexturesReporter());
 }
 
 void
 GLContext::CacheCanUploadNPOT()
 {
     MOZ_ASSERT(NS_IsMainThread(), "Can't cache prefs off the main thread.");
     MOZ_ASSERT(!sPowerOfTwoPrefCached, "Must only call this function once!");
 
--- a/gfx/gl/GLContext.h
+++ b/gfx/gl/GLContext.h
@@ -1,9 +1,10 @@
-/* -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40; -*- */
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* vim: set ts=8 sts=4 et sw=4 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef GLCONTEXT_H_
 #define GLCONTEXT_H_
 
 #include <stdio.h>
@@ -35,16 +36,17 @@
 #include "nsISupportsImpl.h"
 #include "prlink.h"
 #include "plstr.h"
 
 #include "nsDataHashtable.h"
 #include "nsHashKeys.h"
 #include "nsRegion.h"
 #include "nsAutoPtr.h"
+#include "nsIMemoryReporter.h"
 #include "nsThreadUtils.h"
 #include "GLContextTypes.h"
 #include "GLTextureImage.h"
 #include "SurfaceTypes.h"
 #include "GLScreenBuffer.h"
 
 typedef char realGLboolean;
 
@@ -3439,31 +3441,50 @@ public:
     nsTArray<NamedResource> mTrackedPrograms;
     nsTArray<NamedResource> mTrackedShaders;
     nsTArray<NamedResource> mTrackedTextures;
     nsTArray<NamedResource> mTrackedFramebuffers;
     nsTArray<NamedResource> mTrackedRenderbuffers;
     nsTArray<NamedResource> mTrackedBuffers;
     nsTArray<NamedResource> mTrackedQueries;
 #endif
-
+};
+
+class GfxTexturesReporter MOZ_FINAL : public MemoryReporterBase
+{
 public:
+    GfxTexturesReporter()
+      : MemoryReporterBase("gfx-textures", KIND_OTHER, UNITS_BYTES,
+                           "Memory used for storing GL textures.")
+    {
+#ifdef DEBUG
+        // There must be only one instance of this class, due to |sAmount|
+        // being static.  Assert this.
+        static bool hasRun = false;
+        MOZ_ASSERT(!hasRun);
+        hasRun = true;
+#endif
+    }
+
     enum MemoryUse {
         // when memory being allocated is reported to a memory reporter
         MemoryAllocated,
         // when memory being freed is reported to a memory reporter
         MemoryFreed
     };
 
-    // When memory is used/freed for tile textures, call this method
-    // to update the value reported by the memory reporter.
-    static void UpdateTextureMemoryUsage(MemoryUse action,
-                                         GLenum format,
-                                         GLenum type,
-                                         uint16_t tileSize);
+    // When memory is used/freed for tile textures, call this method to update
+    // the value reported by this memory reporter.
+    static void UpdateAmount(MemoryUse action, GLenum format, GLenum type,
+                             uint16_t tileSize);
+
+private:
+    int64_t Amount() MOZ_OVERRIDE { return sAmount; }
+
+    static int64_t sAmount;
 };
 
 inline bool
 DoesStringMatch(const char* aString, const char *aWantedString)
 {
     if (!aString || !aWantedString)
         return false;
 
--- a/gfx/layers/ipc/ShadowLayerUtilsGralloc.cpp
+++ b/gfx/layers/ipc/ShadowLayerUtilsGralloc.cpp
@@ -178,49 +178,65 @@ PixelFormatForContentType(gfxASurface::g
 }
 
 static gfxASurface::gfxContentType
 ContentTypeFromPixelFormat(android::PixelFormat aFormat)
 {
   return gfxASurface::ContentFromFormat(ImageFormatForPixelFormat(aFormat));
 }
 
-static size_t sCurrentAlloc;
-static int64_t GetGrallocSize() { return sCurrentAlloc; }
+class GrallocReporter MOZ_FINAL : public MemoryReporterBase
+{
+  friend class GrallocBufferActor;
 
-NS_MEMORY_REPORTER_IMPLEMENT(GrallocBufferActor,
-  "gralloc",
-  KIND_OTHER,
-  UNITS_BYTES,
-  GetGrallocSize,
-  "Special RAM that can be shared between processes and directly "
-  "accessed by both the CPU and GPU.  Gralloc memory is usually a "
-  "relatively precious resource, with much less available than generic "
-  "RAM.  When it's exhausted, graphics performance can suffer. "
-  "This value can be incorrect because of race conditions.");
+public:
+  GrallocReporter()
+    : MemoryReporterBase("gralloc", KIND_OTHER, UNITS_BYTES,
+"Special RAM that can be shared between processes and directly accessed by "
+"both the CPU and GPU.  Gralloc memory is usually a relatively precious "
+"resource, with much less available than generic RAM.  When it's exhausted, "
+"graphics performance can suffer. This value can be incorrect because of race "
+"conditions.")
+  {
+#ifdef DEBUG
+    // There must be only one instance of this class, due to |sAmount|
+    // being static.  Assert this.
+    static bool hasRun = false;
+    MOZ_ASSERT(!hasRun);
+    hasRun = true;
+#endif
+  }
+
+private:
+  int64_t Amount() MOZ_OVERRIDE { return sAmount; }
+
+  static int64_t sAmount;
+};
+
+int64_t GrallocReporter::sAmount = 0;
 
 GrallocBufferActor::GrallocBufferActor()
 : mAllocBytes(0)
 , mDeprecatedTextureHost(nullptr)
 {
   static bool registered;
   if (!registered) {
     // We want to be sure that the first call here will always run on
     // the main thread.
     NS_ASSERTION(NS_IsMainThread(), "Should be on main thread.");
 
-    NS_RegisterMemoryReporter(new NS_MEMORY_REPORTER_NAME(GrallocBufferActor));
+    NS_RegisterMemoryReporter(new GrallocReporter());
     registered = true;
   }
 }
 
 GrallocBufferActor::~GrallocBufferActor()
 {
   if (mAllocBytes > 0) {
-    sCurrentAlloc -= mAllocBytes;
+    GrallocReporter::sAmount -= mAllocBytes;
   }
 }
 
 /*static*/ PGrallocBufferParent*
 GrallocBufferActor::Create(const gfxIntSize& aSize,
                            const uint32_t& aFormat,
                            const uint32_t& aUsage,
                            MaybeMagicGrallocBufferHandle* aOutHandle)
@@ -237,17 +253,17 @@ GrallocBufferActor::Create(const gfxIntS
   }
 
   sp<GraphicBuffer> buffer(new GraphicBuffer(aSize.width, aSize.height, format, usage));
   if (buffer->initCheck() != OK)
     return actor;
 
   size_t bpp = BytesPerPixelForPixelFormat(format);
   actor->mAllocBytes = aSize.width * aSize.height * bpp;
-  sCurrentAlloc += actor->mAllocBytes;
+  GrallocReporter::sAmount += actor->mAllocBytes;
 
   actor->mGraphicBuffer = buffer;
   *aOutHandle = MagicGrallocBufferHandle(buffer);
 
   return actor;
 }
 
 // used only for hacky fix in gecko 23 for bug 862324
--- a/gfx/layers/opengl/TextureHostOGL.cpp
+++ b/gfx/layers/opengl/TextureHostOGL.cpp
@@ -843,18 +843,19 @@ TiledDeprecatedTextureHostOGL::SetCompos
 
 void
 TiledDeprecatedTextureHostOGL::DeleteTextures()
 {
   if (mTextureHandle) {
     mGL->MakeCurrent();
     mGL->fDeleteTextures(1, &mTextureHandle);
 
-    gl::GLContext::UpdateTextureMemoryUsage(gl::GLContext::MemoryFreed, mGLFormat,
-                                            GetTileType(), TILEDLAYERBUFFER_TILE_SIZE);
+    gl::GfxTexturesReporter::UpdateAmount(gl::GfxTexturesReporter::MemoryFreed,
+                                          mGLFormat, GetTileType(),
+                                          TILEDLAYERBUFFER_TILE_SIZE);
     mTextureHandle = 0;
   }
 }
 
 void
 TiledDeprecatedTextureHostOGL::Update(gfxReusableSurfaceWrapper* aReusableSurface, TextureFlags aFlags, const gfx::IntSize& aSize)
 {
   mSize = aSize;
@@ -866,30 +867,32 @@ TiledDeprecatedTextureHostOGL::Update(gf
     mGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_MIN_FILTER, LOCAL_GL_LINEAR);
     mGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_MAG_FILTER, LOCAL_GL_LINEAR);
     mGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_S, LOCAL_GL_CLAMP_TO_EDGE);
     mGL->fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_T, LOCAL_GL_CLAMP_TO_EDGE);
   } else {
     mGL->fBindTexture(LOCAL_GL_TEXTURE_2D, 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.
-    gl::GLContext::UpdateTextureMemoryUsage(gl::GLContext::MemoryFreed, mGLFormat,
-                                            GetTileType(), TILEDLAYERBUFFER_TILE_SIZE);
+    gl::GfxTexturesReporter::UpdateAmount(gl::GfxTexturesReporter::MemoryFreed,
+                                          mGLFormat, GetTileType(),
+                                          TILEDLAYERBUFFER_TILE_SIZE);
   }
 
   GLenum type;
   GetFormatAndTileForImageFormat(aReusableSurface->Format(), mGLFormat, type);
 
   const unsigned char* buf = aReusableSurface->GetReadOnlyData();
   mGL->fTexImage2D(LOCAL_GL_TEXTURE_2D, 0, mGLFormat,
                    TILEDLAYERBUFFER_TILE_SIZE, TILEDLAYERBUFFER_TILE_SIZE, 0,
                    mGLFormat, type, buf);
 
-  gl::GLContext::UpdateTextureMemoryUsage(gl::GLContext::MemoryAllocated, mGLFormat,
-                                          type, TILEDLAYERBUFFER_TILE_SIZE);
+  gl::GfxTexturesReporter::UpdateAmount(gl::GfxTexturesReporter::MemoryAllocated,
+                                        mGLFormat, type,
+                                        TILEDLAYERBUFFER_TILE_SIZE);
 
   if (mGLFormat == LOCAL_GL_RGB) {
     mFormat = FORMAT_R8G8B8X8;
   } else {
     mFormat = FORMAT_B8G8R8A8;
   }
 }