blob: d971b584f3434f1f75cd5e3e0c1fcba0a1ce4f8d [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>
dongseong.hwang961aa9d2015-07-21 20:12:1013#include <IOSurface/IOSurface.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(
ccameron347a77b2015-06-10 22:44:0444 bool needs_gl_finish_workaround,
45 bool use_ns_apis);
[email protected]77a3bbbb2012-04-05 19:39:3246
ccameronefa5e1b2015-06-09 18:47:0247 // 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
ccameron649349b2014-10-01 01:09:2352 // Set IOSurfaceTexture that will be drawn on the next NSView drawRect.
ccameronfb55e5a2014-10-02 01:05:1753 bool SetIOSurface(
ccameronfb55e5a2014-10-02 01:05:1754 IOSurfaceID io_surface_id,
55 const gfx::Size& pixel_size) WARN_UNUSED_RESULT;
[email protected]77a3bbbb2012-04-05 19:39:3256
[email protected]c3e93242013-11-28 06:01:1557 // 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.
ccameronfb55e5a2014-10-02 01:05:1761 bool DrawIOSurface() WARN_UNUSED_RESULT;
ccameron2dc24662015-06-07 20:42:0362 bool DrawIOSurfaceWithDamageRect(gfx::Rect damage_rect) WARN_UNUSED_RESULT;
[email protected]77a3bbbb2012-04-05 19:39:3263
ccameron4f30f972014-10-09 20:37:3864 // Returns true if the offscreen context used by this surface has been
65 // poisoned.
66 bool HasBeenPoisoned() const;
[email protected]d7598022014-03-25 16:03:3267
[email protected]77a3bbbb2012-04-05 19:39:3268 private:
ccameron649349b2014-10-01 01:09:2369 friend class base::RefCounted<IOSurfaceTexture>;
[email protected]f63bdd12014-05-23 10:35:2970
ccameron649349b2014-10-01 01:09:2371 IOSurfaceTexture(
tapted736e1442014-12-01 22:25:4672 const scoped_refptr<IOSurfaceContext>& context,
ccameron347a77b2015-06-10 22:44:0473 bool use_ns_apis,
tapted736e1442014-12-01 22:25:4674 bool needs_gl_finish_workaround);
ccameron649349b2014-10-01 01:09:2375 ~IOSurfaceTexture();
[email protected]4d23fa72013-05-09 09:20:1876
ccameronfb55e5a2014-10-02 01:05:1777 // Unref the IOSurfaceTexture and delete the associated GL texture. If the GPU
78 // process is no longer referencing it, this will delete the IOSurface.
ccamerone7747832014-10-07 23:11:3679 void ReleaseIOSurfaceAndTexture();
[email protected]77a3bbbb2012-04-05 19:39:3280
[email protected]58469852013-06-18 12:12:2181 // Check for GL errors and store the result in error_. Only return new
82 // errors
83 GLenum GetAndSaveGLError();
84
[email protected]c3e93242013-11-28 06:01:1585 // 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.
ccameron4f30f972014-10-09 20:37:3888 scoped_refptr<IOSurfaceContext> offscreen_context_;
[email protected]77a3bbbb2012-04-05 19:39:3289
ccameronfb55e5a2014-10-02 01:05:1790 // The IOSurface and its non-rounded size.
[email protected]c3a6b4a2014-06-04 09:25:5391 base::ScopedCFTypeRef<IOSurfaceRef> io_surface_;
ccameronfb55e5a2014-10-02 01:05:1792 gfx::Size pixel_size_;
[email protected]77a3bbbb2012-04-05 19:39:3293
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]58469852013-06-18 12:12:21101 // Error saved by GetAndSaveGLError
102 GLint gl_error_;
[email protected]572be882014-04-24 03:43:15103
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 };
ccameron649349b2014-10-01 01:09:23113 typedef std::list<IOSurfaceTexture*> EvictionQueue;
[email protected]572be882014-04-24 03:43:15114 void EvictionMarkUpdated();
115 void EvictionMarkEvicted();
116 EvictionQueue::iterator eviction_queue_iterator_;
117 bool eviction_has_been_drawn_since_updated_;
ccameron347a77b2015-06-10 22:44:04118
tapted736e1442014-12-01 22:25:46119 const bool needs_gl_finish_workaround_;
[email protected]572be882014-04-24 03:43:15120
ccameron347a77b2015-06-10 22:44:04121 // Set if this is for access through NS APIs.
122 const bool using_ns_apis_;
123
[email protected]572be882014-04-24 03:43:15124 static void EvictionScheduleDoEvict();
125 static void EvictionDoEvict();
126 static base::LazyInstance<EvictionQueue> eviction_queue_;
127 static bool eviction_scheduled_;
[email protected]77a3bbbb2012-04-05 19:39:32128};
129
tapted736e1442014-12-01 22:25:46130} // namespace ui
[email protected]fc4616f2012-07-21 01:29:58131
tapted736e1442014-12-01 22:25:46132#endif // UI_ACCELERATED_WIDGET_MAC_IO_SURFACE_TEXTURE_H_