summaryrefslogtreecommitdiff
path: root/chromium/ui/ozone/platform/x11/gl_surface_egl_readback_x11.cc
blob: 3a38c4d6996a213758973da065f8edb930f1fe11 (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
// Copyright 2018 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/ozone/platform/x11/gl_surface_egl_readback_x11.h"

#include "ui/gfx/x/x11_types.h"

namespace ui {

GLSurfaceEglReadbackX11::GLSurfaceEglReadbackX11(gfx::AcceleratedWidget window)
    : window_(window), xdisplay_(gfx::GetXDisplay()) {}

bool GLSurfaceEglReadbackX11::Initialize(gl::GLSurfaceFormat format) {
  if (!GLSurfaceEglReadback::Initialize(format))
    return false;

  // We don't need to reinitialize |window_graphics_context_|.
  if (window_graphics_context_)
    return true;

  window_graphics_context_ = XCreateGC(xdisplay_, window_, 0, nullptr);
  if (!window_graphics_context_) {
    DLOG(ERROR) << "XCreateGC failed";
    Destroy();
    return false;
  }

  return true;
}

void GLSurfaceEglReadbackX11::Destroy() {
  if (pixmap_graphics_context_) {
    XFreeGC(xdisplay_, pixmap_graphics_context_);
    pixmap_graphics_context_ = nullptr;
  }

  if (pixmap_) {
    XFreePixmap(xdisplay_, pixmap_);
    pixmap_ = x11::None;
  }

  if (window_graphics_context_) {
    XFreeGC(xdisplay_, window_graphics_context_);
    window_graphics_context_ = nullptr;
  }

  XSync(xdisplay_, x11::False);

  GLSurfaceEglReadback::Destroy();
}

bool GLSurfaceEglReadbackX11::Resize(const gfx::Size& size,
                                     float scale_factor,
                                     ColorSpace color_space,
                                     bool has_alpha) {
  if (!GLSurfaceEglReadback::Resize(size, scale_factor, color_space,
                                    has_alpha)) {
    return false;
  }

  XWindowAttributes attributes;
  if (!XGetWindowAttributes(xdisplay_, window_, &attributes)) {
    DLOG(ERROR) << "XGetWindowAttributes failed";
    return false;
  }

  // Destroy the previous pixmap and graphics context.
  if (pixmap_graphics_context_) {
    XFreeGC(xdisplay_, pixmap_graphics_context_);
    pixmap_graphics_context_ = nullptr;
  }
  if (pixmap_) {
    XFreePixmap(xdisplay_, pixmap_);
    pixmap_ = x11::None;
  }

  // Recreate a pixmap to hold the frame.
  pixmap_ = XCreatePixmap(xdisplay_, window_, size.width(), size.height(),
                          attributes.depth);
  if (!pixmap_) {
    DLOG(ERROR) << "XCreatePixmap failed";
    return false;
  }

  // Recreate a graphics context for the pixmap.
  pixmap_graphics_context_ = XCreateGC(xdisplay_, pixmap_, 0, nullptr);
  if (!pixmap_graphics_context_) {
    DLOG(ERROR) << "XCreateGC failed";
    return false;
  }

  return true;
}

GLSurfaceEglReadbackX11::~GLSurfaceEglReadbackX11() {
  Destroy();
}

bool GLSurfaceEglReadbackX11::HandlePixels(uint8_t* pixels) {
  XWindowAttributes attributes;
  if (!XGetWindowAttributes(xdisplay_, window_, &attributes)) {
    DLOG(ERROR) << "XGetWindowAttributes failed";
    return false;
  }

  // Copy pixels into pixmap and then update the XWindow.
  const gfx::Size size = GetSize();
  gfx::PutARGBImage(xdisplay_, attributes.visual, attributes.depth, pixmap_,
                    pixmap_graphics_context_, pixels, size.width(),
                    size.height());
  XCopyArea(xdisplay_, pixmap_, window_, window_graphics_context_, 0, 0,
            size.width(), size.height(), 0, 0);

  return true;
}

}  // namespace ui