15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef UI_SURFACE_ACCELERATED_SURFACE_MAC_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UI_SURFACE_ACCELERATED_SURFACE_MAC_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <CoreFoundation/CoreFoundation.h>
946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include <IOSurface/IOSurfaceAPI.h>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/mac/scoped_cftyperef.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/rect.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/size.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gl/gl_context.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gl/gl_surface.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gl/gpu_preference.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/surface/surface_export.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Should not include GL headers in a header file. Forward declare these types
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// instead.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef struct _CGLContextObject* CGLContextObj;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef unsigned int GLenum;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef unsigned int GLuint;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace gfx {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Rect;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Encapsulates an accelerated GL surface that can be shared across processes
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// on systems that support it (10.6 and above).
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SURFACE_EXPORT AcceleratedSurface {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AcceleratedSurface();
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~AcceleratedSurface();
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set up internal buffers. |share_context|, if non-NULL, is a context
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // with which the internally created OpenGL context shares textures and
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // other resources. |allocate_fbo| indicates whether or not this surface
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // should allocate an offscreen frame buffer object (FBO) internally. If
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not, then the user is expected to allocate one. NOTE that allocating
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // an FBO internally does NOT work properly with client code which uses
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // OpenGL (i.e., via GLES2 command buffers), because the GLES2
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // implementation does not know to bind the accelerated surface's
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // internal FBO when the default FBO is bound. |gpu_preference| indicates
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the GPU preference for the internally allocated GLContext. If
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |share_context| is non-NULL, then on platforms supporting dual GPUs,
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // its GPU preference must match the passed one. Returns false upon
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // failure.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Initialize(gfx::GLContext* share_context,
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  bool allocate_fbo,
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  gfx::GpuPreference gpu_preference);
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tear down. Must be called before destructor to prevent leaks.
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Destroy();
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These methods are used only once the accelerated surface is initialized.
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the accelerated surface to the given size, creating a new one if
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the height or width changes. Returns a unique id of the IOSurface to
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // which the surface is bound, or 0 if no changes were made or an error
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // occurred. MakeCurrent() will have been called on the new surface.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32 SetSurfaceSize(const gfx::Size& size);
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns the id of this surface's IOSurface.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32 GetSurfaceId();
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the GL context to be the current one for drawing. Returns true if
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it succeeded.
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool MakeCurrent();
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Clear the surface to be transparent. Assumes the caller has already called
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // MakeCurrent().
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Clear(const gfx::Rect& rect);
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Call after making changes to the surface which require a visual update.
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Makes the rendering show up in other processes. Assumes the caller has
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // already called MakeCurrent().
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If this AcceleratedSurface is configured with its own FBO, then
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // this call causes the color buffer to be transmitted. Otherwise,
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it causes the frame buffer of the current GL context to be copied
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // into an internal texture via glCopyTexSubImage2D.
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The size of the rectangle copied is the size last specified via
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SetSurfaceSize.  If another GL context than the one this
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // AcceleratedSurface contains is responsible for the production of
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the pixels, then when this entry point is called, the color
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // buffer must be in a state where a glCopyTexSubImage2D is
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // legal. (For example, if using multisampled FBOs, the FBO must
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // have been resolved into a non-multisampled color texture.)
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Additionally, in this situation, the contexts must share
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // server-side GL objects, so that this AcceleratedSurface's texture
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // is a legal name in the namespace of the current context.
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SwapBuffers();
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CGLContextObj context() {
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return static_cast<CGLContextObj>(gl_context_->GetHandle());
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the accelerated surface size.
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Size GetSize() const { return surface_size_; }
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function to generate names for the backing texture and FBO.  On
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // return, the resulting names can be attached to |fbo_|.  |target| is
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the target type for the color buffer.
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AllocateRenderBuffers(GLenum target, const gfx::Size& size);
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function to attach the buffers previously allocated by a call to
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // AllocateRenderBuffers().  On return, |fbo_| can be used for
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // rendering.  |target| must be the same value as used in the call to
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // AllocateRenderBuffers().  Returns |true| if the resulting framebuffer
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // object is valid.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SetupFrameBufferObject(GLenum target);
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Size ClampToValidDimensions(const gfx::Size& size);
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The OpenGL context, and pbuffer drawable, used to transfer data
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // to the shared region (IOSurface).
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<gfx::GLSurface> gl_surface_;
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<gfx::GLContext> gl_context_;
12246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  base::ScopedCFTypeRef<IOSurfaceRef> io_surface_;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The id of |io_surface_| or 0 if that's NULL.
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32 io_surface_id_;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Size surface_size_;
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It's important to avoid allocating zero-width or zero-height
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // IOSurfaces and textures on the Mac, so we clamp each to a minimum
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of 1. This is the real size of the surface; surface_size_ is what
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the user requested.
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  gfx::Size real_surface_size_;
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(kbr): the FBO management should not be in this class at all.
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // However, if it is factored out, care needs to be taken to not
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // introduce another copy of the color data on the GPU; the direct
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // binding of the internal texture to the IOSurface saves a copy.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool allocate_fbo_;
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This texture object is always allocated, regardless of whether
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the user requests an FBO be allocated.
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GLuint texture_;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The FBO and renderbuffer are only allocated if allocate_fbo_ is
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // true.
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GLuint fbo_;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // UI_SURFACE_ACCELERATED_SURFACE_MAC_H_
147