Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / accelerated_widget_mac / io_surface_texture.h
blobd971b584f3434f1f75cd5e3e0c1fcba0a1ce4f8d
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.
5 #ifndef UI_ACCELERATED_WIDGET_MAC_IO_SURFACE_TEXTURE_H_
6 #define UI_ACCELERATED_WIDGET_MAC_IO_SURFACE_TEXTURE_H_
8 #include <deque>
9 #include <list>
10 #include <vector>
12 #import <Cocoa/Cocoa.h>
13 #include <IOSurface/IOSurface.h>
14 #include <QuartzCore/QuartzCore.h>
16 #include "base/callback.h"
17 #include "base/lazy_instance.h"
18 #include "base/mac/scoped_cftyperef.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/time/time.h"
22 #include "ui/gfx/geometry/size.h"
23 #include "ui/gfx/native_widget_types.h"
25 class SkBitmap;
27 namespace gfx {
28 class Rect;
31 namespace ui {
33 class IOSurfaceContext;
34 class RenderWidgetHostViewFrameSubscriber;
35 class RenderWidgetHostViewMac;
37 // This class manages an OpenGL context and IOSurfaceTexture for the accelerated
38 // compositing code path. The GL context is attached to
39 // RenderWidgetHostViewCocoa for blitting the IOSurfaceTexture.
40 class IOSurfaceTexture
41 : public base::RefCounted<IOSurfaceTexture> {
42 public:
43 static scoped_refptr<IOSurfaceTexture> Create(
44 bool needs_gl_finish_workaround,
45 bool use_ns_apis);
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;
52 // Set IOSurfaceTexture that will be drawn on the next NSView drawRect.
53 bool SetIOSurface(
54 IOSurfaceID io_surface_id,
55 const gfx::Size& pixel_size) WARN_UNUSED_RESULT;
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.
61 bool DrawIOSurface() WARN_UNUSED_RESULT;
62 bool DrawIOSurfaceWithDamageRect(gfx::Rect damage_rect) WARN_UNUSED_RESULT;
64 // Returns true if the offscreen context used by this surface has been
65 // poisoned.
66 bool HasBeenPoisoned() const;
68 private:
69 friend class base::RefCounted<IOSurfaceTexture>;
71 IOSurfaceTexture(
72 const scoped_refptr<IOSurfaceContext>& context,
73 bool use_ns_apis,
74 bool needs_gl_finish_workaround);
75 ~IOSurfaceTexture();
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.
79 void ReleaseIOSurfaceAndTexture();
81 // Check for GL errors and store the result in error_. Only return new
82 // errors
83 GLenum GetAndSaveGLError();
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.
88 scoped_refptr<IOSurfaceContext> offscreen_context_;
90 // The IOSurface and its non-rounded size.
91 base::ScopedCFTypeRef<IOSurfaceRef> io_surface_;
92 gfx::Size pixel_size_;
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_;
101 // Error saved by GetAndSaveGLError
102 GLint gl_error_;
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,
113 typedef std::list<IOSurfaceTexture*> EvictionQueue;
114 void EvictionMarkUpdated();
115 void EvictionMarkEvicted();
116 EvictionQueue::iterator eviction_queue_iterator_;
117 bool eviction_has_been_drawn_since_updated_;
119 const bool needs_gl_finish_workaround_;
121 // Set if this is for access through NS APIs.
122 const bool using_ns_apis_;
124 static void EvictionScheduleDoEvict();
125 static void EvictionDoEvict();
126 static base::LazyInstance<EvictionQueue> eviction_queue_;
127 static bool eviction_scheduled_;
130 } // namespace ui
132 #endif // UI_ACCELERATED_WIDGET_MAC_IO_SURFACE_TEXTURE_H_