[email protected] | 1cda048 | 2012-01-25 17:30:07 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 5 | #include "base/logging.h" |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 6 | #include "third_party/mesa/src/include/GL/osmesa.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 7 | #include "ui/gl/gl_bindings.h" |
| 8 | #include "ui/gl/gl_context.h" |
| 9 | #include "ui/gl/gl_surface_osmesa.h" |
[email protected] | 1e9c0c8 | 2013-06-06 14:59:24 | [diff] [blame] | 10 | #include "ui/gl/scoped_make_current.h" |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 11 | |
| 12 | namespace gfx { |
| 13 | |
[email protected] | 4da882a5 | 2014-08-16 08:29:51 | [diff] [blame^] | 14 | GLSurfaceOSMesa::GLSurfaceOSMesa(OSMesaSurfaceFormat format, |
| 15 | const gfx::Size& size) |
| 16 | : size_(size) { |
| 17 | switch (format) { |
| 18 | case OSMesaSurfaceFormatBGRA: |
| 19 | format_ = OSMESA_BGRA; |
| 20 | break; |
| 21 | case OSMesaSurfaceFormatRGBA: |
| 22 | format_ = OSMESA_RGBA; |
| 23 | break; |
| 24 | } |
[email protected] | 772aa83 | 2014-05-30 01:27:47 | [diff] [blame] | 25 | // Implementations of OSMesa surface do not support having a 0 size. In such |
| 26 | // cases use a (1, 1) surface. |
| 27 | if (size_.GetArea() == 0) |
| 28 | size_.SetSize(1, 1); |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 29 | } |
| 30 | |
[email protected] | 1cda048 | 2012-01-25 17:30:07 | [diff] [blame] | 31 | bool GLSurfaceOSMesa::Initialize() { |
| 32 | return Resize(size_); |
| 33 | } |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 34 | |
[email protected] | 1cda048 | 2012-01-25 17:30:07 | [diff] [blame] | 35 | void GLSurfaceOSMesa::Destroy() { |
| 36 | buffer_.reset(); |
| 37 | } |
| 38 | |
| 39 | bool GLSurfaceOSMesa::Resize(const gfx::Size& new_size) { |
[email protected] | 1e9c0c8 | 2013-06-06 14:59:24 | [diff] [blame] | 40 | scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current; |
[email protected] | f189b25 | 2011-12-08 22:30:14 | [diff] [blame] | 41 | GLContext* current_context = GLContext::GetCurrent(); |
[email protected] | 1e9c0c8 | 2013-06-06 14:59:24 | [diff] [blame] | 42 | bool was_current = |
| 43 | current_context && current_context->IsCurrent(this); |
| 44 | if (was_current) { |
| 45 | scoped_make_current.reset( |
| 46 | new ui::ScopedMakeCurrent(current_context, this)); |
[email protected] | f189b25 | 2011-12-08 22:30:14 | [diff] [blame] | 47 | current_context->ReleaseCurrent(this); |
[email protected] | 1e9c0c8 | 2013-06-06 14:59:24 | [diff] [blame] | 48 | } |
[email protected] | f189b25 | 2011-12-08 22:30:14 | [diff] [blame] | 49 | |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 50 | // Preserve the old buffer. |
[email protected] | d925120 | 2013-01-16 22:40:09 | [diff] [blame] | 51 | scoped_ptr<int32[]> old_buffer(buffer_.release()); |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 52 | |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 53 | // Allocate a new one. |
[email protected] | 1cda048 | 2012-01-25 17:30:07 | [diff] [blame] | 54 | buffer_.reset(new int32[new_size.GetArea()]); |
| 55 | memset(buffer_.get(), 0, new_size.GetArea() * sizeof(buffer_[0])); |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 56 | |
| 57 | // Copy the old back buffer into the new buffer. |
[email protected] | 1cda048 | 2012-01-25 17:30:07 | [diff] [blame] | 58 | if (old_buffer.get()) { |
| 59 | int copy_width = std::min(size_.width(), new_size.width()); |
| 60 | int copy_height = std::min(size_.height(), new_size.height()); |
| 61 | for (int y = 0; y < copy_height; ++y) { |
| 62 | for (int x = 0; x < copy_width; ++x) { |
| 63 | buffer_[y * new_size.width() + x] = old_buffer[y * size_.width() + x]; |
| 64 | } |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 68 | size_ = new_size; |
[email protected] | f189b25 | 2011-12-08 22:30:14 | [diff] [blame] | 69 | |
[email protected] | 1b5eee1 | 2011-10-26 21:44:12 | [diff] [blame] | 70 | return true; |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 73 | bool GLSurfaceOSMesa::IsOffscreen() { |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | bool GLSurfaceOSMesa::SwapBuffers() { |
| 78 | NOTREACHED() << "Should not call SwapBuffers on an GLSurfaceOSMesa."; |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | gfx::Size GLSurfaceOSMesa::GetSize() { |
| 83 | return size_; |
| 84 | } |
| 85 | |
| 86 | void* GLSurfaceOSMesa::GetHandle() { |
| 87 | return buffer_.get(); |
| 88 | } |
| 89 | |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 90 | unsigned GLSurfaceOSMesa::GetFormat() { |
| 91 | return format_; |
| 92 | } |
| 93 | |
[email protected] | ab9327c | 2012-05-02 02:38:08 | [diff] [blame] | 94 | GLSurfaceOSMesa::~GLSurfaceOSMesa() { |
| 95 | Destroy(); |
| 96 | } |
| 97 | |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 98 | bool GLSurfaceOSMesaHeadless::IsOffscreen() { return false; } |
| 99 | |
| 100 | bool GLSurfaceOSMesaHeadless::SwapBuffers() { return true; } |
| 101 | |
| 102 | GLSurfaceOSMesaHeadless::GLSurfaceOSMesaHeadless() |
[email protected] | 4da882a5 | 2014-08-16 08:29:51 | [diff] [blame^] | 103 | : GLSurfaceOSMesa(OSMesaSurfaceFormatBGRA, gfx::Size(1, 1)) { |
| 104 | } |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 105 | |
| 106 | GLSurfaceOSMesaHeadless::~GLSurfaceOSMesaHeadless() { Destroy(); } |
| 107 | |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 108 | } // namespace gfx |