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