|
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 "CanvasLayerComposite.h" |
|
7 #include "composite/CompositableHost.h" // for CompositableHost |
|
8 #include "gfx2DGlue.h" // for ToFilter, ToMatrix4x4 |
|
9 #include "GraphicsFilter.h" // for GraphicsFilter |
|
10 #include "gfxUtils.h" // for gfxUtils, etc |
|
11 #include "mozilla/gfx/Matrix.h" // for Matrix4x4 |
|
12 #include "mozilla/gfx/Point.h" // for Point |
|
13 #include "mozilla/gfx/Rect.h" // for Rect |
|
14 #include "mozilla/layers/Compositor.h" // for Compositor |
|
15 #include "mozilla/layers/Effects.h" // for EffectChain |
|
16 #include "mozilla/mozalloc.h" // for operator delete |
|
17 #include "nsAString.h" |
|
18 #include "nsAutoPtr.h" // for nsRefPtr |
|
19 #include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc |
|
20 #include "nsPoint.h" // for nsIntPoint |
|
21 #include "nsString.h" // for nsAutoCString |
|
22 |
|
23 using namespace mozilla; |
|
24 using namespace mozilla::layers; |
|
25 using namespace mozilla::gfx; |
|
26 |
|
27 CanvasLayerComposite::CanvasLayerComposite(LayerManagerComposite* aManager) |
|
28 : CanvasLayer(aManager, nullptr) |
|
29 , LayerComposite(aManager) |
|
30 , mImageHost(nullptr) |
|
31 { |
|
32 MOZ_COUNT_CTOR(CanvasLayerComposite); |
|
33 mImplData = static_cast<LayerComposite*>(this); |
|
34 } |
|
35 |
|
36 CanvasLayerComposite::~CanvasLayerComposite() |
|
37 { |
|
38 MOZ_COUNT_DTOR(CanvasLayerComposite); |
|
39 |
|
40 CleanupResources(); |
|
41 } |
|
42 |
|
43 bool |
|
44 CanvasLayerComposite::SetCompositableHost(CompositableHost* aHost) |
|
45 { |
|
46 switch (aHost->GetType()) { |
|
47 case BUFFER_IMAGE_SINGLE: |
|
48 case BUFFER_IMAGE_BUFFERED: |
|
49 case COMPOSITABLE_IMAGE: |
|
50 mImageHost = aHost; |
|
51 return true; |
|
52 default: |
|
53 return false; |
|
54 } |
|
55 |
|
56 } |
|
57 |
|
58 Layer* |
|
59 CanvasLayerComposite::GetLayer() |
|
60 { |
|
61 return this; |
|
62 } |
|
63 |
|
64 LayerRenderState |
|
65 CanvasLayerComposite::GetRenderState() |
|
66 { |
|
67 if (mDestroyed || !mImageHost || !mImageHost->IsAttached()) { |
|
68 return LayerRenderState(); |
|
69 } |
|
70 return mImageHost->GetRenderState(); |
|
71 } |
|
72 |
|
73 void |
|
74 CanvasLayerComposite::RenderLayer(const nsIntRect& aClipRect) |
|
75 { |
|
76 if (!mImageHost || !mImageHost->IsAttached()) { |
|
77 return; |
|
78 } |
|
79 |
|
80 mCompositor->MakeCurrent(); |
|
81 |
|
82 #ifdef MOZ_DUMP_PAINTING |
|
83 if (gfxUtils::sDumpPainting) { |
|
84 RefPtr<gfx::DataSourceSurface> surf = mImageHost->GetAsSurface(); |
|
85 WriteSnapshotToDumpFile(this, surf); |
|
86 } |
|
87 #endif |
|
88 |
|
89 GraphicsFilter filter = mFilter; |
|
90 #ifdef ANDROID |
|
91 // Bug 691354 |
|
92 // Using the LINEAR filter we get unexplained artifacts. |
|
93 // Use NEAREST when no scaling is required. |
|
94 Matrix matrix; |
|
95 bool is2D = GetEffectiveTransform().Is2D(&matrix); |
|
96 if (is2D && !ThebesMatrix(matrix).HasNonTranslationOrFlip()) { |
|
97 filter = GraphicsFilter::FILTER_NEAREST; |
|
98 } |
|
99 #endif |
|
100 |
|
101 EffectChain effectChain(this); |
|
102 |
|
103 LayerManagerComposite::AutoAddMaskEffect autoMaskEffect(mMaskLayer, effectChain); |
|
104 gfx::Rect clipRect(aClipRect.x, aClipRect.y, aClipRect.width, aClipRect.height); |
|
105 |
|
106 mImageHost->Composite(effectChain, |
|
107 GetEffectiveOpacity(), |
|
108 GetEffectiveTransform(), |
|
109 gfx::ToFilter(filter), |
|
110 clipRect); |
|
111 mImageHost->BumpFlashCounter(); |
|
112 } |
|
113 |
|
114 CompositableHost* |
|
115 CanvasLayerComposite::GetCompositableHost() |
|
116 { |
|
117 if ( mImageHost && mImageHost->IsAttached()) { |
|
118 return mImageHost.get(); |
|
119 } |
|
120 |
|
121 return nullptr; |
|
122 } |
|
123 |
|
124 void |
|
125 CanvasLayerComposite::CleanupResources() |
|
126 { |
|
127 if (mImageHost) { |
|
128 mImageHost->Detach(this); |
|
129 } |
|
130 mImageHost = nullptr; |
|
131 } |
|
132 |
|
133 nsACString& |
|
134 CanvasLayerComposite::PrintInfo(nsACString& aTo, const char* aPrefix) |
|
135 { |
|
136 CanvasLayer::PrintInfo(aTo, aPrefix); |
|
137 aTo += "\n"; |
|
138 if (mImageHost && mImageHost->IsAttached()) { |
|
139 nsAutoCString pfx(aPrefix); |
|
140 pfx += " "; |
|
141 mImageHost->PrintInfo(aTo, pfx.get()); |
|
142 } |
|
143 return aTo; |
|
144 } |
|
145 |