1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "mozilla/gfx/Point.h"          // for IntSize, Point
8 #include "mozilla/gfx/Rect.h"           // for Rect
9 #include "mozilla/gfx/Types.h"          // for Color, SurfaceFormat
10 #include "mozilla/layers/Compositor.h"  // for Compositor
11 #include "mozilla/layers/CompositorTypes.h"
12 #include "mozilla/layers/Effects.h"  // for Effect, EffectChain, etc
13 #include "mozilla/TimeStamp.h"       // for TimeStamp, TimeDuration
14 #include "mozilla/Sprintf.h"
15 
16 #include "mozilla/gfx/HelpersSkia.h"
17 #include "skia/include/core/SkFont.h"
18 #include "PaintCounter.h"
19 
20 namespace mozilla {
21 namespace layers {
22 
23 using namespace mozilla::gfx;
24 
25 // Positioned below the chrome UI
26 IntRect PaintCounter::mRect = IntRect(0, 175, 300, 60);
27 
PaintCounter()28 PaintCounter::PaintCounter() {
29   mFormat = SurfaceFormat::B8G8R8A8;
30   mSurface = Factory::CreateDataSourceSurface(mRect.Size(), mFormat);
31   mMap.emplace(mSurface, DataSourceSurface::READ_WRITE);
32   mStride = mMap->GetStride();
33 
34   mCanvas = SkCanvas::MakeRasterDirect(MakeSkiaImageInfo(mRect.Size(), mFormat),
35                                        mMap->GetData(), mStride);
36   mCanvas->clear(SK_ColorWHITE);
37 }
38 
~PaintCounter()39 PaintCounter::~PaintCounter() {
40   mSurface = nullptr;
41   mTextureSource = nullptr;
42   mTexturedEffect = nullptr;
43 }
44 
Draw(Compositor * aCompositor,TimeDuration aPaintTime,TimeDuration aCompositeTime)45 void PaintCounter::Draw(Compositor* aCompositor, TimeDuration aPaintTime,
46                         TimeDuration aCompositeTime) {
47   char buffer[48];
48   SprintfLiteral(buffer, "P: %.2f  C: %.2f", aPaintTime.ToMilliseconds(),
49                  aCompositeTime.ToMilliseconds());
50 
51   SkPaint paint;
52   paint.setColor(SkColorSetRGB(0, 255, 0));
53   paint.setAntiAlias(true);
54 
55   SkFont font(SkTypeface::MakeDefault(), 32);
56 
57   mCanvas->clear(SK_ColorTRANSPARENT);
58   mCanvas->drawString(buffer, 10, 30, font, paint);
59   mCanvas->flush();
60 
61   if (!mTextureSource) {
62     mTextureSource = aCompositor->CreateDataTextureSource();
63     mTexturedEffect = CreateTexturedEffect(mFormat, mTextureSource,
64                                            SamplingFilter::POINT, true);
65     mTexturedEffect->mTextureCoords = Rect(0, 0, 1.0f, 1.0f);
66   }
67 
68   mTextureSource->Update(mSurface);
69 
70   EffectChain effectChain;
71   effectChain.mPrimaryEffect = mTexturedEffect;
72 
73   gfx::Matrix4x4 identity;
74   Rect rect(mRect.X(), mRect.Y(), mRect.Width(), mRect.Height());
75   aCompositor->DrawQuad(rect, mRect, effectChain, 1.0, identity);
76 }
77 
78 }  // end namespace layers
79 }  // end namespace mozilla
80