Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include <algorithm> |
michael@0 | 7 | |
michael@0 | 8 | #include "gfxDrawable.h" |
michael@0 | 9 | #include "gfxPlatform.h" |
michael@0 | 10 | #include "gfxUtils.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "OrientedImage.h" |
michael@0 | 13 | |
michael@0 | 14 | using namespace mozilla::gfx; |
michael@0 | 15 | |
michael@0 | 16 | using std::swap; |
michael@0 | 17 | using mozilla::layers::LayerManager; |
michael@0 | 18 | using mozilla::layers::ImageContainer; |
michael@0 | 19 | |
michael@0 | 20 | namespace mozilla { |
michael@0 | 21 | namespace image { |
michael@0 | 22 | |
michael@0 | 23 | NS_IMPL_ISUPPORTS(OrientedImage, imgIContainer) |
michael@0 | 24 | |
michael@0 | 25 | nsIntRect |
michael@0 | 26 | OrientedImage::FrameRect(uint32_t aWhichFrame) |
michael@0 | 27 | { |
michael@0 | 28 | if (mOrientation.SwapsWidthAndHeight()) { |
michael@0 | 29 | nsIntRect innerRect = InnerImage()->FrameRect(aWhichFrame); |
michael@0 | 30 | return nsIntRect(innerRect.x, innerRect.y, innerRect.height, innerRect.width); |
michael@0 | 31 | } else { |
michael@0 | 32 | return InnerImage()->FrameRect(aWhichFrame); |
michael@0 | 33 | } |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | NS_IMETHODIMP |
michael@0 | 37 | OrientedImage::GetWidth(int32_t* aWidth) |
michael@0 | 38 | { |
michael@0 | 39 | if (mOrientation.SwapsWidthAndHeight()) { |
michael@0 | 40 | return InnerImage()->GetHeight(aWidth); |
michael@0 | 41 | } else { |
michael@0 | 42 | return InnerImage()->GetWidth(aWidth); |
michael@0 | 43 | } |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | NS_IMETHODIMP |
michael@0 | 47 | OrientedImage::GetHeight(int32_t* aHeight) |
michael@0 | 48 | { |
michael@0 | 49 | if (mOrientation.SwapsWidthAndHeight()) { |
michael@0 | 50 | return InnerImage()->GetWidth(aHeight); |
michael@0 | 51 | } else { |
michael@0 | 52 | return InnerImage()->GetHeight(aHeight); |
michael@0 | 53 | } |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | NS_IMETHODIMP |
michael@0 | 57 | OrientedImage::GetIntrinsicSize(nsSize* aSize) |
michael@0 | 58 | { |
michael@0 | 59 | nsresult rv = InnerImage()->GetIntrinsicSize(aSize); |
michael@0 | 60 | |
michael@0 | 61 | if (mOrientation.SwapsWidthAndHeight()) { |
michael@0 | 62 | swap(aSize->width, aSize->height); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | return rv; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | NS_IMETHODIMP |
michael@0 | 69 | OrientedImage::GetIntrinsicRatio(nsSize* aRatio) |
michael@0 | 70 | { |
michael@0 | 71 | nsresult rv = InnerImage()->GetIntrinsicRatio(aRatio); |
michael@0 | 72 | |
michael@0 | 73 | if (mOrientation.SwapsWidthAndHeight()) { |
michael@0 | 74 | swap(aRatio->width, aRatio->height); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | return rv; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | NS_IMETHODIMP_(TemporaryRef<SourceSurface>) |
michael@0 | 81 | OrientedImage::GetFrame(uint32_t aWhichFrame, |
michael@0 | 82 | uint32_t aFlags) |
michael@0 | 83 | { |
michael@0 | 84 | nsresult rv; |
michael@0 | 85 | |
michael@0 | 86 | if (mOrientation.IsIdentity()) { |
michael@0 | 87 | return InnerImage()->GetFrame(aWhichFrame, aFlags); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | // Get the underlying dimensions. |
michael@0 | 91 | int32_t width, height; |
michael@0 | 92 | rv = InnerImage()->GetWidth(&width); |
michael@0 | 93 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 94 | rv = InnerImage()->GetHeight(&height); |
michael@0 | 95 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 96 | if (mOrientation.SwapsWidthAndHeight()) { |
michael@0 | 97 | swap(width, height); |
michael@0 | 98 | } |
michael@0 | 99 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 100 | |
michael@0 | 101 | // Determine an appropriate format for the surface. |
michael@0 | 102 | gfx::SurfaceFormat surfaceFormat; |
michael@0 | 103 | gfxImageFormat imageFormat; |
michael@0 | 104 | if (InnerImage()->FrameIsOpaque(aWhichFrame)) { |
michael@0 | 105 | surfaceFormat = gfx::SurfaceFormat::B8G8R8X8; |
michael@0 | 106 | imageFormat = gfxImageFormat::ARGB32; |
michael@0 | 107 | } else { |
michael@0 | 108 | surfaceFormat = gfx::SurfaceFormat::B8G8R8A8; |
michael@0 | 109 | imageFormat = gfxImageFormat::ARGB32; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | // Create a surface to draw into. |
michael@0 | 113 | mozilla::RefPtr<DrawTarget> target = |
michael@0 | 114 | gfxPlatform::GetPlatform()-> |
michael@0 | 115 | CreateOffscreenContentDrawTarget(IntSize(width, height), surfaceFormat); |
michael@0 | 116 | |
michael@0 | 117 | // Create our drawable. |
michael@0 | 118 | RefPtr<SourceSurface> innerSurface = |
michael@0 | 119 | InnerImage()->GetFrame(aWhichFrame, aFlags); |
michael@0 | 120 | NS_ENSURE_TRUE(innerSurface, nullptr); |
michael@0 | 121 | nsRefPtr<gfxDrawable> drawable = |
michael@0 | 122 | new gfxSurfaceDrawable(innerSurface, gfxIntSize(width, height)); |
michael@0 | 123 | |
michael@0 | 124 | // Draw. |
michael@0 | 125 | nsRefPtr<gfxContext> ctx = new gfxContext(target); |
michael@0 | 126 | gfxRect imageRect(0, 0, width, height); |
michael@0 | 127 | gfxUtils::DrawPixelSnapped(ctx, drawable, OrientationMatrix(nsIntSize(width, height)), |
michael@0 | 128 | imageRect, imageRect, imageRect, imageRect, |
michael@0 | 129 | imageFormat, GraphicsFilter::FILTER_FAST); |
michael@0 | 130 | |
michael@0 | 131 | return target->Snapshot(); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | NS_IMETHODIMP |
michael@0 | 135 | OrientedImage::GetImageContainer(LayerManager* aManager, ImageContainer** _retval) |
michael@0 | 136 | { |
michael@0 | 137 | // XXX(seth): We currently don't have a way of orienting the result of |
michael@0 | 138 | // GetImageContainer. We work around this by always returning null, but if it |
michael@0 | 139 | // ever turns out that OrientedImage is widely used on codepaths that can |
michael@0 | 140 | // actually benefit from GetImageContainer, it would be a good idea to fix |
michael@0 | 141 | // that method for performance reasons. |
michael@0 | 142 | |
michael@0 | 143 | if (mOrientation.IsIdentity()) { |
michael@0 | 144 | return InnerImage()->GetImageContainer(aManager, _retval); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | *_retval = nullptr; |
michael@0 | 148 | return NS_OK; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | gfxMatrix |
michael@0 | 152 | OrientedImage::OrientationMatrix(const nsIntSize& aViewportSize) |
michael@0 | 153 | { |
michael@0 | 154 | gfxMatrix matrix; |
michael@0 | 155 | |
michael@0 | 156 | int32_t width, height; |
michael@0 | 157 | if (InnerImage()->GetType() == imgIContainer::TYPE_VECTOR) { |
michael@0 | 158 | width = mOrientation.SwapsWidthAndHeight() ? aViewportSize.height : aViewportSize.width; |
michael@0 | 159 | height = mOrientation.SwapsWidthAndHeight() ? aViewportSize.width : aViewportSize.height; |
michael@0 | 160 | } else { |
michael@0 | 161 | nsresult rv = InnerImage()->GetWidth(&width); |
michael@0 | 162 | rv = NS_FAILED(rv) ? rv : InnerImage()->GetHeight(&height); |
michael@0 | 163 | if (NS_FAILED(rv)) { |
michael@0 | 164 | // Fall back to identity if the width and height aren't available. |
michael@0 | 165 | return matrix; |
michael@0 | 166 | } |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | // Apply reflection, if present. (This logically happens second, but we |
michael@0 | 170 | // apply it first because these transformations are all premultiplied.) A |
michael@0 | 171 | // translation is necessary to place the image back in the first quadrant. |
michael@0 | 172 | switch (mOrientation.flip) { |
michael@0 | 173 | case Flip::Unflipped: |
michael@0 | 174 | break; |
michael@0 | 175 | case Flip::Horizontal: |
michael@0 | 176 | if (mOrientation.SwapsWidthAndHeight()) { |
michael@0 | 177 | // In the coordinate system after the rotation the reflection we want |
michael@0 | 178 | // is across the x-axis rather than the y-axis. |
michael@0 | 179 | matrix.Translate(gfxPoint(0, height)); |
michael@0 | 180 | matrix.Scale(1.0, -1.0); |
michael@0 | 181 | } else { |
michael@0 | 182 | matrix.Translate(gfxPoint(width, 0)); |
michael@0 | 183 | matrix.Scale(-1.0, 1.0); |
michael@0 | 184 | } |
michael@0 | 185 | break; |
michael@0 | 186 | default: |
michael@0 | 187 | MOZ_ASSERT(false, "Invalid flip value"); |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | // Apply rotation, if present. Again, a translation is used to place the |
michael@0 | 191 | // image back in the first quadrant. |
michael@0 | 192 | switch (mOrientation.rotation) { |
michael@0 | 193 | case Angle::D0: |
michael@0 | 194 | break; |
michael@0 | 195 | case Angle::D90: |
michael@0 | 196 | matrix.Translate(gfxPoint(0, height)); |
michael@0 | 197 | matrix.Rotate(-0.5 * M_PI); |
michael@0 | 198 | break; |
michael@0 | 199 | case Angle::D180: |
michael@0 | 200 | matrix.Translate(gfxPoint(width, height)); |
michael@0 | 201 | matrix.Rotate(-1.0 * M_PI); |
michael@0 | 202 | break; |
michael@0 | 203 | case Angle::D270: |
michael@0 | 204 | matrix.Translate(gfxPoint(width, 0)); |
michael@0 | 205 | matrix.Rotate(-1.5 * M_PI); |
michael@0 | 206 | break; |
michael@0 | 207 | default: |
michael@0 | 208 | MOZ_ASSERT(false, "Invalid rotation value"); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | return matrix; |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | NS_IMETHODIMP |
michael@0 | 215 | OrientedImage::Draw(gfxContext* aContext, |
michael@0 | 216 | GraphicsFilter aFilter, |
michael@0 | 217 | const gfxMatrix& aUserSpaceToImageSpace, |
michael@0 | 218 | const gfxRect& aFill, |
michael@0 | 219 | const nsIntRect& aSubimage, |
michael@0 | 220 | const nsIntSize& aViewportSize, |
michael@0 | 221 | const SVGImageContext* aSVGContext, |
michael@0 | 222 | uint32_t aWhichFrame, |
michael@0 | 223 | uint32_t aFlags) |
michael@0 | 224 | { |
michael@0 | 225 | if (mOrientation.IsIdentity()) { |
michael@0 | 226 | return InnerImage()->Draw(aContext, aFilter, aUserSpaceToImageSpace, |
michael@0 | 227 | aFill, aSubimage, aViewportSize, aSVGContext, |
michael@0 | 228 | aWhichFrame, aFlags); |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | // Update the matrix to reorient the image. |
michael@0 | 232 | gfxMatrix matrix(OrientationMatrix(aViewportSize)); |
michael@0 | 233 | gfxMatrix userSpaceToImageSpace(aUserSpaceToImageSpace * matrix); |
michael@0 | 234 | |
michael@0 | 235 | // Update the subimage. |
michael@0 | 236 | gfxRect gfxSubimage(matrix.TransformBounds(gfxRect(aSubimage.x, aSubimage.y, aSubimage.width, aSubimage.height))); |
michael@0 | 237 | nsIntRect subimage(gfxSubimage.x, gfxSubimage.y, gfxSubimage.width, gfxSubimage.height); |
michael@0 | 238 | |
michael@0 | 239 | // Update the viewport size. (This could be done using TransformBounds but |
michael@0 | 240 | // since it's only a size a swap is enough.) |
michael@0 | 241 | nsIntSize viewportSize(aViewportSize); |
michael@0 | 242 | if (mOrientation.SwapsWidthAndHeight()) { |
michael@0 | 243 | swap(viewportSize.width, viewportSize.height); |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | return InnerImage()->Draw(aContext, aFilter, userSpaceToImageSpace, |
michael@0 | 247 | aFill, subimage, viewportSize, aSVGContext, |
michael@0 | 248 | aWhichFrame, aFlags); |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | } // namespace image |
michael@0 | 252 | } // namespace mozilla |