Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / renderer_host / software_output_device_x11.cc
blob2c7ab2283f5bc8815c85945759cd76ac516c6b74
1 // Copyright (c) 2013 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 "content/browser/renderer_host/software_output_device_x11.h"
7 #include <X11/Xlib.h>
8 #include <X11/Xutil.h>
10 #include "content/public/browser/browser_thread.h"
11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "third_party/skia/include/core/SkDevice.h"
13 #include "ui/compositor/compositor.h"
15 namespace content {
17 SoftwareOutputDeviceX11::SoftwareOutputDeviceX11(ui::Compositor* compositor)
18 : compositor_(compositor),
19 display_(ui::GetXDisplay()),
20 gc_(NULL),
21 image_(NULL) {
22 // TODO(skaslev) Remove this when crbug.com/180702 is fixed.
23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
25 gc_ = XCreateGC(display_, compositor_->widget(), 0, NULL);
28 SoftwareOutputDeviceX11::~SoftwareOutputDeviceX11() {
29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
31 XFreeGC(display_, gc_);
32 ClearImage();
35 void SoftwareOutputDeviceX11::ClearImage() {
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
38 if (image_) {
39 // XDestroyImage deletes the data referenced by the image which
40 // is actually owned by the device_. So we have to reset data here.
41 image_->data = NULL;
42 XDestroyImage(image_);
43 image_ = NULL;
47 void SoftwareOutputDeviceX11::Resize(gfx::Size viewport_size) {
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
50 cc::SoftwareOutputDevice::Resize(viewport_size);
52 ClearImage();
53 if (!device_)
54 return;
56 const SkBitmap& bitmap = device_->accessBitmap(false);
57 image_ = XCreateImage(display_, CopyFromParent,
58 DefaultDepth(display_, DefaultScreen(display_)),
59 ZPixmap, 0,
60 static_cast<char*>(bitmap.getPixels()),
61 viewport_size_.width(), viewport_size_.height(),
62 32, 4 * viewport_size_.width());
65 void SoftwareOutputDeviceX11::EndPaint(cc::SoftwareFrameData* frame_data) {
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
67 DCHECK(device_);
68 DCHECK(frame_data);
70 if (!device_)
71 return;
73 SoftwareOutputDevice::EndPaint(frame_data);
75 gfx::Rect rect = damage_rect_;
76 rect.Intersect(gfx::Rect(viewport_size_));
77 if (rect.IsEmpty())
78 return;
80 // TODO(skaslev): Maybe switch XShmPutImage since it's async.
81 XPutImage(display_, compositor_->widget(), gc_, image_,
82 rect.x(), rect.y(),
83 rect.x(), rect.y(),
84 rect.width(), rect.height());
87 } // namespace content