[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 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 | |
tapted | 736e144 | 2014-12-01 22:25:46 | [diff] [blame] | 5 | #ifndef UI_ACCELERATED_WIDGET_MAC_IO_SURFACE_TEXTURE_H_ |
| 6 | #define UI_ACCELERATED_WIDGET_MAC_IO_SURFACE_TEXTURE_H_ |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 7 | |
[email protected] | 520376b | 2013-03-24 03:18:17 | [diff] [blame] | 8 | #include <deque> |
[email protected] | 572be88 | 2014-04-24 03:43:15 | [diff] [blame] | 9 | #include <list> |
[email protected] | 5c307f9 | 2013-04-16 18:44:56 | [diff] [blame] | 10 | #include <vector> |
[email protected] | 520376b | 2013-03-24 03:18:17 | [diff] [blame] | 11 | |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 12 | #import <Cocoa/Cocoa.h> |
[email protected] | c3a6b4a | 2014-06-04 09:25:53 | [diff] [blame] | 13 | #include <IOSurface/IOSurfaceAPI.h> |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 14 | #include <QuartzCore/QuartzCore.h> |
| 15 | |
[email protected] | a02f64e4 | 2012-09-24 21:32:41 | [diff] [blame] | 16 | #include "base/callback.h" |
[email protected] | 572be88 | 2014-04-24 03:43:15 | [diff] [blame] | 17 | #include "base/lazy_instance.h" |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 18 | #include "base/mac/scoped_cftyperef.h" |
[email protected] | f63bdd1 | 2014-05-23 10:35:29 | [diff] [blame] | 19 | #include "base/memory/ref_counted.h" |
[email protected] | ea2e541 | 2013-03-21 23:07:54 | [diff] [blame] | 20 | #include "base/memory/scoped_ptr.h" |
[email protected] | a43858f | 2013-06-28 15:18:37 | [diff] [blame] | 21 | #include "base/time/time.h" |
tapted | 736e144 | 2014-12-01 22:25:46 | [diff] [blame] | 22 | #include "ui/gfx/geometry/size.h" |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 23 | #include "ui/gfx/native_widget_types.h" |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 24 | |
[email protected] | ea2e541 | 2013-03-21 23:07:54 | [diff] [blame] | 25 | class SkBitmap; |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 26 | |
[email protected] | 474e4dc | 2012-07-27 01:38:34 | [diff] [blame] | 27 | namespace gfx { |
| 28 | class Rect; |
| 29 | } |
| 30 | |
tapted | 736e144 | 2014-12-01 22:25:46 | [diff] [blame] | 31 | namespace ui { |
[email protected] | fc4616f | 2012-07-21 01:29:58 | [diff] [blame] | 32 | |
ccameron | 649349b | 2014-10-01 01:09:23 | [diff] [blame] | 33 | class IOSurfaceContext; |
[email protected] | 1d3d39a | 2013-03-05 06:03:00 | [diff] [blame] | 34 | class RenderWidgetHostViewFrameSubscriber; |
[email protected] | f9f4d2d | 2013-06-07 15:20:18 | [diff] [blame] | 35 | class RenderWidgetHostViewMac; |
[email protected] | 1d3d39a | 2013-03-05 06:03:00 | [diff] [blame] | 36 | |
ccameron | 649349b | 2014-10-01 01:09:23 | [diff] [blame] | 37 | // This class manages an OpenGL context and IOSurfaceTexture for the accelerated |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 38 | // compositing code path. The GL context is attached to |
ccameron | 649349b | 2014-10-01 01:09:23 | [diff] [blame] | 39 | // RenderWidgetHostViewCocoa for blitting the IOSurfaceTexture. |
| 40 | class IOSurfaceTexture |
| 41 | : public base::RefCounted<IOSurfaceTexture> { |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 42 | public: |
tapted | 736e144 | 2014-12-01 22:25:46 | [diff] [blame] | 43 | static scoped_refptr<IOSurfaceTexture> Create( |
| 44 | bool needs_gl_finish_workaround); |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 45 | |
ccameron | efa5e1b | 2015-06-09 18:47:02 | [diff] [blame^] | 46 | // Returns true if there is no need to call SetIOSurface with the provided |
| 47 | // values. |
| 48 | bool IsUpToDate( |
| 49 | IOSurfaceID io_surface_id, const gfx::Size& pixel_size) const; |
| 50 | |
ccameron | 649349b | 2014-10-01 01:09:23 | [diff] [blame] | 51 | // Set IOSurfaceTexture that will be drawn on the next NSView drawRect. |
ccameron | fb55e5a | 2014-10-02 01:05:17 | [diff] [blame] | 52 | bool SetIOSurface( |
ccameron | fb55e5a | 2014-10-02 01:05:17 | [diff] [blame] | 53 | IOSurfaceID io_surface_id, |
| 54 | const gfx::Size& pixel_size) WARN_UNUSED_RESULT; |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 55 | |
[email protected] | c3e9324 | 2013-11-28 06:01:15 | [diff] [blame] | 56 | // Blit the IOSurface to the rectangle specified by |window_rect| in DIPs, |
| 57 | // with the origin in the lower left corner. If the window rect's size is |
| 58 | // larger than the IOSurface, the remaining right and bottom edges will be |
| 59 | // white. |window_scale_factor| is 1 in normal views, 2 in HiDPI views. |
ccameron | fb55e5a | 2014-10-02 01:05:17 | [diff] [blame] | 60 | bool DrawIOSurface() WARN_UNUSED_RESULT; |
ccameron | 2dc2466 | 2015-06-07 20:42:03 | [diff] [blame] | 61 | bool DrawIOSurfaceWithDamageRect(gfx::Rect damage_rect) WARN_UNUSED_RESULT; |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 62 | |
ccameron | 4f30f97 | 2014-10-09 20:37:38 | [diff] [blame] | 63 | // Returns true if the offscreen context used by this surface has been |
| 64 | // poisoned. |
| 65 | bool HasBeenPoisoned() const; |
[email protected] | d759802 | 2014-03-25 16:03:32 | [diff] [blame] | 66 | |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 67 | private: |
ccameron | 649349b | 2014-10-01 01:09:23 | [diff] [blame] | 68 | friend class base::RefCounted<IOSurfaceTexture>; |
[email protected] | f63bdd1 | 2014-05-23 10:35:29 | [diff] [blame] | 69 | |
ccameron | 649349b | 2014-10-01 01:09:23 | [diff] [blame] | 70 | IOSurfaceTexture( |
tapted | 736e144 | 2014-12-01 22:25:46 | [diff] [blame] | 71 | const scoped_refptr<IOSurfaceContext>& context, |
| 72 | bool needs_gl_finish_workaround); |
ccameron | 649349b | 2014-10-01 01:09:23 | [diff] [blame] | 73 | ~IOSurfaceTexture(); |
[email protected] | 4d23fa7 | 2013-05-09 09:20:18 | [diff] [blame] | 74 | |
ccameron | fb55e5a | 2014-10-02 01:05:17 | [diff] [blame] | 75 | // Unref the IOSurfaceTexture and delete the associated GL texture. If the GPU |
| 76 | // process is no longer referencing it, this will delete the IOSurface. |
ccameron | e774783 | 2014-10-07 23:11:36 | [diff] [blame] | 77 | void ReleaseIOSurfaceAndTexture(); |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 78 | |
[email protected] | 5846985 | 2013-06-18 12:12:21 | [diff] [blame] | 79 | // Check for GL errors and store the result in error_. Only return new |
| 80 | // errors |
| 81 | GLenum GetAndSaveGLError(); |
| 82 | |
[email protected] | c3e9324 | 2013-11-28 06:01:15 | [diff] [blame] | 83 | // Offscreen context used for all operations other than drawing to the |
| 84 | // screen. This is in the same share group as the contexts used for |
| 85 | // drawing, and is the same for all IOSurfaces in all windows. |
ccameron | 4f30f97 | 2014-10-09 20:37:38 | [diff] [blame] | 86 | scoped_refptr<IOSurfaceContext> offscreen_context_; |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 87 | |
ccameron | fb55e5a | 2014-10-02 01:05:17 | [diff] [blame] | 88 | // The IOSurface and its non-rounded size. |
[email protected] | c3a6b4a | 2014-06-04 09:25:53 | [diff] [blame] | 89 | base::ScopedCFTypeRef<IOSurfaceRef> io_surface_; |
ccameron | fb55e5a | 2014-10-02 01:05:17 | [diff] [blame] | 90 | gfx::Size pixel_size_; |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 91 | |
| 92 | // The "live" OpenGL texture referring to this IOSurfaceRef. Note |
| 93 | // that per the CGLTexImageIOSurface2D API we do not need to |
| 94 | // explicitly update this texture's contents once created. All we |
| 95 | // need to do is ensure it is re-bound before attempting to draw |
| 96 | // with it. |
| 97 | GLuint texture_; |
| 98 | |
[email protected] | 5846985 | 2013-06-18 12:12:21 | [diff] [blame] | 99 | // Error saved by GetAndSaveGLError |
| 100 | GLint gl_error_; |
[email protected] | 572be88 | 2014-04-24 03:43:15 | [diff] [blame] | 101 | |
| 102 | // Aggressive IOSurface eviction logic. When using CoreAnimation, IOSurfaces |
| 103 | // are used only transiently to transfer from the GPU process to the browser |
| 104 | // process. Once the IOSurface has been drawn to its CALayer, the CALayer |
| 105 | // will not need updating again until its view is hidden and re-shown. |
| 106 | // Aggressively evict surfaces when more than 8 (the number allowed by the |
| 107 | // memory manager for fast tab switching) are allocated. |
| 108 | enum { |
| 109 | kMaximumUnevictedSurfaces = 8, |
| 110 | }; |
ccameron | 649349b | 2014-10-01 01:09:23 | [diff] [blame] | 111 | typedef std::list<IOSurfaceTexture*> EvictionQueue; |
[email protected] | 572be88 | 2014-04-24 03:43:15 | [diff] [blame] | 112 | void EvictionMarkUpdated(); |
| 113 | void EvictionMarkEvicted(); |
| 114 | EvictionQueue::iterator eviction_queue_iterator_; |
| 115 | bool eviction_has_been_drawn_since_updated_; |
tapted | 736e144 | 2014-12-01 22:25:46 | [diff] [blame] | 116 | const bool needs_gl_finish_workaround_; |
[email protected] | 572be88 | 2014-04-24 03:43:15 | [diff] [blame] | 117 | |
| 118 | static void EvictionScheduleDoEvict(); |
| 119 | static void EvictionDoEvict(); |
| 120 | static base::LazyInstance<EvictionQueue> eviction_queue_; |
| 121 | static bool eviction_scheduled_; |
[email protected] | 77a3bbbb | 2012-04-05 19:39:32 | [diff] [blame] | 122 | }; |
| 123 | |
tapted | 736e144 | 2014-12-01 22:25:46 | [diff] [blame] | 124 | } // namespace ui |
[email protected] | fc4616f | 2012-07-21 01:29:58 | [diff] [blame] | 125 | |
tapted | 736e144 | 2014-12-01 22:25:46 | [diff] [blame] | 126 | #endif // UI_ACCELERATED_WIDGET_MAC_IO_SURFACE_TEXTURE_H_ |