| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/display/display_layout.h" | 5 #include "ash/display/display_layout.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <sstream> | 8 #include <sstream> |
| 9 | 9 |
| 10 #include "ash/ash_switches.h" | 10 #include "ash/ash_switches.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 | 41 |
| 42 //////////////////////////////////////////////////////////////////////////////// | 42 //////////////////////////////////////////////////////////////////////////////// |
| 43 // DisplayPlacement | 43 // DisplayPlacement |
| 44 | 44 |
| 45 DisplayPlacement::DisplayPlacement() | 45 DisplayPlacement::DisplayPlacement() |
| 46 : display_id(gfx::Display::kInvalidDisplayID), | 46 : display_id(gfx::Display::kInvalidDisplayID), |
| 47 parent_display_id(gfx::Display::kInvalidDisplayID), | 47 parent_display_id(gfx::Display::kInvalidDisplayID), |
| 48 position(DisplayPlacement::RIGHT), | 48 position(DisplayPlacement::RIGHT), |
| 49 offset(0) {} | 49 offset(0) {} |
| 50 | 50 |
| 51 DisplayPlacement::DisplayPlacement(const DisplayPlacement& placement) | |
| 52 : display_id(placement.display_id), | |
| 53 parent_display_id(placement.parent_display_id), | |
| 54 position(placement.position), | |
| 55 offset(placement.offset) {} | |
| 56 | |
| 57 DisplayPlacement::DisplayPlacement(Position pos, int offset) | 51 DisplayPlacement::DisplayPlacement(Position pos, int offset) |
| 58 : display_id(gfx::Display::kInvalidDisplayID), | 52 : display_id(gfx::Display::kInvalidDisplayID), |
| 59 parent_display_id(gfx::Display::kInvalidDisplayID), | 53 parent_display_id(gfx::Display::kInvalidDisplayID), |
| 60 position(pos), | 54 position(pos), |
| 61 offset(offset) { | 55 offset(offset) { |
| 62 DCHECK_LE(TOP, position); | 56 DCHECK_LE(TOP, position); |
| 63 DCHECK_GE(LEFT, position); | 57 DCHECK_GE(LEFT, position); |
| 64 // Set the default value to |position| in case position is invalid. DCHECKs | 58 // Set the default value to |position| in case position is invalid. DCHECKs |
| 65 // above doesn't stop in Release builds. | 59 // above doesn't stop in Release builds. |
| 66 if (TOP > position || LEFT < position) | 60 if (TOP > position || LEFT < position) |
| 67 this->position = RIGHT; | 61 this->position = RIGHT; |
| 68 | 62 |
| 69 DCHECK_GE(kMaxValidOffset, abs(offset)); | 63 DCHECK_GE(kMaxValidOffset, abs(offset)); |
| 70 } | 64 } |
| 71 | 65 |
| 66 DisplayPlacement::DisplayPlacement(const DisplayPlacement& placement) |
| 67 : display_id(placement.display_id), |
| 68 parent_display_id(placement.parent_display_id), |
| 69 position(placement.position), |
| 70 offset(placement.offset) {} |
| 71 |
| 72 DisplayPlacement& DisplayPlacement::Swap() { | 72 DisplayPlacement& DisplayPlacement::Swap() { |
| 73 switch (position) { | 73 switch (position) { |
| 74 case TOP: | 74 case TOP: |
| 75 position = BOTTOM; | 75 position = BOTTOM; |
| 76 break; | 76 break; |
| 77 case BOTTOM: | 77 case BOTTOM: |
| 78 position = TOP; | 78 position = TOP; |
| 79 break; | 79 break; |
| 80 case RIGHT: | 80 case RIGHT: |
| 81 position = LEFT; | 81 position = LEFT; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 DCHECK(IsIdInList(layout.primary_id, list)); | 160 DCHECK(IsIdInList(layout.primary_id, list)); |
| 161 | 161 |
| 162 // Unified mode, or mirror mode switched from unified mode, | 162 // Unified mode, or mirror mode switched from unified mode, |
| 163 // may not have the placement yet. | 163 // may not have the placement yet. |
| 164 if (layout.placement_list.size() == 0u) | 164 if (layout.placement_list.size() == 0u) |
| 165 return true; | 165 return true; |
| 166 | 166 |
| 167 bool has_primary_as_parent = false; | 167 bool has_primary_as_parent = false; |
| 168 int64_t id = 0; | 168 int64_t id = 0; |
| 169 | 169 |
| 170 for (const auto* placement : layout.placement_list) { | 170 for (const auto& placement : layout.placement_list) { |
| 171 // Placements are sorted by display_id. | 171 // Placements are sorted by display_id. |
| 172 if (id >= placement->display_id) { | 172 if (id >= placement.display_id) { |
| 173 LOG(ERROR) << "PlacementList must be sorted by display_id"; | 173 LOG(ERROR) << "PlacementList must be sorted by display_id"; |
| 174 return false; | 174 return false; |
| 175 } | 175 } |
| 176 if (placement->display_id == gfx::Display::kInvalidDisplayID) { | 176 if (placement.display_id == gfx::Display::kInvalidDisplayID) { |
| 177 LOG(ERROR) << "display_id is not initialized"; | 177 LOG(ERROR) << "display_id is not initialized"; |
| 178 return false; | 178 return false; |
| 179 } | 179 } |
| 180 if (placement->parent_display_id == gfx::Display::kInvalidDisplayID) { | 180 if (placement.parent_display_id == gfx::Display::kInvalidDisplayID) { |
| 181 LOG(ERROR) << "display_parent_id is not initialized"; | 181 LOG(ERROR) << "display_parent_id is not initialized"; |
| 182 return false; | 182 return false; |
| 183 } | 183 } |
| 184 if (placement->display_id == placement->parent_display_id) { | 184 if (placement.display_id == placement.parent_display_id) { |
| 185 LOG(ERROR) << "display_id must not be same as parent_display_id"; | 185 LOG(ERROR) << "display_id must not be same as parent_display_id"; |
| 186 return false; | 186 return false; |
| 187 } | 187 } |
| 188 if (!IsIdInList(placement->display_id, list)) { | 188 if (!IsIdInList(placement.display_id, list)) { |
| 189 LOG(ERROR) << "display_id is not in the id list:" | 189 LOG(ERROR) << "display_id is not in the id list:" << placement.ToString(); |
| 190 << placement->ToString(); | |
| 191 return false; | 190 return false; |
| 192 } | 191 } |
| 193 | 192 |
| 194 if (!IsIdInList(placement->parent_display_id, list)) { | 193 if (!IsIdInList(placement.parent_display_id, list)) { |
| 195 LOG(ERROR) << "parent_display_id is not in the id list:" | 194 LOG(ERROR) << "parent_display_id is not in the id list:" |
| 196 << placement->ToString(); | 195 << placement.ToString(); |
| 197 return false; | 196 return false; |
| 198 } | 197 } |
| 199 has_primary_as_parent |= layout.primary_id == placement->parent_display_id; | 198 has_primary_as_parent |= layout.primary_id == placement.parent_display_id; |
| 200 } | 199 } |
| 201 if (!has_primary_as_parent) | 200 if (!has_primary_as_parent) |
| 202 LOG(ERROR) << "At least, one placement must have the primary as a parent."; | 201 LOG(ERROR) << "At least, one placement must have the primary as a parent."; |
| 203 return has_primary_as_parent; | 202 return has_primary_as_parent; |
| 204 } | 203 } |
| 205 | 204 |
| 206 scoped_ptr<DisplayLayout> DisplayLayout::Copy() const { | 205 scoped_ptr<DisplayLayout> DisplayLayout::Copy() const { |
| 207 scoped_ptr<DisplayLayout> copy(new DisplayLayout); | 206 scoped_ptr<DisplayLayout> copy(new DisplayLayout); |
| 208 for (auto placement : placement_list) | 207 for (const auto& placement : placement_list) |
| 209 copy->placement_list.push_back(new DisplayPlacement(*placement)); | 208 copy->placement_list.push_back(placement); |
| 210 copy->mirrored = mirrored; | 209 copy->mirrored = mirrored; |
| 211 copy->default_unified = default_unified; | 210 copy->default_unified = default_unified; |
| 212 copy->primary_id = primary_id; | 211 copy->primary_id = primary_id; |
| 213 return copy; | 212 return copy; |
| 214 } | 213 } |
| 215 | 214 |
| 216 bool DisplayLayout::HasSamePlacementList(const DisplayLayout& layout) const { | 215 bool DisplayLayout::HasSamePlacementList(const DisplayLayout& layout) const { |
| 217 if (placement_list.size() != layout.placement_list.size()) | 216 if (placement_list.size() != layout.placement_list.size()) |
| 218 return false; | 217 return false; |
| 219 for (size_t index = 0; index < placement_list.size(); index++) { | 218 for (size_t index = 0; index < placement_list.size(); index++) { |
| 220 const DisplayPlacement& placement1 = *placement_list[index]; | 219 const DisplayPlacement& placement1 = placement_list[index]; |
| 221 const DisplayPlacement& placement2 = *layout.placement_list[index]; | 220 const DisplayPlacement& placement2 = layout.placement_list[index]; |
| 222 if (placement1.position != placement2.position || | 221 if (placement1.position != placement2.position || |
| 223 placement1.offset != placement2.offset || | 222 placement1.offset != placement2.offset || |
| 224 placement1.display_id != placement2.display_id || | 223 placement1.display_id != placement2.display_id || |
| 225 placement1.parent_display_id != placement2.parent_display_id) { | 224 placement1.parent_display_id != placement2.parent_display_id) { |
| 226 return false; | 225 return false; |
| 227 } | 226 } |
| 228 } | 227 } |
| 229 return true; | 228 return true; |
| 230 } | 229 } |
| 231 | 230 |
| 232 std::string DisplayLayout::ToString() const { | 231 std::string DisplayLayout::ToString() const { |
| 233 std::stringstream s; | 232 std::stringstream s; |
| 234 s << "primary=" << primary_id; | 233 s << "primary=" << primary_id; |
| 235 if (mirrored) | 234 if (mirrored) |
| 236 s << ", mirrored"; | 235 s << ", mirrored"; |
| 237 if (default_unified) | 236 if (default_unified) |
| 238 s << ", default_unified"; | 237 s << ", default_unified"; |
| 239 bool added = false; | 238 bool added = false; |
| 240 for (const auto* placement : placement_list) { | 239 for (const auto& placement : placement_list) { |
| 241 s << (added ? "),(" : " [("); | 240 s << (added ? "),(" : " [("); |
| 242 s << placement->ToString(); | 241 s << placement.ToString(); |
| 243 added = true; | 242 added = true; |
| 244 } | 243 } |
| 245 if (added) | 244 if (added) |
| 246 s << ")]"; | 245 s << ")]"; |
| 247 return s.str(); | 246 return s.str(); |
| 248 } | 247 } |
| 249 | 248 |
| 250 const DisplayPlacement* DisplayLayout::FindPlacementById( | 249 DisplayPlacement DisplayLayout::FindPlacementById(int64_t display_id) const { |
| 251 int64_t display_id) const { | |
| 252 const auto iter = | 250 const auto iter = |
| 253 std::find_if(placement_list.begin(), placement_list.end(), | 251 std::find_if(placement_list.begin(), placement_list.end(), |
| 254 [display_id](const DisplayPlacement* placement) { | 252 [display_id](const DisplayPlacement& placement) { |
| 255 return placement->display_id == display_id; | 253 return placement.display_id == display_id; |
| 256 }); | 254 }); |
| 257 return (iter == placement_list.end()) ? nullptr : *iter; | 255 return (iter == placement_list.end()) ? DisplayPlacement() |
| 256 : DisplayPlacement(*iter); |
| 258 } | 257 } |
| 259 | 258 |
| 260 } // namespace ash | 259 } // namespace ash |
| OLD | NEW |