Fix search results being clipped in app list.
[chromium-blink-merge.git] / ui / gl / gl_surface_osmesa.cc
blob7f1b1ad881d0bc1b6b2ce5a99a0bf6e80188ae89
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 #include "base/logging.h"
6 #include "base/numerics/safe_math.h"
7 #include "third_party/mesa/src/include/GL/osmesa.h"
8 #include "ui/gl/gl_bindings.h"
9 #include "ui/gl/gl_context.h"
10 #include "ui/gl/gl_surface_osmesa.h"
11 #include "ui/gl/scoped_make_current.h"
13 namespace gfx {
15 GLSurfaceOSMesa::GLSurfaceOSMesa(OSMesaSurfaceFormat format,
16 const gfx::Size& size)
17 : size_(size) {
18 switch (format) {
19 case OSMesaSurfaceFormatBGRA:
20 format_ = OSMESA_BGRA;
21 break;
22 case OSMesaSurfaceFormatRGBA:
23 format_ = OSMESA_RGBA;
24 break;
26 // Implementations of OSMesa surface do not support having a 0 size. In such
27 // cases use a (1, 1) surface.
28 if (size_.GetArea() == 0)
29 size_.SetSize(1, 1);
32 bool GLSurfaceOSMesa::Initialize() {
33 return Resize(size_);
36 void GLSurfaceOSMesa::Destroy() {
37 buffer_.reset();
40 bool GLSurfaceOSMesa::Resize(const gfx::Size& new_size) {
41 scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current;
42 GLContext* current_context = GLContext::GetCurrent();
43 bool was_current =
44 current_context && current_context->IsCurrent(this);
45 if (was_current) {
46 scoped_make_current.reset(
47 new ui::ScopedMakeCurrent(current_context, this));
48 current_context->ReleaseCurrent(this);
51 // Preserve the old buffer.
52 scoped_ptr<int32[]> old_buffer(buffer_.release());
54 base::CheckedNumeric<int> checked_size = sizeof(buffer_[0]);
55 checked_size *= new_size.width();
56 checked_size *= new_size.height();
57 if (!checked_size.IsValid())
58 return false;
60 // Allocate a new one.
61 buffer_.reset(new int32[new_size.GetArea()]);
62 if (!buffer_.get())
63 return false;
65 memset(buffer_.get(), 0, new_size.GetArea() * sizeof(buffer_[0]));
67 // Copy the old back buffer into the new buffer.
68 if (old_buffer.get()) {
69 int copy_width = std::min(size_.width(), new_size.width());
70 int copy_height = std::min(size_.height(), new_size.height());
71 for (int y = 0; y < copy_height; ++y) {
72 for (int x = 0; x < copy_width; ++x) {
73 buffer_[y * new_size.width() + x] = old_buffer[y * size_.width() + x];
78 size_ = new_size;
80 return true;
83 bool GLSurfaceOSMesa::IsOffscreen() {
84 return true;
87 bool GLSurfaceOSMesa::SwapBuffers() {
88 NOTREACHED() << "Should not call SwapBuffers on an GLSurfaceOSMesa.";
89 return false;
92 gfx::Size GLSurfaceOSMesa::GetSize() {
93 return size_;
96 void* GLSurfaceOSMesa::GetHandle() {
97 return buffer_.get();
100 unsigned GLSurfaceOSMesa::GetFormat() {
101 return format_;
104 GLSurfaceOSMesa::~GLSurfaceOSMesa() {
105 Destroy();
108 bool GLSurfaceOSMesaHeadless::IsOffscreen() { return false; }
110 bool GLSurfaceOSMesaHeadless::SwapBuffers() { return true; }
112 GLSurfaceOSMesaHeadless::GLSurfaceOSMesaHeadless()
113 : GLSurfaceOSMesa(OSMesaSurfaceFormatBGRA, gfx::Size(1, 1)) {
116 GLSurfaceOSMesaHeadless::~GLSurfaceOSMesaHeadless() { Destroy(); }
118 } // namespace gfx