Bug 1288649 - Use mfbt IsPowerOfTwo funcs in gfx/. - r=jrmuizel draft
authorJeff Gilbert <jgilbert@mozilla.com>
Fri, 22 Jul 2016 00:51:18 -0700
changeset 391203 4c241d379525934dbb5d2bd6fd43eb7a52062e33
parent 391202 8c8bd01631d6f02c6ba571626a552a5a3505bb08
child 391204 e73b2b78cad260cc5564019843eab752b1a4756d
push id23840
push userbmo:jgilbert@mozilla.com
push dateFri, 22 Jul 2016 07:54:28 +0000
reviewersjrmuizel
bugs1288649
milestone50.0a1
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)