Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(785)

Side by Side Diff: ui/ozone/ozone_platform.cc

Issue 291473002: ozone: Initialize a subsystem only if necessary. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/ozone/ozone_platform.h ('k') | ui/ozone/ozone_platform_list.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 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 "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/debug/trace_event.h" 6 #include "base/debug/trace_event.h"
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "ui/base/cursor/ozone/cursor_factory_ozone.h" 8 #include "ui/base/cursor/ozone/cursor_factory_ozone.h"
9 #include "ui/events/ozone/event_factory_ozone.h" 9 #include "ui/events/ozone/event_factory_ozone.h"
10 #include "ui/gfx/ozone/surface_factory_ozone.h" 10 #include "ui/gfx/ozone/surface_factory_ozone.h"
11 #include "ui/ozone/ime/input_method_context_factory_ozone.h" 11 #include "ui/ozone/ime/input_method_context_factory_ozone.h"
12 #include "ui/ozone/ozone_platform.h" 12 #include "ui/ozone/ozone_platform.h"
13 #include "ui/ozone/ozone_platform_list.h" 13 #include "ui/ozone/ozone_platform_list.h"
14 #include "ui/ozone/ozone_switches.h" 14 #include "ui/ozone/ozone_switches.h"
15 15
16 namespace ui { 16 namespace ui {
17 17
18 namespace { 18 namespace {
19 19
20 // Helper to construct an OzonePlatform by name using the platform list. 20 // Helper to construct an OzonePlatform by name using the platform list.
21 OzonePlatform* CreatePlatform(const std::string& platform_name) { 21 OzonePlatform* CreatePlatform(const std::string& platform_name,
22 bool for_gpu) {
22 // Search for a matching platform in the list. 23 // Search for a matching platform in the list.
23 for (int i = 0; i < kOzonePlatformCount; ++i) 24 for (int i = 0; i < kOzonePlatformCount; ++i)
24 if (platform_name == kOzonePlatforms[i].name) 25 if (platform_name == kOzonePlatforms[i].name) {
25 return kOzonePlatforms[i].constructor(); 26 return for_gpu ? kOzonePlatforms[i].ConstructorForGPU()
27 : kOzonePlatforms[i].ConstructorForUI();
28 }
26 29
27 LOG(FATAL) << "Invalid ozone platform: " << platform_name; 30 LOG(FATAL) << "Invalid ozone platform: " << platform_name;
28 return NULL; // not reached 31 return NULL; // not reached
29 } 32 }
30 33
31 // Returns the name of the platform to use (value of --ozone-platform flag). 34 // Returns the name of the platform to use (value of --ozone-platform flag).
32 std::string GetPlatformName() { 35 std::string GetPlatformName() {
33 // The first platform is the default. 36 // The first platform is the default.
34 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kOzonePlatform) && 37 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kOzonePlatform) &&
35 kOzonePlatformCount > 0) 38 kOzonePlatformCount > 0)
36 return kOzonePlatforms[0].name; 39 return kOzonePlatforms[0].name;
37 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 40 return CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
38 switches::kOzonePlatform); 41 switches::kOzonePlatform);
39 } 42 }
40 43
41 } // namespace 44 } // namespace
42 45
43 OzonePlatform::OzonePlatform() {} 46 OzonePlatform::OzonePlatform() {
47 CHECK(!instance_) << "There should only be a single OzonePlatform.";
48 instance_ = this;
49 }
44 50
45 OzonePlatform::~OzonePlatform() { 51 OzonePlatform::~OzonePlatform() {
46 gfx::SurfaceFactoryOzone::SetInstance(NULL); 52 CHECK_EQ(instance_, this);
47 ui::EventFactoryOzone::SetInstance(NULL); 53 instance_ = NULL;
48 ui::CursorFactoryOzone::SetInstance(NULL);
49 } 54 }
50 55
51 // static 56 // static
52 void OzonePlatform::Initialize() { 57 void OzonePlatform::InitializeForUI() {
53 if (instance_) 58 if (instance_)
54 return; 59 return;
55 60
56 std::string platform = GetPlatformName(); 61 std::string platform = GetPlatformName();
57 62 TRACE_EVENT1("ozone", "OzonePlatform::InitializeForUI", "platform", platform);
58 TRACE_EVENT1("ozone", "OzonePlatform::Initialize", "platform", platform); 63 CreatePlatform(platform, false);
59
60 instance_ = CreatePlatform(platform);
61
62 // Inject ozone interfaces.
63 gfx::SurfaceFactoryOzone::SetInstance(instance_->GetSurfaceFactoryOzone());
64 ui::EventFactoryOzone::SetInstance(instance_->GetEventFactoryOzone());
65 ui::InputMethodContextFactoryOzone::SetInstance( 64 ui::InputMethodContextFactoryOzone::SetInstance(
kalyank 2014/05/15 19:21:45 shouldn't we do the same with this. It should be s
sadrul 2014/05/15 19:27:24 I wanted to, but the SetInstance() here is for the
66 instance_->GetInputMethodContextFactoryOzone()); 65 instance_->GetInputMethodContextFactoryOzone());
67 ui::CursorFactoryOzone::SetInstance(instance_->GetCursorFactoryOzone());
68 } 66 }
69 67
70 // static 68 // static
69 void OzonePlatform::InitializeForGPU() {
70 if (instance_)
71 return;
72
73 std::string platform = GetPlatformName();
74 TRACE_EVENT1("ozone", "OzonePlatform::InitializeForGPU",
75 "platform", platform);
76 CreatePlatform(platform, true);
77 }
78
79 // static
71 OzonePlatform* OzonePlatform::GetInstance() { 80 OzonePlatform* OzonePlatform::GetInstance() {
72 CHECK(instance_) << "OzonePlatform is not initialized"; 81 CHECK(instance_) << "OzonePlatform is not initialized";
73 return instance_; 82 return instance_;
74 } 83 }
75 84
76 // static 85 // static
77 OzonePlatform* OzonePlatform::instance_; 86 OzonePlatform* OzonePlatform::instance_;
78 87
79 } // namespace ui 88 } // namespace ui
OLDNEW
« no previous file with comments | « ui/ozone/ozone_platform.h ('k') | ui/ozone/ozone_platform_list.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698