compositing_iosurface_context_mac.h revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
1// Copyright (c) 2013 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
5#ifndef CONTENT_BROWSER_RENDERER_HOST_COMPOSITING_IOSURFACE_CONTEXT_MAC_H_
6#define CONTENT_BROWSER_RENDERER_HOST_COMPOSITING_IOSURFACE_CONTEXT_MAC_H_
7
8#import <AppKit/NSOpenGL.h>
9#include <OpenGL/OpenGL.h>
10#include <map>
11
12#include "base/basictypes.h"
13#include "base/lazy_instance.h"
14#include "base/mac/scoped_nsobject.h"
15#include "base/memory/ref_counted.h"
16#include "base/memory/scoped_ptr.h"
17#include "content/public/browser/gpu_data_manager_observer.h"
18#include "ui/gl/scoped_cgl.h"
19
20namespace content {
21
22class CompositingIOSurfaceShaderPrograms;
23
24class CompositingIOSurfaceContext
25    : public base::RefCounted<CompositingIOSurfaceContext>,
26      public content::GpuDataManagerObserver {
27 public:
28  enum {
29    // The number used to look up the context used for async readback and for
30    // initializing the IOSurface.
31    kOffscreenContextWindowNumber = -2,
32    // The number used to look up the context used by CAOpenGLLayers.
33    kCALayerContextWindowNumber = -3,
34  };
35
36  // Get or create a GL context for the specified window with the specified
37  // surface ordering. Share these GL contexts as much as possible because
38  // creating and destroying them can be expensive
39  // http://crbug.com/180463
40  static scoped_refptr<CompositingIOSurfaceContext> Get(int window_number);
41
42  // Mark that all the GL contexts in the same sharegroup as this context as
43  // invalid, so they shouldn't be returned anymore by Get, but rather, new
44  // contexts should be created. This is called as a precaution when unexpected
45  // GL errors occur.
46  void PoisonContextAndSharegroup();
47  bool HasBeenPoisoned() const { return poisoned_; }
48
49  CompositingIOSurfaceShaderPrograms* shader_program_cache() const {
50    return shader_program_cache_.get();
51  }
52  CGLContextObj cgl_context() const { return cgl_context_; }
53  bool is_vsync_disabled() const { return is_vsync_disabled_; }
54  int window_number() const { return window_number_; }
55
56  // content::GpuDataManagerObserver implementation.
57  virtual void OnGpuSwitching() OVERRIDE;
58
59 private:
60  friend class base::RefCounted<CompositingIOSurfaceContext>;
61
62  CompositingIOSurfaceContext(
63      int window_number,
64      base::ScopedTypeRef<CGLContextObj> clg_context_strong,
65      CGLContextObj clg_context,
66      bool is_vsync_disabled_,
67      scoped_ptr<CompositingIOSurfaceShaderPrograms> shader_program_cache);
68  virtual ~CompositingIOSurfaceContext();
69
70  int window_number_;
71  base::ScopedTypeRef<CGLContextObj> cgl_context_strong_;
72  // Weak, backed by |cgl_context_strong_|.
73  CGLContextObj cgl_context_;
74
75  bool is_vsync_disabled_;
76  scoped_ptr<CompositingIOSurfaceShaderPrograms> shader_program_cache_;
77  bool poisoned_;
78
79  // The global map from window number and window ordering to
80  // context data.
81  typedef std::map<int, CompositingIOSurfaceContext*> WindowMap;
82  static base::LazyInstance<WindowMap> window_map_;
83  static WindowMap* window_map();
84};
85
86}  // namespace content
87
88#endif  // CONTENT_BROWSER_RENDERER_HOST_COMPOSITING_IOSURFACE_CONTEXT_MAC_H_
89