1 // Copyright 2014 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 "content/common/gpu/client/gpu_memory_buffer_impl_surface_texture.h"
7 #include "base/debug/trace_event.h"
8 #include "base/logging.h"
9 #include "content/common/android/surface_texture_manager.h"
10 #include "ui/gl/gl_bindings.h"
15 int WindowFormat(gfx::GpuMemoryBuffer::Format format
) {
17 case gfx::GpuMemoryBuffer::RGBA_8888
:
18 return WINDOW_FORMAT_RGBA_8888
;
19 case gfx::GpuMemoryBuffer::RGBX_8888
:
20 case gfx::GpuMemoryBuffer::BGRA_8888
:
31 GpuMemoryBufferImplSurfaceTexture::GpuMemoryBufferImplSurfaceTexture(
32 gfx::GpuMemoryBufferId id
,
33 const gfx::Size
& size
,
35 const DestructionCallback
& callback
,
36 ANativeWindow
* native_window
)
37 : GpuMemoryBufferImpl(id
, size
, format
, callback
),
38 native_window_(native_window
),
42 GpuMemoryBufferImplSurfaceTexture::~GpuMemoryBufferImplSurfaceTexture() {
43 ANativeWindow_release(native_window_
);
47 scoped_ptr
<GpuMemoryBufferImpl
>
48 GpuMemoryBufferImplSurfaceTexture::CreateFromHandle(
49 const gfx::GpuMemoryBufferHandle
& handle
,
50 const gfx::Size
& size
,
52 const DestructionCallback
& callback
) {
53 ANativeWindow
* native_window
= SurfaceTextureManager::GetInstance()->
54 AcquireNativeWidgetForSurfaceTexture(handle
.id
);
56 return scoped_ptr
<GpuMemoryBufferImpl
>();
58 ANativeWindow_setBuffersGeometry(
59 native_window
, size
.width(), size
.height(), WindowFormat(format
));
61 return make_scoped_ptr
<GpuMemoryBufferImpl
>(
62 new GpuMemoryBufferImplSurfaceTexture(
63 handle
.id
, size
, format
, callback
, native_window
));
66 void* GpuMemoryBufferImplSurfaceTexture::Map() {
67 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Map");
70 DCHECK(native_window_
);
71 ANativeWindow_Buffer buffer
;
72 int status
= ANativeWindow_lock(native_window_
, &buffer
, NULL
);
74 VLOG(1) << "ANativeWindow_lock failed with error code: " << status
;
78 DCHECK_LE(size_
.width(), buffer
.stride
);
79 stride_
= buffer
.stride
* BytesPerPixel(format_
);
84 void GpuMemoryBufferImplSurfaceTexture::Unmap() {
85 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Unmap");
88 ANativeWindow_unlockAndPost(native_window_
);
92 uint32
GpuMemoryBufferImplSurfaceTexture::GetStride() const {
96 gfx::GpuMemoryBufferHandle
GpuMemoryBufferImplSurfaceTexture::GetHandle()
98 gfx::GpuMemoryBufferHandle handle
;
99 handle
.type
= gfx::SURFACE_TEXTURE_BUFFER
;
104 } // namespace content