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 "Layers.h"
8 
9 #include <inttypes.h>          // for PRIu64
10 #include <stdio.h>             // for stderr
11 #include <algorithm>           // for max, min
12 #include <list>                // for list
13 #include <set>                 // for set
14 #include <string>              // for char_traits, string, basic_string
15 #include <type_traits>         // for remove_reference<>::type
16 #include "CompositableHost.h"  // for CompositableHost
17 #include "LayerUserData.h"     // for LayerUserData
18 #include "TreeTraversal.h"  // for ForwardIterator, ForEachNode, DepthFirstSearch, TraversalFlag, TraversalFl...
19 #include "UnitTransforms.h"  // for ViewAs, PixelCastJustification, PixelCastJustification::RenderTargetIsPare...
20 #include "apz/src/AsyncPanZoomController.h"  // for AsyncPanZoomController
21 #include "gfx2DGlue.h"              // for ThebesMatrix, ToPoint, ThebesRect
22 #include "gfxEnv.h"                 // for gfxEnv
23 #include "gfxMatrix.h"              // for gfxMatrix
24 #include "gfxUtils.h"               // for gfxUtils, gfxUtils::sDumpPaintFile
25 #include "mozilla/ArrayIterator.h"  // for ArrayIterator
26 #include "mozilla/DebugOnly.h"      // for DebugOnly
27 #include "mozilla/Logging.h"  // for LogLevel, LogLevel::Debug, MOZ_LOG_TEST
28 #include "mozilla/ProfilerMarkers.h"  // for profiler_thread_is_being_profiled_for_markers, PROFILER_MARKER_TEXT
29 #include "mozilla/ScrollPositionUpdate.h"  // for ScrollPositionUpdate
30 #include "mozilla/Telemetry.h"             // for AccumulateTimeDelta
31 #include "mozilla/TelemetryHistogramEnums.h"  // for KEYPRESS_PRESENT_LATENCY, SCROLL_PRESENT_LATENCY
32 #include "mozilla/ToString.h"  // for ToString
33 #include "mozilla/gfx/2D.h"  // for SourceSurface, DrawTarget, DataSourceSurface
34 #include "mozilla/gfx/BasePoint3D.h"  // for BasePoint3D<>::(anonymous union)::(anonymous), BasePoint3D<>::(anonymous)
35 #include "mozilla/gfx/BaseRect.h"  // for operator<<, BaseRect (ptr only)
36 #include "mozilla/gfx/BaseSize.h"  // for operator<<, BaseSize<>::(anonymous union)::(anonymous), BaseSize<>::(anony...
37 #include "mozilla/gfx/Matrix.h"  // for Matrix4x4, Matrix, Matrix4x4Typed<>::(anonymous union)::(anonymous), Matri...
38 #include "mozilla/gfx/MatrixFwd.h"              // for Float
39 #include "mozilla/gfx/Polygon.h"                // for Polygon, PolygonTyped
40 #include "mozilla/layers/BSPTree.h"             // for LayerPolygon, BSPTree
41 #include "mozilla/layers/CompositableClient.h"  // for CompositableClient
42 #include "mozilla/layers/Compositor.h"          // for Compositor
43 #include "mozilla/layers/LayersMessages.h"  // for SpecificLayerAttributes, CompositorAnimations (ptr only), ContainerLayerAt...
44 #include "mozilla/layers/LayersTypes.h"  // for EventRegions, operator<<, CompositionPayload, CSSTransformMatrix, MOZ_LAYE...
45 #include "nsBaseHashtable.h"  // for nsBaseHashtable<>::Iterator, nsBaseHashtable<>::LookupResult
46 #include "nsISupportsUtils.h"  // for NS_ADDREF, NS_RELEASE
47 #include "nsPrintfCString.h"   // for nsPrintfCString
48 #include "nsRegionFwd.h"       // for IntRegion
49 #include "nsString.h"          // for nsTSubstring
50 
51 // Undo the damage done by mozzconf.h
52 #undef compress
53 #include "mozilla/Compression.h"
54 
55 namespace mozilla {
56 namespace layers {
57 
58 typedef ScrollableLayerGuid::ViewID ViewID;
59 
60 using namespace mozilla::gfx;
61 using namespace mozilla::Compression;
62 
63 #ifdef MOZ_DUMP_PAINTING
64 template <typename T>
WriteSnapshotToDumpFile_internal(T * aObj,DataSourceSurface * aSurf)65 void WriteSnapshotToDumpFile_internal(T* aObj, DataSourceSurface* aSurf) {
66   nsCString string(aObj->Name());
67   string.Append('-');
68   string.AppendInt((uint64_t)aObj);
69   if (gfxUtils::sDumpPaintFile != stderr) {
70     fprintf_stderr(gfxUtils::sDumpPaintFile, R"(array["%s"]=")",
71                    string.BeginReading());
72   }
73   gfxUtils::DumpAsDataURI(aSurf, gfxUtils::sDumpPaintFile);
74   if (gfxUtils::sDumpPaintFile != stderr) {
75     fprintf_stderr(gfxUtils::sDumpPaintFile, R"(";)");
76   }
77 }
78 
WriteSnapshotToDumpFile(Compositor * aCompositor,DrawTarget * aTarget)79 void WriteSnapshotToDumpFile(Compositor* aCompositor, DrawTarget* aTarget) {
80   RefPtr<SourceSurface> surf = aTarget->Snapshot();
81   RefPtr<DataSourceSurface> dSurf = surf->GetDataSurface();
82   WriteSnapshotToDumpFile_internal(aCompositor, dSurf);
83 }
84 #endif
85 
ToOutsideIntRect(const gfxRect & aRect)86 IntRect ToOutsideIntRect(const gfxRect& aRect) {
87   return IntRect::RoundOut(aRect.X(), aRect.Y(), aRect.Width(), aRect.Height());
88 }
89 
RecordCompositionPayloadsPresented(const TimeStamp & aCompositionEndTime,const nsTArray<CompositionPayload> & aPayloads)90 void RecordCompositionPayloadsPresented(
91     const TimeStamp& aCompositionEndTime,
92     const nsTArray<CompositionPayload>& aPayloads) {
93   if (aPayloads.Length()) {
94     TimeStamp presented = aCompositionEndTime;
95     for (const CompositionPayload& payload : aPayloads) {
96       if (profiler_thread_is_being_profiled_for_markers()) {
97         MOZ_RELEASE_ASSERT(payload.mType <= kHighestCompositionPayloadType);
98         nsAutoCString name(
99             kCompositionPayloadTypeNames[uint8_t(payload.mType)]);
100         name.AppendLiteral(" Payload Presented");
101         // This doesn't really need to be a text marker. Once we have a version
102         // of profiler_add_marker that accepts both a start time and an end
103         // time, we could use that here.
104         nsPrintfCString text(
105             "Latency: %dms",
106             int32_t((presented - payload.mTimeStamp).ToMilliseconds()));
107         PROFILER_MARKER_TEXT(
108             name, GRAPHICS,
109             MarkerTiming::Interval(payload.mTimeStamp, presented), text);
110       }
111 
112       if (payload.mType == CompositionPayloadType::eKeyPress) {
113         Telemetry::AccumulateTimeDelta(
114             mozilla::Telemetry::KEYPRESS_PRESENT_LATENCY, payload.mTimeStamp,
115             presented);
116       } else if (payload.mType == CompositionPayloadType::eAPZScroll) {
117         Telemetry::AccumulateTimeDelta(
118             mozilla::Telemetry::SCROLL_PRESENT_LATENCY, payload.mTimeStamp,
119             presented);
120       } else if (payload.mType ==
121                  CompositionPayloadType::eMouseUpFollowedByClick) {
122         Telemetry::AccumulateTimeDelta(
123             mozilla::Telemetry::MOUSEUP_FOLLOWED_BY_CLICK_PRESENT_LATENCY,
124             payload.mTimeStamp, presented);
125       }
126     }
127   }
128 }
129 
130 }  // namespace layers
131 }  // namespace mozilla
132