OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/gfx/surface/accelerated_surface_mac.h" | 5 #include "ui/gfx/surface/accelerated_surface_mac.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/mac/scoped_cftyperef.h" | 8 #include "base/mac/scoped_cftyperef.h" |
9 #include "ui/gfx/gl/gl_bindings.h" | 9 #include "ui/gfx/gl/gl_bindings.h" |
10 #include "ui/gfx/gl/gl_implementation.h" | 10 #include "ui/gfx/gl/gl_implementation.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 // Ensure GL is initialized before trying to create an offscreen GL context. | 29 // Ensure GL is initialized before trying to create an offscreen GL context. |
30 if (!gfx::GLSurface::InitializeOneOff()) | 30 if (!gfx::GLSurface::InitializeOneOff()) |
31 return false; | 31 return false; |
32 | 32 |
33 // Drawing to IOSurfaces via OpenGL only works with desktop GL and | 33 // Drawing to IOSurfaces via OpenGL only works with desktop GL and |
34 // not with the OSMesa software renderer. | 34 // not with the OSMesa software renderer. |
35 if (gfx::GetGLImplementation() != gfx::kGLImplementationDesktopGL) | 35 if (gfx::GetGLImplementation() != gfx::kGLImplementationDesktopGL) |
36 return false; | 36 return false; |
37 | 37 |
38 scoped_ptr<gfx::GLSurface> surface(gfx::GLSurface::CreateOffscreenGLSurface( | 38 gl_surface_.reset(gfx::GLSurface::CreateOffscreenGLSurface(gfx::Size(1, 1))); |
39 gfx::Size(1, 1))); | 39 if (!gl_surface_.get()) { |
40 if (!surface.get()) { | |
41 Destroy(); | 40 Destroy(); |
42 return false; | 41 return false; |
43 } | 42 } |
44 | 43 |
45 gl_context_.reset(gfx::GLContext::CreateGLContext(surface.release(), | 44 gl_context_.reset(gfx::GLContext::CreateGLContext(share_context)); |
46 share_context)); | |
47 if (!gl_context_.get()) { | 45 if (!gl_context_.get()) { |
48 Destroy(); | 46 Destroy(); |
49 return false; | 47 return false; |
50 } | 48 } |
51 | 49 |
52 // Now we're ready to handle SetSurfaceSize calls, which will | 50 // Now we're ready to handle SetSurfaceSize calls, which will |
53 // allocate and/or reallocate the IOSurface and associated offscreen | 51 // allocate and/or reallocate the IOSurface and associated offscreen |
54 // OpenGL structures for rendering. | 52 // OpenGL structures for rendering. |
55 return true; | 53 return true; |
56 } | 54 } |
57 | 55 |
58 void AcceleratedSurface::Destroy() { | 56 void AcceleratedSurface::Destroy() { |
59 // The FBO and texture objects will be destroyed when the OpenGL context, | 57 // The FBO and texture objects will be destroyed when the OpenGL context, |
60 // and any other contexts sharing resources with it, is. We don't want to | 58 // and any other contexts sharing resources with it, is. We don't want to |
61 // make the context current one last time here just in order to delete | 59 // make the context current one last time here just in order to delete |
62 // these objects. | 60 // these objects. |
63 | 61 |
64 // Release the old TransportDIB in the browser. | 62 // Release the old TransportDIB in the browser. |
65 if (dib_free_callback_.get() && transport_dib_.get()) { | 63 if (dib_free_callback_.get() && transport_dib_.get()) { |
66 dib_free_callback_->Run(transport_dib_->id()); | 64 dib_free_callback_->Run(transport_dib_->id()); |
67 } | 65 } |
68 transport_dib_.reset(); | 66 transport_dib_.reset(); |
69 | 67 |
70 if (gl_context_.get()) | |
71 gl_context_->Destroy(); | |
72 gl_context_.reset(); | 68 gl_context_.reset(); |
| 69 gl_surface_.reset(); |
73 } | 70 } |
74 | 71 |
75 // Call after making changes to the surface which require a visual update. | 72 // Call after making changes to the surface which require a visual update. |
76 // Makes the rendering show up in other processes. | 73 // Makes the rendering show up in other processes. |
77 void AcceleratedSurface::SwapBuffers() { | 74 void AcceleratedSurface::SwapBuffers() { |
78 if (io_surface_.get() != NULL) { | 75 if (io_surface_.get() != NULL) { |
79 if (allocate_fbo_) { | 76 if (allocate_fbo_) { |
80 // Bind and unbind the framebuffer to make changes to the | 77 // Bind and unbind the framebuffer to make changes to the |
81 // IOSurface show up in the other process. | 78 // IOSurface show up in the other process. |
82 glFlush(); | 79 glFlush(); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 GL_RENDERBUFFER_EXT, | 189 GL_RENDERBUFFER_EXT, |
193 depth_stencil_renderbuffer_); | 190 depth_stencil_renderbuffer_); |
194 fbo_status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); | 191 fbo_status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); |
195 } | 192 } |
196 return fbo_status == GL_FRAMEBUFFER_COMPLETE_EXT; | 193 return fbo_status == GL_FRAMEBUFFER_COMPLETE_EXT; |
197 } | 194 } |
198 | 195 |
199 bool AcceleratedSurface::MakeCurrent() { | 196 bool AcceleratedSurface::MakeCurrent() { |
200 if (!gl_context_.get()) | 197 if (!gl_context_.get()) |
201 return false; | 198 return false; |
202 return gl_context_->MakeCurrent(); | 199 return gl_context_->MakeCurrent(gl_surface_.get()); |
203 } | 200 } |
204 | 201 |
205 void AcceleratedSurface::Clear(const gfx::Rect& rect) { | 202 void AcceleratedSurface::Clear(const gfx::Rect& rect) { |
206 DCHECK(gl_context_->IsCurrent()); | 203 DCHECK(gl_context_->IsCurrent(gl_surface_.get())); |
207 glClearColor(0, 0, 0, 0); | 204 glClearColor(0, 0, 0, 0); |
208 glViewport(0, 0, rect.width(), rect.height()); | 205 glViewport(0, 0, rect.width(), rect.height()); |
209 glMatrixMode(GL_PROJECTION); | 206 glMatrixMode(GL_PROJECTION); |
210 glLoadIdentity(); | 207 glLoadIdentity(); |
211 glOrtho(0, rect.width(), 0, rect.height(), -1, 1); | 208 glOrtho(0, rect.width(), 0, rect.height(), -1, 1); |
212 glClear(GL_COLOR_BUFFER_BIT); | 209 glClear(GL_COLOR_BUFFER_BIT); |
213 } | 210 } |
214 | 211 |
215 uint64 AcceleratedSurface::SetSurfaceSize(const gfx::Size& size) { | 212 uint64 AcceleratedSurface::SetSurfaceSize(const gfx::Size& size) { |
216 if (surface_size_ == size) { | 213 if (surface_size_ == size) { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 return TransportDIB::DefaultHandleValue(); | 324 return TransportDIB::DefaultHandleValue(); |
328 } | 325 } |
329 transport_dib_.reset(TransportDIB::Map(dib_handle)); | 326 transport_dib_.reset(TransportDIB::Map(dib_handle)); |
330 if (transport_dib_.get() == NULL) { | 327 if (transport_dib_.get() == NULL) { |
331 // TODO(dspringer): if the Map() fails, should the deallocator be run so | 328 // TODO(dspringer): if the Map() fails, should the deallocator be run so |
332 // that the DIB is deallocated in the browser? | 329 // that the DIB is deallocated in the browser? |
333 return TransportDIB::DefaultHandleValue(); | 330 return TransportDIB::DefaultHandleValue(); |
334 } | 331 } |
335 | 332 |
336 if (allocate_fbo_) { | 333 if (allocate_fbo_) { |
337 DCHECK(gl_context_->IsCurrent()); | 334 DCHECK(gl_context_->IsCurrent(gl_surface_.get())); |
338 // Set up the render buffers and reserve enough space on the card for the | 335 // Set up the render buffers and reserve enough space on the card for the |
339 // framebuffer texture. | 336 // framebuffer texture. |
340 GLenum target = GL_TEXTURE_RECTANGLE_ARB; | 337 GLenum target = GL_TEXTURE_RECTANGLE_ARB; |
341 AllocateRenderBuffers(target, size); | 338 AllocateRenderBuffers(target, size); |
342 glTexImage2D(target, | 339 glTexImage2D(target, |
343 0, // mipmap level 0 | 340 0, // mipmap level 0 |
344 GL_RGBA8, // internal pixel format | 341 GL_RGBA8, // internal pixel format |
345 size.width(), | 342 size.width(), |
346 size.height(), | 343 size.height(), |
347 0, // 0 border | 344 0, // 0 border |
348 GL_BGRA, // Used for consistency | 345 GL_BGRA, // Used for consistency |
349 GL_UNSIGNED_INT_8_8_8_8_REV, | 346 GL_UNSIGNED_INT_8_8_8_8_REV, |
350 NULL); // No data, just reserve room on the card. | 347 NULL); // No data, just reserve room on the card. |
351 SetupFrameBufferObject(target); | 348 SetupFrameBufferObject(target); |
352 } | 349 } |
353 return transport_dib_->handle(); | 350 return transport_dib_->handle(); |
354 } | 351 } |
355 | 352 |
356 void AcceleratedSurface::SetTransportDIBAllocAndFree( | 353 void AcceleratedSurface::SetTransportDIBAllocAndFree( |
357 Callback2<size_t, TransportDIB::Handle*>::Type* allocator, | 354 Callback2<size_t, TransportDIB::Handle*>::Type* allocator, |
358 Callback1<TransportDIB::Id>::Type* deallocator) { | 355 Callback1<TransportDIB::Id>::Type* deallocator) { |
359 dib_alloc_callback_.reset(allocator); | 356 dib_alloc_callback_.reset(allocator); |
360 dib_free_callback_.reset(deallocator); | 357 dib_free_callback_.reset(deallocator); |
361 } | 358 } |
OLD | NEW |