File: xr_plane_manager.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (96 lines) | stat: -rw-r--r-- 3,435 bytes parent folder | download | duplicates (5)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/xr/xr_plane_manager.h"

#include "base/containers/contains.h"
#include "base/trace_event/trace_event.h"
#include "third_party/blink/renderer/modules/xr/xr_plane.h"
#include "third_party/blink/renderer/modules/xr/xr_plane_set.h"

namespace blink {

XRPlaneManager::XRPlaneManager(base::PassKey<XRSession> pass_key,
                               XRSession* session)
    : session_(session) {}

void XRPlaneManager::ProcessPlaneInformation(
    const device::mojom::blink::XRPlaneDetectionData* detected_planes_data,
    double timestamp) {
  TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("xr.debug"),
               "ProcessPlaneInformation");

  if (!detected_planes_data) {
    DVLOG(3) << __func__ << ": detected_planes_data is null";

    // We have received a nullopt - plane detection is not supported or
    // disabled. Clear stored planes (if any).
    // The device can send either null or empty data - in both cases, it means
    // that there are no planes available.
    plane_ids_to_planes_.clear();
    return;
  }

  TRACE_COUNTER2("xr", "Plane statistics", "All planes",
                 detected_planes_data->all_planes_ids.size(), "Updated planes",
                 detected_planes_data->updated_planes_data.size());

  DVLOG(3) << __func__ << ": updated planes size="
           << detected_planes_data->updated_planes_data.size()
           << ", all planes size="
           << detected_planes_data->all_planes_ids.size();

  HeapHashMap<uint64_t, Member<XRPlane>> updated_planes;

  // First, process all planes that had their information updated (new planes
  // are also processed here).
  for (const auto& plane : detected_planes_data->updated_planes_data) {
    DCHECK(plane);

    auto it = plane_ids_to_planes_.find(plane->id);
    if (it != plane_ids_to_planes_.end()) {
      updated_planes.insert(plane->id, it->value);
      it->value->Update(*plane, timestamp);
    } else {
      updated_planes.insert(
          plane->id, MakeGarbageCollected<XRPlane>(plane->id, session_, *plane,
                                                   timestamp));
    }
  }

  // Then, copy over the planes that were not updated but are still present.
  for (const auto& plane_id : detected_planes_data->all_planes_ids) {
    // If the plane was already updated, there is nothing to do as it was
    // already moved to |updated_planes|. If it's not updated, just copy it over
    // as-is.
    if (!base::Contains(updated_planes, plane_id)) {
      auto it = plane_ids_to_planes_.find(plane_id);
      CHECK(it != plane_ids_to_planes_.end());
      updated_planes.insert(plane_id, it->value);
    }
  }

  plane_ids_to_planes_.swap(updated_planes);
}

XRPlaneSet* XRPlaneManager::GetDetectedPlanes() const {
  if (!session_->IsFeatureEnabled(
          device::mojom::XRSessionFeature::PLANE_DETECTION)) {
    return MakeGarbageCollected<XRPlaneSet>(HeapHashSet<Member<XRPlane>>{});
  }

  HeapHashSet<Member<XRPlane>> result;
  for (auto& plane_id_and_plane : plane_ids_to_planes_) {
    result.insert(plane_id_and_plane.value);
  }

  return MakeGarbageCollected<XRPlaneSet>(result);
}

void XRPlaneManager::Trace(Visitor* visitor) const {
  visitor->Trace(session_);
  visitor->Trace(plane_ids_to_planes_);
}

}  // namespace blink