gecko/gfx/thebes/gfx2DGlue.h
Nicholas Nethercote 000b911cc5 Bug 1223690 - Remove implicit Rect conversions. r=jrmuizel.
gfxRect can be implicitly constructed from IntRect, which hides a number of
implicit conversion points, makes Moz2Dification harder, and has some
surprising effects.

This patch removes the implicit constructor and replaces it with an explicit
conversion function:

  gfxRect ThebesRect(const IntRect&)

This is the obvious outcome of removing the constructor.

But there is also a second, less obvious outcome: currently we do a number of
IntRect-to-Rect conversions using ToRect(), which (surprisingly) works because
it turns into an implicit IntRect-to-gfxRect conversion (via the implicit
constructor) combined with an explicit gfxRect-to-Rect conversion (via
ToRect()). I.e. we do two conversions, going from a Moz2D type to a Thebes
type and back to a Moz2D type!

So this patch also changes these conversion. It moves this existing function:

  Rect ToRect(const IntRect&)

from gfx2DGlue.h -- where it doesn't really belong because it doesn't involve
any Thebes types -- to gfx/2d/Rect.h, templatifying and renaming it as
IntRectToRect() in the process.

The rest of the patch deals with fall-out from these changes. The call sites
change as follows:

- IntRect-to-gfxRect conversions:
  - old: implicit
  - new: ThebesRect()

- IntRect-to-Rect conversions:
  - old: ToRect()
  - new: IntRectToRect()
2015-11-11 14:23:14 -08:00

132 lines
3.2 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 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 GFX_2D_GLUE_H
#define GFX_2D_GLUE_H
#include "gfxPlatform.h"
#include "gfxRect.h"
#include "gfxMatrix.h"
#include "gfxContext.h"
#include "mozilla/gfx/Matrix.h"
#include "mozilla/gfx/Rect.h"
#include "mozilla/gfx/2D.h"
namespace mozilla {
namespace gfx {
inline Rect ToRect(const gfxRect &aRect)
{
return Rect(Float(aRect.x), Float(aRect.y),
Float(aRect.width), Float(aRect.height));
}
inline RectDouble ToRectDouble(const gfxRect &aRect)
{
return RectDouble(aRect.x, aRect.y, aRect.width, aRect.height);
}
inline Matrix ToMatrix(const gfxMatrix &aMatrix)
{
return Matrix(Float(aMatrix._11), Float(aMatrix._12), Float(aMatrix._21),
Float(aMatrix._22), Float(aMatrix._31), Float(aMatrix._32));
}
inline gfxMatrix ThebesMatrix(const Matrix &aMatrix)
{
return gfxMatrix(aMatrix._11, aMatrix._12, aMatrix._21,
aMatrix._22, aMatrix._31, aMatrix._32);
}
inline Point ToPoint(const gfxPoint &aPoint)
{
return Point(Float(aPoint.x), Float(aPoint.y));
}
inline Size ToSize(const gfxSize &aSize)
{
return Size(Float(aSize.width), Float(aSize.height));
}
inline gfxPoint ThebesPoint(const Point &aPoint)
{
return gfxPoint(aPoint.x, aPoint.y);
}
inline gfxSize ThebesSize(const Size &aSize)
{
return gfxSize(aSize.width, aSize.height);
}
inline gfxRect ThebesRect(const Rect &aRect)
{
return gfxRect(aRect.x, aRect.y, aRect.width, aRect.height);
}
inline gfxRect ThebesRect(const IntRect &aRect)
{
return gfxRect(aRect.x, aRect.y, aRect.width, aRect.height);
}
inline gfxRect ThebesRect(const RectDouble &aRect)
{
return gfxRect(aRect.x, aRect.y, aRect.width, aRect.height);
}
inline gfxImageFormat SurfaceFormatToImageFormat(SurfaceFormat aFormat)
{
switch (aFormat) {
case SurfaceFormat::B8G8R8A8:
return gfxImageFormat::ARGB32;
case SurfaceFormat::B8G8R8X8:
return gfxImageFormat::RGB24;
case SurfaceFormat::R5G6B5_UINT16:
return gfxImageFormat::RGB16_565;
case SurfaceFormat::A8:
return gfxImageFormat::A8;
default:
return gfxImageFormat::Unknown;
}
}
inline SurfaceFormat ImageFormatToSurfaceFormat(gfxImageFormat aFormat)
{
switch (aFormat) {
case gfxImageFormat::ARGB32:
return SurfaceFormat::B8G8R8A8;
case gfxImageFormat::RGB24:
return SurfaceFormat::B8G8R8X8;
case gfxImageFormat::RGB16_565:
return SurfaceFormat::R5G6B5_UINT16;
case gfxImageFormat::A8:
return SurfaceFormat::A8;
default:
case gfxImageFormat::Unknown:
return SurfaceFormat::B8G8R8A8;
}
}
inline gfxContentType ContentForFormat(const SurfaceFormat &aFormat)
{
switch (aFormat) {
case SurfaceFormat::R5G6B5_UINT16:
case SurfaceFormat::B8G8R8X8:
case SurfaceFormat::R8G8B8X8:
return gfxContentType::COLOR;
case SurfaceFormat::A8:
return gfxContentType::ALPHA;
case SurfaceFormat::B8G8R8A8:
case SurfaceFormat::R8G8B8A8:
default:
return gfxContentType::COLOR_ALPHA;
}
}
} // namespace gfx
} // namespace mozilla
#endif