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 #ifndef UI_OZONE_PLATFORM_DRM_GPU_DRM_DUMB_BUFFER_H_
6 #define UI_OZONE_PLATFORM_DRM_GPU_DRM_DUMB_BUFFER_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include "base/macros.h"
12 #include "third_party/skia/include/core/SkRefCnt.h"
13 #include "ui/ozone/platform/drm/gpu/drm_framebuffer.h"
14 
15 class SkCanvas;
16 struct SkImageInfo;
17 class SkSurface;
18 
19 namespace ui {
20 
21 class DrmDevice;
22 
23 // Wrapper for a DRM allocated buffer. Keeps track of the native properties of
24 // the buffer and wraps the pixel memory into a SkSurface which can be used to
25 // draw into using Skia.
26 class DrmDumbBuffer {
27  public:
28   enum class HandleCloser {
29     DESTROY_DUMB,
30     GEM_CLOSE,
31   };
32 
33   DrmDumbBuffer(const scoped_refptr<DrmDevice>& drm);
34   ~DrmDumbBuffer();
35 
36   // Allocates a new dumb buffer, maps it, and wraps it in an SkSurface.
37   // |info| determines the buffer characteristics (size, color format).
38   bool Initialize(const SkImageInfo& info);
39 
40   // Imports an existing framebuffer, maps it, and wraps it in an SkSurface.
41   bool InitializeFromFramebuffer(uint32_t framebuffer_id);
42 
43   SkCanvas* GetCanvas() const;
surface()44   SkSurface* surface() const { return surface_.get(); }
45 
46   uint32_t GetHandle() const;
47   gfx::Size GetSize() const;
stride()48   uint32_t stride() const { return stride_; }
49 
50  private:
51   bool MapDumbBuffer(const SkImageInfo& info);
52 
53   const scoped_refptr<DrmDevice> drm_;
54 
55   // Length of a row of pixels.
56   uint32_t stride_ = 0;
57 
58   // Buffer handle used by the DRM allocator.
59   uint32_t handle_ = 0;
60 
61   // Method of closing |handle_|.
62   HandleCloser handle_closer_ = HandleCloser::DESTROY_DUMB;
63 
64   // Base address for memory mapping.
65   void* mmap_base_ = 0;
66 
67   // Size for memory mapping.
68   size_t mmap_size_ = 0;
69 
70   // Wrapper around the native pixel memory.
71   sk_sp<SkSurface> surface_;
72 
73   DISALLOW_COPY_AND_ASSIGN(DrmDumbBuffer);
74 };
75 
76 }  // namespace ui
77 
78 #endif  // UI_OZONE_PLATFORM_DRM_GPU_DRM_DUMB_BUFFER_H_
79