- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / aura / software_output_device_x11.cc
1 // Copyright 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.
4
5 #include "content/browser/aura/software_output_device_x11.h"
6
7 #include <X11/Xlib.h>
8 #include <X11/Xutil.h>
9
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"
14
15 namespace content {
16
17 SoftwareOutputDeviceX11::SoftwareOutputDeviceX11(ui::Compositor* compositor)
18     : compositor_(compositor),
19       display_(gfx::GetXDisplay()),
20       gc_(NULL),
21       image_(NULL) {
22   // TODO(skaslev) Remove this when crbug.com/180702 is fixed.
23   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
24
25   gc_ = XCreateGC(display_, compositor_->widget(), 0, NULL);
26 }
27
28 SoftwareOutputDeviceX11::~SoftwareOutputDeviceX11() {
29   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
30
31   XFreeGC(display_, gc_);
32   ClearImage();
33 }
34
35 void SoftwareOutputDeviceX11::ClearImage() {
36   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
37
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;
44   }
45 }
46
47 void SoftwareOutputDeviceX11::Resize(gfx::Size viewport_size) {
48   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
49
50   cc::SoftwareOutputDevice::Resize(viewport_size);
51
52   ClearImage();
53   if (!device_)
54     return;
55
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());
63 }
64
65 void SoftwareOutputDeviceX11::EndPaint(cc::SoftwareFrameData* frame_data) {
66   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
67   DCHECK(device_);
68   DCHECK(frame_data);
69
70   if (!device_)
71     return;
72
73   SoftwareOutputDevice::EndPaint(frame_data);
74
75   gfx::Rect rect = damage_rect_;
76   rect.Intersect(gfx::Rect(viewport_size_));
77   if (rect.IsEmpty())
78     return;
79
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());
85 }
86
87 }  // namespace content