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.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "third_party/skia/include/core/SkCanvas.h"
7 #include "third_party/skia/include/core/SkColor.h"
8 #include "third_party/skia/include/core/SkDevice.h"
9 #include "ui/ozone/platform/dri/dri_buffer.h"
10 #include "ui/ozone/platform/dri/dri_surface.h"
11 #include "ui/ozone/platform/dri/hardware_display_controller.h"
12 #include "ui/ozone/platform/dri/test/mock_dri_surface.h"
13 #include "ui/ozone/platform/dri/test/mock_dri_wrapper.h"
17 // Create a basic mode for a 6x4 screen.
18 const drmModeModeInfo kDefaultMode
=
19 {0, 6, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, {'\0'}};
23 class DriSurfaceTest
: public testing::Test
{
27 virtual void SetUp() OVERRIDE
;
28 virtual void TearDown() OVERRIDE
;
31 scoped_ptr
<ui::MockDriWrapper
> drm_
;
32 scoped_ptr
<ui::HardwareDisplayController
> controller_
;
33 scoped_ptr
<ui::MockDriSurface
> surface_
;
36 DISALLOW_COPY_AND_ASSIGN(DriSurfaceTest
);
39 void DriSurfaceTest::SetUp() {
40 drm_
.reset(new ui::MockDriWrapper(3));
41 controller_
.reset(new ui::HardwareDisplayController(drm_
.get(), 1, 1));
43 surface_
.reset(new ui::MockDriSurface(
44 drm_
.get(), gfx::Size(kDefaultMode
.hdisplay
, kDefaultMode
.vdisplay
)));
47 void DriSurfaceTest::TearDown() {
53 TEST_F(DriSurfaceTest
, FailInitialization
) {
54 surface_
->set_initialize_expectation(false);
55 EXPECT_FALSE(surface_
->Initialize());
58 TEST_F(DriSurfaceTest
, SuccessfulInitialization
) {
59 EXPECT_TRUE(surface_
->Initialize());
62 TEST_F(DriSurfaceTest
, CheckFBIDOnSwap
) {
63 EXPECT_TRUE(surface_
->Initialize());
64 controller_
->BindSurfaceToController(surface_
.PassAs
<ui::ScanoutSurface
>(),
67 // Check that the framebuffer ID is correct.
68 EXPECT_EQ(2u, controller_
->surface()->GetFramebufferId());
70 controller_
->surface()->SwapBuffers();
72 EXPECT_EQ(1u, controller_
->surface()->GetFramebufferId());
75 TEST_F(DriSurfaceTest
, CheckPixelPointerOnSwap
) {
76 EXPECT_TRUE(surface_
->Initialize());
78 void* bitmap_pixels1
= surface_
->GetDrawableForWidget()->getDevice()
79 ->accessBitmap(false).getPixels();
81 surface_
->SwapBuffers();
83 void* bitmap_pixels2
= surface_
->GetDrawableForWidget()->getDevice()
84 ->accessBitmap(false).getPixels();
86 // Check that once the buffers have been swapped the drawable's underlying
87 // pixels have been changed.
88 EXPECT_NE(bitmap_pixels1
, bitmap_pixels2
);
91 TEST_F(DriSurfaceTest
, CheckCorrectBufferSync
) {
92 EXPECT_TRUE(surface_
->Initialize());
94 SkCanvas
* canvas
= surface_
->GetDrawableForWidget();
96 // Modify part of the canvas.
98 canvas
->getDeviceSize().width() / 2,
99 canvas
->getDeviceSize().height() / 2);
100 canvas
->clipRect(clip
, SkRegion::kReplace_Op
);
102 canvas
->drawColor(SK_ColorWHITE
);
104 surface_
->SwapBuffers();
106 // Verify that the modified contents have been copied over on swap (make sure
107 // the 2 buffers have the same content).
108 for (int i
= 0; i
< canvas
->getDeviceSize().height(); ++i
) {
109 for (int j
= 0; j
< canvas
->getDeviceSize().width(); ++j
) {
110 if (i
< clip
.height() && j
< clip
.width())
111 EXPECT_EQ(SK_ColorWHITE
,
112 canvas
->getDevice()->accessBitmap(false).getColor(j
, i
));
114 EXPECT_EQ(SK_ColorBLACK
,
115 canvas
->getDevice()->accessBitmap(false).getColor(j
, i
));