1 // Copyright 2015 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.
5 #include "chromecast/ozone/surface_factory_cast.h"
7 #include "base/callback_helpers.h"
8 #include "chromecast/ozone/cast_egl_platform.h"
9 #include "chromecast/ozone/surface_ozone_egl_cast.h"
11 namespace chromecast
{
14 SurfaceFactoryCast::SurfaceFactoryCast(scoped_ptr
<CastEglPlatform
> egl_platform
)
15 : state_(kUninitialized
),
16 destroy_window_pending_state_(kNoDestroyPending
),
19 default_display_size_(egl_platform
->GetDefaultDisplaySize()),
20 display_size_(default_display_size_
),
21 new_display_size_(default_display_size_
),
22 egl_platform_(egl_platform
.Pass()) {
25 SurfaceFactoryCast::~SurfaceFactoryCast() {
26 DestroyDisplayTypeAndWindow();
29 void SurfaceFactoryCast::InitializeHardware() {
30 if (state_
== kInitialized
) {
33 CHECK_EQ(state_
, kUninitialized
);
35 if (egl_platform_
->InitializeHardware()) {
36 state_
= kInitialized
;
43 void SurfaceFactoryCast::ShutdownHardware() {
44 DestroyDisplayTypeAndWindow();
46 egl_platform_
->ShutdownHardware();
48 state_
= kUninitialized
;
51 intptr_t SurfaceFactoryCast::GetNativeDisplay() {
52 CreateDisplayTypeAndWindowIfNeeded();
53 return reinterpret_cast<intptr_t>(display_type_
);
56 void SurfaceFactoryCast::CreateDisplayTypeAndWindowIfNeeded() {
57 if (state_
== kUninitialized
) {
60 if (new_display_size_
!= display_size_
) {
61 DestroyDisplayTypeAndWindow();
62 display_size_
= new_display_size_
;
64 DCHECK_EQ(state_
, kInitialized
);
66 display_type_
= egl_platform_
->CreateDisplayType(display_size_
);
68 window_
= egl_platform_
->CreateWindow(display_type_
, display_size_
);
70 DestroyDisplayTypeAndWindow();
72 LOG(FATAL
) << "Create EGLNativeWindowType(" << display_size_
.ToString()
77 LOG(FATAL
) << "Create EGLNativeDisplayType(" << display_size_
.ToString()
83 intptr_t SurfaceFactoryCast::GetNativeWindow() {
84 CreateDisplayTypeAndWindowIfNeeded();
88 bool SurfaceFactoryCast::ResizeDisplay(gfx::Size size
) {
89 // set size to at least 1280x720 even if passed 1x1
90 size
.SetToMax(default_display_size_
);
91 if (display_type_
&& size
!= display_size_
) {
92 DestroyDisplayTypeAndWindow();
98 void SurfaceFactoryCast::DestroyDisplayTypeAndWindow() {
100 egl_platform_
->DestroyWindow(window_
);
104 egl_platform_
->DestroyDisplayType(display_type_
);
109 scoped_ptr
<ui::SurfaceOzoneEGL
> SurfaceFactoryCast::CreateEGLSurfaceForWidget(
110 gfx::AcceleratedWidget widget
) {
111 new_display_size_
= gfx::Size(widget
>> 16, widget
& 0xFFFF);
112 new_display_size_
.SetToMax(default_display_size_
);
113 destroy_window_pending_state_
= kSurfaceExists
;
114 SendRelinquishResponse();
115 return make_scoped_ptr
<ui::SurfaceOzoneEGL
>(new SurfaceOzoneEglCast(this));
118 void SurfaceFactoryCast::SetToRelinquishDisplay(const base::Closure
& callback
) {
119 // This is called in response to a RelinquishDisplay message from the
120 // browser task. This call may come before or after the display surface
121 // is actually destroyed.
122 relinquish_display_callback_
= callback
;
123 switch (destroy_window_pending_state_
) {
124 case kNoDestroyPending
:
125 case kSurfaceDestroyedRecently
:
126 DestroyDisplayTypeAndWindow();
127 SendRelinquishResponse();
128 destroy_window_pending_state_
= kNoDestroyPending
;
131 destroy_window_pending_state_
= kWindowDestroyPending
;
133 case kWindowDestroyPending
:
140 void SurfaceFactoryCast::ChildDestroyed() {
141 if (destroy_window_pending_state_
== kWindowDestroyPending
) {
142 DestroyDisplayTypeAndWindow();
143 SendRelinquishResponse();
144 destroy_window_pending_state_
= kNoDestroyPending
;
146 destroy_window_pending_state_
= kSurfaceDestroyedRecently
;
150 void SurfaceFactoryCast::SendRelinquishResponse() {
151 if (!relinquish_display_callback_
.is_null()) {
152 base::ResetAndReturn(&relinquish_display_callback_
).Run();
156 const int32
* SurfaceFactoryCast::GetEGLSurfaceProperties(
157 const int32
* desired_list
) {
158 return egl_platform_
->GetEGLSurfaceProperties(desired_list
);
161 bool SurfaceFactoryCast::LoadEGLGLES2Bindings(
162 AddGLLibraryCallback add_gl_library
,
163 SetGLGetProcAddressProcCallback set_gl_get_proc_address
) {
164 if (state_
!= kInitialized
) {
165 InitializeHardware();
166 if (state_
!= kInitialized
) {
171 return egl_platform_
->LoadEGLGLES2Bindings(add_gl_library
,
172 set_gl_get_proc_address
);
176 } // namespace chromecast