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/buffer_data.h"
6
7 #include <gbm.h>
8
9 #include "base/logging.h"
10 #include "ui/ozone/platform/dri/dri_wrapper.h"
11
12 namespace ui {
13
14 // Pixel configuration for the current buffer format.
15 // TODO(dnicoara) These will need to change once we query the hardware for
16 // supported configurations.
17 const uint8_t kColorDepth = 24;
18 const uint8_t kPixelDepth = 32;
19
BufferData(DriWrapper * dri,gbm_bo * buffer)20 BufferData::BufferData(DriWrapper* dri, gbm_bo* buffer)
21 : dri_(dri),
22 handle_(gbm_bo_get_handle(buffer).u32),
23 framebuffer_(0) {
24 // Register the buffer with the controller. This will allow us to scan out the
25 // buffer once we're done drawing into it. If we can't register the buffer
26 // then there's no point in having BufferData associated with it.
27 if (!dri_->AddFramebuffer(gbm_bo_get_width(buffer),
28 gbm_bo_get_height(buffer),
29 kColorDepth,
30 kPixelDepth,
31 gbm_bo_get_stride(buffer),
32 handle_,
33 &framebuffer_)) {
34 LOG(ERROR) << "Failed to register buffer";
35 }
36 }
37
~BufferData()38 BufferData::~BufferData() {
39 if (framebuffer_)
40 dri_->RemoveFramebuffer(framebuffer_);
41 }
42
43 // static
CreateData(DriWrapper * dri,gbm_bo * buffer)44 BufferData* BufferData::CreateData(DriWrapper* dri,
45 gbm_bo* buffer) {
46 BufferData* data = new BufferData(dri, buffer);
47 if (!data->framebuffer()) {
48 delete data;
49 return NULL;
50 }
51
52 // GBM can destroy the buffers at any time as long as they aren't locked. This
53 // sets a callback such that we can clean up all our state when GBM destroys
54 // the buffer.
55 gbm_bo_set_user_data(buffer, data, BufferData::Destroy);
56
57 return data;
58 }
59
60 // static
Destroy(gbm_bo * buffer,void * data)61 void BufferData::Destroy(gbm_bo* buffer, void* data) {
62 BufferData* bd = static_cast<BufferData*>(data);
63 delete bd;
64 }
65
66 // static
GetData(gbm_bo * buffer)67 BufferData* BufferData::GetData(gbm_bo* buffer) {
68 return static_cast<BufferData*>(gbm_bo_get_user_data(buffer));
69 }
70
71 } // namespace ui
72