Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "cc/surfaces/referenced_surface_tracker.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 | |
| 11 namespace cc { | |
| 12 | |
| 13 ReferencedSurfaceTracker::ReferencedSurfaceTracker( | |
| 14 const FrameSinkId& frame_sink_id) | |
| 15 : current_surface_id_(frame_sink_id, LocalFrameId()) { | |
| 16 DCHECK(current_surface_id_.frame_sink_id().is_valid()); | |
| 17 } | |
| 18 | |
| 19 ReferencedSurfaceTracker::~ReferencedSurfaceTracker() {} | |
| 20 | |
| 21 void ReferencedSurfaceTracker::UpdateReferences( | |
| 22 const LocalFrameId& local_frame_id, | |
| 23 const std::vector<SurfaceId>& referenced_surfaces) { | |
| 24 DCHECK(local_frame_id.is_valid()); | |
| 25 | |
| 26 // Clear references to add/remove from the last frame. | |
| 27 references_to_remove_.clear(); | |
| 28 references_to_add_.clear(); | |
| 29 | |
| 30 // If |current_surface_id_| is changing then update |current_surface_id_|. | |
| 31 // Also clear |referenced_surfaces_| because we haven't added any references | |
| 32 // from the new SurfaceId yet. | |
| 33 if (current_surface_id_.local_frame_id() != local_frame_id) { | |
| 34 current_surface_id_ = | |
| 35 SurfaceId(current_surface_id_.frame_sink_id(), local_frame_id); | |
| 36 referenced_surfaces_.clear(); | |
| 37 } | |
| 38 | |
| 39 UpdateReferencedSurfaces(referenced_surfaces); | |
| 40 } | |
| 41 | |
| 42 void ReferencedSurfaceTracker::UpdateReferencedSurfaces( | |
| 43 const std::vector<SurfaceId>& new_referenced_surfaces) { | |
| 44 std::unordered_set<SurfaceId, SurfaceIdHash> new_referenced_surfaces_set; | |
| 45 | |
| 46 // Add references for each SurfaceId in |new_referenced_surfaces| if they | |
| 47 // aren't already referenced. | |
|
Fady Samuel
2017/01/18 01:08:55
Can you use std::set_difference?
kylechar
2017/01/18 15:20:15
It's possible but I don't think the right solution
| |
| 48 for (const SurfaceId& surface_id : new_referenced_surfaces) { | |
| 49 ReferenceSurface(surface_id); | |
| 50 new_referenced_surfaces_set.insert(surface_id); | |
| 51 } | |
| 52 | |
| 53 // Check for SurfaceIds that are no longer referenced and remove them. | |
| 54 for (const SurfaceId& surface_id : referenced_surfaces_) { | |
| 55 if (new_referenced_surfaces_set.count(surface_id) == 0) | |
| 56 UnreferenceSurface(surface_id); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 void ReferencedSurfaceTracker::ReferenceSurface(const SurfaceId& surface_id) { | |
| 61 // If we already have a reference to |surface_id| then return early. | |
| 62 if (referenced_surfaces_.count(surface_id) > 0) | |
| 63 return; | |
| 64 | |
| 65 // Record that surface is referenced and add a reference. | |
| 66 references_to_add_.push_back( | |
| 67 SurfaceReference(current_surface_id_, surface_id)); | |
| 68 referenced_surfaces_.insert(surface_id); | |
| 69 } | |
| 70 | |
| 71 void ReferencedSurfaceTracker::UnreferenceSurface(const SurfaceId& surface_id) { | |
| 72 auto iter = referenced_surfaces_.find(surface_id); | |
| 73 if (iter == referenced_surfaces_.end()) | |
| 74 return; | |
| 75 | |
| 76 // Remove record that surface is referenced and remove reference. | |
| 77 references_to_remove_.push_back( | |
| 78 SurfaceReference(current_surface_id_, surface_id)); | |
| 79 referenced_surfaces_.erase(iter); | |
| 80 } | |
| 81 | |
| 82 } // namespace cc | |
| OLD | NEW |