OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "content/common/gpu/client/gpu_memory_buffer_impl_surface_texture.h" |
| 6 |
| 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" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 // static |
| 15 bool GpuMemoryBufferImplSurfaceTexture::IsFormatSupported( |
| 16 unsigned internalformat) { |
| 17 switch (internalformat) { |
| 18 case GL_RGBA8_OES: |
| 19 return true; |
| 20 default: |
| 21 return false; |
| 22 } |
| 23 } |
| 24 |
| 25 // static |
| 26 int GpuMemoryBufferImplSurfaceTexture::WindowFormat(unsigned internalformat) { |
| 27 switch (internalformat) { |
| 28 case GL_RGBA8_OES: |
| 29 return WINDOW_FORMAT_RGBA_8888; |
| 30 default: |
| 31 NOTREACHED(); |
| 32 return 0; |
| 33 } |
| 34 } |
| 35 |
| 36 GpuMemoryBufferImplSurfaceTexture::GpuMemoryBufferImplSurfaceTexture( |
| 37 gfx::Size size, |
| 38 unsigned internalformat) |
| 39 : GpuMemoryBufferImpl(size, internalformat), |
| 40 native_window_(NULL), |
| 41 stride_(0u) {} |
| 42 |
| 43 GpuMemoryBufferImplSurfaceTexture::~GpuMemoryBufferImplSurfaceTexture() { |
| 44 if (native_window_) |
| 45 ANativeWindow_release(native_window_); |
| 46 } |
| 47 |
| 48 bool GpuMemoryBufferImplSurfaceTexture::Initialize( |
| 49 gfx::GpuMemoryBufferHandle handle) { |
| 50 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Initialize"); |
| 51 |
| 52 DCHECK(!native_window_); |
| 53 native_window_ = SurfaceTextureLookup::GetInstance()->AcquireNativeWidget( |
| 54 handle.surface_texture_id.primary_id, |
| 55 handle.surface_texture_id.secondary_id); |
| 56 if (!native_window_) |
| 57 return false; |
| 58 |
| 59 ANativeWindow_setBuffersGeometry(native_window_, |
| 60 size_.width(), |
| 61 size_.height(), |
| 62 WindowFormat(internalformat_)); |
| 63 |
| 64 surface_texture_id_ = handle.surface_texture_id; |
| 65 return true; |
| 66 } |
| 67 |
| 68 void GpuMemoryBufferImplSurfaceTexture::Map(AccessMode mode, void** vaddr) { |
| 69 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Map"); |
| 70 |
| 71 DCHECK(!mapped_); |
| 72 DCHECK(native_window_); |
| 73 ANativeWindow_Buffer buffer; |
| 74 int status = ANativeWindow_lock(native_window_, &buffer, NULL); |
| 75 if (status) { |
| 76 VLOG(1) << "ANativeWindow_lock failed with error code: " << status; |
| 77 *vaddr = NULL; |
| 78 return; |
| 79 } |
| 80 |
| 81 DCHECK_LE(size_.width(), buffer.stride); |
| 82 *vaddr = buffer.bits; |
| 83 stride_ = buffer.stride * BytesPerPixel(internalformat_); |
| 84 mapped_ = true; |
| 85 } |
| 86 |
| 87 void GpuMemoryBufferImplSurfaceTexture::Unmap() { |
| 88 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Unmap"); |
| 89 |
| 90 DCHECK(mapped_); |
| 91 ANativeWindow_unlockAndPost(native_window_); |
| 92 mapped_ = false; |
| 93 } |
| 94 |
| 95 uint32 GpuMemoryBufferImplSurfaceTexture::GetStride() const { return stride_; } |
| 96 |
| 97 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplSurfaceTexture::GetHandle() |
| 98 const { |
| 99 gfx::GpuMemoryBufferHandle handle; |
| 100 handle.type = gfx::SURFACE_TEXTURE_BUFFER; |
| 101 handle.surface_texture_id = surface_texture_id_; |
| 102 return handle; |
| 103 } |
| 104 |
| 105 } // namespace content |
OLD | NEW |