summaryrefslogtreecommitdiffstats
path: root/ui/gfx/gl/gl_surface_osmesa.cc
blob: 3fdf8fbbfcd30ed6930c24424742f0b1d44d5995 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/gl/gl_surface_osmesa.h"
#include "base/logging.h"
#include "ui/gfx/gl/gl_bindings.h"

namespace gfx {

GLSurfaceOSMesa::GLSurfaceOSMesa(unsigned format, const gfx::Size& size)
    : format_(format),
      size_(size) {
}

GLSurfaceOSMesa::~GLSurfaceOSMesa() {
  Destroy();
}

void GLSurfaceOSMesa::Resize(const gfx::Size& new_size) {
  if (new_size == size_)
    return;

  // Preserve the old buffer.
  scoped_array<int32> old_buffer(buffer_.release());

  // Allocate a new one.
  AllocateBuffer(new_size);

  // Copy the old back buffer into the new buffer.
  int copy_width = std::min(size_.width(), new_size.width());
  int copy_height = std::min(size_.height(), new_size.height());
  for (int y = 0; y < copy_height; ++y) {
    for (int x = 0; x < copy_width; ++x) {
      buffer_[y * new_size.width() + x] = old_buffer[y * size_.width() + x];
    }
  }

  size_ = new_size;
}

bool GLSurfaceOSMesa::Initialize() {
  AllocateBuffer(size_);
  return true;
}

void GLSurfaceOSMesa::Destroy() {
  buffer_.reset();
}

bool GLSurfaceOSMesa::IsOffscreen() {
  return true;
}

bool GLSurfaceOSMesa::SwapBuffers() {
  NOTREACHED() << "Should not call SwapBuffers on an GLSurfaceOSMesa.";
  return false;
}

gfx::Size GLSurfaceOSMesa::GetSize() {
  return size_;
}

void* GLSurfaceOSMesa::GetHandle() {
  return buffer_.get();
}

unsigned GLSurfaceOSMesa::GetFormat() {
  return format_;
}

void GLSurfaceOSMesa::AllocateBuffer(const Size& size) {
  buffer_.reset(new int32[size.GetArea()]);
  memset(buffer_.get(), 0, size.GetArea() * sizeof(buffer_[0]));
}

}  // namespace gfx