content: Refactor GPU memory buffer framework.
[chromium-blink-merge.git] / content / common / gpu / client / gpu_memory_buffer_impl_surface_texture.cc
blob401d37a39cd082097e0b84abc818e8dd54608c7c
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"
12 namespace content {
13 namespace {
15 int WindowFormat(gfx::GpuMemoryBuffer::Format format) {
16 switch (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:
21 NOTREACHED();
22 return 0;
25 NOTREACHED();
26 return 0;
29 } // namespace
31 GpuMemoryBufferImplSurfaceTexture::GpuMemoryBufferImplSurfaceTexture(
32 gfx::GpuMemoryBufferId id,
33 const gfx::Size& size,
34 Format format,
35 const DestructionCallback& callback,
36 ANativeWindow* native_window)
37 : GpuMemoryBufferImpl(id, size, format, callback),
38 native_window_(native_window),
39 stride_(0) {
42 GpuMemoryBufferImplSurfaceTexture::~GpuMemoryBufferImplSurfaceTexture() {
43 ANativeWindow_release(native_window_);
46 // static
47 scoped_ptr<GpuMemoryBufferImpl>
48 GpuMemoryBufferImplSurfaceTexture::CreateFromHandle(
49 const gfx::GpuMemoryBufferHandle& handle,
50 const gfx::Size& size,
51 Format format,
52 const DestructionCallback& callback) {
53 ANativeWindow* native_window = SurfaceTextureManager::GetInstance()->
54 AcquireNativeWidgetForSurfaceTexture(handle.id);
55 if (!native_window)
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");
69 DCHECK(!mapped_);
70 DCHECK(native_window_);
71 ANativeWindow_Buffer buffer;
72 int status = ANativeWindow_lock(native_window_, &buffer, NULL);
73 if (status) {
74 VLOG(1) << "ANativeWindow_lock failed with error code: " << status;
75 return NULL;
78 DCHECK_LE(size_.width(), buffer.stride);
79 stride_ = buffer.stride * BytesPerPixel(format_);
80 mapped_ = true;
81 return buffer.bits;
84 void GpuMemoryBufferImplSurfaceTexture::Unmap() {
85 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Unmap");
87 DCHECK(mapped_);
88 ANativeWindow_unlockAndPost(native_window_);
89 mapped_ = false;
92 uint32 GpuMemoryBufferImplSurfaceTexture::GetStride() const {
93 return stride_;
96 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplSurfaceTexture::GetHandle()
97 const {
98 gfx::GpuMemoryBufferHandle handle;
99 handle.type = gfx::SURFACE_TEXTURE_BUFFER;
100 handle.id = id_;
101 return handle;
104 } // namespace content