summaryrefslogtreecommitdiffstats
path: root/ui/gl/gl_image_ozone_native_pixmap.cc
blob: 17c233bcd9f04cdc645e4fcab634e0e26f1c2094 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gl/gl_image_ozone_native_pixmap.h"

#define FOURCC(a, b, c, d)                                    \
  ((static_cast<uint32>(a)) | (static_cast<uint32>(b) << 8) | \
   (static_cast<uint32>(c) << 16) | (static_cast<uint32>(d) << 24))

#define DRM_FORMAT_ARGB8888 FOURCC('A', 'R', '2', '4')
#define DRM_FORMAT_XRGB8888 FOURCC('X', 'R', '2', '4')

namespace gfx {
namespace {

bool ValidInternalFormat(unsigned internalformat) {
  switch (internalformat) {
    case GL_RGB:
    case GL_BGRA_EXT:
      return true;
    default:
      return false;
  }
}

bool ValidFormat(gfx::BufferFormat format) {
  switch (format) {
    case BufferFormat::BGRA_8888:
    case BufferFormat::BGRX_8888:
      return true;
    case BufferFormat::ATC:
    case BufferFormat::ATCIA:
    case BufferFormat::DXT1:
    case BufferFormat::DXT5:
    case BufferFormat::ETC1:
    case BufferFormat::R_8:
    case BufferFormat::RGBA_4444:
    case BufferFormat::RGBA_8888:
    case BufferFormat::YUV_420:
    case BufferFormat::YUV_420_BIPLANAR:
      return false;
  }

  NOTREACHED();
  return false;
}

EGLint FourCC(gfx::BufferFormat format) {
  switch (format) {
    case BufferFormat::BGRA_8888:
      return DRM_FORMAT_ARGB8888;
    case BufferFormat::BGRX_8888:
      return DRM_FORMAT_XRGB8888;
    case BufferFormat::ATC:
    case BufferFormat::ATCIA:
    case BufferFormat::DXT1:
    case BufferFormat::DXT5:
    case BufferFormat::ETC1:
    case BufferFormat::R_8:
    case BufferFormat::RGBA_4444:
    case BufferFormat::RGBA_8888:
    case BufferFormat::YUV_420:
    case BufferFormat::YUV_420_BIPLANAR:
      NOTREACHED();
      return 0;
  }

  NOTREACHED();
  return 0;
}

}  // namespace

GLImageOzoneNativePixmap::GLImageOzoneNativePixmap(const Size& size,
                                                   unsigned internalformat)
    : GLImageEGL(size), internalformat_(internalformat) {}

GLImageOzoneNativePixmap::~GLImageOzoneNativePixmap() {
}

bool GLImageOzoneNativePixmap::Initialize(ui::NativePixmap* pixmap,
                                          BufferFormat format) {
  DCHECK(!pixmap_);

  bool result = true;
  if (pixmap->GetEGLClientBuffer()) {
    EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE};
    result = GLImageEGL::Initialize(EGL_NATIVE_PIXMAP_KHR,
                                    pixmap->GetEGLClientBuffer(), attrs);
  } else if (pixmap->GetDmaBufFd() >= 0) {
    if (!ValidInternalFormat(internalformat_)) {
      LOG(ERROR) << "Invalid internalformat: " << internalformat_;
      return false;
    }

    if (!ValidFormat(format)) {
      LOG(ERROR) << "Invalid format: " << static_cast<int>(format);
      return false;
    }

    // Note: If eglCreateImageKHR is successful for a EGL_LINUX_DMA_BUF_EXT
    // target, the EGL will take a reference to the dma_buf.
    EGLint attrs[] = {EGL_WIDTH,
                      size_.width(),
                      EGL_HEIGHT,
                      size_.height(),
                      EGL_LINUX_DRM_FOURCC_EXT,
                      FourCC(format),
                      EGL_DMA_BUF_PLANE0_FD_EXT,
                      pixmap->GetDmaBufFd(),
                      EGL_DMA_BUF_PLANE0_OFFSET_EXT,
                      0,
                      EGL_DMA_BUF_PLANE0_PITCH_EXT,
                      pixmap->GetDmaBufPitch(),
                      EGL_NONE};
    result = GLImageEGL::Initialize(
        EGL_LINUX_DMA_BUF_EXT, static_cast<EGLClientBuffer>(nullptr), attrs);
  }

  if (result)
    pixmap_ = pixmap;
  return result;
}

unsigned GLImageOzoneNativePixmap::GetInternalFormat() {
  return internalformat_;
}

void GLImageOzoneNativePixmap::Destroy(bool have_context) {
  GLImageEGL::Destroy(have_context);
}

bool GLImageOzoneNativePixmap::ScheduleOverlayPlane(AcceleratedWidget widget,
                                                    int z_order,
                                                    OverlayTransform transform,
                                                    const Rect& bounds_rect,
                                                    const RectF& crop_rect) {
  DCHECK(pixmap_);
  return pixmap_->ScheduleOverlayPlane(widget, z_order, transform, bounds_rect,
                                       crop_rect);
}

}  // namespace gfx