| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/common/gpu/client/gpu_memory_buffer_impl_surface_texture.h" | 5 #include "content/common/gpu/client/gpu_memory_buffer_impl_surface_texture.h" |
| 6 | 6 |
| 7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "content/common/android/surface_texture_lookup.h" | 9 #include "content/common/android/surface_texture_lookup.h" |
| 10 #include "ui/gl/gl_bindings.h" | 10 #include "ui/gl/gl_bindings.h" |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 GpuMemoryBufferImplSurfaceTexture::GpuMemoryBufferImplSurfaceTexture( | 14 GpuMemoryBufferImplSurfaceTexture::GpuMemoryBufferImplSurfaceTexture( |
| 15 const gfx::Size& size, | 15 const gfx::Size& size, |
| 16 unsigned internalformat, | 16 Format format, |
| 17 const DestructionCallback& callback, | 17 const DestructionCallback& callback, |
| 18 const gfx::SurfaceTextureId& surface_texture_id, | 18 const gfx::SurfaceTextureId& surface_texture_id, |
| 19 ANativeWindow* native_window) | 19 ANativeWindow* native_window) |
| 20 : GpuMemoryBufferImpl(size, internalformat, callback), | 20 : GpuMemoryBufferImpl(size, format, callback), |
| 21 surface_texture_id_(surface_texture_id), | 21 surface_texture_id_(surface_texture_id), |
| 22 native_window_(native_window), | 22 native_window_(native_window), |
| 23 stride_(0u) { | 23 stride_(0u) { |
| 24 } | 24 } |
| 25 | 25 |
| 26 GpuMemoryBufferImplSurfaceTexture::~GpuMemoryBufferImplSurfaceTexture() { | 26 GpuMemoryBufferImplSurfaceTexture::~GpuMemoryBufferImplSurfaceTexture() { |
| 27 ANativeWindow_release(native_window_); | 27 ANativeWindow_release(native_window_); |
| 28 } | 28 } |
| 29 | 29 |
| 30 // static | 30 // static |
| 31 scoped_ptr<GpuMemoryBufferImpl> | 31 scoped_ptr<GpuMemoryBufferImpl> |
| 32 GpuMemoryBufferImplSurfaceTexture::CreateFromHandle( | 32 GpuMemoryBufferImplSurfaceTexture::CreateFromHandle( |
| 33 const gfx::GpuMemoryBufferHandle& handle, | 33 const gfx::GpuMemoryBufferHandle& handle, |
| 34 const gfx::Size& size, | 34 const gfx::Size& size, |
| 35 unsigned internalformat, | 35 Format format, |
| 36 const DestructionCallback& callback) { | 36 const DestructionCallback& callback) { |
| 37 DCHECK(IsFormatSupported(internalformat)); | 37 DCHECK(IsFormatSupported(format)); |
| 38 | 38 |
| 39 ANativeWindow* native_window = | 39 ANativeWindow* native_window = |
| 40 SurfaceTextureLookup::GetInstance()->AcquireNativeWidget( | 40 SurfaceTextureLookup::GetInstance()->AcquireNativeWidget( |
| 41 handle.surface_texture_id.primary_id, | 41 handle.surface_texture_id.primary_id, |
| 42 handle.surface_texture_id.secondary_id); | 42 handle.surface_texture_id.secondary_id); |
| 43 if (!native_window) | 43 if (!native_window) |
| 44 return scoped_ptr<GpuMemoryBufferImpl>(); | 44 return scoped_ptr<GpuMemoryBufferImpl>(); |
| 45 | 45 |
| 46 ANativeWindow_setBuffersGeometry( | 46 ANativeWindow_setBuffersGeometry( |
| 47 native_window, size.width(), size.height(), WindowFormat(internalformat)); | 47 native_window, size.width(), size.height(), WindowFormat(format)); |
| 48 | 48 |
| 49 return make_scoped_ptr<GpuMemoryBufferImpl>( | 49 return make_scoped_ptr<GpuMemoryBufferImpl>( |
| 50 new GpuMemoryBufferImplSurfaceTexture(size, | 50 new GpuMemoryBufferImplSurfaceTexture( |
| 51 internalformat, | 51 size, format, callback, handle.surface_texture_id, native_window)); |
| 52 callback, | |
| 53 handle.surface_texture_id, | |
| 54 native_window)); | |
| 55 } | 52 } |
| 56 | 53 |
| 57 // static | 54 // static |
| 58 bool GpuMemoryBufferImplSurfaceTexture::IsFormatSupported( | 55 bool GpuMemoryBufferImplSurfaceTexture::IsFormatSupported(Format format) { |
| 59 unsigned internalformat) { | 56 switch (format) { |
| 60 switch (internalformat) { | 57 case RGBA_8888: |
| 61 case GL_RGBA8_OES: | |
| 62 return true; | 58 return true; |
| 63 default: | 59 case RGBX_8888: |
| 60 case BGRA_8888 |
| 64 return false; | 61 return false; |
| 65 } | 62 } |
| 63 |
| 64 NOTREACHED(); |
| 65 return false; |
| 66 } | 66 } |
| 67 | 67 |
| 68 // static | 68 // static |
| 69 bool GpuMemoryBufferImplSurfaceTexture::IsUsageSupported(unsigned usage) { | 69 bool GpuMemoryBufferImplSurfaceTexture::IsUsageSupported( |
| 70 Usage usage) { |
| 70 switch (usage) { | 71 switch (usage) { |
| 71 case GL_IMAGE_MAP_CHROMIUM: | 72 case MAP: |
| 72 return true; | 73 return true; |
| 73 default: | 74 case SCANOUT: |
| 74 return false; | 75 return false; |
| 75 } | 76 } |
| 77 |
| 78 NOTREACHED(); |
| 79 return false; |
| 76 } | 80 } |
| 77 | 81 |
| 78 // static | 82 // static |
| 79 bool GpuMemoryBufferImplSurfaceTexture::IsConfigurationSupported( | 83 bool GpuMemoryBufferImplSurfaceTexture::IsConfigurationSupported(Format format, |
| 80 unsigned internalformat, | 84 Usage usage) { |
| 81 unsigned usage) { | 85 return IsFormatSupported(format) && IsUsageSupported(usage); |
| 82 return IsFormatSupported(internalformat) && IsUsageSupported(usage); | |
| 83 } | 86 } |
| 84 | 87 |
| 85 // static | 88 // static |
| 86 int GpuMemoryBufferImplSurfaceTexture::WindowFormat(unsigned internalformat) { | 89 int GpuMemoryBufferImplSurfaceTexture::WindowFormat(Format format) { |
| 87 switch (internalformat) { | 90 switch (format) { |
| 88 case GL_RGBA8_OES: | 91 case RGBA_8888: |
| 89 return WINDOW_FORMAT_RGBA_8888; | 92 return WINDOW_FORMAT_RGBA_8888; |
| 90 default: | 93 case RGBX_8888: |
| 91 NOTREACHED(); | 94 case BGRA_8888: |
| 92 return 0; | 95 break; |
| 93 } | 96 } |
| 97 |
| 98 NOTREACHED(); |
| 99 return 0; |
| 94 } | 100 } |
| 95 | 101 |
| 96 void* GpuMemoryBufferImplSurfaceTexture::Map() { | 102 void* GpuMemoryBufferImplSurfaceTexture::Map() { |
| 97 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Map"); | 103 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Map"); |
| 98 | 104 |
| 99 DCHECK(!mapped_); | 105 DCHECK(!mapped_); |
| 100 DCHECK(native_window_); | 106 DCHECK(native_window_); |
| 101 ANativeWindow_Buffer buffer; | 107 ANativeWindow_Buffer buffer; |
| 102 int status = ANativeWindow_lock(native_window_, &buffer, NULL); | 108 int status = ANativeWindow_lock(native_window_, &buffer, NULL); |
| 103 if (status) { | 109 if (status) { |
| 104 VLOG(1) << "ANativeWindow_lock failed with error code: " << status; | 110 VLOG(1) << "ANativeWindow_lock failed with error code: " << status; |
| 105 return NULL; | 111 return NULL; |
| 106 } | 112 } |
| 107 | 113 |
| 108 DCHECK_LE(size_.width(), buffer.stride); | 114 DCHECK_LE(size_.width(), buffer.stride); |
| 109 stride_ = buffer.stride * BytesPerPixel(internalformat_); | 115 stride_ = buffer.stride * BytesPerPixel(format_); |
| 110 mapped_ = true; | 116 mapped_ = true; |
| 111 return buffer.bits; | 117 return buffer.bits; |
| 112 } | 118 } |
| 113 | 119 |
| 114 void GpuMemoryBufferImplSurfaceTexture::Unmap() { | 120 void GpuMemoryBufferImplSurfaceTexture::Unmap() { |
| 115 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Unmap"); | 121 TRACE_EVENT0("gpu", "GpuMemoryBufferImplSurfaceTexture::Unmap"); |
| 116 | 122 |
| 117 DCHECK(mapped_); | 123 DCHECK(mapped_); |
| 118 ANativeWindow_unlockAndPost(native_window_); | 124 ANativeWindow_unlockAndPost(native_window_); |
| 119 mapped_ = false; | 125 mapped_ = false; |
| 120 } | 126 } |
| 121 | 127 |
| 122 uint32 GpuMemoryBufferImplSurfaceTexture::GetStride() const { return stride_; } | 128 uint32 GpuMemoryBufferImplSurfaceTexture::GetStride() const { |
| 129 return stride_; |
| 130 } |
| 123 | 131 |
| 124 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplSurfaceTexture::GetHandle() | 132 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplSurfaceTexture::GetHandle() |
| 125 const { | 133 const { |
| 126 gfx::GpuMemoryBufferHandle handle; | 134 gfx::GpuMemoryBufferHandle handle; |
| 127 handle.type = gfx::SURFACE_TEXTURE_BUFFER; | 135 handle.type = gfx::SURFACE_TEXTURE_BUFFER; |
| 128 handle.surface_texture_id = surface_texture_id_; | 136 handle.surface_texture_id = surface_texture_id_; |
| 129 return handle; | 137 return handle; |
| 130 } | 138 } |
| 131 | 139 |
| 132 } // namespace content | 140 } // namespace content |
| OLD | NEW |