Make castv2 performance test work.
[chromium-blink-merge.git] / ui / accelerated_widget_mac / io_surface_texture.h
blob473e69cb4a37475ca67d03a6a684c322b661e263
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/IOSurfaceAPI.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 // Returns NULL if IOSurfaceTexture or GL API calls fail.
44 static scoped_refptr<IOSurfaceTexture> Create(
45 bool needs_gl_finish_workaround);
47 // Set IOSurfaceTexture that will be drawn on the next NSView drawRect.
48 bool SetIOSurface(
49 IOSurfaceID io_surface_id,
50 const gfx::Size& pixel_size) WARN_UNUSED_RESULT;
52 // Blit the IOSurface to the rectangle specified by |window_rect| in DIPs,
53 // with the origin in the lower left corner. If the window rect's size is
54 // larger than the IOSurface, the remaining right and bottom edges will be
55 // white. |window_scale_factor| is 1 in normal views, 2 in HiDPI views.
56 bool DrawIOSurface() WARN_UNUSED_RESULT;
58 // Returns true if the offscreen context used by this surface has been
59 // poisoned.
60 bool HasBeenPoisoned() const;
62 private:
63 friend class base::RefCounted<IOSurfaceTexture>;
65 IOSurfaceTexture(
66 const scoped_refptr<IOSurfaceContext>& context,
67 bool needs_gl_finish_workaround);
68 ~IOSurfaceTexture();
70 // Unref the IOSurfaceTexture and delete the associated GL texture. If the GPU
71 // process is no longer referencing it, this will delete the IOSurface.
72 void ReleaseIOSurfaceAndTexture();
74 // Check for GL errors and store the result in error_. Only return new
75 // errors
76 GLenum GetAndSaveGLError();
78 // Offscreen context used for all operations other than drawing to the
79 // screen. This is in the same share group as the contexts used for
80 // drawing, and is the same for all IOSurfaces in all windows.
81 scoped_refptr<IOSurfaceContext> offscreen_context_;
83 // The IOSurface and its non-rounded size.
84 base::ScopedCFTypeRef<IOSurfaceRef> io_surface_;
85 gfx::Size pixel_size_;
87 // The "live" OpenGL texture referring to this IOSurfaceRef. Note
88 // that per the CGLTexImageIOSurface2D API we do not need to
89 // explicitly update this texture's contents once created. All we
90 // need to do is ensure it is re-bound before attempting to draw
91 // with it.
92 GLuint texture_;
94 // Error saved by GetAndSaveGLError
95 GLint gl_error_;
97 // Aggressive IOSurface eviction logic. When using CoreAnimation, IOSurfaces
98 // are used only transiently to transfer from the GPU process to the browser
99 // process. Once the IOSurface has been drawn to its CALayer, the CALayer
100 // will not need updating again until its view is hidden and re-shown.
101 // Aggressively evict surfaces when more than 8 (the number allowed by the
102 // memory manager for fast tab switching) are allocated.
103 enum {
104 kMaximumUnevictedSurfaces = 8,
106 typedef std::list<IOSurfaceTexture*> EvictionQueue;
107 void EvictionMarkUpdated();
108 void EvictionMarkEvicted();
109 EvictionQueue::iterator eviction_queue_iterator_;
110 bool eviction_has_been_drawn_since_updated_;
111 const bool needs_gl_finish_workaround_;
113 static void EvictionScheduleDoEvict();
114 static void EvictionDoEvict();
115 static base::LazyInstance<EvictionQueue> eviction_queue_;
116 static bool eviction_scheduled_;
119 } // namespace ui
121 #endif // UI_ACCELERATED_WIDGET_MAC_IO_SURFACE_TEXTURE_H_