1/*
2 *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/modules/desktop_capture/mac/scoped_pixel_buffer_object.h"
12
13#include <stddef.h>
14
15namespace webrtc {
16
17ScopedPixelBufferObject::ScopedPixelBufferObject()
18    : cgl_context_(NULL),
19      pixel_buffer_object_(0) {
20}
21
22ScopedPixelBufferObject::~ScopedPixelBufferObject() {
23  Release();
24}
25
26bool ScopedPixelBufferObject::Init(CGLContextObj cgl_context,
27                                      int size_in_bytes) {
28  cgl_context_ = cgl_context;
29  CGLContextObj CGL_MACRO_CONTEXT = cgl_context_;
30  glGenBuffersARB(1, &pixel_buffer_object_);
31  if (glGetError() == GL_NO_ERROR) {
32    glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pixel_buffer_object_);
33    glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, size_in_bytes, NULL,
34                    GL_STREAM_READ_ARB);
35    glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0);
36    if (glGetError() != GL_NO_ERROR) {
37      Release();
38    }
39  } else {
40    cgl_context_ = NULL;
41    pixel_buffer_object_ = 0;
42  }
43  return pixel_buffer_object_ != 0;
44}
45
46void ScopedPixelBufferObject::Release() {
47  if (pixel_buffer_object_) {
48    CGLContextObj CGL_MACRO_CONTEXT = cgl_context_;
49    glDeleteBuffersARB(1, &pixel_buffer_object_);
50    cgl_context_ = NULL;
51    pixel_buffer_object_ = 0;
52  }
53}
54
55}  // namespace webrtc
56