summaryrefslogtreecommitdiffstats
path: root/layout/painting/WindowRenderer.cpp
blob: d695ef8ebae4e2067ab1c9fdee40eae7c5e26675 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* -*- 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 "WindowRenderer.h"

#include "gfxPlatform.h"
#include "mozilla/dom/Animation.h"  // for Animation
#include "mozilla/dom/AnimationEffect.h"
#include "mozilla/EffectSet.h"
#include "mozilla/layers/PersistentBufferProvider.h"  // for PersistentBufferProviderBasic, PersistentBufferProvider (ptr only)
#include "nsDisplayList.h"

using namespace mozilla::gfx;
using namespace mozilla::layers;

namespace mozilla {

/**
 * StartFrameTimeRecording, together with StopFrameTimeRecording
 * enable recording of frame intervals.
 *
 * To allow concurrent consumers, a cyclic array is used which serves all
 * consumers, practically stateless with regard to consumers.
 *
 * To save resources, the buffer is allocated on first call to
 * StartFrameTimeRecording and recording is paused if no consumer which called
 * StartFrameTimeRecording is able to get valid results (because the cyclic
 * buffer was overwritten since that call).
 *
 * To determine availability of the data upon StopFrameTimeRecording:
 * - mRecording.mNextIndex increases on each RecordFrame, and never resets.
 * - Cyclic buffer position is realized as mNextIndex % bufferSize.
 * - StartFrameTimeRecording returns mNextIndex. When StopFrameTimeRecording is
 *   called, the required start index is passed as an arg, and we're able to
 *   calculate the required length. If this length is bigger than bufferSize, it
 *   means data was overwritten.  otherwise, we can return the entire sequence.
 * - To determine if we need to pause, mLatestStartIndex is updated to
 *   mNextIndex on each call to StartFrameTimeRecording. If this index gets
 *   overwritten, it means that all earlier start indices obtained via
 *   StartFrameTimeRecording were also overwritten, hence, no point in
 *   recording, so pause.
 * - mCurrentRunStartIndex indicates the oldest index of the recording after
 *   which the recording was not paused. If StopFrameTimeRecording is invoked
 *   with a start index older than this, it means that some frames were not
 *   recorded, so data is invalid.
 */
uint32_t FrameRecorder::StartFrameTimeRecording(int32_t aBufferSize) {
  if (mRecording.mIsPaused) {
    mRecording.mIsPaused = false;

    if (!mRecording.mIntervals.Length()) {  // Initialize recording buffers
      mRecording.mIntervals.SetLength(aBufferSize);
    }

    // After being paused, recent values got invalid. Update them to now.
    mRecording.mLastFrameTime = TimeStamp::Now();

    // Any recording which started before this is invalid, since we were paused.
    mRecording.mCurrentRunStartIndex = mRecording.mNextIndex;
  }

  // If we'll overwrite this index, there are no more consumers with aStartIndex
  // for which we're able to provide the full recording, so no point in keep
  // recording.
  mRecording.mLatestStartIndex = mRecording.mNextIndex;
  return mRecording.mNextIndex;
}

void FrameRecorder::RecordFrame() {
  if (!mRecording.mIsPaused) {
    TimeStamp now = TimeStamp::Now();
    uint32_t i = mRecording.mNextIndex % mRecording.mIntervals.Length();
    mRecording.mIntervals[i] =
        static_cast<float>((now - mRecording.mLastFrameTime).ToMilliseconds());
    mRecording.mNextIndex++;
    mRecording.mLastFrameTime = now;

    if (mRecording.mNextIndex >
        (mRecording.mLatestStartIndex + mRecording.mIntervals.Length())) {
      // We've just overwritten the most recent recording start -> pause.
      mRecording.mIsPaused = true;
    }
  }
}

void FrameRecorder::StopFrameTimeRecording(uint32_t aStartIndex,
                                           nsTArray<float>& aFrameIntervals) {
  uint32_t bufferSize = mRecording.mIntervals.Length();
  uint32_t length = mRecording.mNextIndex - aStartIndex;
  if (mRecording.mIsPaused || length > bufferSize ||
      aStartIndex < mRecording.mCurrentRunStartIndex) {
    // aStartIndex is too old. Also if aStartIndex was issued before
    // mRecordingNextIndex overflowed (uint32_t)
    //   and stopped after the overflow (would happen once every 828 days of
    //   constant 60fps).
    length = 0;
  }

  if (!length) {
    aFrameIntervals.Clear();
    return;  // empty recording, return empty arrays.
  }
  // Set length in advance to avoid possibly repeated reallocations
  aFrameIntervals.SetLength(length);

  uint32_t cyclicPos = aStartIndex % bufferSize;
  for (uint32_t i = 0; i < length; i++, cyclicPos++) {
    if (cyclicPos == bufferSize) {
      cyclicPos = 0;
    }
    aFrameIntervals[i] = mRecording.mIntervals[cyclicPos];
  }
}

already_AddRefed<PersistentBufferProvider>
WindowRenderer::CreatePersistentBufferProvider(
    const mozilla::gfx::IntSize& aSize, mozilla::gfx::SurfaceFormat aFormat) {
  RefPtr<PersistentBufferProviderBasic> bufferProvider;
  // If we are using remote canvas we don't want to use acceleration in
  // non-remote layer managers, so we always use the fallback software one.
  if (!gfxPlatform::UseRemoteCanvas() ||
      !gfxPlatform::IsBackendAccelerated(
          gfxPlatform::GetPlatform()->GetPreferredCanvasBackend())) {
    bufferProvider = PersistentBufferProviderBasic::Create(
        aSize, aFormat,
        gfxPlatform::GetPlatform()->GetPreferredCanvasBackend());
  }

  if (!bufferProvider) {
    bufferProvider = PersistentBufferProviderBasic::Create(
        aSize, aFormat, gfxPlatform::GetPlatform()->GetFallbackCanvasBackend());
  }

  return bufferProvider.forget();
}

void WindowRenderer::AddPartialPrerenderedAnimation(
    uint64_t aCompositorAnimationId, dom::Animation* aAnimation) {
  mPartialPrerenderedAnimations.InsertOrUpdate(aCompositorAnimationId,
                                               RefPtr{aAnimation});
  aAnimation->SetPartialPrerendered(aCompositorAnimationId);
}
void WindowRenderer::RemovePartialPrerenderedAnimation(
    uint64_t aCompositorAnimationId, dom::Animation* aAnimation) {
  MOZ_ASSERT(aAnimation);
#ifdef DEBUG
  RefPtr<dom::Animation> animation;
  if (mPartialPrerenderedAnimations.Remove(aCompositorAnimationId,
                                           getter_AddRefs(animation)) &&
      // It may be possible that either animation's effect has already been
      // nulled out via Animation::SetEffect() so ignore such cases.
      aAnimation->GetEffect() && aAnimation->GetEffect()->AsKeyframeEffect() &&
      animation->GetEffect() && animation->GetEffect()->AsKeyframeEffect()) {
    MOZ_ASSERT(
        EffectSet::GetForEffect(aAnimation->GetEffect()->AsKeyframeEffect()) ==
        EffectSet::GetForEffect(animation->GetEffect()->AsKeyframeEffect()));
  }
#else
  mPartialPrerenderedAnimations.Remove(aCompositorAnimationId);
#endif
  aAnimation->ResetPartialPrerendered();
}
void WindowRenderer::UpdatePartialPrerenderedAnimations(
    const nsTArray<uint64_t>& aJankedAnimations) {
  for (uint64_t id : aJankedAnimations) {
    RefPtr<dom::Animation> animation;
    if (mPartialPrerenderedAnimations.Remove(id, getter_AddRefs(animation))) {
      animation->UpdatePartialPrerendered();
    }
  }
}

void FallbackRenderer::SetTarget(gfxContext* aTarget,
                                 layers::BufferMode aDoubleBuffering) {
  mTarget = aTarget;
  mBufferMode = aDoubleBuffering;
}

bool FallbackRenderer::BeginTransaction(const nsCString& aURL) {
  if (!mTarget) {
    return false;
  }

  return true;
}

void FallbackRenderer::EndTransactionWithColor(const nsIntRect& aRect,
                                               const gfx::DeviceColor& aColor) {
  mTarget->GetDrawTarget()->FillRect(Rect(aRect), ColorPattern(aColor));
  mAnimationReadyTime = TimeStamp::Now();
}

void FallbackRenderer::EndTransactionWithList(nsDisplayListBuilder* aBuilder,
                                              nsDisplayList* aList,
                                              int32_t aAppUnitsPerDevPixel,
                                              EndTransactionFlags aFlags) {
  if (aFlags & EndTransactionFlags::END_NO_COMPOSITE) {
    return;
  }

  DrawTarget* dt = mTarget->GetDrawTarget();

  BackendType backend = gfxPlatform::GetPlatform()->GetContentBackendFor(
      LayersBackend::LAYERS_NONE);
  RefPtr<DrawTarget> dest =
      gfxPlatform::GetPlatform()->CreateDrawTargetForBackend(
          backend, dt->GetSize(), dt->GetFormat());
  if (dest) {
    gfxContext ctx(dest, /* aPreserveTransform */ true);

    nsRegion opaque = aList->GetOpaqueRegion(aBuilder);
    if (opaque.Contains(aList->GetComponentAlphaBounds(aBuilder))) {
      dest->SetPermitSubpixelAA(true);
    }

    aList->Paint(aBuilder, &ctx, aAppUnitsPerDevPixel);

    RefPtr<SourceSurface> snapshot = dest->Snapshot();
    dt->DrawSurface(snapshot, Rect(dest->GetRect()), Rect(dest->GetRect()),
                    DrawSurfaceOptions(),
                    DrawOptions(1.0f, CompositionOp::OP_SOURCE));
  }
  mAnimationReadyTime = TimeStamp::Now();
}

}  // namespace mozilla