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 "ui/ozone/gpu/gpu_memory_buffer_factory_ozone_native_buffer.h"
7 #include "base/logging.h"
8 #include "ui/gl/gl_image_egl.h"
9 #include "ui/ozone/public/native_pixmap.h"
10 #include "ui/ozone/public/surface_factory_ozone.h"
11 #include "ui/ozone/public/surface_ozone_egl.h"
15 class GLImageOzoneNativePixmap : public gfx::GLImageEGL {
17 explicit GLImageOzoneNativePixmap(const gfx::Size& size) : GLImageEGL(size) {}
19 bool Initialize(scoped_refptr<NativePixmap> pixmap) {
20 EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE};
21 if (!Initialize(EGL_NATIVE_PIXMAP_KHR, pixmap->GetEGLClientBuffer(), attrs))
27 virtual bool ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
29 gfx::OverlayTransform transform,
30 const gfx::Rect& bounds_rect,
31 const gfx::RectF& crop_rect) OVERRIDE {
32 return SurfaceFactoryOzone::GetInstance()->ScheduleOverlayPlane(
33 widget, z_order, transform, pixmap_, bounds_rect, crop_rect);
37 virtual ~GLImageOzoneNativePixmap() {}
40 using gfx::GLImageEGL::Initialize;
41 scoped_refptr<NativePixmap> pixmap_;
44 SurfaceFactoryOzone::BufferFormat GetOzoneFormatFor(unsigned internal_format) {
45 switch (internal_format) {
47 return SurfaceFactoryOzone::RGBA_8888;
49 return SurfaceFactoryOzone::RGBX_8888;
52 return SurfaceFactoryOzone::RGBA_8888;
56 std::pair<uint32_t, uint32_t> GetIndex(const gfx::GpuMemoryBufferId& id) {
57 return std::pair<uint32_t, uint32_t>(id.primary_id, id.secondary_id);
61 GpuMemoryBufferFactoryOzoneNativeBuffer::
62 GpuMemoryBufferFactoryOzoneNativeBuffer() {
65 GpuMemoryBufferFactoryOzoneNativeBuffer::
66 ~GpuMemoryBufferFactoryOzoneNativeBuffer() {
69 bool GpuMemoryBufferFactoryOzoneNativeBuffer::CreateGpuMemoryBuffer(
70 const gfx::GpuMemoryBufferId& id,
71 const gfx::Size& size,
72 unsigned internalformat,
74 scoped_refptr<NativePixmap> pixmap =
75 SurfaceFactoryOzone::GetInstance()->CreateNativePixmap(
76 size, GetOzoneFormatFor(internalformat));
78 LOG(ERROR) << "Failed to create pixmap " << size.width() << "x"
79 << size.height() << " format " << internalformat << ", usage "
83 native_pixmap_map_[GetIndex(id)] = pixmap;
87 void GpuMemoryBufferFactoryOzoneNativeBuffer::DestroyGpuMemoryBuffer(
88 const gfx::GpuMemoryBufferId& id) {
89 native_pixmap_map_.erase(GetIndex(id));
92 scoped_refptr<gfx::GLImage>
93 GpuMemoryBufferFactoryOzoneNativeBuffer::CreateImageForGpuMemoryBuffer(
94 const gfx::GpuMemoryBufferId& id,
95 const gfx::Size& size,
96 unsigned internalformat) {
97 BufferToPixmapMap::iterator it = native_pixmap_map_.find(GetIndex(id));
98 if (it == native_pixmap_map_.end()) {
99 return scoped_refptr<gfx::GLImage>();
101 scoped_refptr<GLImageOzoneNativePixmap> image =
102 new GLImageOzoneNativePixmap(size);
103 if (!image->Initialize(it->second)) {
104 return scoped_refptr<gfx::GLImage>();