| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "content/common/gpu/media/rendering_helper.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
| 8 #import <OpenGL/gl.h> |
| 9 #import <OpenGL/CGLMacro.h> |
| 10 |
| 11 #include "base/mac/scoped_nsautorelease_pool.h" |
| 12 #include "base/memory/scoped_nsobject.h" |
| 13 #include "base/message_loop.h" |
| 14 #include "base/synchronization/waitable_event.h" |
| 15 |
| 16 // Gets the pixel format to be used by the OpenGL view. |
| 17 static scoped_nsobject<NSOpenGLPixelFormat> GetPixelFormat() { |
| 18 NSOpenGLPixelFormatAttribute attributes[] = { |
| 19 NSOpenGLPFAWindow, |
| 20 NSOpenGLPFADoubleBuffer, |
| 21 NSOpenGLPFAAccelerated, |
| 22 NSOpenGLPFANoRecovery, |
| 23 NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)32, |
| 24 NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute)8, |
| 25 NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)24, |
| 26 (NSOpenGLPixelFormatAttribute)0 |
| 27 }; |
| 28 return scoped_nsobject<NSOpenGLPixelFormat>( |
| 29 [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes]); |
| 30 } |
| 31 |
| 32 // Gets the CGLContext from the given OpenGL view. |
| 33 static CGLContextObj GetCGLContext(NSOpenGLView* gl_view) { |
| 34 return static_cast<CGLContextObj>([[gl_view openGLContext] CGLContextObj]); |
| 35 } |
| 36 |
| 37 // Sets up a view port for the OpenGL view. |
| 38 static void SetupGLViewPort(NSOpenGLView* gl_view, int width, int height) { |
| 39 CGLContextObj cgl_ctx = GetCGLContext(gl_view); |
| 40 glViewport(0, 0, width, height); |
| 41 glClearColor(1.0, 0.0, 0.0, 0.0); |
| 42 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 43 [[gl_view openGLContext] flushBuffer]; |
| 44 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); |
| 45 } |
| 46 |
| 47 // Draw the given texture to the OpenGL view. |
| 48 static void DrawTexture(NSOpenGLView* gl_view, GLuint texture_id) { |
| 49 CGLContextObj cgl_ctx = GetCGLContext(gl_view); |
| 50 [gl_view lockFocus]; |
| 51 |
| 52 GLfloat width = [gl_view bounds].size.width; |
| 53 GLfloat height = [gl_view bounds].size.height; |
| 54 |
| 55 glEnable(GL_TEXTURE_RECTANGLE_ARB); |
| 56 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture_id); |
| 57 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 58 |
| 59 glBegin(GL_QUADS); |
| 60 glTexCoord2f(0.0, height); |
| 61 glVertex3f(-1.0, -1.0, 0.0); |
| 62 glTexCoord2f(width, height); |
| 63 glVertex3f(1.0, -1.0, 0.0); |
| 64 glTexCoord2f(width, 0.0); |
| 65 glVertex3f(1.0, 1.0, 0.0); |
| 66 glTexCoord2f(0.0, 0.0); |
| 67 glVertex3f(-1.0, 1.0, 0.0); |
| 68 glEnd(); |
| 69 |
| 70 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); |
| 71 glDisable(GL_TEXTURE_RECTANGLE_ARB); |
| 72 |
| 73 [[gl_view openGLContext] flushBuffer]; |
| 74 [gl_view unlockFocus]; |
| 75 CHECK_EQ(static_cast<int>(glGetError()), GL_NO_ERROR); |
| 76 } |
| 77 |
| 78 namespace video_test_util { |
| 79 |
| 80 class RenderingHelperMac : public RenderingHelper { |
| 81 public: |
| 82 RenderingHelperMac(); |
| 83 virtual ~RenderingHelperMac(); |
| 84 |
| 85 // Implement RenderingHelper. |
| 86 virtual void Initialize(bool suppress_swap_to_display, |
| 87 int num_windows, |
| 88 int width, |
| 89 int height, |
| 90 base::WaitableEvent* done) OVERRIDE; |
| 91 virtual void UnInitialize(base::WaitableEvent* done) OVERRIDE; |
| 92 virtual void CreateTexture(int window_id, |
| 93 GLuint* texture_id, |
| 94 base::WaitableEvent* done) OVERRIDE; |
| 95 virtual void RenderTexture(GLuint texture_id) OVERRIDE; |
| 96 virtual void DeleteTexture(GLuint texture_id) OVERRIDE; |
| 97 virtual void* GetGLContext() OVERRIDE; |
| 98 virtual void* GetGLDisplay() OVERRIDE; |
| 99 |
| 100 private: |
| 101 MessageLoop* message_loop_; |
| 102 int width_; |
| 103 int height_; |
| 104 scoped_nsobject<NSWindow> window_; |
| 105 scoped_nsobject<NSOpenGLView> gl_view_; |
| 106 base::mac::ScopedNSAutoreleasePool pool_; |
| 107 }; |
| 108 |
| 109 // static |
| 110 RenderingHelper* RenderingHelper::Create() { |
| 111 return new RenderingHelperMac; |
| 112 } |
| 113 |
| 114 // static |
| 115 void RenderingHelper::InitializePlatform() { |
| 116 // Initialize the Cocoa framework. |
| 117 base::mac::ScopedNSAutoreleasePool pool_; |
| 118 [NSApplication sharedApplication]; |
| 119 } |
| 120 |
| 121 RenderingHelperMac::RenderingHelperMac() |
| 122 : message_loop_(NULL), |
| 123 width_(0), |
| 124 height_(0) { |
| 125 } |
| 126 |
| 127 RenderingHelperMac::~RenderingHelperMac() { |
| 128 CHECK_EQ(width_, 0) << "Must call UnInitialize before dtor."; |
| 129 } |
| 130 |
| 131 void RenderingHelperMac::Initialize(bool suppress_swap_to_display, |
| 132 int num_windows, |
| 133 int width, |
| 134 int height, |
| 135 base::WaitableEvent* done) { |
| 136 // Use width_ != 0 as a proxy for the class having already been |
| 137 // Initialize()'d, and UnInitialize() before continuing. |
| 138 if (width_) { |
| 139 base::WaitableEvent done2(false, false); |
| 140 UnInitialize(&done2); |
| 141 done2.Wait(); |
| 142 } |
| 143 |
| 144 // A separate window is created for each decoder. Since the Mac API |
| 145 // only supports a single instance only one window should be created. |
| 146 CHECK_EQ(num_windows, 1); |
| 147 |
| 148 // Suppress swap is only used when multiple NALUs are sent to a single |
| 149 // Decode() call. This is also not support by the Mac API. |
| 150 CHECK(!suppress_swap_to_display); |
| 151 |
| 152 width_ = width; |
| 153 height_ = height; |
| 154 message_loop_ = MessageLoop::current(); |
| 155 |
| 156 // Create a window to host the OpenGL contents. |
| 157 NSRect rect = NSMakeRect(0, 0, width_, height_); |
| 158 window_.reset([[NSWindow alloc] |
| 159 initWithContentRect:rect |
| 160 styleMask:NSTitledWindowMask |
| 161 backing:NSBackingStoreBuffered |
| 162 defer:NO]); |
| 163 [window_ center]; |
| 164 [window_ makeKeyAndOrderFront:nil]; |
| 165 |
| 166 // Create an OpenGL view. |
| 167 scoped_nsobject<NSOpenGLPixelFormat> pixel_format(GetPixelFormat()); |
| 168 gl_view_.reset([[NSOpenGLView alloc] initWithFrame:rect |
| 169 pixelFormat:pixel_format]); |
| 170 [[window_ contentView] addSubview:gl_view_]; |
| 171 SetupGLViewPort(gl_view_, width_, height_); |
| 172 |
| 173 done->Signal(); |
| 174 } |
| 175 |
| 176 void RenderingHelperMac::UnInitialize(base::WaitableEvent* done) { |
| 177 CHECK_EQ(MessageLoop::current(), message_loop_); |
| 178 width_ = 0; |
| 179 height_ = 0; |
| 180 message_loop_ = NULL; |
| 181 [window_ close]; |
| 182 window_.reset(); |
| 183 gl_view_.reset(); |
| 184 done->Signal(); |
| 185 } |
| 186 |
| 187 void RenderingHelperMac::CreateTexture(int window_id, |
| 188 GLuint* texture_id, |
| 189 base::WaitableEvent* done) { |
| 190 CHECK_EQ(MessageLoop::current(), message_loop_); |
| 191 CGLContextObj cgl_ctx = GetCGLContext(gl_view_); |
| 192 glGenTextures(1, texture_id); |
| 193 CHECK_EQ(GL_NO_ERROR, static_cast<int>(glGetError())); |
| 194 done->Signal(); |
| 195 } |
| 196 |
| 197 void RenderingHelperMac::RenderTexture(GLuint texture_id) { |
| 198 CHECK_EQ(MessageLoop::current(), message_loop_); |
| 199 DrawTexture(gl_view_, texture_id); |
| 200 } |
| 201 |
| 202 void RenderingHelperMac::DeleteTexture(GLuint texture_id) { |
| 203 CHECK_EQ(MessageLoop::current(), message_loop_); |
| 204 CGLContextObj cgl_ctx = GetCGLContext(gl_view_); |
| 205 glDeleteTextures(1, &texture_id); |
| 206 CHECK_EQ(GL_NO_ERROR, static_cast<int>(glGetError())); |
| 207 } |
| 208 |
| 209 void* RenderingHelperMac::GetGLContext() { |
| 210 return GetCGLContext(gl_view_); |
| 211 } |
| 212 |
| 213 void* RenderingHelperMac::GetGLDisplay() { |
| 214 return NULL; |
| 215 } |
| 216 |
| 217 } // namespace video_test_util |
| OLD | NEW |