base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / ui / surface / accelerated_surface_mac.h
blob4191d36fe3a8c48fc4977b2b4c21acc7073c62c3
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.
5 #ifndef UI_SURFACE_ACCELERATED_SURFACE_MAC_H_
6 #define UI_SURFACE_ACCELERATED_SURFACE_MAC_H_
8 #include <CoreFoundation/CoreFoundation.h>
9 #include <IOSurface/IOSurfaceAPI.h>
11 #include "base/callback.h"
12 #include "base/mac/scoped_cftyperef.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "ui/gfx/geometry/rect.h"
15 #include "ui/gfx/geometry/size.h"
16 #include "ui/gl/gl_context.h"
17 #include "ui/gl/gl_surface.h"
18 #include "ui/gl/gpu_preference.h"
19 #include "ui/surface/surface_export.h"
21 // Should not include GL headers in a header file. Forward declare these types
22 // instead.
23 typedef struct _CGLContextObject* CGLContextObj;
24 typedef unsigned int GLenum;
25 typedef unsigned int GLuint;
27 namespace gfx {
28 class Rect;
31 // Encapsulates an accelerated GL surface that can be shared across processes
32 // on systems that support it (10.6 and above).
34 class SURFACE_EXPORT AcceleratedSurface {
35 public:
36 AcceleratedSurface();
37 virtual ~AcceleratedSurface();
39 // Set up internal buffers. |share_context|, if non-NULL, is a context
40 // with which the internally created OpenGL context shares textures and
41 // other resources. |allocate_fbo| indicates whether or not this surface
42 // should allocate an offscreen frame buffer object (FBO) internally. If
43 // not, then the user is expected to allocate one. NOTE that allocating
44 // an FBO internally does NOT work properly with client code which uses
45 // OpenGL (i.e., via GLES2 command buffers), because the GLES2
46 // implementation does not know to bind the accelerated surface's
47 // internal FBO when the default FBO is bound. |gpu_preference| indicates
48 // the GPU preference for the internally allocated GLContext. If
49 // |share_context| is non-NULL, then on platforms supporting dual GPUs,
50 // its GPU preference must match the passed one. Returns false upon
51 // failure.
52 bool Initialize(gfx::GLContext* share_context,
53 bool allocate_fbo,
54 gfx::GpuPreference gpu_preference);
55 // Tear down. Must be called before destructor to prevent leaks.
56 void Destroy();
58 // These methods are used only once the accelerated surface is initialized.
60 // Sets the accelerated surface to the given size, creating a new one if
61 // the height or width changes. Returns a unique id of the IOSurface to
62 // which the surface is bound, or 0 if no changes were made or an error
63 // occurred. MakeCurrent() will have been called on the new surface.
64 uint32 SetSurfaceSize(const gfx::Size& size);
66 // Returns the id of this surface's IOSurface.
67 uint32 GetSurfaceId();
69 // Sets the GL context to be the current one for drawing. Returns true if
70 // it succeeded.
71 bool MakeCurrent();
72 // Clear the surface to be transparent. Assumes the caller has already called
73 // MakeCurrent().
74 void Clear(const gfx::Rect& rect);
75 // Call after making changes to the surface which require a visual update.
76 // Makes the rendering show up in other processes. Assumes the caller has
77 // already called MakeCurrent().
79 // If this AcceleratedSurface is configured with its own FBO, then
80 // this call causes the color buffer to be transmitted. Otherwise,
81 // it causes the frame buffer of the current GL context to be copied
82 // into an internal texture via glCopyTexSubImage2D.
84 // The size of the rectangle copied is the size last specified via
85 // SetSurfaceSize. If another GL context than the one this
86 // AcceleratedSurface contains is responsible for the production of
87 // the pixels, then when this entry point is called, the color
88 // buffer must be in a state where a glCopyTexSubImage2D is
89 // legal. (For example, if using multisampled FBOs, the FBO must
90 // have been resolved into a non-multisampled color texture.)
91 // Additionally, in this situation, the contexts must share
92 // server-side GL objects, so that this AcceleratedSurface's texture
93 // is a legal name in the namespace of the current context.
94 void SwapBuffers();
96 CGLContextObj context() {
97 return static_cast<CGLContextObj>(gl_context_->GetHandle());
100 // Get the accelerated surface size.
101 gfx::Size GetSize() const { return surface_size_; }
103 private:
104 // Helper function to generate names for the backing texture and FBO. On
105 // return, the resulting names can be attached to |fbo_|. |target| is
106 // the target type for the color buffer.
107 void AllocateRenderBuffers(GLenum target, const gfx::Size& size);
109 // Helper function to attach the buffers previously allocated by a call to
110 // AllocateRenderBuffers(). On return, |fbo_| can be used for
111 // rendering. |target| must be the same value as used in the call to
112 // AllocateRenderBuffers(). Returns |true| if the resulting framebuffer
113 // object is valid.
114 bool SetupFrameBufferObject(GLenum target);
116 gfx::Size ClampToValidDimensions(const gfx::Size& size);
118 // The OpenGL context, and pbuffer drawable, used to transfer data
119 // to the shared region (IOSurface).
120 scoped_refptr<gfx::GLSurface> gl_surface_;
121 scoped_refptr<gfx::GLContext> gl_context_;
122 base::ScopedCFTypeRef<IOSurfaceRef> io_surface_;
124 // The id of |io_surface_| or 0 if that's NULL.
125 uint32 io_surface_id_;
127 gfx::Size surface_size_;
128 // It's important to avoid allocating zero-width or zero-height
129 // IOSurfaces and textures on the Mac, so we clamp each to a minimum
130 // of 1. This is the real size of the surface; surface_size_ is what
131 // the user requested.
132 gfx::Size real_surface_size_;
133 // TODO(kbr): the FBO management should not be in this class at all.
134 // However, if it is factored out, care needs to be taken to not
135 // introduce another copy of the color data on the GPU; the direct
136 // binding of the internal texture to the IOSurface saves a copy.
137 bool allocate_fbo_;
138 // This texture object is always allocated, regardless of whether
139 // the user requests an FBO be allocated.
140 GLuint texture_;
141 // The FBO and renderbuffer are only allocated if allocate_fbo_ is
142 // true.
143 GLuint fbo_;
146 #endif // UI_SURFACE_ACCELERATED_SURFACE_MAC_H_