1// Copyright 2014 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/ozone/platform/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/SkCanvas.h"
14#include "ui/gfx/geometry/rect.h"
15#include "ui/gfx/skia_util.h"
16#include "ui/ozone/platform/dri/dri_buffer.h"
17#include "ui/ozone/platform/dri/dri_wrapper.h"
18
19namespace ui {
20
21DriSurface::DriSurface(
22    DriWrapper* dri, const gfx::Size& size)
23    : dri_(dri),
24      bitmaps_(),
25      front_buffer_(0),
26      size_(size) {
27}
28
29DriSurface::~DriSurface() {
30}
31
32bool DriSurface::Initialize() {
33  for (size_t i = 0; i < arraysize(bitmaps_); ++i) {
34    bitmaps_[i].reset(CreateBuffer());
35    // TODO(dnicoara) Should select the configuration based on what the
36    // underlying system supports.
37    SkImageInfo info = SkImageInfo::Make(size_.width(),
38                                         size_.height(),
39                                         kPMColor_SkColorType,
40                                         kPremul_SkAlphaType);
41    if (!bitmaps_[i]->Initialize(info)) {
42      return false;
43    }
44  }
45
46  return true;
47}
48
49uint32_t DriSurface::GetFramebufferId() const {
50  CHECK(backbuffer());
51  return backbuffer()->framebuffer();
52}
53
54uint32_t DriSurface::GetHandle() const {
55  CHECK(backbuffer());
56  return backbuffer()->handle();
57}
58
59// This call is made after the hardware just started displaying our back buffer.
60// We need to update our pointer reference and synchronize the two buffers.
61void DriSurface::SwapBuffers() {
62  CHECK(frontbuffer());
63  CHECK(backbuffer());
64
65  // Update our front buffer pointer.
66  front_buffer_ ^= 1;
67}
68
69gfx::Size DriSurface::Size() const {
70  return size_;
71}
72
73SkCanvas* DriSurface::GetDrawableForWidget() {
74  CHECK(backbuffer());
75  return backbuffer()->canvas();
76}
77
78DriBuffer* DriSurface::CreateBuffer() { return new DriBuffer(dri_); }
79
80}  // namespace ui
81