OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef UI_GFX_SURFACE_ACCELERATED_SURFACE_MAC_H_ | |
6 #define UI_GFX_SURFACE_ACCELERATED_SURFACE_MAC_H_ | |
7 #pragma once | |
8 | |
9 #include <CoreFoundation/CoreFoundation.h> | |
10 | |
11 #include "base/callback.h" | |
12 #include "base/mac/scoped_cftyperef.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "ui/gfx/gl/gl_context.h" | |
15 #include "ui/gfx/gl/gl_surface.h" | |
16 #include "ui/gfx/gl/gpu_preference.h" | |
17 #include "ui/gfx/rect.h" | |
18 #include "ui/gfx/size.h" | |
19 #include "ui/gfx/surface/surface_export.h" | |
20 #include "ui/gfx/surface/transport_dib.h" | |
21 | |
22 // Should not include GL headers in a header file. Forward declare these types | |
23 // instead. | |
24 typedef struct _CGLContextObject* CGLContextObj; | |
25 typedef unsigned int GLenum; | |
26 typedef unsigned int GLuint; | |
27 | |
28 namespace gfx { | |
29 class Rect; | |
30 } | |
31 | |
32 // Encapsulates an accelerated GL surface that can be shared across processes | |
33 // on systems that support it (10.6 and above). For systems that do not, it | |
34 // uses a regular dib. There will either be an IOSurface or a TransportDIB, | |
35 // never both. | |
36 | |
37 class SURFACE_EXPORT AcceleratedSurface { | |
38 public: | |
39 AcceleratedSurface(); | |
40 virtual ~AcceleratedSurface(); | |
41 | |
42 // Set up internal buffers. |share_context|, if non-NULL, is a context | |
43 // with which the internally created OpenGL context shares textures and | |
44 // other resources. |allocate_fbo| indicates whether or not this surface | |
45 // should allocate an offscreen frame buffer object (FBO) internally. If | |
46 // not, then the user is expected to allocate one. NOTE that allocating | |
47 // an FBO internally does NOT work properly with client code which uses | |
48 // OpenGL (i.e., via GLES2 command buffers), because the GLES2 | |
49 // implementation does not know to bind the accelerated surface's | |
50 // internal FBO when the default FBO is bound. |gpu_preference| indicates | |
51 // the GPU preference for the internally allocated GLContext. If | |
52 // |share_context| is non-NULL, then on platforms supporting dual GPUs, | |
53 // its GPU preference must match the passed one. Returns false upon | |
54 // failure. | |
55 bool Initialize(gfx::GLContext* share_context, | |
56 bool allocate_fbo, | |
57 gfx::GpuPreference gpu_preference); | |
58 // Tear down. Must be called before destructor to prevent leaks. | |
59 void Destroy(); | |
60 | |
61 // These methods are used only once the accelerated surface is initialized. | |
62 | |
63 // Sets the accelerated surface to the given size, creating a new one if | |
64 // the height or width changes. Returns a unique id of the IOSurface to | |
65 // which the surface is bound, or 0 if no changes were made or an error | |
66 // occurred. MakeCurrent() will have been called on the new surface. | |
67 uint32 SetSurfaceSize(const gfx::Size& size); | |
68 | |
69 // Returns the id of this surface's IOSurface, or 0 for | |
70 // transport DIB surfaces. | |
71 uint32 GetSurfaceId(); | |
72 | |
73 // Sets the GL context to be the current one for drawing. Returns true if | |
74 // it succeeded. | |
75 bool MakeCurrent(); | |
76 // Clear the surface to be transparent. Assumes the caller has already called | |
77 // MakeCurrent(). | |
78 void Clear(const gfx::Rect& rect); | |
79 // Call after making changes to the surface which require a visual update. | |
80 // Makes the rendering show up in other processes. Assumes the caller has | |
81 // already called MakeCurrent(). | |
82 // | |
83 // If this AcceleratedSurface is configured with its own FBO, then | |
84 // this call causes the color buffer to be transmitted. Otherwise, | |
85 // it causes the frame buffer of the current GL context to be copied | |
86 // either into an internal texture via glCopyTexSubImage2D or into a | |
87 // TransportDIB via glReadPixels. | |
88 // | |
89 // The size of the rectangle copied is the size last specified via | |
90 // SetSurfaceSize. If another GL context than the one this | |
91 // AcceleratedSurface contains is responsible for the production of | |
92 // the pixels, then when this entry point is called, the color | |
93 // buffer must be in a state where a glCopyTexSubImage2D or | |
94 // glReadPixels is legal. (For example, if using multisampled FBOs, | |
95 // the FBO must have been resolved into a non-multisampled color | |
96 // texture.) Additionally, in this situation, the contexts must | |
97 // share server-side GL objects, so that this AcceleratedSurface's | |
98 // texture is a legal name in the namespace of the current context. | |
99 void SwapBuffers(); | |
100 | |
101 CGLContextObj context() { | |
102 return static_cast<CGLContextObj>(gl_context_->GetHandle()); | |
103 } | |
104 | |
105 // These methods are only used when there is a transport DIB. | |
106 | |
107 // Sets the transport DIB to the given size, creating a new one if the | |
108 // height or width changes. Returns a handle to the new DIB, or a default | |
109 // handle if no changes were made. Assumes the caller has already called | |
110 // MakeCurrent(). | |
111 TransportDIB::Handle SetTransportDIBSize(const gfx::Size& size); | |
112 // Sets the methods to use for allocating and freeing memory for the | |
113 // transport DIB. | |
114 void SetTransportDIBAllocAndFree( | |
115 const base::Callback<void(size_t, TransportDIB::Handle*)>& allocator, | |
116 const base::Callback<void(TransportDIB::Id)>& deallocator); | |
117 | |
118 // Get the accelerated surface size. | |
119 gfx::Size GetSize() const { return surface_size_; } | |
120 | |
121 private: | |
122 // Helper function to generate names for the backing texture and FBO. On | |
123 // return, the resulting names can be attached to |fbo_|. |target| is | |
124 // the target type for the color buffer. | |
125 void AllocateRenderBuffers(GLenum target, const gfx::Size& size); | |
126 | |
127 // Helper function to attach the buffers previously allocated by a call to | |
128 // AllocateRenderBuffers(). On return, |fbo_| can be used for | |
129 // rendering. |target| must be the same value as used in the call to | |
130 // AllocateRenderBuffers(). Returns |true| if the resulting framebuffer | |
131 // object is valid. | |
132 bool SetupFrameBufferObject(GLenum target); | |
133 | |
134 gfx::Size ClampToValidDimensions(const gfx::Size& size); | |
135 | |
136 // The OpenGL context, and pbuffer drawable, used to transfer data | |
137 // to the shared region (IOSurface or TransportDIB). Strictly | |
138 // speaking, we do not need to allocate a GL context all of the | |
139 // time. We only need one if (a) we are using the IOSurface code | |
140 // path, or (b) if we are allocating an FBO internally. | |
141 scoped_refptr<gfx::GLSurface> gl_surface_; | |
142 scoped_refptr<gfx::GLContext> gl_context_; | |
143 // Either |io_surface_| or |transport_dib_| is a valid pointer, but not both. | |
144 // |io_surface_| is non-NULL if the IOSurface APIs are supported (Mac OS X | |
145 // 10.6 and later). | |
146 // TODO(dspringer,kbr): Should the GPU backing store be encapsulated in its | |
147 // own class so all this implementation detail is hidden? | |
148 base::mac::ScopedCFTypeRef<CFTypeRef> io_surface_; | |
149 | |
150 // The id of |io_surface_| or 0 if that's NULL. | |
151 uint32 io_surface_id_; | |
152 | |
153 // TODO(dspringer): If we end up keeping this TransportDIB mechanism, this | |
154 // should really be a scoped_ptr_malloc<>, with a deallocate functor that | |
155 // runs |dib_free_callback_|. I was not able to figure out how to | |
156 // make this work (or even compile). | |
157 scoped_ptr<TransportDIB> transport_dib_; | |
158 gfx::Size surface_size_; | |
159 // It's important to avoid allocating zero-width or zero-height | |
160 // IOSurfaces and textures on the Mac, so we clamp each to a minimum | |
161 // of 1. This is the real size of the surface; surface_size_ is what | |
162 // the user requested. | |
163 gfx::Size real_surface_size_; | |
164 // TODO(kbr): the FBO management should not be in this class at all. | |
165 // However, if it is factored out, care needs to be taken to not | |
166 // introduce another copy of the color data on the GPU; the direct | |
167 // binding of the internal texture to the IOSurface saves a copy. | |
168 bool allocate_fbo_; | |
169 // If the IOSurface code path is being used, then this texture | |
170 // object is always allocated. Otherwise, it is only allocated if | |
171 // the user requests an FBO be allocated. | |
172 GLuint texture_; | |
173 // The FBO and renderbuffer are only allocated if allocate_fbo_ is | |
174 // true. | |
175 GLuint fbo_; | |
176 // Allocate a TransportDIB in the renderer. | |
177 base::Callback<void(size_t, TransportDIB::Handle*)> dib_alloc_callback_; | |
178 base::Callback<void(TransportDIB::Id)> dib_free_callback_; | |
179 }; | |
180 | |
181 #endif // UI_GFX_SURFACE_ACCELERATED_SURFACE_MAC_H_ | |
OLD | NEW |