OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "ui/gfx/gl/gl_surface.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "third_party/mesa/MesaLib/include/GL/osmesa.h" | |
10 #include "ui/gfx/gl/gl_bindings.h" | |
11 #include "ui/gfx/gl/gl_implementation.h" | |
12 #include "ui/gfx/gl/gl_surface_egl.h" | |
13 #include "ui/gfx/gl/gl_surface_osmesa.h" | |
14 #include "ui/gfx/gl/gl_surface_stub.h" | |
15 #include "ui/gfx/gl/gl_surface_wgl.h" | |
16 | |
17 namespace gfx { | |
18 | |
19 // This OSMesa GL surface can use GDI to swap the contents of the buffer to a | |
20 // view. | |
21 class NativeViewGLSurfaceOSMesa : public GLSurfaceOSMesa { | |
22 public: | |
23 explicit NativeViewGLSurfaceOSMesa(gfx::AcceleratedWidget window); | |
24 virtual ~NativeViewGLSurfaceOSMesa(); | |
25 | |
26 // Implement subset of GLSurface. | |
27 virtual bool Initialize() OVERRIDE; | |
28 virtual void Destroy() OVERRIDE; | |
29 virtual bool IsOffscreen() OVERRIDE; | |
30 virtual bool SwapBuffers() OVERRIDE; | |
31 virtual std::string GetExtensions() OVERRIDE; | |
32 virtual bool PostSubBuffer(int x, int y, int width, int height) OVERRIDE; | |
33 | |
34 private: | |
35 gfx::AcceleratedWidget window_; | |
36 HDC device_context_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(NativeViewGLSurfaceOSMesa); | |
39 }; | |
40 | |
41 // Helper routine that does one-off initialization like determining the | |
42 // pixel format and initializing the GL bindings. | |
43 bool GLSurface::InitializeOneOffInternal() { | |
44 switch (GetGLImplementation()) { | |
45 case kGLImplementationDesktopGL: | |
46 if (!GLSurfaceWGL::InitializeOneOff()) { | |
47 LOG(ERROR) << "GLSurfaceWGL::InitializeOneOff failed."; | |
48 return false; | |
49 } | |
50 break; | |
51 case kGLImplementationEGLGLES2: | |
52 if (!GLSurfaceEGL::InitializeOneOff()) { | |
53 LOG(ERROR) << "GLSurfaceEGL::InitializeOneOff failed."; | |
54 return false; | |
55 } | |
56 break; | |
57 } | |
58 return true; | |
59 } | |
60 | |
61 NativeViewGLSurfaceOSMesa::NativeViewGLSurfaceOSMesa( | |
62 gfx::AcceleratedWidget window) | |
63 : GLSurfaceOSMesa(OSMESA_RGBA, gfx::Size(1, 1)), | |
64 window_(window), | |
65 device_context_(NULL) { | |
66 DCHECK(window); | |
67 } | |
68 | |
69 NativeViewGLSurfaceOSMesa::~NativeViewGLSurfaceOSMesa() { | |
70 Destroy(); | |
71 } | |
72 | |
73 bool NativeViewGLSurfaceOSMesa::Initialize() { | |
74 if (!GLSurfaceOSMesa::Initialize()) | |
75 return false; | |
76 | |
77 device_context_ = GetDC(window_); | |
78 return true; | |
79 } | |
80 | |
81 void NativeViewGLSurfaceOSMesa::Destroy() { | |
82 if (window_ && device_context_) | |
83 ReleaseDC(window_, device_context_); | |
84 | |
85 device_context_ = NULL; | |
86 | |
87 GLSurfaceOSMesa::Destroy(); | |
88 } | |
89 | |
90 bool NativeViewGLSurfaceOSMesa::IsOffscreen() { | |
91 return false; | |
92 } | |
93 | |
94 bool NativeViewGLSurfaceOSMesa::SwapBuffers() { | |
95 DCHECK(device_context_); | |
96 | |
97 gfx::Size size = GetSize(); | |
98 | |
99 // Note: negating the height below causes GDI to treat the bitmap data as row | |
100 // 0 being at the top. | |
101 BITMAPV4HEADER info = { sizeof(BITMAPV4HEADER) }; | |
102 info.bV4Width = size.width(); | |
103 info.bV4Height = -size.height(); | |
104 info.bV4Planes = 1; | |
105 info.bV4BitCount = 32; | |
106 info.bV4V4Compression = BI_BITFIELDS; | |
107 info.bV4RedMask = 0x000000FF; | |
108 info.bV4GreenMask = 0x0000FF00; | |
109 info.bV4BlueMask = 0x00FF0000; | |
110 info.bV4AlphaMask = 0xFF000000; | |
111 | |
112 // Copy the back buffer to the window's device context. Do not check whether | |
113 // StretchDIBits succeeds or not. It will fail if the window has been | |
114 // destroyed but it is preferable to allow rendering to silently fail if the | |
115 // window is destroyed. This is because the primary application of this | |
116 // class of GLContext is for testing and we do not want every GL related ui / | |
117 // browser test to become flaky if there is a race condition between GL | |
118 // context destruction and window destruction. | |
119 StretchDIBits(device_context_, | |
120 0, 0, size.width(), size.height(), | |
121 0, 0, size.width(), size.height(), | |
122 GetHandle(), | |
123 reinterpret_cast<BITMAPINFO*>(&info), | |
124 DIB_RGB_COLORS, | |
125 SRCCOPY); | |
126 | |
127 return true; | |
128 } | |
129 | |
130 std::string NativeViewGLSurfaceOSMesa::GetExtensions() { | |
131 std::string extensions = gfx::GLSurfaceOSMesa::GetExtensions(); | |
132 extensions += extensions.empty() ? "" : " "; | |
133 extensions += "GL_CHROMIUM_post_sub_buffer"; | |
134 return extensions; | |
135 } | |
136 | |
137 bool NativeViewGLSurfaceOSMesa::PostSubBuffer( | |
138 int x, int y, int width, int height) { | |
139 DCHECK(device_context_); | |
140 | |
141 gfx::Size size = GetSize(); | |
142 | |
143 // Note: negating the height below causes GDI to treat the bitmap data as row | |
144 // 0 being at the top. | |
145 BITMAPV4HEADER info = { sizeof(BITMAPV4HEADER) }; | |
146 info.bV4Width = size.width(); | |
147 info.bV4Height = -size.height(); | |
148 info.bV4Planes = 1; | |
149 info.bV4BitCount = 32; | |
150 info.bV4V4Compression = BI_BITFIELDS; | |
151 info.bV4RedMask = 0x000000FF; | |
152 info.bV4GreenMask = 0x0000FF00; | |
153 info.bV4BlueMask = 0x00FF0000; | |
154 info.bV4AlphaMask = 0xFF000000; | |
155 | |
156 // Copy the back buffer to the window's device context. Do not check whether | |
157 // StretchDIBits succeeds or not. It will fail if the window has been | |
158 // destroyed but it is preferable to allow rendering to silently fail if the | |
159 // window is destroyed. This is because the primary application of this | |
160 // class of GLContext is for testing and we do not want every GL related ui / | |
161 // browser test to become flaky if there is a race condition between GL | |
162 // context destruction and window destruction. | |
163 StretchDIBits(device_context_, | |
164 x, size.height() - y - height, width, height, | |
165 x, y, width, height, | |
166 GetHandle(), | |
167 reinterpret_cast<BITMAPINFO*>(&info), | |
168 DIB_RGB_COLORS, | |
169 SRCCOPY); | |
170 | |
171 return true; | |
172 } | |
173 | |
174 scoped_refptr<GLSurface> GLSurface::CreateViewGLSurface( | |
175 bool software, | |
176 gfx::AcceleratedWidget window) { | |
177 switch (GetGLImplementation()) { | |
178 case kGLImplementationOSMesaGL: { | |
179 scoped_refptr<GLSurface> surface( | |
180 new NativeViewGLSurfaceOSMesa(window)); | |
181 if (!surface->Initialize()) | |
182 return NULL; | |
183 | |
184 return surface; | |
185 } | |
186 case kGLImplementationEGLGLES2: { | |
187 scoped_refptr<GLSurface> surface(new NativeViewGLSurfaceEGL(software, | |
188 window)); | |
189 if (!surface->Initialize()) | |
190 return NULL; | |
191 | |
192 return surface; | |
193 } | |
194 case kGLImplementationDesktopGL: { | |
195 if (software) | |
196 return NULL; | |
197 scoped_refptr<GLSurface> surface(new NativeViewGLSurfaceWGL( | |
198 window)); | |
199 if (!surface->Initialize()) | |
200 return NULL; | |
201 | |
202 return surface; | |
203 } | |
204 case kGLImplementationMockGL: | |
205 return new GLSurfaceStub; | |
206 default: | |
207 NOTREACHED(); | |
208 return NULL; | |
209 } | |
210 } | |
211 | |
212 scoped_refptr<GLSurface> GLSurface::CreateOffscreenGLSurface( | |
213 bool software, | |
214 const gfx::Size& size) { | |
215 switch (GetGLImplementation()) { | |
216 case kGLImplementationOSMesaGL: { | |
217 scoped_refptr<GLSurface> surface(new GLSurfaceOSMesa(OSMESA_RGBA, | |
218 size)); | |
219 if (!surface->Initialize()) | |
220 return NULL; | |
221 | |
222 return surface; | |
223 } | |
224 case kGLImplementationEGLGLES2: { | |
225 scoped_refptr<GLSurface> surface(new PbufferGLSurfaceEGL(software, size)); | |
226 if (!surface->Initialize()) | |
227 return NULL; | |
228 | |
229 return surface; | |
230 } | |
231 case kGLImplementationDesktopGL: { | |
232 if (software) | |
233 return NULL; | |
234 scoped_refptr<GLSurface> surface(new PbufferGLSurfaceWGL(size)); | |
235 if (!surface->Initialize()) | |
236 return NULL; | |
237 | |
238 return surface; | |
239 } | |
240 case kGLImplementationMockGL: | |
241 return new GLSurfaceStub; | |
242 default: | |
243 NOTREACHED(); | |
244 return NULL; | |
245 } | |
246 } | |
247 | |
248 } // namespace gfx | |
OLD | NEW |