OrientedImage.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "OrientedImage.h"
  6. #include <algorithm>
  7. #include "gfx2DGlue.h"
  8. #include "gfxDrawable.h"
  9. #include "gfxPlatform.h"
  10. #include "gfxUtils.h"
  11. #include "ImageRegion.h"
  12. #include "SVGImageContext.h"
  13. using std::swap;
  14. namespace mozilla {
  15. using namespace gfx;
  16. using layers::LayerManager;
  17. using layers::ImageContainer;
  18. namespace image {
  19. NS_IMPL_ISUPPORTS_INHERITED0(OrientedImage, ImageWrapper)
  20. NS_IMETHODIMP
  21. OrientedImage::GetWidth(int32_t* aWidth)
  22. {
  23. if (mOrientation.SwapsWidthAndHeight()) {
  24. return InnerImage()->GetHeight(aWidth);
  25. } else {
  26. return InnerImage()->GetWidth(aWidth);
  27. }
  28. }
  29. NS_IMETHODIMP
  30. OrientedImage::GetHeight(int32_t* aHeight)
  31. {
  32. if (mOrientation.SwapsWidthAndHeight()) {
  33. return InnerImage()->GetWidth(aHeight);
  34. } else {
  35. return InnerImage()->GetHeight(aHeight);
  36. }
  37. }
  38. NS_IMETHODIMP
  39. OrientedImage::GetIntrinsicSize(nsSize* aSize)
  40. {
  41. nsresult rv = InnerImage()->GetIntrinsicSize(aSize);
  42. if (mOrientation.SwapsWidthAndHeight()) {
  43. swap(aSize->width, aSize->height);
  44. }
  45. return rv;
  46. }
  47. NS_IMETHODIMP
  48. OrientedImage::GetIntrinsicRatio(AspectRatio* aRatio)
  49. {
  50. nsresult rv = InnerImage()->GetIntrinsicRatio(aRatio);
  51. if (mOrientation.SwapsWidthAndHeight()) {
  52. *aRatio = aRatio->Inverted();
  53. }
  54. return rv;
  55. }
  56. NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
  57. OrientedImage::GetFrame(uint32_t aWhichFrame,
  58. uint32_t aFlags)
  59. {
  60. nsresult rv;
  61. if (mOrientation.IsIdentity()) {
  62. return InnerImage()->GetFrame(aWhichFrame, aFlags);
  63. }
  64. // Get the underlying dimensions.
  65. IntSize size;
  66. rv = InnerImage()->GetWidth(&size.width);
  67. NS_ENSURE_SUCCESS(rv, nullptr);
  68. rv = InnerImage()->GetHeight(&size.height);
  69. NS_ENSURE_SUCCESS(rv, nullptr);
  70. // Determine an appropriate format for the surface.
  71. gfx::SurfaceFormat surfaceFormat;
  72. if (InnerImage()->WillDrawOpaqueNow()) {
  73. surfaceFormat = gfx::SurfaceFormat::B8G8R8X8;
  74. } else {
  75. surfaceFormat = gfx::SurfaceFormat::B8G8R8A8;
  76. }
  77. // Create a surface to draw into.
  78. RefPtr<DrawTarget> target =
  79. gfxPlatform::GetPlatform()->
  80. CreateOffscreenContentDrawTarget(size, surfaceFormat);
  81. if (!target || !target->IsValid()) {
  82. NS_ERROR("Could not create a DrawTarget");
  83. return nullptr;
  84. }
  85. // Create our drawable.
  86. RefPtr<SourceSurface> innerSurface =
  87. InnerImage()->GetFrame(aWhichFrame, aFlags);
  88. NS_ENSURE_TRUE(innerSurface, nullptr);
  89. RefPtr<gfxDrawable> drawable =
  90. new gfxSurfaceDrawable(innerSurface, size);
  91. // Draw.
  92. RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(target);
  93. MOZ_ASSERT(ctx); // already checked the draw target above
  94. ctx->Multiply(OrientationMatrix(size));
  95. gfxUtils::DrawPixelSnapped(ctx, drawable, size, ImageRegion::Create(size),
  96. surfaceFormat, SamplingFilter::LINEAR);
  97. return target->Snapshot();
  98. }
  99. NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
  100. OrientedImage::GetFrameAtSize(const IntSize& aSize,
  101. uint32_t aWhichFrame,
  102. uint32_t aFlags)
  103. {
  104. // XXX(seth): It'd be nice to support downscale-during-decode for this case,
  105. // but right now we just fall back to the intrinsic size.
  106. return GetFrame(aWhichFrame, aFlags);
  107. }
  108. NS_IMETHODIMP_(bool)
  109. OrientedImage::IsImageContainerAvailable(LayerManager* aManager, uint32_t aFlags)
  110. {
  111. if (mOrientation.IsIdentity()) {
  112. return InnerImage()->IsImageContainerAvailable(aManager, aFlags);
  113. }
  114. return false;
  115. }
  116. NS_IMETHODIMP_(already_AddRefed<ImageContainer>)
  117. OrientedImage::GetImageContainer(LayerManager* aManager, uint32_t aFlags)
  118. {
  119. // XXX(seth): We currently don't have a way of orienting the result of
  120. // GetImageContainer. We work around this by always returning null, but if it
  121. // ever turns out that OrientedImage is widely used on codepaths that can
  122. // actually benefit from GetImageContainer, it would be a good idea to fix
  123. // that method for performance reasons.
  124. if (mOrientation.IsIdentity()) {
  125. return InnerImage()->GetImageContainer(aManager, aFlags);
  126. }
  127. return nullptr;
  128. }
  129. struct MatrixBuilder
  130. {
  131. explicit MatrixBuilder(bool aInvert) : mInvert(aInvert) { }
  132. gfxMatrix Build() { return mMatrix; }
  133. void Scale(gfxFloat aX, gfxFloat aY)
  134. {
  135. if (mInvert) {
  136. mMatrix *= gfxMatrix::Scaling(1.0 / aX, 1.0 / aY);
  137. } else {
  138. mMatrix.Scale(aX, aY);
  139. }
  140. }
  141. void Rotate(gfxFloat aPhi)
  142. {
  143. if (mInvert) {
  144. mMatrix *= gfxMatrix::Rotation(-aPhi);
  145. } else {
  146. mMatrix.Rotate(aPhi);
  147. }
  148. }
  149. void Translate(gfxPoint aDelta)
  150. {
  151. if (mInvert) {
  152. mMatrix *= gfxMatrix::Translation(-aDelta);
  153. } else {
  154. mMatrix.Translate(aDelta);
  155. }
  156. }
  157. private:
  158. gfxMatrix mMatrix;
  159. bool mInvert;
  160. };
  161. /*
  162. * OrientationMatrix() computes a matrix that applies the rotation and
  163. * reflection specified by mOrientation, or that matrix's inverse if aInvert is
  164. * true.
  165. *
  166. * @param aSize The scaled size of the inner image. (When outside code specifies
  167. * the scaled size, as with imgIContainer::Draw and its aSize
  168. * parameter, it's necessary to swap the width and height if
  169. * mOrientation.SwapsWidthAndHeight() is true.)
  170. * @param aInvert If true, compute the inverse of the orientation matrix. Prefer
  171. * this approach to OrientationMatrix(..).Invert(), because it's
  172. * more numerically accurate.
  173. */
  174. gfxMatrix
  175. OrientedImage::OrientationMatrix(const nsIntSize& aSize,
  176. bool aInvert /* = false */)
  177. {
  178. MatrixBuilder builder(aInvert);
  179. // Apply reflection, if present. (This logically happens second, but we
  180. // apply it first because these transformations are all premultiplied.) A
  181. // translation is necessary to place the image back in the first quadrant.
  182. switch (mOrientation.flip) {
  183. case Flip::Unflipped:
  184. break;
  185. case Flip::Horizontal:
  186. if (mOrientation.SwapsWidthAndHeight()) {
  187. builder.Translate(gfxPoint(aSize.height, 0));
  188. } else {
  189. builder.Translate(gfxPoint(aSize.width, 0));
  190. }
  191. builder.Scale(-1.0, 1.0);
  192. break;
  193. default:
  194. MOZ_ASSERT(false, "Invalid flip value");
  195. }
  196. // Apply rotation, if present. Again, a translation is used to place the
  197. // image back in the first quadrant.
  198. switch (mOrientation.rotation) {
  199. case Angle::D0:
  200. break;
  201. case Angle::D90:
  202. builder.Translate(gfxPoint(aSize.height, 0));
  203. builder.Rotate(-1.5 * M_PI);
  204. break;
  205. case Angle::D180:
  206. builder.Translate(gfxPoint(aSize.width, aSize.height));
  207. builder.Rotate(-1.0 * M_PI);
  208. break;
  209. case Angle::D270:
  210. builder.Translate(gfxPoint(0, aSize.width));
  211. builder.Rotate(-0.5 * M_PI);
  212. break;
  213. default:
  214. MOZ_ASSERT(false, "Invalid rotation value");
  215. }
  216. return builder.Build();
  217. }
  218. NS_IMETHODIMP_(DrawResult)
  219. OrientedImage::Draw(gfxContext* aContext,
  220. const nsIntSize& aSize,
  221. const ImageRegion& aRegion,
  222. uint32_t aWhichFrame,
  223. SamplingFilter aSamplingFilter,
  224. const Maybe<SVGImageContext>& aSVGContext,
  225. uint32_t aFlags)
  226. {
  227. if (mOrientation.IsIdentity()) {
  228. return InnerImage()->Draw(aContext, aSize, aRegion,
  229. aWhichFrame, aSamplingFilter,
  230. aSVGContext, aFlags);
  231. }
  232. // Update the image size to match the image's coordinate system. (This could
  233. // be done using TransformBounds but since it's only a size a swap is enough.)
  234. nsIntSize size(aSize);
  235. if (mOrientation.SwapsWidthAndHeight()) {
  236. swap(size.width, size.height);
  237. }
  238. // Update the matrix so that we transform the image into the orientation
  239. // expected by the caller before drawing.
  240. gfxMatrix matrix(OrientationMatrix(size));
  241. gfxContextMatrixAutoSaveRestore saveMatrix(aContext);
  242. aContext->Multiply(matrix);
  243. // The region is already in the orientation expected by the caller, but we
  244. // need it to be in the image's coordinate system, so we transform it using
  245. // the inverse of the orientation matrix.
  246. gfxMatrix inverseMatrix(OrientationMatrix(size, /* aInvert = */ true));
  247. ImageRegion region(aRegion);
  248. region.TransformBoundsBy(inverseMatrix);
  249. auto orientViewport = [&](const SVGImageContext& aOldContext) {
  250. CSSIntSize viewportSize(aOldContext.GetViewportSize());
  251. if (mOrientation.SwapsWidthAndHeight()) {
  252. swap(viewportSize.width, viewportSize.height);
  253. }
  254. return SVGImageContext(viewportSize,
  255. aOldContext.GetPreserveAspectRatio());
  256. };
  257. return InnerImage()->Draw(aContext, size, region, aWhichFrame, aSamplingFilter,
  258. aSVGContext.map(orientViewport), aFlags);
  259. }
  260. nsIntSize
  261. OrientedImage::OptimalImageSizeForDest(const gfxSize& aDest,
  262. uint32_t aWhichFrame,
  263. SamplingFilter aSamplingFilter,
  264. uint32_t aFlags)
  265. {
  266. if (!mOrientation.SwapsWidthAndHeight()) {
  267. return InnerImage()->OptimalImageSizeForDest(aDest, aWhichFrame,
  268. aSamplingFilter, aFlags);
  269. }
  270. // Swap the size for the calculation, then swap it back for the caller.
  271. gfxSize destSize(aDest.height, aDest.width);
  272. nsIntSize innerImageSize(InnerImage()->OptimalImageSizeForDest(destSize,
  273. aWhichFrame,
  274. aSamplingFilter,
  275. aFlags));
  276. return nsIntSize(innerImageSize.height, innerImageSize.width);
  277. }
  278. NS_IMETHODIMP_(nsIntRect)
  279. OrientedImage::GetImageSpaceInvalidationRect(const nsIntRect& aRect)
  280. {
  281. nsIntRect rect(InnerImage()->GetImageSpaceInvalidationRect(aRect));
  282. if (mOrientation.IsIdentity()) {
  283. return rect;
  284. }
  285. nsIntSize innerSize;
  286. nsresult rv = InnerImage()->GetWidth(&innerSize.width);
  287. rv = NS_FAILED(rv) ? rv : InnerImage()->GetHeight(&innerSize.height);
  288. if (NS_FAILED(rv)) {
  289. // Fall back to identity if the width and height aren't available.
  290. return rect;
  291. }
  292. // Transform the invalidation rect into the correct orientation.
  293. gfxMatrix matrix(OrientationMatrix(innerSize));
  294. gfxRect invalidRect(matrix.TransformBounds(gfxRect(rect.x, rect.y,
  295. rect.width, rect.height)));
  296. return IntRect::RoundOut(invalidRect.x, invalidRect.y,
  297. invalidRect.width, invalidRect.height);
  298. }
  299. } // namespace image
  300. } // namespace mozilla