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_lookup.h"
10 #include "ui/gl/gl_bindings.h"
14 GpuMemoryBufferImplSurfaceTexture::GpuMemoryBufferImplSurfaceTexture(
15 const gfx::Size
& size
,
16 unsigned internalformat
)
17 : GpuMemoryBufferImpl(size
, internalformat
),
21 GpuMemoryBufferImplSurfaceTexture::~GpuMemoryBufferImplSurfaceTexture() {
23 ANativeWindow_release(native_window_
);
27 bool GpuMemoryBufferImplSurfaceTexture::IsFormatSupported(
28 unsigned internalformat
) {
29 switch (internalformat
) {
38 bool GpuMemoryBufferImplSurfaceTexture::IsUsageSupported(unsigned usage
) {
40 case GL_IMAGE_MAP_CHROMIUM
:
48 bool GpuMemoryBufferImplSurfaceTexture::IsConfigurationSupported(
49 unsigned internalformat
,
51 return IsFormatSupported(internalformat
) && IsUsageSupported(usage
);
55 int GpuMemoryBufferImplSurfaceTexture::WindowFormat(unsigned internalformat
) {
56 switch (internalformat
) {
58 return WINDOW_FORMAT_RGBA_8888
;
65 bool GpuMemoryBufferImplSurfaceTexture::InitializeFromHandle(
66 gfx::GpuMemoryBufferHandle handle
) {
68 "GpuMemoryBufferImplSurfaceTexture::InitializeFromHandle");
70 DCHECK(IsFormatSupported(internalformat_
));
71 DCHECK(!native_window_
);
72 native_window_
= SurfaceTextureLookup::GetInstance()->AcquireNativeWidget(
73 handle
.surface_texture_id
.primary_id
,
74 handle
.surface_texture_id
.secondary_id
);
78 ANativeWindow_setBuffersGeometry(native_window_
,
81 WindowFormat(internalformat_
));
83 surface_texture_id_
= handle
.surface_texture_id
;
87 void* GpuMemoryBufferImplSurfaceTexture::Map() {
88 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Map");
91 DCHECK(native_window_
);
92 ANativeWindow_Buffer buffer
;
93 int status
= ANativeWindow_lock(native_window_
, &buffer
, NULL
);
95 VLOG(1) << "ANativeWindow_lock failed with error code: " << status
;
99 DCHECK_LE(size_
.width(), buffer
.stride
);
100 stride_
= buffer
.stride
* BytesPerPixel(internalformat_
);
105 void GpuMemoryBufferImplSurfaceTexture::Unmap() {
106 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Unmap");
109 ANativeWindow_unlockAndPost(native_window_
);
113 uint32
GpuMemoryBufferImplSurfaceTexture::GetStride() const { return stride_
; }
115 gfx::GpuMemoryBufferHandle
GpuMemoryBufferImplSurfaceTexture::GetHandle()
117 gfx::GpuMemoryBufferHandle handle
;
118 handle
.type
= gfx::SURFACE_TEXTURE_BUFFER
;
119 handle
.surface_texture_id
= surface_texture_id_
;
123 } // namespace content