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)); | 45 gl_surface_.get())); |
47 if (!gl_context_.get()) { | 46 if (!gl_context_.get()) { |
48 Destroy(); | 47 Destroy(); |
49 return false; | 48 return false; |
50 } | 49 } |
51 | 50 |
52 // Now we're ready to handle SetSurfaceSize calls, which will | 51 // Now we're ready to handle SetSurfaceSize calls, which will |
53 // allocate and/or reallocate the IOSurface and associated offscreen | 52 // allocate and/or reallocate the IOSurface and associated offscreen |
54 // OpenGL structures for rendering. | 53 // OpenGL structures for rendering. |
55 return true; | 54 return true; |
56 } | 55 } |
57 | 56 |
58 void AcceleratedSurface::Destroy() { | 57 void AcceleratedSurface::Destroy() { |
59 // The FBO and texture objects will be destroyed when the OpenGL context, | 58 // 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 | 59 // 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 | 60 // make the context current one last time here just in order to delete |
62 // these objects. | 61 // these objects. |
63 | 62 |
64 // Release the old TransportDIB in the browser. | 63 // Release the old TransportDIB in the browser. |
65 if (dib_free_callback_.get() && transport_dib_.get()) { | 64 if (dib_free_callback_.get() && transport_dib_.get()) { |
66 dib_free_callback_->Run(transport_dib_->id()); | 65 dib_free_callback_->Run(transport_dib_->id()); |
67 } | 66 } |
68 transport_dib_.reset(); | 67 transport_dib_.reset(); |
69 | 68 |
70 if (gl_context_.get()) | |
71 gl_context_->Destroy(); | |
72 gl_context_.reset(); | 69 gl_context_.reset(); |
| 70 gl_surface_.reset(); |
73 } | 71 } |
74 | 72 |
75 // Call after making changes to the surface which require a visual update. | 73 // Call after making changes to the surface which require a visual update. |
76 // Makes the rendering show up in other processes. | 74 // Makes the rendering show up in other processes. |
77 void AcceleratedSurface::SwapBuffers() { | 75 void AcceleratedSurface::SwapBuffers() { |
78 if (io_surface_.get() != NULL) { | 76 if (io_surface_.get() != NULL) { |
79 if (allocate_fbo_) { | 77 if (allocate_fbo_) { |
80 // Bind and unbind the framebuffer to make changes to the | 78 // Bind and unbind the framebuffer to make changes to the |
81 // IOSurface show up in the other process. | 79 // IOSurface show up in the other process. |
82 glFlush(); | 80 glFlush(); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 GL_RENDERBUFFER_EXT, | 190 GL_RENDERBUFFER_EXT, |
193 depth_stencil_renderbuffer_); | 191 depth_stencil_renderbuffer_); |
194 fbo_status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); | 192 fbo_status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); |
195 } | 193 } |
196 return fbo_status == GL_FRAMEBUFFER_COMPLETE_EXT; | 194 return fbo_status == GL_FRAMEBUFFER_COMPLETE_EXT; |
197 } | 195 } |
198 | 196 |
199 bool AcceleratedSurface::MakeCurrent() { | 197 bool AcceleratedSurface::MakeCurrent() { |
200 if (!gl_context_.get()) | 198 if (!gl_context_.get()) |
201 return false; | 199 return false; |
202 return gl_context_->MakeCurrent(); | 200 return gl_context_->MakeCurrent(gl_surface_.get()); |
203 } | 201 } |
204 | 202 |
205 void AcceleratedSurface::Clear(const gfx::Rect& rect) { | 203 void AcceleratedSurface::Clear(const gfx::Rect& rect) { |
206 DCHECK(gl_context_->IsCurrent()); | 204 DCHECK(gl_context_->IsCurrent(gl_surface_.get())); |
207 glClearColor(0, 0, 0, 0); | 205 glClearColor(0, 0, 0, 0); |
208 glViewport(0, 0, rect.width(), rect.height()); | 206 glViewport(0, 0, rect.width(), rect.height()); |
209 glMatrixMode(GL_PROJECTION); | 207 glMatrixMode(GL_PROJECTION); |
210 glLoadIdentity(); | 208 glLoadIdentity(); |
211 glOrtho(0, rect.width(), 0, rect.height(), -1, 1); | 209 glOrtho(0, rect.width(), 0, rect.height(), -1, 1); |
212 glClear(GL_COLOR_BUFFER_BIT); | 210 glClear(GL_COLOR_BUFFER_BIT); |
213 } | 211 } |
214 | 212 |
215 uint64 AcceleratedSurface::SetSurfaceSize(const gfx::Size& size) { | 213 uint64 AcceleratedSurface::SetSurfaceSize(const gfx::Size& size) { |
216 if (surface_size_ == size) { | 214 if (surface_size_ == size) { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 return TransportDIB::DefaultHandleValue(); | 325 return TransportDIB::DefaultHandleValue(); |
328 } | 326 } |
329 transport_dib_.reset(TransportDIB::Map(dib_handle)); | 327 transport_dib_.reset(TransportDIB::Map(dib_handle)); |
330 if (transport_dib_.get() == NULL) { | 328 if (transport_dib_.get() == NULL) { |
331 // TODO(dspringer): if the Map() fails, should the deallocator be run so | 329 // TODO(dspringer): if the Map() fails, should the deallocator be run so |
332 // that the DIB is deallocated in the browser? | 330 // that the DIB is deallocated in the browser? |
333 return TransportDIB::DefaultHandleValue(); | 331 return TransportDIB::DefaultHandleValue(); |
334 } | 332 } |
335 | 333 |
336 if (allocate_fbo_) { | 334 if (allocate_fbo_) { |
337 DCHECK(gl_context_->IsCurrent()); | 335 DCHECK(gl_context_->IsCurrent(gl_surface_.get())); |
338 // Set up the render buffers and reserve enough space on the card for the | 336 // Set up the render buffers and reserve enough space on the card for the |
339 // framebuffer texture. | 337 // framebuffer texture. |
340 GLenum target = GL_TEXTURE_RECTANGLE_ARB; | 338 GLenum target = GL_TEXTURE_RECTANGLE_ARB; |
341 AllocateRenderBuffers(target, size); | 339 AllocateRenderBuffers(target, size); |
342 glTexImage2D(target, | 340 glTexImage2D(target, |
343 0, // mipmap level 0 | 341 0, // mipmap level 0 |
344 GL_RGBA8, // internal pixel format | 342 GL_RGBA8, // internal pixel format |
345 size.width(), | 343 size.width(), |
346 size.height(), | 344 size.height(), |
347 0, // 0 border | 345 0, // 0 border |
348 GL_BGRA, // Used for consistency | 346 GL_BGRA, // Used for consistency |
349 GL_UNSIGNED_INT_8_8_8_8_REV, | 347 GL_UNSIGNED_INT_8_8_8_8_REV, |
350 NULL); // No data, just reserve room on the card. | 348 NULL); // No data, just reserve room on the card. |
351 SetupFrameBufferObject(target); | 349 SetupFrameBufferObject(target); |
352 } | 350 } |
353 return transport_dib_->handle(); | 351 return transport_dib_->handle(); |
354 } | 352 } |
355 | 353 |
356 void AcceleratedSurface::SetTransportDIBAllocAndFree( | 354 void AcceleratedSurface::SetTransportDIBAllocAndFree( |
357 Callback2<size_t, TransportDIB::Handle*>::Type* allocator, | 355 Callback2<size_t, TransportDIB::Handle*>::Type* allocator, |
358 Callback1<TransportDIB::Id>::Type* deallocator) { | 356 Callback1<TransportDIB::Id>::Type* deallocator) { |
359 dib_alloc_callback_.reset(allocator); | 357 dib_alloc_callback_.reset(allocator); |
360 dib_free_callback_.reset(deallocator); | 358 dib_free_callback_.reset(deallocator); |
361 } | 359 } |
OLD | NEW |