Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / common / gpu / client / gpu_memory_buffer_impl_surface_texture.cc
blob0fa6e814c896c45177791ab0a6c4c33cb9cfec77
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::R_8:
25 case gfx::GpuMemoryBuffer::RGBX_8888:
26 case gfx::GpuMemoryBuffer::BGRA_8888:
27 case gfx::GpuMemoryBuffer::YUV_420:
28 NOTREACHED();
29 return 0;
32 NOTREACHED();
33 return 0;
36 } // namespace
38 GpuMemoryBufferImplSurfaceTexture::GpuMemoryBufferImplSurfaceTexture(
39 gfx::GpuMemoryBufferId id,
40 const gfx::Size& size,
41 Format format,
42 const DestructionCallback& callback,
43 ANativeWindow* native_window)
44 : GpuMemoryBufferImpl(id, size, format, callback),
45 native_window_(native_window),
46 stride_(0) {
49 GpuMemoryBufferImplSurfaceTexture::~GpuMemoryBufferImplSurfaceTexture() {
50 ANativeWindow_release(native_window_);
53 // static
54 scoped_ptr<GpuMemoryBufferImpl>
55 GpuMemoryBufferImplSurfaceTexture::CreateFromHandle(
56 const gfx::GpuMemoryBufferHandle& handle,
57 const gfx::Size& size,
58 Format format,
59 const DestructionCallback& callback) {
60 ANativeWindow* native_window = SurfaceTextureManager::GetInstance()->
61 AcquireNativeWidgetForSurfaceTexture(handle.id);
62 if (!native_window)
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");
76 DCHECK(!mapped_);
77 DCHECK(native_window_);
78 ANativeWindow_Buffer buffer;
79 int status = ANativeWindow_lock(native_window_, &buffer, NULL);
80 if (status) {
81 VLOG(1) << "ANativeWindow_lock failed with error code: " << status;
82 return false;
85 size_t stride_in_bytes = 0;
86 if (!StrideInBytes(buffer.stride, format_, 0, &stride_in_bytes))
87 return false;
89 DCHECK_LE(size_.width(), buffer.stride);
90 stride_ = stride_in_bytes;
91 mapped_ = true;
92 *data = buffer.bits;
93 return true;
96 void GpuMemoryBufferImplSurfaceTexture::Unmap() {
97 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Unmap");
99 DCHECK(mapped_);
100 ANativeWindow_unlockAndPost(native_window_);
101 mapped_ = false;
104 void GpuMemoryBufferImplSurfaceTexture::GetStride(int* stride) const {
105 *stride = stride_;
108 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplSurfaceTexture::GetHandle()
109 const {
110 gfx::GpuMemoryBufferHandle handle;
111 handle.type = gfx::SURFACE_TEXTURE_BUFFER;
112 handle.id = id_;
113 return handle;
116 } // namespace content