Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; 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 "BasicCompositor.h" |
michael@0 | 7 | #include "BasicLayersImpl.h" // for FillRectWithMask |
michael@0 | 8 | #include "TextureHostBasic.h" |
michael@0 | 9 | #include "mozilla/layers/Effects.h" |
michael@0 | 10 | #include "mozilla/layers/YCbCrImageDataSerializer.h" |
michael@0 | 11 | #include "nsIWidget.h" |
michael@0 | 12 | #include "gfx2DGlue.h" |
michael@0 | 13 | #include "mozilla/gfx/2D.h" |
michael@0 | 14 | #include "mozilla/gfx/Helpers.h" |
michael@0 | 15 | #include "gfxUtils.h" |
michael@0 | 16 | #include "YCbCrUtils.h" |
michael@0 | 17 | #include <algorithm> |
michael@0 | 18 | #include "ImageContainer.h" |
michael@0 | 19 | #include "gfxPrefs.h" |
michael@0 | 20 | #define PIXMAN_DONT_DEFINE_STDINT |
michael@0 | 21 | #include "pixman.h" // for pixman_f_transform, etc |
michael@0 | 22 | |
michael@0 | 23 | namespace mozilla { |
michael@0 | 24 | using namespace mozilla::gfx; |
michael@0 | 25 | |
michael@0 | 26 | namespace layers { |
michael@0 | 27 | |
michael@0 | 28 | class DataTextureSourceBasic : public DataTextureSource |
michael@0 | 29 | , public TextureSourceBasic |
michael@0 | 30 | { |
michael@0 | 31 | public: |
michael@0 | 32 | |
michael@0 | 33 | virtual TextureSourceBasic* AsSourceBasic() MOZ_OVERRIDE { return this; } |
michael@0 | 34 | |
michael@0 | 35 | virtual gfx::SourceSurface* GetSurface(DrawTarget* aTarget) MOZ_OVERRIDE { return mSurface; } |
michael@0 | 36 | |
michael@0 | 37 | SurfaceFormat GetFormat() const MOZ_OVERRIDE |
michael@0 | 38 | { |
michael@0 | 39 | return mSurface->GetFormat(); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | virtual IntSize GetSize() const MOZ_OVERRIDE |
michael@0 | 43 | { |
michael@0 | 44 | return mSurface->GetSize(); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | virtual bool Update(gfx::DataSourceSurface* aSurface, |
michael@0 | 48 | nsIntRegion* aDestRegion = nullptr, |
michael@0 | 49 | gfx::IntPoint* aSrcOffset = nullptr) MOZ_OVERRIDE |
michael@0 | 50 | { |
michael@0 | 51 | // XXX - For this to work with IncrementalContentHost we will need to support |
michael@0 | 52 | // the aDestRegion and aSrcOffset parameters properly; |
michael@0 | 53 | mSurface = aSurface; |
michael@0 | 54 | return true; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | virtual void DeallocateDeviceData() MOZ_OVERRIDE |
michael@0 | 58 | { |
michael@0 | 59 | mSurface = nullptr; |
michael@0 | 60 | SetUpdateSerial(0); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | public: |
michael@0 | 64 | RefPtr<gfx::DataSourceSurface> mSurface; |
michael@0 | 65 | }; |
michael@0 | 66 | |
michael@0 | 67 | BasicCompositor::BasicCompositor(nsIWidget *aWidget) |
michael@0 | 68 | : mWidget(aWidget) |
michael@0 | 69 | { |
michael@0 | 70 | MOZ_COUNT_CTOR(BasicCompositor); |
michael@0 | 71 | SetBackend(LayersBackend::LAYERS_BASIC); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | BasicCompositor::~BasicCompositor() |
michael@0 | 75 | { |
michael@0 | 76 | MOZ_COUNT_DTOR(BasicCompositor); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | void BasicCompositor::Destroy() |
michael@0 | 80 | { |
michael@0 | 81 | mWidget->CleanupRemoteDrawing(); |
michael@0 | 82 | mWidget = nullptr; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | TemporaryRef<CompositingRenderTarget> |
michael@0 | 86 | BasicCompositor::CreateRenderTarget(const IntRect& aRect, SurfaceInitMode aInit) |
michael@0 | 87 | { |
michael@0 | 88 | RefPtr<DrawTarget> target = mDrawTarget->CreateSimilarDrawTarget(aRect.Size(), SurfaceFormat::B8G8R8A8); |
michael@0 | 89 | |
michael@0 | 90 | RefPtr<BasicCompositingRenderTarget> rt = new BasicCompositingRenderTarget(target, aRect); |
michael@0 | 91 | |
michael@0 | 92 | return rt.forget(); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | TemporaryRef<CompositingRenderTarget> |
michael@0 | 96 | BasicCompositor::CreateRenderTargetFromSource(const IntRect &aRect, |
michael@0 | 97 | const CompositingRenderTarget *aSource, |
michael@0 | 98 | const IntPoint &aSourcePoint) |
michael@0 | 99 | { |
michael@0 | 100 | RefPtr<DrawTarget> target = mDrawTarget->CreateSimilarDrawTarget(aRect.Size(), SurfaceFormat::B8G8R8A8); |
michael@0 | 101 | RefPtr<BasicCompositingRenderTarget> rt = new BasicCompositingRenderTarget(target, aRect); |
michael@0 | 102 | |
michael@0 | 103 | DrawTarget *source; |
michael@0 | 104 | if (aSource) { |
michael@0 | 105 | const BasicCompositingRenderTarget* sourceSurface = |
michael@0 | 106 | static_cast<const BasicCompositingRenderTarget*>(aSource); |
michael@0 | 107 | source = sourceSurface->mDrawTarget; |
michael@0 | 108 | } else { |
michael@0 | 109 | source = mDrawTarget; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | RefPtr<SourceSurface> snapshot = source->Snapshot(); |
michael@0 | 113 | |
michael@0 | 114 | IntRect sourceRect(aSourcePoint, aRect.Size()); |
michael@0 | 115 | rt->mDrawTarget->CopySurface(snapshot, sourceRect, IntPoint(0, 0)); |
michael@0 | 116 | return rt.forget(); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | TemporaryRef<DataTextureSource> |
michael@0 | 120 | BasicCompositor::CreateDataTextureSource(TextureFlags aFlags) |
michael@0 | 121 | { |
michael@0 | 122 | RefPtr<DataTextureSource> result = new DataTextureSourceBasic(); |
michael@0 | 123 | return result.forget(); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | bool |
michael@0 | 127 | BasicCompositor::SupportsEffect(EffectTypes aEffect) |
michael@0 | 128 | { |
michael@0 | 129 | return static_cast<EffectTypes>(aEffect) != EFFECT_YCBCR; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | static void |
michael@0 | 133 | DrawSurfaceWithTextureCoords(DrawTarget *aDest, |
michael@0 | 134 | const gfx::Rect& aDestRect, |
michael@0 | 135 | SourceSurface *aSource, |
michael@0 | 136 | const gfx::Rect& aTextureCoords, |
michael@0 | 137 | gfx::Filter aFilter, |
michael@0 | 138 | float aOpacity, |
michael@0 | 139 | SourceSurface *aMask, |
michael@0 | 140 | const Matrix* aMaskTransform) |
michael@0 | 141 | { |
michael@0 | 142 | // Convert aTextureCoords into aSource's coordinate space |
michael@0 | 143 | gfxRect sourceRect(aTextureCoords.x * aSource->GetSize().width, |
michael@0 | 144 | aTextureCoords.y * aSource->GetSize().height, |
michael@0 | 145 | aTextureCoords.width * aSource->GetSize().width, |
michael@0 | 146 | aTextureCoords.height * aSource->GetSize().height); |
michael@0 | 147 | // Compute a transform that maps sourceRect to aDestRect. |
michael@0 | 148 | gfxMatrix transform = |
michael@0 | 149 | gfxUtils::TransformRectToRect(sourceRect, |
michael@0 | 150 | gfxPoint(aDestRect.x, aDestRect.y), |
michael@0 | 151 | gfxPoint(aDestRect.XMost(), aDestRect.y), |
michael@0 | 152 | gfxPoint(aDestRect.XMost(), aDestRect.YMost())); |
michael@0 | 153 | Matrix matrix = ToMatrix(transform); |
michael@0 | 154 | |
michael@0 | 155 | // Only use REPEAT if aTextureCoords is outside (0, 0, 1, 1). |
michael@0 | 156 | gfx::Rect unitRect(0, 0, 1, 1); |
michael@0 | 157 | ExtendMode mode = unitRect.Contains(aTextureCoords) ? ExtendMode::CLAMP : ExtendMode::REPEAT; |
michael@0 | 158 | |
michael@0 | 159 | FillRectWithMask(aDest, aDestRect, aSource, aFilter, DrawOptions(aOpacity), |
michael@0 | 160 | mode, aMask, aMaskTransform, &matrix); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | static pixman_transform |
michael@0 | 164 | Matrix3DToPixman(const gfx3DMatrix& aMatrix) |
michael@0 | 165 | { |
michael@0 | 166 | pixman_f_transform transform; |
michael@0 | 167 | |
michael@0 | 168 | transform.m[0][0] = aMatrix._11; |
michael@0 | 169 | transform.m[0][1] = aMatrix._21; |
michael@0 | 170 | transform.m[0][2] = aMatrix._41; |
michael@0 | 171 | transform.m[1][0] = aMatrix._12; |
michael@0 | 172 | transform.m[1][1] = aMatrix._22; |
michael@0 | 173 | transform.m[1][2] = aMatrix._42; |
michael@0 | 174 | transform.m[2][0] = aMatrix._14; |
michael@0 | 175 | transform.m[2][1] = aMatrix._24; |
michael@0 | 176 | transform.m[2][2] = aMatrix._44; |
michael@0 | 177 | |
michael@0 | 178 | pixman_transform result; |
michael@0 | 179 | pixman_transform_from_pixman_f_transform(&result, &transform); |
michael@0 | 180 | |
michael@0 | 181 | return result; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | static void |
michael@0 | 185 | PixmanTransform(DataSourceSurface* aDest, |
michael@0 | 186 | DataSourceSurface* aSource, |
michael@0 | 187 | const gfx3DMatrix& aTransform, |
michael@0 | 188 | gfxPoint aDestOffset) |
michael@0 | 189 | { |
michael@0 | 190 | IntSize destSize = aDest->GetSize(); |
michael@0 | 191 | pixman_image_t* dest = pixman_image_create_bits(PIXMAN_a8r8g8b8, |
michael@0 | 192 | destSize.width, |
michael@0 | 193 | destSize.height, |
michael@0 | 194 | (uint32_t*)aDest->GetData(), |
michael@0 | 195 | aDest->Stride()); |
michael@0 | 196 | |
michael@0 | 197 | IntSize srcSize = aSource->GetSize(); |
michael@0 | 198 | pixman_image_t* src = pixman_image_create_bits(PIXMAN_a8r8g8b8, |
michael@0 | 199 | srcSize.width, |
michael@0 | 200 | srcSize.height, |
michael@0 | 201 | (uint32_t*)aSource->GetData(), |
michael@0 | 202 | aSource->Stride()); |
michael@0 | 203 | |
michael@0 | 204 | NS_ABORT_IF_FALSE(src && dest, "Failed to create pixman images?"); |
michael@0 | 205 | |
michael@0 | 206 | pixman_transform pixTransform = Matrix3DToPixman(aTransform); |
michael@0 | 207 | pixman_transform pixTransformInverted; |
michael@0 | 208 | |
michael@0 | 209 | // If the transform is singular then nothing would be drawn anyway, return here |
michael@0 | 210 | if (!pixman_transform_invert(&pixTransformInverted, &pixTransform)) { |
michael@0 | 211 | pixman_image_unref(dest); |
michael@0 | 212 | pixman_image_unref(src); |
michael@0 | 213 | return; |
michael@0 | 214 | } |
michael@0 | 215 | pixman_image_set_transform(src, &pixTransformInverted); |
michael@0 | 216 | |
michael@0 | 217 | pixman_image_composite32(PIXMAN_OP_SRC, |
michael@0 | 218 | src, |
michael@0 | 219 | nullptr, |
michael@0 | 220 | dest, |
michael@0 | 221 | aDestOffset.x, |
michael@0 | 222 | aDestOffset.y, |
michael@0 | 223 | 0, |
michael@0 | 224 | 0, |
michael@0 | 225 | 0, |
michael@0 | 226 | 0, |
michael@0 | 227 | destSize.width, |
michael@0 | 228 | destSize.height); |
michael@0 | 229 | |
michael@0 | 230 | pixman_image_unref(dest); |
michael@0 | 231 | pixman_image_unref(src); |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | static inline IntRect |
michael@0 | 235 | RoundOut(Rect r) |
michael@0 | 236 | { |
michael@0 | 237 | r.RoundOut(); |
michael@0 | 238 | return IntRect(r.x, r.y, r.width, r.height); |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | void |
michael@0 | 242 | BasicCompositor::DrawQuad(const gfx::Rect& aRect, |
michael@0 | 243 | const gfx::Rect& aClipRect, |
michael@0 | 244 | const EffectChain &aEffectChain, |
michael@0 | 245 | gfx::Float aOpacity, |
michael@0 | 246 | const gfx::Matrix4x4 &aTransform) |
michael@0 | 247 | { |
michael@0 | 248 | RefPtr<DrawTarget> buffer = mRenderTarget->mDrawTarget; |
michael@0 | 249 | |
michael@0 | 250 | // For 2D drawing, |dest| and |buffer| are the same surface. For 3D drawing, |
michael@0 | 251 | // |dest| is a temporary surface. |
michael@0 | 252 | RefPtr<DrawTarget> dest = buffer; |
michael@0 | 253 | |
michael@0 | 254 | buffer->PushClipRect(aClipRect); |
michael@0 | 255 | AutoSaveTransform autoSaveTransform(dest); |
michael@0 | 256 | |
michael@0 | 257 | Matrix newTransform; |
michael@0 | 258 | Rect transformBounds; |
michael@0 | 259 | gfx3DMatrix new3DTransform; |
michael@0 | 260 | IntPoint offset = mRenderTarget->GetOrigin(); |
michael@0 | 261 | |
michael@0 | 262 | if (aTransform.Is2D()) { |
michael@0 | 263 | newTransform = aTransform.As2D(); |
michael@0 | 264 | } else { |
michael@0 | 265 | // Create a temporary surface for the transform. |
michael@0 | 266 | dest = gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(RoundOut(aRect).Size(), SurfaceFormat::B8G8R8A8); |
michael@0 | 267 | if (!dest) { |
michael@0 | 268 | return; |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | // Get the bounds post-transform. |
michael@0 | 272 | To3DMatrix(aTransform, new3DTransform); |
michael@0 | 273 | gfxRect bounds = new3DTransform.TransformBounds(ThebesRect(aRect)); |
michael@0 | 274 | bounds.IntersectRect(bounds, gfxRect(offset.x, offset.y, buffer->GetSize().width, buffer->GetSize().height)); |
michael@0 | 275 | |
michael@0 | 276 | transformBounds = ToRect(bounds); |
michael@0 | 277 | transformBounds.RoundOut(); |
michael@0 | 278 | |
michael@0 | 279 | // Propagate the coordinate offset to our 2D draw target. |
michael@0 | 280 | newTransform.Translate(transformBounds.x, transformBounds.y); |
michael@0 | 281 | |
michael@0 | 282 | // When we apply the 3D transformation, we do it against a temporary |
michael@0 | 283 | // surface, so undo the coordinate offset. |
michael@0 | 284 | new3DTransform = new3DTransform * gfx3DMatrix::Translation(-transformBounds.x, -transformBounds.y, 0); |
michael@0 | 285 | |
michael@0 | 286 | transformBounds.MoveTo(0, 0); |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | newTransform.PostTranslate(-offset.x, -offset.y); |
michael@0 | 290 | buffer->SetTransform(newTransform); |
michael@0 | 291 | |
michael@0 | 292 | RefPtr<SourceSurface> sourceMask; |
michael@0 | 293 | Matrix maskTransform; |
michael@0 | 294 | if (aEffectChain.mSecondaryEffects[EFFECT_MASK]) { |
michael@0 | 295 | EffectMask *effectMask = static_cast<EffectMask*>(aEffectChain.mSecondaryEffects[EFFECT_MASK].get()); |
michael@0 | 296 | sourceMask = effectMask->mMaskTexture->AsSourceBasic()->GetSurface(dest); |
michael@0 | 297 | MOZ_ASSERT(effectMask->mMaskTransform.Is2D(), "How did we end up with a 3D transform here?!"); |
michael@0 | 298 | MOZ_ASSERT(!effectMask->mIs3D); |
michael@0 | 299 | maskTransform = effectMask->mMaskTransform.As2D(); |
michael@0 | 300 | maskTransform.Translate(-offset.x, -offset.y); |
michael@0 | 301 | } |
michael@0 | 302 | |
michael@0 | 303 | switch (aEffectChain.mPrimaryEffect->mType) { |
michael@0 | 304 | case EFFECT_SOLID_COLOR: { |
michael@0 | 305 | EffectSolidColor* effectSolidColor = |
michael@0 | 306 | static_cast<EffectSolidColor*>(aEffectChain.mPrimaryEffect.get()); |
michael@0 | 307 | |
michael@0 | 308 | FillRectWithMask(dest, aRect, effectSolidColor->mColor, |
michael@0 | 309 | DrawOptions(aOpacity), sourceMask, &maskTransform); |
michael@0 | 310 | break; |
michael@0 | 311 | } |
michael@0 | 312 | case EFFECT_RGB: { |
michael@0 | 313 | TexturedEffect* texturedEffect = |
michael@0 | 314 | static_cast<TexturedEffect*>(aEffectChain.mPrimaryEffect.get()); |
michael@0 | 315 | TextureSourceBasic* source = texturedEffect->mTexture->AsSourceBasic(); |
michael@0 | 316 | |
michael@0 | 317 | DrawSurfaceWithTextureCoords(dest, aRect, |
michael@0 | 318 | source->GetSurface(dest), |
michael@0 | 319 | texturedEffect->mTextureCoords, |
michael@0 | 320 | texturedEffect->mFilter, |
michael@0 | 321 | aOpacity, sourceMask, &maskTransform); |
michael@0 | 322 | break; |
michael@0 | 323 | } |
michael@0 | 324 | case EFFECT_YCBCR: { |
michael@0 | 325 | NS_RUNTIMEABORT("Can't (easily) support component alpha with BasicCompositor!"); |
michael@0 | 326 | break; |
michael@0 | 327 | } |
michael@0 | 328 | case EFFECT_RENDER_TARGET: { |
michael@0 | 329 | EffectRenderTarget* effectRenderTarget = |
michael@0 | 330 | static_cast<EffectRenderTarget*>(aEffectChain.mPrimaryEffect.get()); |
michael@0 | 331 | RefPtr<BasicCompositingRenderTarget> surface |
michael@0 | 332 | = static_cast<BasicCompositingRenderTarget*>(effectRenderTarget->mRenderTarget.get()); |
michael@0 | 333 | RefPtr<SourceSurface> sourceSurf = surface->mDrawTarget->Snapshot(); |
michael@0 | 334 | |
michael@0 | 335 | DrawSurfaceWithTextureCoords(dest, aRect, |
michael@0 | 336 | sourceSurf, |
michael@0 | 337 | effectRenderTarget->mTextureCoords, |
michael@0 | 338 | effectRenderTarget->mFilter, |
michael@0 | 339 | aOpacity, sourceMask, &maskTransform); |
michael@0 | 340 | break; |
michael@0 | 341 | } |
michael@0 | 342 | case EFFECT_COMPONENT_ALPHA: { |
michael@0 | 343 | NS_RUNTIMEABORT("Can't (easily) support component alpha with BasicCompositor!"); |
michael@0 | 344 | break; |
michael@0 | 345 | } |
michael@0 | 346 | default: { |
michael@0 | 347 | NS_RUNTIMEABORT("Invalid effect type!"); |
michael@0 | 348 | break; |
michael@0 | 349 | } |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | if (!aTransform.Is2D()) { |
michael@0 | 353 | dest->Flush(); |
michael@0 | 354 | |
michael@0 | 355 | RefPtr<SourceSurface> snapshot = dest->Snapshot(); |
michael@0 | 356 | RefPtr<DataSourceSurface> source = snapshot->GetDataSurface(); |
michael@0 | 357 | RefPtr<DataSourceSurface> temp = |
michael@0 | 358 | Factory::CreateDataSourceSurface(RoundOut(transformBounds).Size(), SurfaceFormat::B8G8R8A8); |
michael@0 | 359 | if (!temp) { |
michael@0 | 360 | return; |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | PixmanTransform(temp, source, new3DTransform, gfxPoint(0, 0)); |
michael@0 | 364 | |
michael@0 | 365 | buffer->DrawSurface(temp, transformBounds, transformBounds); |
michael@0 | 366 | } |
michael@0 | 367 | |
michael@0 | 368 | buffer->PopClip(); |
michael@0 | 369 | } |
michael@0 | 370 | |
michael@0 | 371 | void |
michael@0 | 372 | BasicCompositor::BeginFrame(const nsIntRegion& aInvalidRegion, |
michael@0 | 373 | const gfx::Rect *aClipRectIn, |
michael@0 | 374 | const gfx::Matrix& aTransform, |
michael@0 | 375 | const gfx::Rect& aRenderBounds, |
michael@0 | 376 | gfx::Rect *aClipRectOut /* = nullptr */, |
michael@0 | 377 | gfx::Rect *aRenderBoundsOut /* = nullptr */) |
michael@0 | 378 | { |
michael@0 | 379 | nsIntRect intRect; |
michael@0 | 380 | mWidget->GetClientBounds(intRect); |
michael@0 | 381 | mWidgetSize = gfx::ToIntSize(intRect.Size()); |
michael@0 | 382 | |
michael@0 | 383 | // The result of GetClientBounds is shifted over by the size of the window |
michael@0 | 384 | // manager styling. We want to ignore that. |
michael@0 | 385 | intRect.MoveTo(0, 0); |
michael@0 | 386 | Rect rect = Rect(0, 0, intRect.width, intRect.height); |
michael@0 | 387 | |
michael@0 | 388 | // Sometimes the invalid region is larger than we want to draw. |
michael@0 | 389 | nsIntRegion invalidRegionSafe; |
michael@0 | 390 | invalidRegionSafe.And(aInvalidRegion, intRect); |
michael@0 | 391 | |
michael@0 | 392 | // FIXME: Redraw the whole screen in every frame to work around bug 972728. |
michael@0 | 393 | invalidRegionSafe = intRect; |
michael@0 | 394 | |
michael@0 | 395 | nsIntRect invalidRect = invalidRegionSafe.GetBounds(); |
michael@0 | 396 | mInvalidRect = IntRect(invalidRect.x, invalidRect.y, invalidRect.width, invalidRect.height); |
michael@0 | 397 | mInvalidRegion = invalidRegionSafe; |
michael@0 | 398 | |
michael@0 | 399 | if (aRenderBoundsOut) { |
michael@0 | 400 | *aRenderBoundsOut = Rect(); |
michael@0 | 401 | } |
michael@0 | 402 | |
michael@0 | 403 | if (mInvalidRect.width <= 0 || mInvalidRect.height <= 0) { |
michael@0 | 404 | return; |
michael@0 | 405 | } |
michael@0 | 406 | |
michael@0 | 407 | if (mCopyTarget) { |
michael@0 | 408 | // If we have a copy target, then we don't have a widget-provided mDrawTarget (currently). Create a dummy |
michael@0 | 409 | // placeholder so that CreateRenderTarget() works. |
michael@0 | 410 | mDrawTarget = gfxPlatform::GetPlatform()->CreateOffscreenCanvasDrawTarget(IntSize(1,1), SurfaceFormat::B8G8R8A8); |
michael@0 | 411 | } else { |
michael@0 | 412 | mDrawTarget = mWidget->StartRemoteDrawing(); |
michael@0 | 413 | } |
michael@0 | 414 | if (!mDrawTarget) { |
michael@0 | 415 | return; |
michael@0 | 416 | } |
michael@0 | 417 | |
michael@0 | 418 | // Setup an intermediate render target to buffer all compositing. We will |
michael@0 | 419 | // copy this into mDrawTarget (the widget), and/or mCopyTarget in EndFrame() |
michael@0 | 420 | RefPtr<CompositingRenderTarget> target = CreateRenderTarget(mInvalidRect, INIT_MODE_CLEAR); |
michael@0 | 421 | SetRenderTarget(target); |
michael@0 | 422 | |
michael@0 | 423 | // We only allocate a surface sized to the invalidated region, so we need to |
michael@0 | 424 | // translate future coordinates. |
michael@0 | 425 | Matrix transform; |
michael@0 | 426 | transform.Translate(-invalidRect.x, -invalidRect.y); |
michael@0 | 427 | mRenderTarget->mDrawTarget->SetTransform(transform); |
michael@0 | 428 | |
michael@0 | 429 | gfxUtils::ClipToRegion(mRenderTarget->mDrawTarget, invalidRegionSafe); |
michael@0 | 430 | |
michael@0 | 431 | if (aRenderBoundsOut) { |
michael@0 | 432 | *aRenderBoundsOut = rect; |
michael@0 | 433 | } |
michael@0 | 434 | |
michael@0 | 435 | if (aClipRectIn) { |
michael@0 | 436 | mRenderTarget->mDrawTarget->PushClipRect(*aClipRectIn); |
michael@0 | 437 | } else { |
michael@0 | 438 | mRenderTarget->mDrawTarget->PushClipRect(rect); |
michael@0 | 439 | if (aClipRectOut) { |
michael@0 | 440 | *aClipRectOut = rect; |
michael@0 | 441 | } |
michael@0 | 442 | } |
michael@0 | 443 | } |
michael@0 | 444 | |
michael@0 | 445 | void |
michael@0 | 446 | BasicCompositor::EndFrame() |
michael@0 | 447 | { |
michael@0 | 448 | // Pop aClipRectIn/bounds rect |
michael@0 | 449 | mRenderTarget->mDrawTarget->PopClip(); |
michael@0 | 450 | |
michael@0 | 451 | if (gfxPrefs::WidgetUpdateFlashing()) { |
michael@0 | 452 | float r = float(rand()) / RAND_MAX; |
michael@0 | 453 | float g = float(rand()) / RAND_MAX; |
michael@0 | 454 | float b = float(rand()) / RAND_MAX; |
michael@0 | 455 | // We're still clipped to mInvalidRegion, so just fill the bounds. |
michael@0 | 456 | mRenderTarget->mDrawTarget->FillRect(ToRect(mInvalidRegion.GetBounds()), |
michael@0 | 457 | ColorPattern(Color(r, g, b, 0.2f))); |
michael@0 | 458 | } |
michael@0 | 459 | |
michael@0 | 460 | // Pop aInvalidregion |
michael@0 | 461 | mRenderTarget->mDrawTarget->PopClip(); |
michael@0 | 462 | |
michael@0 | 463 | // Note: Most platforms require us to buffer drawing to the widget surface. |
michael@0 | 464 | // That's why we don't draw to mDrawTarget directly. |
michael@0 | 465 | RefPtr<SourceSurface> source = mRenderTarget->mDrawTarget->Snapshot(); |
michael@0 | 466 | RefPtr<DrawTarget> dest(mCopyTarget ? mCopyTarget : mDrawTarget); |
michael@0 | 467 | |
michael@0 | 468 | // The source DrawTarget is clipped to the invalidation region, so we have |
michael@0 | 469 | // to copy the individual rectangles in the region or else we'll draw blank |
michael@0 | 470 | // pixels. |
michael@0 | 471 | nsIntRegionRectIterator iter(mInvalidRegion); |
michael@0 | 472 | for (const nsIntRect *r = iter.Next(); r; r = iter.Next()) { |
michael@0 | 473 | dest->CopySurface(source, |
michael@0 | 474 | IntRect(r->x - mInvalidRect.x, r->y - mInvalidRect.y, r->width, r->height), |
michael@0 | 475 | IntPoint(r->x, r->y)); |
michael@0 | 476 | } |
michael@0 | 477 | if (!mCopyTarget) { |
michael@0 | 478 | mWidget->EndRemoteDrawing(); |
michael@0 | 479 | } |
michael@0 | 480 | |
michael@0 | 481 | mDrawTarget = nullptr; |
michael@0 | 482 | mRenderTarget = nullptr; |
michael@0 | 483 | } |
michael@0 | 484 | |
michael@0 | 485 | void |
michael@0 | 486 | BasicCompositor::AbortFrame() |
michael@0 | 487 | { |
michael@0 | 488 | mRenderTarget->mDrawTarget->PopClip(); |
michael@0 | 489 | mRenderTarget->mDrawTarget->PopClip(); |
michael@0 | 490 | mDrawTarget = nullptr; |
michael@0 | 491 | mRenderTarget = nullptr; |
michael@0 | 492 | } |
michael@0 | 493 | |
michael@0 | 494 | } |
michael@0 | 495 | } |