OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "services/ui/gles2/ozone_gpu_memory_buffer.h" |
| 6 |
| 7 #include "ui/gfx/buffer_format_util.h" |
| 8 #include "ui/ozone/public/client_native_pixmap.h" |
| 9 #include "ui/ozone/public/client_native_pixmap_factory.h" |
| 10 #include "ui/ozone/public/native_pixmap.h" |
| 11 #include "ui/ozone/public/ozone_platform.h" |
| 12 #include "ui/ozone/public/surface_factory_ozone.h" |
| 13 |
| 14 namespace ui { |
| 15 |
| 16 OzoneGpuMemoryBuffer::OzoneGpuMemoryBuffer( |
| 17 gfx::GpuMemoryBufferId id, |
| 18 const gfx::Size& size, |
| 19 gfx::BufferFormat format, |
| 20 std::unique_ptr<ui::ClientNativePixmap> client_pixmap, |
| 21 scoped_refptr<ui::NativePixmap> native_pixmap) |
| 22 : GpuMemoryBufferImpl(id, size, format), |
| 23 client_pixmap_(std::move(client_pixmap)), |
| 24 native_pixmap_(native_pixmap) {} |
| 25 |
| 26 OzoneGpuMemoryBuffer::~OzoneGpuMemoryBuffer() { |
| 27 DCHECK(!mapped_); |
| 28 } |
| 29 |
| 30 // static |
| 31 OzoneGpuMemoryBuffer* OzoneGpuMemoryBuffer::FromClientBuffer( |
| 32 ClientBuffer buffer) { |
| 33 return reinterpret_cast<OzoneGpuMemoryBuffer*>(buffer); |
| 34 } |
| 35 |
| 36 // static |
| 37 std::unique_ptr<gfx::GpuMemoryBuffer> |
| 38 OzoneGpuMemoryBuffer::CreateOzoneGpuMemoryBuffer( |
| 39 const gfx::Size& size, |
| 40 gfx::BufferFormat format, |
| 41 gfx::BufferUsage usage, |
| 42 gfx::AcceleratedWidget widget) { |
| 43 scoped_refptr<ui::NativePixmap> pixmap = |
| 44 ui::OzonePlatform::GetInstance() |
| 45 ->GetSurfaceFactoryOzone() |
| 46 ->CreateNativePixmap(widget, size, format, usage); |
| 47 |
| 48 DCHECK(pixmap) << "need pixmap to exist!"; |
| 49 |
| 50 if (!pixmap.get()) { |
| 51 DLOG(ERROR) << "Failed to create pixmap " << size.width() << "x" |
| 52 << size.height() << " format " << static_cast<int>(format) |
| 53 << ", usage " << static_cast<int>(usage); |
| 54 return nullptr; |
| 55 } |
| 56 |
| 57 // We construct a ui::NativePixmapHandle |
| 58 gfx::NativePixmapHandle native_pixmap_handle = pixmap->ExportHandle(); |
| 59 DCHECK(ui::ClientNativePixmapFactory::GetInstance()) |
| 60 << "need me a ClientNativePixmapFactory"; |
| 61 std::unique_ptr<ui::ClientNativePixmap> client_native_pixmap = |
| 62 ui::ClientNativePixmapFactory::GetInstance()->ImportFromHandle( |
| 63 native_pixmap_handle, size, usage); |
| 64 |
| 65 std::unique_ptr<OzoneGpuMemoryBuffer> nb( |
| 66 new OzoneGpuMemoryBuffer(gfx::GpuMemoryBufferId(0), size, format, |
| 67 std::move(client_native_pixmap), pixmap)); |
| 68 return std::move(nb); |
| 69 } |
| 70 |
| 71 bool OzoneGpuMemoryBuffer::Map() { |
| 72 DCHECK(!mapped_); |
| 73 if (!client_pixmap_->Map()) |
| 74 return false; |
| 75 mapped_ = true; |
| 76 return mapped_; |
| 77 } |
| 78 |
| 79 void* OzoneGpuMemoryBuffer::memory(size_t plane) { |
| 80 DCHECK(mapped_); |
| 81 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); |
| 82 return client_pixmap_->Map(); |
| 83 } |
| 84 |
| 85 void OzoneGpuMemoryBuffer::Unmap() { |
| 86 DCHECK(mapped_); |
| 87 client_pixmap_->Unmap(); |
| 88 mapped_ = false; |
| 89 } |
| 90 |
| 91 int OzoneGpuMemoryBuffer::stride(size_t plane) const { |
| 92 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); |
| 93 int stride; |
| 94 client_pixmap_->GetStride(&stride); |
| 95 return stride; |
| 96 } |
| 97 |
| 98 gfx::GpuMemoryBufferHandle OzoneGpuMemoryBuffer::GetHandle() const { |
| 99 gfx::GpuMemoryBufferHandle handle; |
| 100 handle.type = gfx::OZONE_NATIVE_PIXMAP; |
| 101 handle.id = id_; |
| 102 return handle; |
| 103 } |
| 104 |
| 105 gfx::GpuMemoryBufferType OzoneGpuMemoryBuffer::GetBufferType() const { |
| 106 return gfx::OZONE_NATIVE_PIXMAP; |
| 107 } |
| 108 |
| 109 #if defined(USE_OZONE) |
| 110 scoped_refptr<ui::NativePixmap> OzoneGpuMemoryBuffer::GetNativePixmap() { |
| 111 return native_pixmap_; |
| 112 } |
| 113 #endif |
| 114 |
| 115 } // namespace ui |
OLD | NEW |