Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / common / gpu / client / gpu_memory_buffer_impl_surface_texture.cc
blobc0f4db478ae60a3f418fdc7efe0df359417d4131
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::RGBA_4444:
26 case gfx::GpuMemoryBuffer::RGBX_8888:
27 case gfx::GpuMemoryBuffer::BGRA_8888:
28 case gfx::GpuMemoryBuffer::YUV_420:
29 NOTREACHED();
30 return 0;
33 NOTREACHED();
34 return 0;
37 } // namespace
39 GpuMemoryBufferImplSurfaceTexture::GpuMemoryBufferImplSurfaceTexture(
40 gfx::GpuMemoryBufferId id,
41 const gfx::Size& size,
42 Format format,
43 const DestructionCallback& callback,
44 ANativeWindow* native_window)
45 : GpuMemoryBufferImpl(id, size, format, callback),
46 native_window_(native_window),
47 stride_(0) {
50 GpuMemoryBufferImplSurfaceTexture::~GpuMemoryBufferImplSurfaceTexture() {
51 ANativeWindow_release(native_window_);
54 // static
55 scoped_ptr<GpuMemoryBufferImpl>
56 GpuMemoryBufferImplSurfaceTexture::CreateFromHandle(
57 const gfx::GpuMemoryBufferHandle& handle,
58 const gfx::Size& size,
59 Format format,
60 const DestructionCallback& callback) {
61 ANativeWindow* native_window = SurfaceTextureManager::GetInstance()->
62 AcquireNativeWidgetForSurfaceTexture(handle.id);
63 if (!native_window)
64 return scoped_ptr<GpuMemoryBufferImpl>();
66 ANativeWindow_setBuffersGeometry(
67 native_window, size.width(), size.height(), WindowFormat(format));
69 return make_scoped_ptr<GpuMemoryBufferImpl>(
70 new GpuMemoryBufferImplSurfaceTexture(
71 handle.id, size, format, callback, native_window));
74 bool GpuMemoryBufferImplSurfaceTexture::Map(void** data) {
75 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Map");
77 DCHECK(!mapped_);
78 DCHECK(native_window_);
79 ANativeWindow_Buffer buffer;
80 int status = ANativeWindow_lock(native_window_, &buffer, NULL);
81 if (status) {
82 VLOG(1) << "ANativeWindow_lock failed with error code: " << status;
83 return false;
86 DCHECK_LE(size_.width(), buffer.stride);
87 size_t row_size_in_bytes = 0;
88 bool valid_row_size =
89 RowSizeInBytes(buffer.stride, format_, 0, &row_size_in_bytes);
90 DCHECK(valid_row_size);
92 stride_ = row_size_in_bytes;
93 mapped_ = true;
94 *data = buffer.bits;
95 return true;
98 void GpuMemoryBufferImplSurfaceTexture::Unmap() {
99 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Unmap");
101 DCHECK(mapped_);
102 ANativeWindow_unlockAndPost(native_window_);
103 mapped_ = false;
106 void GpuMemoryBufferImplSurfaceTexture::GetStride(int* stride) const {
107 *stride = stride_;
110 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplSurfaceTexture::GetHandle()
111 const {
112 gfx::GpuMemoryBufferHandle handle;
113 handle.type = gfx::SURFACE_TEXTURE_BUFFER;
114 handle.id = id_;
115 return handle;
118 } // namespace content