| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_COMPOSITOR_IO_SURFACE_TEXTURE_MAC_H_ | 5 #ifndef CONTENT_BROWSER_COMPOSITOR_IO_SURFACE_TEXTURE_MAC_H_ |
| 6 #define CONTENT_BROWSER_COMPOSITOR_IO_SURFACE_TEXTURE_MAC_H_ | 6 #define CONTENT_BROWSER_COMPOSITOR_IO_SURFACE_TEXTURE_MAC_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <list> | 9 #include <list> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 // This class manages an OpenGL context and IOSurfaceTexture for the accelerated | 41 // This class manages an OpenGL context and IOSurfaceTexture for the accelerated |
| 42 // compositing code path. The GL context is attached to | 42 // compositing code path. The GL context is attached to |
| 43 // RenderWidgetHostViewCocoa for blitting the IOSurfaceTexture. | 43 // RenderWidgetHostViewCocoa for blitting the IOSurfaceTexture. |
| 44 class IOSurfaceTexture | 44 class IOSurfaceTexture |
| 45 : public base::RefCounted<IOSurfaceTexture> { | 45 : public base::RefCounted<IOSurfaceTexture> { |
| 46 public: | 46 public: |
| 47 // Returns NULL if IOSurfaceTexture or GL API calls fail. | 47 // Returns NULL if IOSurfaceTexture or GL API calls fail. |
| 48 static scoped_refptr<IOSurfaceTexture> Create(); | 48 static scoped_refptr<IOSurfaceTexture> Create(); |
| 49 | 49 |
| 50 // Set IOSurfaceTexture that will be drawn on the next NSView drawRect. | 50 // Set IOSurfaceTexture that will be drawn on the next NSView drawRect. |
| 51 bool SetIOSurfaceWithContextCurrent( | 51 bool SetIOSurface( |
| 52 scoped_refptr<IOSurfaceContext> current_context, | 52 scoped_refptr<IOSurfaceContext> context, |
| 53 IOSurfaceID io_surface_handle, | 53 IOSurfaceID io_surface_id, |
| 54 const gfx::Size& size, | 54 const gfx::Size& pixel_size) WARN_UNUSED_RESULT; |
| 55 float scale_factor) WARN_UNUSED_RESULT; | |
| 56 | 55 |
| 57 // Get the CGL renderer ID currently associated with this context. | 56 // Get the CGL renderer ID currently associated with this context. |
| 58 int GetRendererID(); | 57 int GetRendererID(); |
| 59 | 58 |
| 60 // Blit the IOSurface to the rectangle specified by |window_rect| in DIPs, | 59 // Blit the IOSurface to the rectangle specified by |window_rect| in DIPs, |
| 61 // with the origin in the lower left corner. If the window rect's size is | 60 // with the origin in the lower left corner. If the window rect's size is |
| 62 // larger than the IOSurface, the remaining right and bottom edges will be | 61 // larger than the IOSurface, the remaining right and bottom edges will be |
| 63 // white. |window_scale_factor| is 1 in normal views, 2 in HiDPI views. | 62 // white. |window_scale_factor| is 1 in normal views, 2 in HiDPI views. |
| 64 bool DrawIOSurface( | 63 bool DrawIOSurface() WARN_UNUSED_RESULT; |
| 65 scoped_refptr<IOSurfaceContext> drawing_context, | |
| 66 const gfx::Rect& window_rect, | |
| 67 float window_scale_factor) WARN_UNUSED_RESULT; | |
| 68 | 64 |
| 69 bool HasIOSurface() { return !!io_surface_.get(); } | 65 bool HasIOSurface() { return !!io_surface_.get(); } |
| 70 | 66 |
| 71 const gfx::Size& pixel_io_surface_size() const { | |
| 72 return pixel_io_surface_size_; | |
| 73 } | |
| 74 // In cocoa view units / DIPs. | |
| 75 const gfx::Size& dip_io_surface_size() const { return dip_io_surface_size_; } | |
| 76 float scale_factor() const { return scale_factor_; } | |
| 77 | |
| 78 // Returns true if asynchronous readback is supported on this system. | |
| 79 bool IsAsynchronousReadbackSupported(); | |
| 80 | |
| 81 // Scan the list of started asynchronous copies and test if each one has | |
| 82 // completed. If |block_until_finished| is true, then block until all | |
| 83 // pending copies are finished. | |
| 84 void CheckIfAllCopiesAreFinished(bool block_until_finished); | |
| 85 | |
| 86 // Returns true if the offscreen context used by this surface has been | 67 // Returns true if the offscreen context used by this surface has been |
| 87 // poisoned. | 68 // poisoned. |
| 88 bool HasBeenPoisoned() const; | 69 bool HasBeenPoisoned() const; |
| 89 | 70 |
| 90 private: | 71 private: |
| 91 // Unref the IOSurfaceTexture and delete the associated GL texture. If the GPU | |
| 92 // process is no longer referencing it, this will delete the IOSurfaceTexture. | |
| 93 void UnrefIOSurface(); | |
| 94 | |
| 95 friend class base::RefCounted<IOSurfaceTexture>; | 72 friend class base::RefCounted<IOSurfaceTexture>; |
| 96 | 73 |
| 97 // Vertex structure for use in glDraw calls. | |
| 98 struct SurfaceVertex { | |
| 99 SurfaceVertex() : x_(0.0f), y_(0.0f), tx_(0.0f), ty_(0.0f) { } | |
| 100 void set(float x, float y, float tx, float ty) { | |
| 101 x_ = x; | |
| 102 y_ = y; | |
| 103 tx_ = tx; | |
| 104 ty_ = ty; | |
| 105 } | |
| 106 void set_position(float x, float y) { | |
| 107 x_ = x; | |
| 108 y_ = y; | |
| 109 } | |
| 110 void set_texcoord(float tx, float ty) { | |
| 111 tx_ = tx; | |
| 112 ty_ = ty; | |
| 113 } | |
| 114 float x_; | |
| 115 float y_; | |
| 116 float tx_; | |
| 117 float ty_; | |
| 118 }; | |
| 119 | |
| 120 // Counter-clockwise verts starting from upper-left corner (0, 0). | |
| 121 struct SurfaceQuad { | |
| 122 void set_size(gfx::Size vertex_size, gfx::Size texcoord_size) { | |
| 123 // Texture coordinates are flipped vertically so they can be drawn on | |
| 124 // a projection with a flipped y-axis (origin is top left). | |
| 125 float vw = static_cast<float>(vertex_size.width()); | |
| 126 float vh = static_cast<float>(vertex_size.height()); | |
| 127 float tw = static_cast<float>(texcoord_size.width()); | |
| 128 float th = static_cast<float>(texcoord_size.height()); | |
| 129 verts_[0].set(0.0f, 0.0f, 0.0f, th); | |
| 130 verts_[1].set(0.0f, vh, 0.0f, 0.0f); | |
| 131 verts_[2].set(vw, vh, tw, 0.0f); | |
| 132 verts_[3].set(vw, 0.0f, tw, th); | |
| 133 } | |
| 134 void set_rect(float x1, float y1, float x2, float y2) { | |
| 135 verts_[0].set_position(x1, y1); | |
| 136 verts_[1].set_position(x1, y2); | |
| 137 verts_[2].set_position(x2, y2); | |
| 138 verts_[3].set_position(x2, y1); | |
| 139 } | |
| 140 void set_texcoord_rect(float tx1, float ty1, float tx2, float ty2) { | |
| 141 // Texture coordinates are flipped vertically so they can be drawn on | |
| 142 // a projection with a flipped y-axis (origin is top left). | |
| 143 verts_[0].set_texcoord(tx1, ty2); | |
| 144 verts_[1].set_texcoord(tx1, ty1); | |
| 145 verts_[2].set_texcoord(tx2, ty1); | |
| 146 verts_[3].set_texcoord(tx2, ty2); | |
| 147 } | |
| 148 SurfaceVertex verts_[4]; | |
| 149 }; | |
| 150 | |
| 151 IOSurfaceTexture( | 74 IOSurfaceTexture( |
| 152 const scoped_refptr<IOSurfaceContext>& context); | 75 const scoped_refptr<IOSurfaceContext>& context); |
| 153 ~IOSurfaceTexture(); | 76 ~IOSurfaceTexture(); |
| 154 | 77 |
| 155 // Returns true if IOSurfaceTexture is ready to render. False otherwise. | 78 // Unref the IOSurfaceTexture and delete the associated GL texture. If the GPU |
| 156 bool MapIOSurfaceToTextureWithContextCurrent( | 79 // process is no longer referencing it, this will delete the IOSurface. |
| 157 const scoped_refptr<IOSurfaceContext>& current_context, | 80 void UnrefIOSurface(); |
| 158 const gfx::Size pixel_size, | |
| 159 float scale_factor, | |
| 160 IOSurfaceID io_surface_handle) WARN_UNUSED_RESULT; | |
| 161 | |
| 162 void UnrefIOSurfaceWithContextCurrent(); | 81 void UnrefIOSurfaceWithContextCurrent(); |
| 163 | 82 |
| 164 void DrawQuad(const SurfaceQuad& quad); | |
| 165 | |
| 166 // Check for GL errors and store the result in error_. Only return new | 83 // Check for GL errors and store the result in error_. Only return new |
| 167 // errors | 84 // errors |
| 168 GLenum GetAndSaveGLError(); | 85 GLenum GetAndSaveGLError(); |
| 169 | 86 |
| 170 // Offscreen context used for all operations other than drawing to the | 87 // Offscreen context used for all operations other than drawing to the |
| 171 // screen. This is in the same share group as the contexts used for | 88 // screen. This is in the same share group as the contexts used for |
| 172 // drawing, and is the same for all IOSurfaces in all windows. | 89 // drawing, and is the same for all IOSurfaces in all windows. |
| 173 scoped_refptr<IOSurfaceContext> offscreen_context_; | 90 scoped_refptr<IOSurfaceContext> offscreen_context_; |
| 174 | 91 |
| 175 // IOSurfaceTexture data. | 92 // The IOSurface and its non-rounded size. |
| 176 IOSurfaceID io_surface_handle_; | |
| 177 base::ScopedCFTypeRef<IOSurfaceRef> io_surface_; | 93 base::ScopedCFTypeRef<IOSurfaceRef> io_surface_; |
| 178 | 94 gfx::Size pixel_size_; |
| 179 // The width and height of the io surface. | |
| 180 gfx::Size pixel_io_surface_size_; // In pixels. | |
| 181 gfx::Size dip_io_surface_size_; // In view / density independent pixels. | |
| 182 float scale_factor_; | |
| 183 | 95 |
| 184 // The "live" OpenGL texture referring to this IOSurfaceRef. Note | 96 // The "live" OpenGL texture referring to this IOSurfaceRef. Note |
| 185 // that per the CGLTexImageIOSurface2D API we do not need to | 97 // that per the CGLTexImageIOSurface2D API we do not need to |
| 186 // explicitly update this texture's contents once created. All we | 98 // explicitly update this texture's contents once created. All we |
| 187 // need to do is ensure it is re-bound before attempting to draw | 99 // need to do is ensure it is re-bound before attempting to draw |
| 188 // with it. | 100 // with it. |
| 189 GLuint texture_; | 101 GLuint texture_; |
| 190 | 102 |
| 191 // Error saved by GetAndSaveGLError | 103 // Error saved by GetAndSaveGLError |
| 192 GLint gl_error_; | 104 GLint gl_error_; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 208 | 120 |
| 209 static void EvictionScheduleDoEvict(); | 121 static void EvictionScheduleDoEvict(); |
| 210 static void EvictionDoEvict(); | 122 static void EvictionDoEvict(); |
| 211 static base::LazyInstance<EvictionQueue> eviction_queue_; | 123 static base::LazyInstance<EvictionQueue> eviction_queue_; |
| 212 static bool eviction_scheduled_; | 124 static bool eviction_scheduled_; |
| 213 }; | 125 }; |
| 214 | 126 |
| 215 } // namespace content | 127 } // namespace content |
| 216 | 128 |
| 217 #endif // CONTENT_BROWSER_COMPOSITOR_IO_SURFACE_TEXTURE_MAC_H_ | 129 #endif // CONTENT_BROWSER_COMPOSITOR_IO_SURFACE_TEXTURE_MAC_H_ |
| OLD | NEW |