We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / content / common / gpu / client / gpu_memory_buffer_impl_surface_texture.cc
blob8ed95a9037f7e9282364615944eec7243aa6c7e7
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"
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::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:
26 case gfx::GpuMemoryBuffer::YUV_420:
27 NOTREACHED();
28 return 0;
31 NOTREACHED();
32 return 0;
35 } // namespace
37 GpuMemoryBufferImplSurfaceTexture::GpuMemoryBufferImplSurfaceTexture(
38 gfx::GpuMemoryBufferId id,
39 const gfx::Size& size,
40 Format format,
41 const DestructionCallback& callback,
42 ANativeWindow* native_window)
43 : GpuMemoryBufferImpl(id, size, format, callback),
44 native_window_(native_window),
45 stride_(0) {
48 GpuMemoryBufferImplSurfaceTexture::~GpuMemoryBufferImplSurfaceTexture() {
49 ANativeWindow_release(native_window_);
52 // static
53 scoped_ptr<GpuMemoryBufferImpl>
54 GpuMemoryBufferImplSurfaceTexture::CreateFromHandle(
55 const gfx::GpuMemoryBufferHandle& handle,
56 const gfx::Size& size,
57 Format format,
58 const DestructionCallback& callback) {
59 ANativeWindow* native_window = SurfaceTextureManager::GetInstance()->
60 AcquireNativeWidgetForSurfaceTexture(handle.id);
61 if (!native_window)
62 return scoped_ptr<GpuMemoryBufferImpl>();
64 ANativeWindow_setBuffersGeometry(
65 native_window, size.width(), size.height(), WindowFormat(format));
67 return make_scoped_ptr<GpuMemoryBufferImpl>(
68 new GpuMemoryBufferImplSurfaceTexture(
69 handle.id, size, format, callback, native_window));
72 bool GpuMemoryBufferImplSurfaceTexture::Map(void** data) {
73 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Map");
75 DCHECK(!mapped_);
76 DCHECK(native_window_);
77 ANativeWindow_Buffer buffer;
78 int status = ANativeWindow_lock(native_window_, &buffer, NULL);
79 if (status) {
80 VLOG(1) << "ANativeWindow_lock failed with error code: " << status;
81 return false;
84 size_t stride_in_bytes = 0;
85 if (!StrideInBytes(buffer.stride, format_, 0, &stride_in_bytes))
86 return false;
88 DCHECK_LE(size_.width(), buffer.stride);
89 stride_ = stride_in_bytes;
90 mapped_ = true;
91 *data = buffer.bits;
92 return true;
95 void GpuMemoryBufferImplSurfaceTexture::Unmap() {
96 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Unmap");
98 DCHECK(mapped_);
99 ANativeWindow_unlockAndPost(native_window_);
100 mapped_ = false;
103 void GpuMemoryBufferImplSurfaceTexture::GetStride(uint32* stride) const {
104 *stride = stride_;
107 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplSurfaceTexture::GetHandle()
108 const {
109 gfx::GpuMemoryBufferHandle handle;
110 handle.type = gfx::SURFACE_TEXTURE_BUFFER;
111 handle.id = id_;
112 return handle;
115 } // namespace content