Line data Source code
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
9 : #include "gfxEnv.h" // for gfxEnv, etc
10 : #include "mozilla/gfx/Matrix.h" // for Matrix4x4
11 : #include "mozilla/gfx/Point.h" // for Point
12 : #include "mozilla/gfx/Rect.h" // for Rect
13 : #include "mozilla/layers/Compositor.h" // for Compositor
14 : #include "mozilla/layers/Effects.h" // for EffectChain
15 : #include "mozilla/mozalloc.h" // for operator delete
16 : #include "nsAString.h"
17 : #include "mozilla/RefPtr.h" // for nsRefPtr
18 : #include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc
19 : #include "nsString.h" // for nsAutoCString
20 :
21 : namespace mozilla {
22 : namespace layers {
23 :
24 : using namespace mozilla::gfx;
25 :
26 0 : CanvasLayerComposite::CanvasLayerComposite(LayerManagerComposite* aManager)
27 : : CanvasLayer(aManager, nullptr)
28 : , LayerComposite(aManager)
29 0 : , mCompositableHost(nullptr)
30 : {
31 0 : MOZ_COUNT_CTOR(CanvasLayerComposite);
32 0 : mImplData = static_cast<LayerComposite*>(this);
33 0 : }
34 :
35 0 : CanvasLayerComposite::~CanvasLayerComposite()
36 : {
37 0 : MOZ_COUNT_DTOR(CanvasLayerComposite);
38 :
39 0 : CleanupResources();
40 0 : }
41 :
42 : bool
43 0 : CanvasLayerComposite::SetCompositableHost(CompositableHost* aHost)
44 : {
45 0 : switch (aHost->GetType()) {
46 : case CompositableType::IMAGE:
47 0 : mCompositableHost = aHost;
48 0 : return true;
49 : default:
50 0 : return false;
51 : }
52 :
53 : }
54 :
55 : Layer*
56 0 : CanvasLayerComposite::GetLayer()
57 : {
58 0 : return this;
59 : }
60 :
61 : void
62 0 : CanvasLayerComposite::SetLayerManager(HostLayerManager* aManager)
63 : {
64 0 : LayerComposite::SetLayerManager(aManager);
65 0 : mManager = aManager;
66 0 : if (mCompositableHost && mCompositor) {
67 0 : mCompositableHost->SetTextureSourceProvider(mCompositor);
68 : }
69 0 : }
70 :
71 : void
72 0 : CanvasLayerComposite::RenderLayer(const IntRect& aClipRect,
73 : const Maybe<gfx::Polygon>& aGeometry)
74 : {
75 0 : if (!mCompositableHost || !mCompositableHost->IsAttached()) {
76 0 : return;
77 : }
78 :
79 0 : mCompositor->MakeCurrent();
80 :
81 : #ifdef MOZ_DUMP_PAINTING
82 0 : if (gfxEnv::DumpCompositorTextures()) {
83 0 : RefPtr<gfx::DataSourceSurface> surf = mCompositableHost->GetAsSurface();
84 0 : if (surf) {
85 0 : WriteSnapshotToDumpFile(this, surf);
86 : }
87 : }
88 : #endif
89 :
90 0 : RenderWithAllMasks(this, mCompositor, aClipRect,
91 0 : [&](EffectChain& effectChain, const IntRect& clipRect) {
92 0 : mCompositableHost->Composite(mCompositor, this, effectChain,
93 : GetEffectiveOpacity(),
94 : GetEffectiveTransform(),
95 : GetSamplingFilter(),
96 0 : clipRect);
97 0 : });
98 :
99 0 : mCompositableHost->BumpFlashCounter();
100 : }
101 :
102 : CompositableHost*
103 0 : CanvasLayerComposite::GetCompositableHost()
104 : {
105 0 : if (mCompositableHost && mCompositableHost->IsAttached()) {
106 0 : return mCompositableHost.get();
107 : }
108 :
109 0 : return nullptr;
110 : }
111 :
112 : void
113 0 : CanvasLayerComposite::CleanupResources()
114 : {
115 0 : if (mCompositableHost) {
116 0 : mCompositableHost->Detach(this);
117 : }
118 0 : mCompositableHost = nullptr;
119 0 : }
120 :
121 : gfx::SamplingFilter
122 0 : CanvasLayerComposite::GetSamplingFilter()
123 : {
124 0 : gfx::SamplingFilter filter = mSamplingFilter;
125 : #ifdef ANDROID
126 : // Bug 691354
127 : // Using the LINEAR filter we get unexplained artifacts.
128 : // Use NEAREST when no scaling is required.
129 : Matrix matrix;
130 : bool is2D = GetEffectiveTransform().Is2D(&matrix);
131 : if (is2D && !ThebesMatrix(matrix).HasNonTranslationOrFlip()) {
132 : filter = SamplingFilter::POINT;
133 : }
134 : #endif
135 0 : return filter;
136 : }
137 :
138 : void
139 0 : CanvasLayerComposite::GenEffectChain(EffectChain& aEffect)
140 : {
141 0 : aEffect.mLayerRef = this;
142 0 : aEffect.mPrimaryEffect = mCompositableHost->GenEffect(GetSamplingFilter());
143 0 : }
144 :
145 : void
146 0 : CanvasLayerComposite::PrintInfo(std::stringstream& aStream, const char* aPrefix)
147 : {
148 0 : CanvasLayer::PrintInfo(aStream, aPrefix);
149 0 : aStream << "\n";
150 0 : if (mCompositableHost && mCompositableHost->IsAttached()) {
151 0 : nsAutoCString pfx(aPrefix);
152 0 : pfx += " ";
153 0 : mCompositableHost->PrintInfo(aStream, pfx.get());
154 : }
155 0 : }
156 :
157 : } // namespace layers
158 9 : } // namespace mozilla
|