Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / ui / gfx / ozone / dri / dri_surface.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 "ui/gfx/ozone/dri/dri_surface.h"
6
7 #include <errno.h>
8 #include <sys/mman.h>
9 #include <sys/types.h>
10 #include <xf86drm.h>
11
12 #include "base/logging.h"
13 #include "third_party/skia/include/core/SkBitmap.h"
14 #include "third_party/skia/include/core/SkBitmapDevice.h"
15 #include "third_party/skia/include/core/SkCanvas.h"
16 #include "ui/gfx/ozone/dri/dri_skbitmap.h"
17 #include "ui/gfx/ozone/dri/dri_wrapper.h"
18 #include "ui/gfx/skia_util.h"
19
20 namespace gfx {
21
22 namespace {
23
24 // Extends the SkBitmapDevice to allow setting the SkPixelRef. We use the setter
25 // to change the SkPixelRef such that the device always points to the
26 // backbuffer.
27 class CustomSkBitmapDevice : public SkBitmapDevice {
28  public:
29   CustomSkBitmapDevice(const SkBitmap& bitmap) : SkBitmapDevice(bitmap) {}
30   virtual ~CustomSkBitmapDevice() {}
31
32   void SetPixelRef(SkPixelRef* pixel_ref) { setPixelRef(pixel_ref); }
33
34  private:
35   DISALLOW_COPY_AND_ASSIGN(CustomSkBitmapDevice);
36 };
37
38 }  // namespace
39
40 ////////////////////////////////////////////////////////////////////////////////
41 // DriSurface implementation
42
43 DriSurface::DriSurface(
44     DriWrapper* dri, const gfx::Size& size)
45     : dri_(dri),
46       bitmaps_(),
47       front_buffer_(0),
48       size_(size) {
49 }
50
51 DriSurface::~DriSurface() {
52 }
53
54 bool DriSurface::Initialize() {
55   for (int i = 0; i < 2; ++i) {
56     bitmaps_[i].reset(CreateBuffer());
57     // TODO(dnicoara) Should select the configuration based on what the
58     // underlying system supports.
59     SkImageInfo info = SkImageInfo::Make(size_.width(),
60                                          size_.height(),
61                                          kPMColor_SkColorType,
62                                          kPremul_SkAlphaType);
63     if (!bitmaps_[i]->Initialize(info)) {
64       return false;
65     }
66   }
67
68   skia_device_ = skia::AdoptRef(
69       new CustomSkBitmapDevice(*bitmaps_[front_buffer_ ^ 1].get()));
70   skia_canvas_ = skia::AdoptRef(new SkCanvas(skia_device_.get()));
71
72   return true;
73 }
74
75 uint32_t DriSurface::GetFramebufferId() const {
76   CHECK(bitmaps_[0].get() && bitmaps_[1].get());
77   return bitmaps_[front_buffer_ ^ 1]->get_framebuffer();
78 }
79
80 uint32_t DriSurface::GetHandle() const {
81   CHECK(bitmaps_[0].get() && bitmaps_[1].get());
82   return bitmaps_[front_buffer_ ^ 1]->get_handle();
83 }
84
85 // This call is made after the hardware just started displaying our back buffer.
86 // We need to update our pointer reference and synchronize the two buffers.
87 void DriSurface::SwapBuffers() {
88   CHECK(bitmaps_[0].get() && bitmaps_[1].get());
89
90   // Update our front buffer pointer.
91   front_buffer_ ^= 1;
92
93   // Unlocking will unset the pixel pointer, so it won't be pointing to the old
94   // PixelRef.
95   skia_device_->accessBitmap(false).unlockPixels();
96   // Update the backing pixels for the bitmap device.
97   static_cast<CustomSkBitmapDevice*>(skia_device_.get())->SetPixelRef(
98       bitmaps_[front_buffer_ ^ 1]->pixelRef());
99   // Locking the pixels will set the pixel pointer based on the PixelRef value.
100   skia_device_->accessBitmap(false).lockPixels();
101
102   SkIRect device_damage;
103   skia_canvas_->getClipDeviceBounds(&device_damage);
104   SkRect damage = SkRect::Make(device_damage);
105
106   skia_canvas_->drawBitmapRectToRect(*bitmaps_[front_buffer_].get(),
107                                      &damage,
108                                      damage);
109 }
110
111 SkCanvas* DriSurface::GetDrawableForWidget() {
112   return skia_canvas_.get();
113 }
114
115 DriSkBitmap* DriSurface::CreateBuffer() {
116   return new DriSkBitmap(dri_->get_fd());
117 }
118
119 }  // namespace gfx