summaryrefslogtreecommitdiffstats
path: root/gfx/layers/composite/PaintCounter.cpp
blob: fbf155211677a86f80ee4f67b6a9589874ae5c31 (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
/* -*- 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 "mozilla/gfx/Point.h"          // for IntSize, Point
#include "mozilla/gfx/Rect.h"           // for Rect
#include "mozilla/gfx/Types.h"          // for Color, SurfaceFormat
#include "mozilla/layers/Compositor.h"  // for Compositor
#include "mozilla/layers/CompositorTypes.h"
#include "mozilla/layers/Effects.h"  // for Effect, EffectChain, etc
#include "mozilla/TimeStamp.h"       // for TimeStamp, TimeDuration
#include "mozilla/Sprintf.h"

#include "mozilla/gfx/HelpersSkia.h"
#include "skia/include/core/SkFont.h"
#include "PaintCounter.h"

namespace mozilla {
namespace layers {

using namespace mozilla::gfx;

// Positioned below the chrome UI
IntRect PaintCounter::mRect = IntRect(0, 175, 300, 60);

PaintCounter::PaintCounter() {
  mFormat = SurfaceFormat::B8G8R8A8;
  mSurface = Factory::CreateDataSourceSurface(mRect.Size(), mFormat);
  mMap.emplace(mSurface, DataSourceSurface::READ_WRITE);
  mStride = mMap->GetStride();

  mCanvas = SkCanvas::MakeRasterDirect(MakeSkiaImageInfo(mRect.Size(), mFormat),
                                       mMap->GetData(), mStride);
  mCanvas->clear(SK_ColorWHITE);
}

PaintCounter::~PaintCounter() {
  mSurface = nullptr;
  mTextureSource = nullptr;
  mTexturedEffect = nullptr;
}

void PaintCounter::Draw(Compositor* aCompositor, TimeDuration aPaintTime,
                        TimeDuration aCompositeTime) {
  char buffer[48];
  SprintfLiteral(buffer, "P: %.2f  C: %.2f", aPaintTime.ToMilliseconds(),
                 aCompositeTime.ToMilliseconds());

  SkPaint paint;
  paint.setColor(SkColorSetRGB(0, 255, 0));
  paint.setAntiAlias(true);

  SkFont font(SkTypeface::MakeDefault(), 32);

  mCanvas->clear(SK_ColorTRANSPARENT);
  mCanvas->drawString(buffer, 10, 30, font, paint);
  mCanvas->flush();

  if (!mTextureSource) {
    mTextureSource = aCompositor->CreateDataTextureSource();
    mTexturedEffect = CreateTexturedEffect(mFormat, mTextureSource,
                                           SamplingFilter::POINT, true);
    mTexturedEffect->mTextureCoords = Rect(0, 0, 1.0f, 1.0f);
  }

  mTextureSource->Update(mSurface);

  EffectChain effectChain;
  effectChain.mPrimaryEffect = mTexturedEffect;

  gfx::Matrix4x4 identity;
  Rect rect(mRect.X(), mRect.Y(), mRect.Width(), mRect.Height());
  aCompositor->DrawQuad(rect, mRect, effectChain, 1.0, identity);
}

}  // end namespace layers
}  // end namespace mozilla