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