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/logging.h"
8 #include "base/trace_event/trace_event.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::ATC
:
20 case gfx::GpuMemoryBuffer::ATCIA
:
21 case gfx::GpuMemoryBuffer::DXT1
:
22 case gfx::GpuMemoryBuffer::DXT5
:
23 case gfx::GpuMemoryBuffer::ETC1
:
24 case gfx::GpuMemoryBuffer::RGBX_8888
:
25 case gfx::GpuMemoryBuffer::BGRA_8888
:
36 GpuMemoryBufferImplSurfaceTexture::GpuMemoryBufferImplSurfaceTexture(
37 gfx::GpuMemoryBufferId id
,
38 const gfx::Size
& size
,
40 const DestructionCallback
& callback
,
41 ANativeWindow
* native_window
)
42 : GpuMemoryBufferImpl(id
, size
, format
, callback
),
43 native_window_(native_window
),
47 GpuMemoryBufferImplSurfaceTexture::~GpuMemoryBufferImplSurfaceTexture() {
48 ANativeWindow_release(native_window_
);
52 scoped_ptr
<GpuMemoryBufferImpl
>
53 GpuMemoryBufferImplSurfaceTexture::CreateFromHandle(
54 const gfx::GpuMemoryBufferHandle
& handle
,
55 const gfx::Size
& size
,
57 const DestructionCallback
& callback
) {
58 ANativeWindow
* native_window
= SurfaceTextureManager::GetInstance()->
59 AcquireNativeWidgetForSurfaceTexture(handle
.id
);
61 return scoped_ptr
<GpuMemoryBufferImpl
>();
63 ANativeWindow_setBuffersGeometry(
64 native_window
, size
.width(), size
.height(), WindowFormat(format
));
66 return make_scoped_ptr
<GpuMemoryBufferImpl
>(
67 new GpuMemoryBufferImplSurfaceTexture(
68 handle
.id
, size
, format
, callback
, native_window
));
71 bool GpuMemoryBufferImplSurfaceTexture::Map(void** data
) {
72 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Map");
75 DCHECK(native_window_
);
76 ANativeWindow_Buffer buffer
;
77 int status
= ANativeWindow_lock(native_window_
, &buffer
, NULL
);
79 VLOG(1) << "ANativeWindow_lock failed with error code: " << status
;
83 size_t stride_in_bytes
= 0;
84 if (!StrideInBytes(buffer
.stride
, format_
, &stride_in_bytes
))
87 DCHECK_LE(size_
.width(), buffer
.stride
);
88 stride_
= stride_in_bytes
;
94 void GpuMemoryBufferImplSurfaceTexture::Unmap() {
95 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Unmap");
98 ANativeWindow_unlockAndPost(native_window_
);
102 void GpuMemoryBufferImplSurfaceTexture::GetStride(uint32
* stride
) const {
106 gfx::GpuMemoryBufferHandle
GpuMemoryBufferImplSurfaceTexture::GetHandle()
108 gfx::GpuMemoryBufferHandle handle
;
109 handle
.type
= gfx::SURFACE_TEXTURE_BUFFER
;
114 } // namespace content