Bug 1288649 - Use mfbt IsPowerOfTwo funcs in gfx/. - r=jrmuizel
authorJeff Gilbert <jgilbert@mozilla.com>
Fri, 22 Jul 2016 00:51:18 -0700
changeset 348686 cb620c9d91391333dfac149254997517f59e9e61
parent 348685 91f0e0f0e39823826522a9a0494b5cdb38663795
child 348687 eb943039fc6d935cfd9598f6eb8708048f53d250
push id1230
push userjlund@mozilla.com
push dateMon, 31 Oct 2016 18:13:35 +0000
treeherdermozilla-release@5e06e3766db2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjrmuizel
bugs1288649
milestone50.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 1288649 - Use mfbt IsPowerOfTwo funcs in gfx/. - r=jrmuizel While fixing non-unified-build errors in dom/canvas, I started hitting a static_assert that we were calling IsPowerOfTwo with a signed type. It turns out we have at least three copies of IsPowerOfTwo() in the tree. Let's drop the non-mfbt ones. MozReview-Commit-ID: 1fwQw0CrgiE
gfx/gl/GLUploadHelpers.cpp
gfx/thebes/gfxUtils.h
--- a/gfx/gl/GLUploadHelpers.cpp
+++ b/gfx/gl/GLUploadHelpers.cpp
@@ -2,60 +2,28 @@
 /* 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 "GLUploadHelpers.h"
 
 #include "GLContext.h"
 #include "mozilla/gfx/2D.h"
+#include "gfxUtils.h"
 #include "mozilla/gfx/Tools.h"  // For BytesPerPixel
 #include "nsRegion.h"
 #include "GfxTexturesReporter.h"
 #include "mozilla/gfx/Logging.h"
 
 namespace mozilla {
 
 using namespace gfx;
 
 namespace gl {
 
-/* These two techniques are suggested by "Bit Twiddling Hacks"
- */
-
-/**
- * Returns true if |aNumber| is a power of two
- * 0 is incorreclty considered a power of two
- */
-static bool
-IsPowerOfTwo(int aNumber)
-{
-    return (aNumber & (aNumber - 1)) == 0;
-}
-
-/**
- * Returns the first integer greater than |aNumber| which is a power of two
- * Undefined for |aNumber| < 0
- */
-static int
-NextPowerOfTwo(int aNumber)
-{
-#if defined(__arm__)
-    return 1 << (32 - __builtin_clz(aNumber - 1));
-#else
-    --aNumber;
-    aNumber |= aNumber >> 1;
-    aNumber |= aNumber >> 2;
-    aNumber |= aNumber >> 4;
-    aNumber |= aNumber >> 8;
-    aNumber |= aNumber >> 16;
-    return ++aNumber;
-#endif
-}
-
 static unsigned int
 DataOffset(const IntPoint& aPoint, int32_t aStride, SurfaceFormat aFormat)
 {
   unsigned int data = aPoint.y * aStride;
   data += aPoint.x * BytesPerPixel(aFormat);
   return data;
 }
 
@@ -280,20 +248,21 @@ TexImage2DHelper(GLContext* gl,
                  GLint pixelsize, GLint border, GLenum format,
                  GLenum type, const GLvoid* pixels)
 {
     if (gl->IsGLES()) {
 
         NS_ASSERTION(format == (GLenum)internalformat,
                     "format and internalformat not the same for glTexImage2D on GLES2");
 
+        MOZ_ASSERT(width >= 0 && height >= 0);
         if (!CanUploadNonPowerOfTwo(gl)
             && (stride != width * pixelsize
-            || !IsPowerOfTwo(width)
-            || !IsPowerOfTwo(height))) {
+            || !IsPowerOfTwo((uint32_t)width)
+            || !IsPowerOfTwo((uint32_t)height))) {
 
             // Pad out texture width and height to the next power of two
             // as we don't support/want non power of two texture uploads
             GLsizei paddedWidth = NextPowerOfTwo(width);
             GLsizei paddedHeight = NextPowerOfTwo(height);
 
             GLvoid* paddedPixels = new unsigned char[paddedWidth * paddedHeight * pixelsize];
 
--- a/gfx/thebes/gfxUtils.h
+++ b/gfx/thebes/gfxUtils.h
@@ -287,47 +287,24 @@ namespace gfx {
  * color to a device color using the transform returened by gfxPlatform::
  * GetCMSRGBTransform().  If the CMS mode is some other value, the color is
  * returned unchanged (other than a type change to Moz2D Color, if
  * applicable).
  */
 Color ToDeviceColor(Color aColor);
 Color ToDeviceColor(nscolor aColor);
 
-/* These techniques are suggested by "Bit Twiddling Hacks"
- */
-
 /**
- * Returns true if |aNumber| is a power of two
- * 0 is incorreclty considered a power of two
+ * Returns the first integer greater than |aNumber| which is a power of two.
  */
-static inline bool
-IsPowerOfTwo(int aNumber)
-{
-    return (aNumber & (aNumber - 1)) == 0;
-}
-
-/**
- * Returns the first integer greater than or equal to |aNumber| which is a
- * power of two. Undefined for |aNumber| < 0.
- */
-static inline int
+static int
 NextPowerOfTwo(int aNumber)
 {
-#if defined(__arm__)
-    return 1 << (32 - __builtin_clz(aNumber - 1));
-#else
-    --aNumber;
-    aNumber |= aNumber >> 1;
-    aNumber |= aNumber >> 2;
-    aNumber |= aNumber >> 4;
-    aNumber |= aNumber >> 8;
-    aNumber |= aNumber >> 16;
-    return ++aNumber;
-#endif
+    MOZ_ASSERT(aNumber >= 0);
+    return RoundUpPow2((size_t)aNumber + 1);
 }
 
 /**
  * Performs a checked multiply of the given width, height, and bytes-per-pixel
  * values.
  */
 static inline CheckedInt<uint32_t>
 SafeBytesForBitmap(uint32_t aWidth, uint32_t aHeight, unsigned aBytesPerPixel)