| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef APP_SURFACE_ACCELERATED_SURFACE_MAC_H_ | 5 #ifndef APP_SURFACE_ACCELERATED_SURFACE_MAC_H_ |
| 6 #define APP_SURFACE_ACCELERATED_SURFACE_MAC_H_ | 6 #define APP_SURFACE_ACCELERATED_SURFACE_MAC_H_ |
| 7 | 7 |
| 8 #include <CoreFoundation/CoreFoundation.h> | 8 #include <CoreFoundation/CoreFoundation.h> |
| 9 | 9 |
| 10 #include "app/gfx/gl/gl_context.h" |
| 10 #include "app/surface/transport_dib.h" | 11 #include "app/surface/transport_dib.h" |
| 11 #include "base/callback.h" | 12 #include "base/callback.h" |
| 12 #include "base/scoped_cftyperef.h" | 13 #include "base/scoped_cftyperef.h" |
| 13 #include "base/scoped_ptr.h" | 14 #include "base/scoped_ptr.h" |
| 14 #include "gfx/rect.h" | 15 #include "gfx/rect.h" |
| 15 #include "gfx/size.h" | 16 #include "gfx/size.h" |
| 16 | 17 |
| 17 // Should not include GL headers in a header file. Forward declare these types | 18 // Should not include GL headers in a header file. Forward declare these types |
| 18 // instead. | 19 // instead. |
| 19 typedef struct _CGLContextObject* CGLContextObj; | 20 typedef struct _CGLContextObject* CGLContextObj; |
| 20 typedef struct _CGLPBufferObject* CGLPBufferObj; | |
| 21 typedef unsigned int GLenum; | 21 typedef unsigned int GLenum; |
| 22 typedef unsigned int GLuint; | 22 typedef unsigned int GLuint; |
| 23 | 23 |
| 24 namespace gfx { | 24 namespace gfx { |
| 25 class Rect; | 25 class Rect; |
| 26 } | 26 } |
| 27 | 27 |
| 28 // Encapsulates an accelerated GL surface that can be shared across processes | 28 // Encapsulates an accelerated GL surface that can be shared across processes |
| 29 // on systems that support it (10.6 and above). For systems that do not, it | 29 // on systems that support it (10.6 and above). For systems that do not, it |
| 30 // uses a regular dib. There will either be a GL Context or a TransportDIB, | 30 // uses a regular dib. There will either be an IOSurface or a TransportDIB, |
| 31 // never both. | 31 // never both. |
| 32 | 32 |
| 33 class AcceleratedSurface { | 33 class AcceleratedSurface { |
| 34 public: | 34 public: |
| 35 AcceleratedSurface(); | 35 AcceleratedSurface(); |
| 36 virtual ~AcceleratedSurface() { } | 36 virtual ~AcceleratedSurface() { } |
| 37 | 37 |
| 38 // Set up internal buffers. |share_context|, if non-NULL, is a context | 38 // Set up internal buffers. |share_context|, if non-NULL, is a context |
| 39 // with which the internally created OpenGL context shares textures and | 39 // with which the internally created OpenGL context shares textures and |
| 40 // other resources. |allocate_fbo| indicates whether or not this surface | 40 // other resources. |allocate_fbo| indicates whether or not this surface |
| 41 // should allocate an offscreen frame buffer object (FBO) internally. If | 41 // should allocate an offscreen frame buffer object (FBO) internally. If |
| 42 // not, then the user is expected to allocate one. NOTE that allocating | 42 // not, then the user is expected to allocate one. NOTE that allocating |
| 43 // an FBO internally does NOT work properly with client code which uses | 43 // an FBO internally does NOT work properly with client code which uses |
| 44 // OpenGL (i.e., via GLES2 command buffers), because the GLES2 | 44 // OpenGL (i.e., via GLES2 command buffers), because the GLES2 |
| 45 // implementation does not know to bind the accelerated surface's | 45 // implementation does not know to bind the accelerated surface's |
| 46 // internal FBO when the default FBO is bound. Returns false upon | 46 // internal FBO when the default FBO is bound. Returns false upon |
| 47 // failure. | 47 // failure. |
| 48 bool Initialize(CGLContextObj share_context, bool allocate_fbo); | 48 bool Initialize(gfx::GLContext* share_context, bool allocate_fbo); |
| 49 // Tear down. Must be called before destructor to prevent leaks. | 49 // Tear down. Must be called before destructor to prevent leaks. |
| 50 void Destroy(); | 50 void Destroy(); |
| 51 | 51 |
| 52 // These methods are used only when there is a GL surface. | 52 // These methods are used only once the accelerated surface is initialized. |
| 53 | 53 |
| 54 // Sets the accelerated surface to the given size, creating a new one if | 54 // Sets the accelerated surface to the given size, creating a new one if |
| 55 // the height or width changes. Returns a unique id of the IOSurface to | 55 // the height or width changes. Returns a unique id of the IOSurface to |
| 56 // which the surface is bound, or 0 if no changes were made or an error | 56 // which the surface is bound, or 0 if no changes were made or an error |
| 57 // occurred. MakeCurrent() will have been called on the new surface. | 57 // occurred. MakeCurrent() will have been called on the new surface. |
| 58 uint64 SetSurfaceSize(const gfx::Size& size); | 58 uint64 SetSurfaceSize(const gfx::Size& size); |
| 59 | 59 |
| 60 // Sets the GL context to be the current one for drawing. Returns true if | 60 // Sets the GL context to be the current one for drawing. Returns true if |
| 61 // it succeeded. | 61 // it succeeded. |
| 62 bool MakeCurrent(); | 62 bool MakeCurrent(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 77 // AcceleratedSurface contains is responsible for the production of | 77 // AcceleratedSurface contains is responsible for the production of |
| 78 // the pixels, then when this entry point is called, the color | 78 // the pixels, then when this entry point is called, the color |
| 79 // buffer must be in a state where a glCopyTexSubImage2D or | 79 // buffer must be in a state where a glCopyTexSubImage2D or |
| 80 // glReadPixels is legal. (For example, if using multisampled FBOs, | 80 // glReadPixels is legal. (For example, if using multisampled FBOs, |
| 81 // the FBO must have been resolved into a non-multisampled color | 81 // the FBO must have been resolved into a non-multisampled color |
| 82 // texture.) Additionally, in this situation, the contexts must | 82 // texture.) Additionally, in this situation, the contexts must |
| 83 // share server-side GL objects, so that this AcceleratedSurface's | 83 // share server-side GL objects, so that this AcceleratedSurface's |
| 84 // texture is a legal name in the namespace of the current context. | 84 // texture is a legal name in the namespace of the current context. |
| 85 void SwapBuffers(); | 85 void SwapBuffers(); |
| 86 | 86 |
| 87 CGLContextObj context() { return gl_context_; } | 87 CGLContextObj context() { |
| 88 return static_cast<CGLContextObj>(gl_context_->GetHandle()); |
| 89 } |
| 88 | 90 |
| 89 // These methods are only used when there is a transport DIB. | 91 // These methods are only used when there is a transport DIB. |
| 90 | 92 |
| 91 // Sets the transport DIB to the given size, creating a new one if the | 93 // Sets the transport DIB to the given size, creating a new one if the |
| 92 // height or width changes. Returns a handle to the new DIB, or a default | 94 // height or width changes. Returns a handle to the new DIB, or a default |
| 93 // handle if no changes were made. | 95 // handle if no changes were made. Assumes the caller has already called |
| 96 // MakeCurrent(). |
| 94 TransportDIB::Handle SetTransportDIBSize(const gfx::Size& size); | 97 TransportDIB::Handle SetTransportDIBSize(const gfx::Size& size); |
| 95 // Sets the methods to use for allocating and freeing memory for the | 98 // Sets the methods to use for allocating and freeing memory for the |
| 96 // transport DIB. | 99 // transport DIB. |
| 97 void SetTransportDIBAllocAndFree( | 100 void SetTransportDIBAllocAndFree( |
| 98 Callback2<size_t, TransportDIB::Handle*>::Type* allocator, | 101 Callback2<size_t, TransportDIB::Handle*>::Type* allocator, |
| 99 Callback1<TransportDIB::Id>::Type* deallocator); | 102 Callback1<TransportDIB::Id>::Type* deallocator); |
| 100 | 103 |
| 101 // Get the accelerated surface size. | 104 // Get the accelerated surface size. |
| 102 gfx::Size GetSize() const { return surface_size_; } | 105 gfx::Size GetSize() const { return surface_size_; } |
| 103 | 106 |
| 104 private: | 107 private: |
| 105 // Helper function to generate names for the backing texture, render buffers | 108 // Helper function to generate names for the backing texture, render buffers |
| 106 // and FBO. On return, the resulting buffer names can be attached to |fbo_|. | 109 // and FBO. On return, the resulting buffer names can be attached to |fbo_|. |
| 107 // |target| is the target type for the color buffer. | 110 // |target| is the target type for the color buffer. |
| 108 void AllocateRenderBuffers(GLenum target, const gfx::Size& size); | 111 void AllocateRenderBuffers(GLenum target, const gfx::Size& size); |
| 109 | 112 |
| 110 // Helper function to attach the buffers previously allocated by a call to | 113 // Helper function to attach the buffers previously allocated by a call to |
| 111 // AllocateRenderBuffers(). On return, |fbo_| can be used for | 114 // AllocateRenderBuffers(). On return, |fbo_| can be used for |
| 112 // rendering. |target| must be the same value as used in the call to | 115 // rendering. |target| must be the same value as used in the call to |
| 113 // AllocateRenderBuffers(). Returns |true| if the resulting framebuffer | 116 // AllocateRenderBuffers(). Returns |true| if the resulting framebuffer |
| 114 // object is valid. | 117 // object is valid. |
| 115 bool SetupFrameBufferObject(GLenum target); | 118 bool SetupFrameBufferObject(GLenum target); |
| 116 | 119 |
| 117 // The OpenGL context, and pbuffer drawable, used to transfer data | 120 // The OpenGL context, and pbuffer drawable, used to transfer data |
| 118 // to the shared region (IOSurface or TransportDIB). Strictly | 121 // to the shared region (IOSurface or TransportDIB). Strictly |
| 119 // speaking, we do not need to allocate a GL context all of the | 122 // speaking, we do not need to allocate a GL context all of the |
| 120 // time. We only need one if (a) we are using the IOSurface code | 123 // time. We only need one if (a) we are using the IOSurface code |
| 121 // path, or (b) if we are allocating an FBO internally. | 124 // path, or (b) if we are allocating an FBO internally. |
| 122 CGLContextObj gl_context_; | 125 scoped_ptr<gfx::GLContext> gl_context_; |
| 123 CGLPBufferObj pbuffer_; | |
| 124 // Either |io_surface_| or |transport_dib_| is a valid pointer, but not both. | 126 // Either |io_surface_| or |transport_dib_| is a valid pointer, but not both. |
| 125 // |io_surface_| is non-NULL if the IOSurface APIs are supported (Mac OS X | 127 // |io_surface_| is non-NULL if the IOSurface APIs are supported (Mac OS X |
| 126 // 10.6 and later). | 128 // 10.6 and later). |
| 127 // TODO(dspringer,kbr): Should the GPU backing store be encapsulated in its | 129 // TODO(dspringer,kbr): Should the GPU backing store be encapsulated in its |
| 128 // own class so all this implementation detail is hidden? | 130 // own class so all this implementation detail is hidden? |
| 129 scoped_cftyperef<CFTypeRef> io_surface_; | 131 scoped_cftyperef<CFTypeRef> io_surface_; |
| 130 // TODO(dspringer): If we end up keeping this TransportDIB mechanism, this | 132 // TODO(dspringer): If we end up keeping this TransportDIB mechanism, this |
| 131 // should really be a scoped_ptr_malloc<>, with a deallocate functor that | 133 // should really be a scoped_ptr_malloc<>, with a deallocate functor that |
| 132 // runs |dib_free_callback_|. I was not able to figure out how to | 134 // runs |dib_free_callback_|. I was not able to figure out how to |
| 133 // make this work (or even compile). | 135 // make this work (or even compile). |
| (...skipping 12 matching lines...) Expand all Loading... |
| 146 // true. | 148 // true. |
| 147 GLuint fbo_; | 149 GLuint fbo_; |
| 148 GLuint depth_stencil_renderbuffer_; | 150 GLuint depth_stencil_renderbuffer_; |
| 149 // Allocate a TransportDIB in the renderer. | 151 // Allocate a TransportDIB in the renderer. |
| 150 scoped_ptr<Callback2<size_t, TransportDIB::Handle*>::Type> | 152 scoped_ptr<Callback2<size_t, TransportDIB::Handle*>::Type> |
| 151 dib_alloc_callback_; | 153 dib_alloc_callback_; |
| 152 scoped_ptr<Callback1<TransportDIB::Id>::Type> dib_free_callback_; | 154 scoped_ptr<Callback1<TransportDIB::Id>::Type> dib_free_callback_; |
| 153 }; | 155 }; |
| 154 | 156 |
| 155 #endif // APP_SURFACE_ACCELERATED_SURFACE_MAC_H_ | 157 #endif // APP_SURFACE_ACCELERATED_SURFACE_MAC_H_ |
| OLD | NEW |