Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gl/gl_image_shm.h" | 5 #include "ui/gl/gl_image_shm.h" |
| 6 | 6 |
| 7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "base/process/process_handle.h" | 8 #include "base/process/process_handle.h" |
| 9 #include "ui/gl/gl_bindings.h" | 9 #include "ui/gl/gl_bindings.h" |
| 10 | 10 |
| 11 namespace gfx { | 11 namespace gfx { |
| 12 | 12 |
| 13 GLImageShm::GLImageShm(gfx::Size size) : size_(size) { | 13 GLImageShm::GLImageShm(const gfx::Size& size, unsigned internalformat) |
| 14 : size_(size), | |
| 15 internalformat_(internalformat) { | |
| 14 } | 16 } |
| 15 | 17 |
| 16 GLImageShm::~GLImageShm() { | 18 GLImageShm::~GLImageShm() { |
| 17 Destroy(); | 19 Destroy(); |
| 18 } | 20 } |
| 19 | 21 |
| 20 bool GLImageShm::Initialize(gfx::GpuMemoryBufferHandle buffer) { | 22 bool GLImageShm::Initialize(gfx::GpuMemoryBufferHandle buffer) { |
| 21 if (!base::SharedMemory::IsHandleValid(buffer.handle)) | 23 if (!base::SharedMemory::IsHandleValid(buffer.handle)) |
| 22 return false; | 24 return false; |
| 23 | 25 |
| 24 base::SharedMemory shared_memory(buffer.handle, true); | 26 base::SharedMemory shared_memory(buffer.handle, true); |
| 25 | 27 |
| 26 // Duplicate the handle. | 28 // Duplicate the handle. |
| 27 base::SharedMemoryHandle duped_shared_memory_handle; | 29 base::SharedMemoryHandle duped_shared_memory_handle; |
| 28 if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), | 30 if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), |
| 29 &duped_shared_memory_handle)) { | 31 &duped_shared_memory_handle)) { |
| 30 DVLOG(0) << "Failed to duplicate shared memory handle."; | 32 DVLOG(0) << "Failed to duplicate shared memory handle."; |
| 31 return false; | 33 return false; |
| 32 } | 34 } |
| 33 | 35 |
| 34 shared_memory_.reset( | 36 shared_memory_.reset( |
| 35 new base::SharedMemory(duped_shared_memory_handle, true)); | 37 new base::SharedMemory(duped_shared_memory_handle, true)); |
| 36 return true; | 38 return true; |
| 37 } | 39 } |
| 38 | 40 |
| 39 bool GLImageShm::BindTexImage() { | 41 bool GLImageShm::BindTexImage() { |
| 40 TRACE_EVENT0("gpu", "GLImageShm::BindTexImage"); | 42 TRACE_EVENT0("gpu", "GLImageShm::BindTexImage"); |
| 41 DCHECK(shared_memory_); | 43 DCHECK(shared_memory_); |
| 42 | 44 |
| 43 const int kBytesPerPixel = 4; | 45 GLenum format; |
| 44 size_t size = size_.GetArea() * kBytesPerPixel; | 46 GLenum type; |
| 47 int bytes_per_pixel; | |
| 48 switch (internalformat_) { | |
| 49 case GL_RGBA: | |
| 50 format = GL_RGBA; | |
| 51 type = GL_UNSIGNED_BYTE; | |
| 52 bytes_per_pixel = 4; | |
| 53 break; | |
| 54 case GL_BGRA_EXT: | |
| 55 format = GL_BGRA_EXT; | |
| 56 type = GL_UNSIGNED_BYTE; | |
| 57 bytes_per_pixel = 4; | |
| 58 break; | |
| 59 default: | |
| 60 NOTREACHED(); | |
|
piman
2013/08/01 21:19:30
nit: this comes from the (untrusted) client side.
reveman
2013/08/08 23:19:00
Good point. Fixed in latest patch.
| |
| 61 return false; | |
| 62 } | |
| 63 | |
| 64 size_t size = size_.GetArea() * bytes_per_pixel; | |
| 45 DCHECK(!shared_memory_->memory()); | 65 DCHECK(!shared_memory_->memory()); |
| 46 if (!shared_memory_->Map(size)) { | 66 if (!shared_memory_->Map(size)) { |
| 47 DVLOG(0) << "Failed to map shared memory."; | 67 DVLOG(0) << "Failed to map shared memory."; |
| 48 return false; | 68 return false; |
| 49 } | 69 } |
| 50 | 70 |
| 51 DCHECK(shared_memory_->memory()); | 71 DCHECK(shared_memory_->memory()); |
| 52 glTexImage2D(GL_TEXTURE_2D, | 72 glTexImage2D(GL_TEXTURE_2D, |
| 53 0, // mip level | 73 0, // mip level |
| 54 GL_RGBA, | 74 internalformat_, |
| 55 size_.width(), | 75 size_.width(), |
| 56 size_.height(), | 76 size_.height(), |
| 57 0, // border | 77 0, // border |
| 58 GL_RGBA, | 78 format, |
| 59 GL_UNSIGNED_BYTE, | 79 type, |
| 60 shared_memory_->memory()); | 80 shared_memory_->memory()); |
| 61 | 81 |
| 62 shared_memory_->Unmap(); | 82 shared_memory_->Unmap(); |
| 63 return true; | 83 return true; |
| 64 } | 84 } |
| 65 | 85 |
| 66 gfx::Size GLImageShm::GetSize() { | 86 gfx::Size GLImageShm::GetSize() { |
| 67 return size_; | 87 return size_; |
| 68 } | 88 } |
| 69 | 89 |
| 70 void GLImageShm::Destroy() { | 90 void GLImageShm::Destroy() { |
| 71 } | 91 } |
| 72 | 92 |
| 73 void GLImageShm::ReleaseTexImage() { | 93 void GLImageShm::ReleaseTexImage() { |
| 74 } | 94 } |
| 75 | 95 |
| 76 } // namespace gfx | 96 } // namespace gfx |
| OLD | NEW |