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