summaryrefslogtreecommitdiffstats
path: root/gfx/layers/composite/PaintedLayerComposite.cpp
blob: fe8028d12fe58a195fe660f133646128f70eb6e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "PaintedLayerComposite.h"
#include "CompositableHost.h"            // for TiledLayerProperties, etc
#include "FrameMetrics.h"                // for FrameMetrics
#include "Units.h"                       // for CSSRect, LayerPixel, etc
#include "gfxEnv.h"                      // for gfxEnv
#include "mozilla/Assertions.h"          // for MOZ_ASSERT, etc
#include "mozilla/gfx/Matrix.h"          // for Matrix4x4
#include "mozilla/gfx/Point.h"           // for Point
#include "mozilla/gfx/Polygon.h"         // for Polygon
#include "mozilla/gfx/Rect.h"            // for RoundedToInt, Rect
#include "mozilla/gfx/Types.h"           // for SamplingFilter::LINEAR
#include "mozilla/layers/Compositor.h"   // for Compositor
#include "mozilla/layers/ContentHost.h"  // for ContentHost
#include "mozilla/layers/Effects.h"      // for EffectChain
#include "mozilla/layers/LayerManagerCompositeUtils.h"
#include "mozilla/mozalloc.h"  // for operator delete
#include "nsAString.h"
#include "mozilla/RefPtr.h"   // for nsRefPtr
#include "nsISupportsImpl.h"  // for MOZ_COUNT_CTOR, etc
#include "nsMathUtils.h"      // for NS_lround
#include "nsString.h"         // for nsAutoCString
#include "TextRenderer.h"
#include "GeckoProfiler.h"

namespace mozilla {
namespace layers {

PaintedLayerComposite::PaintedLayerComposite(LayerManagerComposite* aManager)
    : PaintedLayer(aManager, nullptr),
      LayerComposite(aManager),
      mBuffer(nullptr) {
  MOZ_COUNT_CTOR(PaintedLayerComposite);
  mImplData = static_cast<LayerComposite*>(this);
}

PaintedLayerComposite::~PaintedLayerComposite() {
  MOZ_COUNT_DTOR(PaintedLayerComposite);
  CleanupResources();
}

bool PaintedLayerComposite::SetCompositableHost(CompositableHost* aHost) {
  switch (aHost->GetType()) {
    case CompositableType::CONTENT_TILED:
    case CompositableType::CONTENT_SINGLE:
    case CompositableType::CONTENT_DOUBLE: {
      ContentHost* newBuffer = static_cast<ContentHost*>(aHost);
      if (mBuffer && newBuffer != mBuffer) {
        mBuffer->Detach(this);
      }
      mBuffer = newBuffer;
      return true;
    }
    default:
      return false;
  }
}

void PaintedLayerComposite::Disconnect() { Destroy(); }

void PaintedLayerComposite::Destroy() {
  if (!mDestroyed) {
    CleanupResources();
    mDestroyed = true;
  }
}

Layer* PaintedLayerComposite::GetLayer() { return this; }

void PaintedLayerComposite::SetLayerManager(HostLayerManager* aManager) {
  LayerComposite::SetLayerManager(aManager);
  mManager = aManager;
  if (mBuffer && mCompositor) {
    mBuffer->SetTextureSourceProvider(mCompositor);
  }
}

void PaintedLayerComposite::RenderLayer(const gfx::IntRect& aClipRect,
                                        const Maybe<gfx::Polygon>& aGeometry) {
  if (!mBuffer || !mBuffer->IsAttached()) {
    return;
  }
  AUTO_PROFILER_LABEL("PaintedLayerComposite::RenderLayer", GRAPHICS);

  Compositor* compositor = mCompositeManager->GetCompositor();

  MOZ_ASSERT(mBuffer->GetTextureSourceProvider() == compositor &&
                 mBuffer->GetLayer() == this,
             "buffer is corrupted");

  const nsIntRegion visibleRegion = GetLocalVisibleRegion().ToUnknownRegion();

#ifdef MOZ_DUMP_PAINTING
  if (gfxEnv::DumpCompositorTextures()) {
    RefPtr<gfx::DataSourceSurface> surf = mBuffer->GetAsSurface();
    if (surf) {
      WriteSnapshotToDumpFile(this, surf);
    }
  }
#endif

  RenderWithAllMasks(
      this, compositor, aClipRect,
      [&](EffectChain& effectChain, const gfx::IntRect& clipRect) {
        mBuffer->SetPaintWillResample(MayResample());

        mBuffer->Composite(compositor, this, effectChain, GetEffectiveOpacity(),
                           GetEffectiveTransform(), GetSamplingFilter(),
                           clipRect, &visibleRegion, aGeometry);
      });

  mBuffer->BumpFlashCounter();

  compositor->MakeCurrent();
}

CompositableHost* PaintedLayerComposite::GetCompositableHost() {
  if (mBuffer && mBuffer->IsAttached()) {
    return mBuffer.get();
  }

  return nullptr;
}

void PaintedLayerComposite::CleanupResources() {
  if (mBuffer) {
    mBuffer->Detach(this);
  }
  mBuffer = nullptr;
}

bool PaintedLayerComposite::IsOpaque() {
  if (!mBuffer || !mBuffer->IsAttached()) {
    return false;
  }
  return PaintedLayer::IsOpaque();
}

void PaintedLayerComposite::GenEffectChain(EffectChain& aEffect) {
  aEffect.mLayerRef = this;
  aEffect.mPrimaryEffect = mBuffer->GenEffect(GetSamplingFilter());
}

void PaintedLayerComposite::PrintInfo(std::stringstream& aStream,
                                      const char* aPrefix) {
  PaintedLayer::PrintInfo(aStream, aPrefix);
  if (mBuffer && mBuffer->IsAttached()) {
    aStream << "\n";
    nsAutoCString pfx(aPrefix);
    pfx += "  ";
    mBuffer->PrintInfo(aStream, pfx.get());
  }
}

const gfx::TiledIntRegion& PaintedLayerComposite::GetInvalidRegion() {
  if (mBuffer) {
    nsIntRegion region = mInvalidRegion.GetRegion();
    mBuffer->AddAnimationInvalidation(region);
  }
  return mInvalidRegion;
}

}  // namespace layers
}  // namespace mozilla