1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "ImageLayerMLGPU.h"
8 #include "LayerManagerMLGPU.h"
9 
10 namespace mozilla {
11 
12 using namespace gfx;
13 
14 namespace layers {
15 
ImageLayerMLGPU(LayerManagerMLGPU * aManager)16 ImageLayerMLGPU::ImageLayerMLGPU(LayerManagerMLGPU* aManager)
17     : ImageLayer(aManager, static_cast<HostLayer*>(this)),
18       TexturedLayerMLGPU(aManager) {}
19 
~ImageLayerMLGPU()20 ImageLayerMLGPU::~ImageLayerMLGPU() { CleanupResources(); }
21 
ComputeEffectiveTransforms(const gfx::Matrix4x4 & aTransformToSurface)22 void ImageLayerMLGPU::ComputeEffectiveTransforms(
23     const gfx::Matrix4x4& aTransformToSurface) {
24   Matrix4x4 local = GetLocalTransform();
25 
26   // Snap image edges to pixel boundaries.
27   gfxRect sourceRect(0, 0, 0, 0);
28   if (mHost && mHost->IsAttached()) {
29     IntSize size = mHost->GetImageSize();
30     sourceRect.SizeTo(size.width, size.height);
31   }
32 
33   // Snap our local transform first, and snap the inherited transform as well.
34   // This makes our snapping equivalent to what would happen if our content
35   // was drawn into a PaintedLayer (gfxContext would snap using the local
36   // transform, then we'd snap again when compositing the PaintedLayer).
37   mEffectiveTransform = SnapTransform(local, sourceRect, nullptr) *
38                         SnapTransformTranslation(aTransformToSurface, nullptr);
39   mEffectiveTransformForBuffer = mEffectiveTransform;
40 
41   if (mScaleMode == ScaleMode::STRETCH && mScaleToSize.width != 0.0 &&
42       mScaleToSize.height != 0.0) {
43     Size scale(sourceRect.Width() / mScaleToSize.width,
44                sourceRect.Height() / mScaleToSize.height);
45     mScale = Some(scale);
46   }
47 
48   ComputeEffectiveTransformForMaskLayers(aTransformToSurface);
49 }
50 
GetSamplingFilter()51 gfx::SamplingFilter ImageLayerMLGPU::GetSamplingFilter() {
52   return ImageLayer::GetSamplingFilter();
53 }
54 
IsContentOpaque()55 bool ImageLayerMLGPU::IsContentOpaque() {
56   if (mPictureRect.Width() == 0 || mPictureRect.Height() == 0) {
57     return false;
58   }
59   if (mScaleMode == ScaleMode::STRETCH) {
60     return gfx::IsOpaque(mHost->CurrentTextureHost()->GetFormat());
61   }
62   return false;
63 }
64 
SetRenderRegion(LayerIntRegion && aRegion)65 void ImageLayerMLGPU::SetRenderRegion(LayerIntRegion&& aRegion) {
66   switch (mScaleMode) {
67     case ScaleMode::STRETCH:
68       // See bug 1264142.
69       aRegion.AndWith(
70           LayerIntRect(0, 0, mScaleToSize.width, mScaleToSize.height));
71       break;
72     default:
73       // Clamp the visible region to the texture size. (see bug 1396507)
74       MOZ_ASSERT(mScaleMode == ScaleMode::SCALE_NONE);
75       aRegion.AndWith(
76           LayerIntRect(0, 0, mPictureRect.Width(), mPictureRect.Height()));
77       break;
78   }
79   LayerMLGPU::SetRenderRegion(Move(aRegion));
80 }
81 
CleanupResources()82 void ImageLayerMLGPU::CleanupResources() {
83   if (mHost) {
84     mHost->CleanupResources();
85     mHost->Detach(this);
86   }
87   mTexture = nullptr;
88   mBigImageTexture = nullptr;
89   mHost = nullptr;
90 }
91 
PrintInfo(std::stringstream & aStream,const char * aPrefix)92 void ImageLayerMLGPU::PrintInfo(std::stringstream& aStream,
93                                 const char* aPrefix) {
94   ImageLayer::PrintInfo(aStream, aPrefix);
95   if (mHost && mHost->IsAttached()) {
96     aStream << "\n";
97     nsAutoCString pfx(aPrefix);
98     pfx += "  ";
99     mHost->PrintInfo(aStream, pfx.get());
100   }
101 }
102 
Disconnect()103 void ImageLayerMLGPU::Disconnect() { CleanupResources(); }
104 
ClearCachedResources()105 void ImageLayerMLGPU::ClearCachedResources() { CleanupResources(); }
106 
107 }  // namespace layers
108 }  // namespace mozilla
109