[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" |
mbarbella | 1ac4ceb | 2015-02-10 18:30:06 | [diff] [blame] | 6 | #include "base/numerics/safe_math.h" |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 7 | #include "third_party/mesa/src/include/GL/osmesa.h" |
[email protected] | c9e2cbbb | 2012-05-12 21:17:27 | [diff] [blame] | 8 | #include "ui/gl/gl_bindings.h" |
| 9 | #include "ui/gl/gl_context.h" |
| 10 | #include "ui/gl/gl_surface_osmesa.h" |
[email protected] | 1e9c0c8 | 2013-06-06 14:59:24 | [diff] [blame] | 11 | #include "ui/gl/scoped_make_current.h" |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 12 | |
| 13 | namespace gfx { |
| 14 | |
[email protected] | 4da882a5 | 2014-08-16 08:29:51 | [diff] [blame] | 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; |
| 25 | } |
[email protected] | 772aa83 | 2014-05-30 01:27:47 | [diff] [blame] | 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); |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 30 | } |
| 31 | |
[email protected] | 1cda048 | 2012-01-25 17:30:07 | [diff] [blame] | 32 | bool GLSurfaceOSMesa::Initialize() { |
| 33 | return Resize(size_); |
| 34 | } |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 35 | |
[email protected] | 1cda048 | 2012-01-25 17:30:07 | [diff] [blame] | 36 | void GLSurfaceOSMesa::Destroy() { |
| 37 | buffer_.reset(); |
| 38 | } |
| 39 | |
| 40 | bool GLSurfaceOSMesa::Resize(const gfx::Size& new_size) { |
[email protected] | 1e9c0c8 | 2013-06-06 14:59:24 | [diff] [blame] | 41 | scoped_ptr<ui::ScopedMakeCurrent> scoped_make_current; |
[email protected] | f189b25 | 2011-12-08 22:30:14 | [diff] [blame] | 42 | GLContext* current_context = GLContext::GetCurrent(); |
[email protected] | 1e9c0c8 | 2013-06-06 14:59:24 | [diff] [blame] | 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)); |
[email protected] | f189b25 | 2011-12-08 22:30:14 | [diff] [blame] | 48 | current_context->ReleaseCurrent(this); |
[email protected] | 1e9c0c8 | 2013-06-06 14:59:24 | [diff] [blame] | 49 | } |
[email protected] | f189b25 | 2011-12-08 22:30:14 | [diff] [blame] | 50 | |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 51 | // Preserve the old buffer. |
[email protected] | d925120 | 2013-01-16 22:40:09 | [diff] [blame] | 52 | scoped_ptr<int32[]> old_buffer(buffer_.release()); |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 53 | |
mbarbella | 1ac4ceb | 2015-02-10 18:30:06 | [diff] [blame] | 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; |
| 59 | |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 60 | // Allocate a new one. |
[email protected] | 1cda048 | 2012-01-25 17:30:07 | [diff] [blame] | 61 | buffer_.reset(new int32[new_size.GetArea()]); |
mbarbella | 1ac4ceb | 2015-02-10 18:30:06 | [diff] [blame] | 62 | if (!buffer_.get()) |
| 63 | return false; |
| 64 | |
[email protected] | 1cda048 | 2012-01-25 17:30:07 | [diff] [blame] | 65 | memset(buffer_.get(), 0, new_size.GetArea() * sizeof(buffer_[0])); |
[email protected] | ffae402 | 2011-05-12 22:54:29 | [diff] [blame] | 66 | |
| 67 | // Copy the old back buffer into the new buffer. |
[email protected] | 1cda048 | 2012-01-25 17:30:07 | [diff] [blame] | 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]; |
| 74 | } |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 78 | size_ = new_size; |
[email protected] | f189b25 | 2011-12-08 22:30:14 | [diff] [blame] | 79 | |
[email protected] | 1b5eee1 | 2011-10-26 21:44:12 | [diff] [blame] | 80 | return true; |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 83 | bool GLSurfaceOSMesa::IsOffscreen() { |
| 84 | return true; |
| 85 | } |
| 86 | |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 87 | gfx::SwapResult GLSurfaceOSMesa::SwapBuffers() { |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 88 | NOTREACHED() << "Should not call SwapBuffers on an GLSurfaceOSMesa."; |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 89 | return gfx::SwapResult::SWAP_FAILED; |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | gfx::Size GLSurfaceOSMesa::GetSize() { |
| 93 | return size_; |
| 94 | } |
| 95 | |
| 96 | void* GLSurfaceOSMesa::GetHandle() { |
| 97 | return buffer_.get(); |
| 98 | } |
| 99 | |
[email protected] | f62a5ab | 2011-05-23 20:34:15 | [diff] [blame] | 100 | unsigned GLSurfaceOSMesa::GetFormat() { |
| 101 | return format_; |
| 102 | } |
| 103 | |
[email protected] | ab9327c | 2012-05-02 02:38:08 | [diff] [blame] | 104 | GLSurfaceOSMesa::~GLSurfaceOSMesa() { |
| 105 | Destroy(); |
| 106 | } |
| 107 | |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 108 | bool GLSurfaceOSMesaHeadless::IsOffscreen() { return false; } |
| 109 | |
achaulk | ec8c2db | 2015-05-29 16:35:03 | [diff] [blame] | 110 | gfx::SwapResult GLSurfaceOSMesaHeadless::SwapBuffers() { |
| 111 | return gfx::SwapResult::SWAP_ACK; |
| 112 | } |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 113 | |
| 114 | GLSurfaceOSMesaHeadless::GLSurfaceOSMesaHeadless() |
[email protected] | 4da882a5 | 2014-08-16 08:29:51 | [diff] [blame] | 115 | : GLSurfaceOSMesa(OSMesaSurfaceFormatBGRA, gfx::Size(1, 1)) { |
| 116 | } |
[email protected] | ffcdc963 | 2014-03-27 17:25:23 | [diff] [blame] | 117 | |
| 118 | GLSurfaceOSMesaHeadless::~GLSurfaceOSMesaHeadless() { Destroy(); } |
| 119 | |
[email protected] | ad7bbbd6 | 2011-04-22 23:04:37 | [diff] [blame] | 120 | } // namespace gfx |