Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / ui / ozone / gpu / gpu_memory_buffer_factory_ozone_native_buffer.cc
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 "ui/ozone/gpu/gpu_memory_buffer_factory_ozone_native_buffer.h"
6
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"
12
13 namespace ui {
14 namespace {
15 class GLImageOzoneNativePixmap : public gfx::GLImageEGL {
16  public:
17   explicit GLImageOzoneNativePixmap(const gfx::Size& size) : GLImageEGL(size) {}
18
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))
22       return false;
23     pixmap_ = pixmap;
24     return true;
25   }
26
27   virtual bool ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
28                                     int z_order,
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);
34   }
35
36  protected:
37   virtual ~GLImageOzoneNativePixmap() {}
38
39  private:
40   using gfx::GLImageEGL::Initialize;
41   scoped_refptr<NativePixmap> pixmap_;
42 };
43
44 SurfaceFactoryOzone::BufferFormat GetOzoneFormatFor(unsigned internal_format) {
45   switch (internal_format) {
46     case GL_RGBA8_OES:
47       return SurfaceFactoryOzone::RGBA_8888;
48     case GL_RGB8_OES:
49       return SurfaceFactoryOzone::RGBX_8888;
50     default:
51       NOTREACHED();
52       return SurfaceFactoryOzone::RGBA_8888;
53   }
54 }
55
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);
58 }
59 }  // namespace
60
61 GpuMemoryBufferFactoryOzoneNativeBuffer::
62     GpuMemoryBufferFactoryOzoneNativeBuffer() {
63 }
64
65 GpuMemoryBufferFactoryOzoneNativeBuffer::
66     ~GpuMemoryBufferFactoryOzoneNativeBuffer() {
67 }
68
69 bool GpuMemoryBufferFactoryOzoneNativeBuffer::CreateGpuMemoryBuffer(
70     const gfx::GpuMemoryBufferId& id,
71     const gfx::Size& size,
72     unsigned internalformat,
73     unsigned usage) {
74   scoped_refptr<NativePixmap> pixmap =
75       SurfaceFactoryOzone::GetInstance()->CreateNativePixmap(
76           size, GetOzoneFormatFor(internalformat));
77   if (!pixmap.get()) {
78     LOG(ERROR) << "Failed to create pixmap " << size.width() << "x"
79                << size.height() << " format " << internalformat << ", usage "
80                << usage;
81     return false;
82   }
83   native_pixmap_map_[GetIndex(id)] = pixmap;
84   return true;
85 }
86
87 void GpuMemoryBufferFactoryOzoneNativeBuffer::DestroyGpuMemoryBuffer(
88     const gfx::GpuMemoryBufferId& id) {
89   native_pixmap_map_.erase(GetIndex(id));
90 }
91
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>();
100   }
101   scoped_refptr<GLImageOzoneNativePixmap> image =
102       new GLImageOzoneNativePixmap(size);
103   if (!image->Initialize(it->second)) {
104     return scoped_refptr<gfx::GLImage>();
105   }
106   return image;
107 }
108
109 }  // namespace ui