|
1 /* -*- Mode: C++; tab-width: 20; 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 |
|
6 #include "ImageLayerComposite.h" |
|
7 #include "CompositableHost.h" // for CompositableHost |
|
8 #include "Layers.h" // for WriteSnapshotToDumpFile, etc |
|
9 #include "gfx2DGlue.h" // for ToFilter, ToMatrix4x4 |
|
10 #include "gfxRect.h" // for gfxRect |
|
11 #include "gfxUtils.h" // for gfxUtils, etc |
|
12 #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc |
|
13 #include "mozilla/gfx/Matrix.h" // for Matrix4x4 |
|
14 #include "mozilla/gfx/Point.h" // for IntSize, Point |
|
15 #include "mozilla/gfx/Rect.h" // for Rect |
|
16 #include "mozilla/layers/Compositor.h" // for Compositor |
|
17 #include "mozilla/layers/Effects.h" // for EffectChain |
|
18 #include "mozilla/layers/TextureHost.h" // for TextureHost, etc |
|
19 #include "mozilla/mozalloc.h" // for operator delete |
|
20 #include "nsAString.h" |
|
21 #include "nsAutoPtr.h" // for nsRefPtr |
|
22 #include "nsDebug.h" // for NS_ASSERTION |
|
23 #include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc |
|
24 #include "nsPoint.h" // for nsIntPoint |
|
25 #include "nsRect.h" // for nsIntRect |
|
26 #include "nsString.h" // for nsAutoCString |
|
27 |
|
28 using namespace mozilla::gfx; |
|
29 |
|
30 namespace mozilla { |
|
31 namespace layers { |
|
32 |
|
33 ImageLayerComposite::ImageLayerComposite(LayerManagerComposite* aManager) |
|
34 : ImageLayer(aManager, nullptr) |
|
35 , LayerComposite(aManager) |
|
36 , mImageHost(nullptr) |
|
37 { |
|
38 MOZ_COUNT_CTOR(ImageLayerComposite); |
|
39 mImplData = static_cast<LayerComposite*>(this); |
|
40 } |
|
41 |
|
42 ImageLayerComposite::~ImageLayerComposite() |
|
43 { |
|
44 MOZ_COUNT_DTOR(ImageLayerComposite); |
|
45 MOZ_ASSERT(mDestroyed); |
|
46 |
|
47 CleanupResources(); |
|
48 } |
|
49 |
|
50 bool |
|
51 ImageLayerComposite::SetCompositableHost(CompositableHost* aHost) |
|
52 { |
|
53 switch (aHost->GetType()) { |
|
54 case BUFFER_IMAGE_SINGLE: |
|
55 case BUFFER_IMAGE_BUFFERED: |
|
56 case COMPOSITABLE_IMAGE: |
|
57 mImageHost = aHost; |
|
58 return true; |
|
59 default: |
|
60 return false; |
|
61 } |
|
62 } |
|
63 |
|
64 void |
|
65 ImageLayerComposite::Disconnect() |
|
66 { |
|
67 Destroy(); |
|
68 } |
|
69 |
|
70 LayerRenderState |
|
71 ImageLayerComposite::GetRenderState() |
|
72 { |
|
73 if (mImageHost && mImageHost->IsAttached()) { |
|
74 return mImageHost->GetRenderState(); |
|
75 } |
|
76 return LayerRenderState(); |
|
77 } |
|
78 |
|
79 Layer* |
|
80 ImageLayerComposite::GetLayer() |
|
81 { |
|
82 return this; |
|
83 } |
|
84 |
|
85 void |
|
86 ImageLayerComposite::RenderLayer(const nsIntRect& aClipRect) |
|
87 { |
|
88 if (!mImageHost || !mImageHost->IsAttached()) { |
|
89 return; |
|
90 } |
|
91 |
|
92 #ifdef MOZ_DUMP_PAINTING |
|
93 if (gfxUtils::sDumpPainting) { |
|
94 RefPtr<gfx::DataSourceSurface> surf = mImageHost->GetAsSurface(); |
|
95 WriteSnapshotToDumpFile(this, surf); |
|
96 } |
|
97 #endif |
|
98 |
|
99 mCompositor->MakeCurrent(); |
|
100 |
|
101 EffectChain effectChain(this); |
|
102 LayerManagerComposite::AutoAddMaskEffect autoMaskEffect(mMaskLayer, effectChain); |
|
103 |
|
104 gfx::Rect clipRect(aClipRect.x, aClipRect.y, aClipRect.width, aClipRect.height); |
|
105 mImageHost->SetCompositor(mCompositor); |
|
106 mImageHost->Composite(effectChain, |
|
107 GetEffectiveOpacity(), |
|
108 GetEffectiveTransform(), |
|
109 gfx::ToFilter(mFilter), |
|
110 clipRect); |
|
111 mImageHost->BumpFlashCounter(); |
|
112 } |
|
113 |
|
114 void |
|
115 ImageLayerComposite::ComputeEffectiveTransforms(const gfx::Matrix4x4& aTransformToSurface) |
|
116 { |
|
117 gfx::Matrix4x4 local = GetLocalTransform(); |
|
118 |
|
119 // Snap image edges to pixel boundaries |
|
120 gfxRect sourceRect(0, 0, 0, 0); |
|
121 if (mImageHost && |
|
122 mImageHost->IsAttached() && |
|
123 mImageHost->GetAsTextureHost()) { |
|
124 IntSize size = mImageHost->GetAsTextureHost()->GetSize(); |
|
125 sourceRect.SizeTo(size.width, size.height); |
|
126 if (mScaleMode != ScaleMode::SCALE_NONE && |
|
127 sourceRect.width != 0.0 && sourceRect.height != 0.0) { |
|
128 NS_ASSERTION(mScaleMode == ScaleMode::STRETCH, |
|
129 "No other scalemodes than stretch and none supported yet."); |
|
130 local.Scale(mScaleToSize.width / sourceRect.width, |
|
131 mScaleToSize.height / sourceRect.height, 1.0); |
|
132 } |
|
133 } |
|
134 // Snap our local transform first, and snap the inherited transform as well. |
|
135 // This makes our snapping equivalent to what would happen if our content |
|
136 // was drawn into a ThebesLayer (gfxContext would snap using the local |
|
137 // transform, then we'd snap again when compositing the ThebesLayer). |
|
138 mEffectiveTransform = |
|
139 SnapTransform(local, sourceRect, nullptr) * |
|
140 SnapTransformTranslation(aTransformToSurface, nullptr); |
|
141 ComputeEffectiveTransformForMaskLayer(aTransformToSurface); |
|
142 } |
|
143 |
|
144 CompositableHost* |
|
145 ImageLayerComposite::GetCompositableHost() |
|
146 { |
|
147 if (mImageHost && mImageHost->IsAttached()) { |
|
148 return mImageHost.get(); |
|
149 } |
|
150 |
|
151 return nullptr; |
|
152 } |
|
153 |
|
154 void |
|
155 ImageLayerComposite::CleanupResources() |
|
156 { |
|
157 if (mImageHost) { |
|
158 mImageHost->Detach(this); |
|
159 } |
|
160 mImageHost = nullptr; |
|
161 } |
|
162 |
|
163 nsACString& |
|
164 ImageLayerComposite::PrintInfo(nsACString& aTo, const char* aPrefix) |
|
165 { |
|
166 ImageLayer::PrintInfo(aTo, aPrefix); |
|
167 aTo += "\n"; |
|
168 if (mImageHost && mImageHost->IsAttached()) { |
|
169 nsAutoCString pfx(aPrefix); |
|
170 pfx += " "; |
|
171 mImageHost->PrintInfo(aTo, pfx.get()); |
|
172 } |
|
173 return aTo; |
|
174 } |
|
175 |
|
176 } /* layers */ |
|
177 } /* mozilla */ |