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"
15 bool ValidFormat(unsigned internalformat
) {
16 switch (internalformat
) {
25 GLenum
TextureFormat(unsigned internalformat
) {
26 switch (internalformat
) {
37 GLenum
DataFormat(unsigned internalformat
) {
38 return TextureFormat(internalformat
);
41 GLenum
DataType(unsigned internalformat
) {
42 switch (internalformat
) {
45 return GL_UNSIGNED_BYTE
;
52 GLenum
BytesPerPixel(unsigned internalformat
) {
53 switch (internalformat
) {
65 GLImageShm::GLImageShm(gfx::Size size
, unsigned internalformat
)
67 internalformat_(internalformat
) {
70 GLImageShm::~GLImageShm() {
74 bool GLImageShm::Initialize(gfx::GpuMemoryBufferHandle buffer
) {
75 if (!ValidFormat(internalformat_
)) {
76 DVLOG(0) << "Invalid format: " << internalformat_
;
80 if (!base::SharedMemory::IsHandleValid(buffer
.handle
))
83 base::SharedMemory
shared_memory(buffer
.handle
, true);
85 // Duplicate the handle.
86 base::SharedMemoryHandle duped_shared_memory_handle
;
87 if (!shared_memory
.ShareToProcess(base::GetCurrentProcessHandle(),
88 &duped_shared_memory_handle
)) {
89 DVLOG(0) << "Failed to duplicate shared memory handle.";
94 new base::SharedMemory(duped_shared_memory_handle
, true));
98 void GLImageShm::Destroy() {
101 gfx::Size
GLImageShm::GetSize() {
105 bool GLImageShm::BindTexImage(unsigned target
) {
106 TRACE_EVENT0("gpu", "GLImageShm::BindTexImage");
107 DCHECK(shared_memory_
);
108 DCHECK(ValidFormat(internalformat_
));
110 size_t size
= size_
.GetArea() * BytesPerPixel(internalformat_
);
111 DCHECK(!shared_memory_
->memory());
112 if (!shared_memory_
->Map(size
)) {
113 DVLOG(0) << "Failed to map shared memory.";
117 DCHECK(shared_memory_
->memory());
120 TextureFormat(internalformat_
),
124 DataFormat(internalformat_
),
125 DataType(internalformat_
),
126 shared_memory_
->memory());
128 shared_memory_
->Unmap();
132 void GLImageShm::ReleaseTexImage(unsigned target
) {
135 void GLImageShm::WillUseTexImage() {
138 void GLImageShm::DidUseTexImage() {