1 // Copyright 2013 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 "ui/gl/gl_image_shm.h"
7 #include "base/debug/trace_event.h"
8 #include "base/process/process_handle.h"
9 #include "ui/gl/gl_bindings.h"
13 GLImageShm::GLImageShm(gfx::Size size, unsigned internalformat)
15 internalformat_(internalformat) {
16 // GL_RGBA8_OES is currently the only supported internalformat.
17 DCHECK_EQ(static_cast<GLenum>(GL_RGBA8_OES), internalformat);
20 GLImageShm::~GLImageShm() {
24 bool GLImageShm::Initialize(gfx::GpuMemoryBufferHandle buffer) {
25 if (!base::SharedMemory::IsHandleValid(buffer.handle))
28 base::SharedMemory shared_memory(buffer.handle, true);
30 // Duplicate the handle.
31 base::SharedMemoryHandle duped_shared_memory_handle;
32 if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(),
33 &duped_shared_memory_handle)) {
34 DVLOG(0) << "Failed to duplicate shared memory handle.";
39 new base::SharedMemory(duped_shared_memory_handle, true));
43 void GLImageShm::Destroy() {
46 gfx::Size GLImageShm::GetSize() {
50 bool GLImageShm::BindTexImage() {
51 TRACE_EVENT0("gpu", "GLImageShm::BindTexImage");
52 DCHECK(shared_memory_);
54 GLenum internalformat;
58 switch (internalformat_) {
60 internalformat = GL_RGBA;
62 type = GL_UNSIGNED_BYTE;
66 DVLOG(0) << "Invalid format: " << internalformat_;
70 size_t size = size_.GetArea() * bytes_per_pixel;
71 DCHECK(!shared_memory_->memory());
72 if (!shared_memory_->Map(size)) {
73 DVLOG(0) << "Failed to map shared memory.";
77 DCHECK(shared_memory_->memory());
78 glTexImage2D(GL_TEXTURE_2D,
86 shared_memory_->memory());
88 shared_memory_->Unmap();
92 void GLImageShm::ReleaseTexImage() {
95 void GLImageShm::WillUseTexImage() {
98 void GLImageShm::DidUseTexImage() {