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::BufferFormat format
) {
17 case gfx::BufferFormat::RGBA_8888
:
18 return WINDOW_FORMAT_RGBA_8888
;
19 case gfx::BufferFormat::ATC
:
20 case gfx::BufferFormat::ATCIA
:
21 case gfx::BufferFormat::DXT1
:
22 case gfx::BufferFormat::DXT5
:
23 case gfx::BufferFormat::ETC1
:
24 case gfx::BufferFormat::R_8
:
25 case gfx::BufferFormat::RGBA_4444
:
26 case gfx::BufferFormat::RGBX_8888
:
27 case gfx::BufferFormat::BGRA_8888
:
28 case gfx::BufferFormat::YUV_420
:
39 GpuMemoryBufferImplSurfaceTexture::GpuMemoryBufferImplSurfaceTexture(
40 gfx::GpuMemoryBufferId id
,
41 const gfx::Size
& size
,
42 gfx::BufferFormat format
,
43 const DestructionCallback
& callback
,
44 ANativeWindow
* native_window
)
45 : GpuMemoryBufferImpl(id
, size
, format
, callback
),
46 native_window_(native_window
),
49 GpuMemoryBufferImplSurfaceTexture::~GpuMemoryBufferImplSurfaceTexture() {
50 ANativeWindow_release(native_window_
);
54 scoped_ptr
<GpuMemoryBufferImpl
>
55 GpuMemoryBufferImplSurfaceTexture::CreateFromHandle(
56 const gfx::GpuMemoryBufferHandle
& handle
,
57 const gfx::Size
& size
,
58 gfx::BufferFormat format
,
59 const DestructionCallback
& callback
) {
60 ANativeWindow
* native_window
= SurfaceTextureManager::GetInstance()->
61 AcquireNativeWidgetForSurfaceTexture(handle
.id
);
63 return scoped_ptr
<GpuMemoryBufferImpl
>();
65 ANativeWindow_setBuffersGeometry(
66 native_window
, size
.width(), size
.height(), WindowFormat(format
));
68 return make_scoped_ptr
<GpuMemoryBufferImpl
>(
69 new GpuMemoryBufferImplSurfaceTexture(
70 handle
.id
, size
, format
, callback
, native_window
));
73 bool GpuMemoryBufferImplSurfaceTexture::Map(void** data
) {
74 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Map");
77 DCHECK(native_window_
);
78 ANativeWindow_Buffer buffer
;
79 int status
= ANativeWindow_lock(native_window_
, &buffer
, NULL
);
81 VLOG(1) << "ANativeWindow_lock failed with error code: " << status
;
85 DCHECK_LE(size_
.width(), buffer
.stride
);
86 size_t row_size_in_bytes
= 0;
88 RowSizeInBytes(buffer
.stride
, format_
, 0, &row_size_in_bytes
);
89 DCHECK(valid_row_size
);
91 stride_
= row_size_in_bytes
;
97 void GpuMemoryBufferImplSurfaceTexture::Unmap() {
98 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Unmap");
101 ANativeWindow_unlockAndPost(native_window_
);
105 void GpuMemoryBufferImplSurfaceTexture::GetStride(int* stride
) const {
109 gfx::GpuMemoryBufferHandle
GpuMemoryBufferImplSurfaceTexture::GetHandle()
111 gfx::GpuMemoryBufferHandle handle
;
112 handle
.type
= gfx::SURFACE_TEXTURE_BUFFER
;
117 } // namespace content