summaryrefslogtreecommitdiffstats
path: root/ui/ozone/platform/drm/gpu/drm_surface_unittest.cc
blob: c384f123bbe52b43dadf09eb05b68cc1424c4380 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2014 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 "base/message_loop/message_loop.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkDevice.h"
#include "ui/ozone/platform/drm/gpu/crtc_controller.h"
#include "ui/ozone/platform/drm/gpu/drm_buffer.h"
#include "ui/ozone/platform/drm/gpu/drm_device_generator.h"
#include "ui/ozone/platform/drm/gpu/drm_device_manager.h"
#include "ui/ozone/platform/drm/gpu/drm_surface.h"
#include "ui/ozone/platform/drm/gpu/drm_window.h"
#include "ui/ozone/platform/drm/gpu/hardware_display_controller.h"
#include "ui/ozone/platform/drm/gpu/screen_manager.h"
#include "ui/ozone/platform/drm/test/mock_drm_device.h"

namespace {

// Create a basic mode for a 6x4 screen.
const drmModeModeInfo kDefaultMode =
    {0, 6, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, {'\0'}};

const gfx::AcceleratedWidget kDefaultWidgetHandle = 1;
const uint32_t kDefaultCrtc = 1;
const uint32_t kDefaultConnector = 2;
const size_t kPlanesPerCrtc = 1;
const uint32_t kDefaultCursorSize = 64;

std::vector<skia::RefPtr<SkSurface>> GetFramebuffers(ui::MockDrmDevice* drm) {
  std::vector<skia::RefPtr<SkSurface>> framebuffers;
  for (const auto& buffer : drm->buffers()) {
    // Skip destroyed buffers and cursor buffers.
    if (!buffer || (buffer->width() == kDefaultCursorSize &&
                    buffer->height() == kDefaultCursorSize))
      continue;

    framebuffers.push_back(buffer);
  }

  return framebuffers;
}

}  // namespace

class DrmSurfaceTest : public testing::Test {
 public:
  DrmSurfaceTest() {}

  void SetUp() override;
  void TearDown() override;

 protected:
  scoped_ptr<base::MessageLoop> message_loop_;
  scoped_refptr<ui::MockDrmDevice> drm_;
  scoped_ptr<ui::DrmBufferGenerator> buffer_generator_;
  scoped_ptr<ui::ScreenManager> screen_manager_;
  scoped_ptr<ui::DrmDeviceManager> drm_device_manager_;
  scoped_ptr<ui::DrmSurface> surface_;

 private:
  DISALLOW_COPY_AND_ASSIGN(DrmSurfaceTest);
};

void DrmSurfaceTest::SetUp() {
  message_loop_.reset(new base::MessageLoopForUI);
  std::vector<uint32_t> crtcs;
  crtcs.push_back(kDefaultCrtc);
  drm_ = new ui::MockDrmDevice(false, crtcs, kPlanesPerCrtc);
  buffer_generator_.reset(new ui::DrmBufferGenerator());
  screen_manager_.reset(new ui::ScreenManager(buffer_generator_.get()));
  screen_manager_->AddDisplayController(drm_, kDefaultCrtc, kDefaultConnector);
  screen_manager_->ConfigureDisplayController(
      drm_, kDefaultCrtc, kDefaultConnector, gfx::Point(), kDefaultMode);

  drm_device_manager_.reset(new ui::DrmDeviceManager(nullptr));
  scoped_ptr<ui::DrmWindow> window(new ui::DrmWindow(
      kDefaultWidgetHandle, drm_device_manager_.get(), screen_manager_.get()));
  window->Initialize();
  window->OnBoundsChanged(
      gfx::Rect(gfx::Size(kDefaultMode.hdisplay, kDefaultMode.vdisplay)));
  screen_manager_->AddWindow(kDefaultWidgetHandle, window.Pass());

  surface_.reset(
      new ui::DrmSurface(screen_manager_->GetWindow(kDefaultWidgetHandle)));
  surface_->ResizeCanvas(
      gfx::Size(kDefaultMode.hdisplay, kDefaultMode.vdisplay));

  // The window has been remapped to a controller. The first swap will cause the
  // SWAP_NAK_RECREATE_BUFFERS without actually using the buffers.
  surface_->PresentCanvas(gfx::Rect());
}

void DrmSurfaceTest::TearDown() {
  surface_.reset();
  scoped_ptr<ui::DrmWindow> window =
      screen_manager_->RemoveWindow(kDefaultWidgetHandle);
  window->Shutdown();
  drm_ = nullptr;
  message_loop_.reset();
}

TEST_F(DrmSurfaceTest, CheckFBIDOnSwap) {
  surface_->PresentCanvas(gfx::Rect());
  drm_->RunCallbacks();

  // Framebuffer ID 1 is allocated in SetUp for the buffer used to modeset and
  // framebuffer ID 2 is used when the window to display mapping is done.
  EXPECT_EQ(3u, drm_->current_framebuffer());
  surface_->PresentCanvas(gfx::Rect());
  drm_->RunCallbacks();
  EXPECT_EQ(4u, drm_->current_framebuffer());
}

TEST_F(DrmSurfaceTest, CheckSurfaceContents) {
  SkPaint paint;
  paint.setColor(SK_ColorWHITE);
  SkRect rect =
      SkRect::MakeWH(kDefaultMode.hdisplay / 2, kDefaultMode.vdisplay / 2);
  surface_->GetSurface()->getCanvas()->drawRect(rect, paint);
  surface_->PresentCanvas(
      gfx::Rect(0, 0, kDefaultMode.hdisplay / 2, kDefaultMode.vdisplay / 2));
  drm_->RunCallbacks();

  SkBitmap image;
  std::vector<skia::RefPtr<SkSurface>> framebuffers =
      GetFramebuffers(drm_.get());

  // Buffer 0 is the modesetting buffer, buffer 2 is the frontbuffer and buffer
  // 1 is the backbuffer.
  EXPECT_EQ(3u, framebuffers.size());

  image.setInfo(framebuffers[1]->getCanvas()->imageInfo());
  EXPECT_TRUE(framebuffers[1]->getCanvas()->readPixels(&image, 0, 0));

  EXPECT_EQ(kDefaultMode.hdisplay, image.width());
  EXPECT_EQ(kDefaultMode.vdisplay, image.height());

  // Make sure the updates are correctly propagated to the native surface.
  for (int i = 0; i < image.height(); ++i) {
    for (int j = 0; j < image.width(); ++j) {
      if (j < kDefaultMode.hdisplay / 2 && i < kDefaultMode.vdisplay / 2)
        EXPECT_EQ(SK_ColorWHITE, image.getColor(j, i));
      else
        EXPECT_EQ(SK_ColorBLACK, image.getColor(j, i));
    }
  }
}

TEST_F(DrmSurfaceTest, CheckSurfaceContentsAfter2QueuedPresents) {
  gfx::Rect rect;
  // Present an empty buffer but don't respond with the page flip event since we
  // want to make sure the following presents will aggregate correctly.
  surface_->PresentCanvas(rect);

  SkPaint paint;
  paint.setColor(SK_ColorWHITE);
  rect.SetRect(0, 0, kDefaultMode.hdisplay / 2, kDefaultMode.vdisplay / 2);
  surface_->GetSurface()->getCanvas()->drawRect(RectToSkRect(rect), paint);
  surface_->PresentCanvas(rect);

  paint.setColor(SK_ColorRED);
  rect.SetRect(0, kDefaultMode.vdisplay / 2, kDefaultMode.hdisplay / 2,
               kDefaultMode.vdisplay / 2);
  surface_->GetSurface()->getCanvas()->drawRect(RectToSkRect(rect), paint);
  surface_->PresentCanvas(rect);

  drm_->RunCallbacks();

  SkBitmap image;
  std::vector<skia::RefPtr<SkSurface>> framebuffers =
      GetFramebuffers(drm_.get());

  // Buffer 0 is the modesetting buffer, buffer 2 is the backbuffer and buffer
  // 1 is the frontbuffer.
  EXPECT_EQ(3u, framebuffers.size());

  image.setInfo(framebuffers[2]->getCanvas()->imageInfo());
  EXPECT_TRUE(framebuffers[2]->getCanvas()->readPixels(&image, 0, 0));

  EXPECT_EQ(kDefaultMode.hdisplay, image.width());
  EXPECT_EQ(kDefaultMode.vdisplay, image.height());

  // Make sure the updates are correctly propagated to the native surface.
  for (int i = 0; i < image.height(); ++i) {
    for (int j = 0; j < image.width(); ++j) {
      if (j < kDefaultMode.hdisplay / 2 && i < kDefaultMode.vdisplay / 2)
        EXPECT_EQ(SK_ColorWHITE, image.getColor(j, i));
      else if (j < kDefaultMode.hdisplay / 2)
        EXPECT_EQ(SK_ColorRED, image.getColor(j, i));
      else
        EXPECT_EQ(SK_ColorBLACK, image.getColor(j, i));
    }
  }
}