blob: 8ff9d63840e4d49aafc7bb188f2e5003bfcdab34 [file] [log] [blame]
[email protected]77a3bbbb2012-04-05 19:39:321// 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
tapted736e1442014-12-01 22:25:465#ifndef UI_ACCELERATED_WIDGET_MAC_IO_SURFACE_TEXTURE_H_
6#define UI_ACCELERATED_WIDGET_MAC_IO_SURFACE_TEXTURE_H_
[email protected]77a3bbbb2012-04-05 19:39:327
[email protected]520376b2013-03-24 03:18:178#include <deque>
[email protected]572be882014-04-24 03:43:159#include <list>
[email protected]5c307f92013-04-16 18:44:5610#include <vector>
[email protected]520376b2013-03-24 03:18:1711
[email protected]77a3bbbb2012-04-05 19:39:3212#import <Cocoa/Cocoa.h>
[email protected]c3a6b4a2014-06-04 09:25:5313#include <IOSurface/IOSurfaceAPI.h>
[email protected]77a3bbbb2012-04-05 19:39:3214#include <QuartzCore/QuartzCore.h>
15
[email protected]a02f64e42012-09-24 21:32:4116#include "base/callback.h"
[email protected]572be882014-04-24 03:43:1517#include "base/lazy_instance.h"
[email protected]77a3bbbb2012-04-05 19:39:3218#include "base/mac/scoped_cftyperef.h"
[email protected]f63bdd12014-05-23 10:35:2919#include "base/memory/ref_counted.h"
[email protected]ea2e5412013-03-21 23:07:5420#include "base/memory/scoped_ptr.h"
[email protected]a43858f2013-06-28 15:18:3721#include "base/time/time.h"
tapted736e1442014-12-01 22:25:4622#include "ui/gfx/geometry/size.h"
[email protected]77a3bbbb2012-04-05 19:39:3223#include "ui/gfx/native_widget_types.h"
[email protected]77a3bbbb2012-04-05 19:39:3224
[email protected]ea2e5412013-03-21 23:07:5425class SkBitmap;
[email protected]77a3bbbb2012-04-05 19:39:3226
[email protected]474e4dc2012-07-27 01:38:3427namespace gfx {
28class Rect;
29}
30
tapted736e1442014-12-01 22:25:4631namespace ui {
[email protected]fc4616f2012-07-21 01:29:5832
ccameron649349b2014-10-01 01:09:2333class IOSurfaceContext;
[email protected]1d3d39a2013-03-05 06:03:0034class RenderWidgetHostViewFrameSubscriber;
[email protected]f9f4d2d2013-06-07 15:20:1835class RenderWidgetHostViewMac;
[email protected]1d3d39a2013-03-05 06:03:0036
ccameron649349b2014-10-01 01:09:2337// This class manages an OpenGL context and IOSurfaceTexture for the accelerated
[email protected]77a3bbbb2012-04-05 19:39:3238// compositing code path. The GL context is attached to
ccameron649349b2014-10-01 01:09:2339// RenderWidgetHostViewCocoa for blitting the IOSurfaceTexture.
40class IOSurfaceTexture
41 : public base::RefCounted<IOSurfaceTexture> {
[email protected]77a3bbbb2012-04-05 19:39:3242 public:
tapted736e1442014-12-01 22:25:4643 static scoped_refptr<IOSurfaceTexture> Create(
44 bool needs_gl_finish_workaround);
[email protected]77a3bbbb2012-04-05 19:39:3245
ccameronefa5e1b2015-06-09 18:47:0246 // 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
ccameron649349b2014-10-01 01:09:2351 // Set IOSurfaceTexture that will be drawn on the next NSView drawRect.
ccameronfb55e5a2014-10-02 01:05:1752 bool SetIOSurface(
ccameronfb55e5a2014-10-02 01:05:1753 IOSurfaceID io_surface_id,
54 const gfx::Size& pixel_size) WARN_UNUSED_RESULT;
[email protected]77a3bbbb2012-04-05 19:39:3255
[email protected]c3e93242013-11-28 06:01:1556 // 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.
ccameronfb55e5a2014-10-02 01:05:1760 bool DrawIOSurface() WARN_UNUSED_RESULT;
ccameron2dc24662015-06-07 20:42:0361 bool DrawIOSurfaceWithDamageRect(gfx::Rect damage_rect) WARN_UNUSED_RESULT;
[email protected]77a3bbbb2012-04-05 19:39:3262
ccameron4f30f972014-10-09 20:37:3863 // Returns true if the offscreen context used by this surface has been
64 // poisoned.
65 bool HasBeenPoisoned() const;
[email protected]d7598022014-03-25 16:03:3266
[email protected]77a3bbbb2012-04-05 19:39:3267 private:
ccameron649349b2014-10-01 01:09:2368 friend class base::RefCounted<IOSurfaceTexture>;
[email protected]f63bdd12014-05-23 10:35:2969
ccameron649349b2014-10-01 01:09:2370 IOSurfaceTexture(
tapted736e1442014-12-01 22:25:4671 const scoped_refptr<IOSurfaceContext>& context,
72 bool needs_gl_finish_workaround);
ccameron649349b2014-10-01 01:09:2373 ~IOSurfaceTexture();
[email protected]4d23fa72013-05-09 09:20:1874
ccameronfb55e5a2014-10-02 01:05:1775 // Unref the IOSurfaceTexture and delete the associated GL texture. If the GPU
76 // process is no longer referencing it, this will delete the IOSurface.
ccamerone7747832014-10-07 23:11:3677 void ReleaseIOSurfaceAndTexture();
[email protected]77a3bbbb2012-04-05 19:39:3278
[email protected]58469852013-06-18 12:12:2179 // Check for GL errors and store the result in error_. Only return new
80 // errors
81 GLenum GetAndSaveGLError();
82
[email protected]c3e93242013-11-28 06:01:1583 // 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.
ccameron4f30f972014-10-09 20:37:3886 scoped_refptr<IOSurfaceContext> offscreen_context_;
[email protected]77a3bbbb2012-04-05 19:39:3287
ccameronfb55e5a2014-10-02 01:05:1788 // The IOSurface and its non-rounded size.
[email protected]c3a6b4a2014-06-04 09:25:5389 base::ScopedCFTypeRef<IOSurfaceRef> io_surface_;
ccameronfb55e5a2014-10-02 01:05:1790 gfx::Size pixel_size_;
[email protected]77a3bbbb2012-04-05 19:39:3291
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]58469852013-06-18 12:12:2199 // Error saved by GetAndSaveGLError
100 GLint gl_error_;
[email protected]572be882014-04-24 03:43:15101
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 };
ccameron649349b2014-10-01 01:09:23111 typedef std::list<IOSurfaceTexture*> EvictionQueue;
[email protected]572be882014-04-24 03:43:15112 void EvictionMarkUpdated();
113 void EvictionMarkEvicted();
114 EvictionQueue::iterator eviction_queue_iterator_;
115 bool eviction_has_been_drawn_since_updated_;
tapted736e1442014-12-01 22:25:46116 const bool needs_gl_finish_workaround_;
[email protected]572be882014-04-24 03:43:15117
118 static void EvictionScheduleDoEvict();
119 static void EvictionDoEvict();
120 static base::LazyInstance<EvictionQueue> eviction_queue_;
121 static bool eviction_scheduled_;
[email protected]77a3bbbb2012-04-05 19:39:32122};
123
tapted736e1442014-12-01 22:25:46124} // namespace ui
[email protected]fc4616f2012-07-21 01:29:58125
tapted736e1442014-12-01 22:25:46126#endif // UI_ACCELERATED_WIDGET_MAC_IO_SURFACE_TEXTURE_H_