Home | History | Annotate | Download | only in dri
      1 // Copyright 2013 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_GFX_OZONE_DRI_DRI_SURFACE_H_
      6 #define UI_GFX_OZONE_DRI_DRI_SURFACE_H_
      7 
      8 #include "base/compiler_specific.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "ui/gfx/gfx_export.h"
     11 #include "ui/gfx/skia_util.h"
     12 
     13 class SkBitmapDevice;
     14 class SkCanvas;
     15 
     16 namespace gfx {
     17 
     18 class DriSkBitmap;
     19 class HardwareDisplayController;
     20 
     21 // DriSurface is used to represent a surface that can be scanned out
     22 // to a monitor. It will store the internal state associated with the drawing
     23 // surface associated with it. DriSurface also performs all the needed
     24 // operations to initialize and update the drawing surface.
     25 //
     26 // The implementation uses dumb buffers, which is used for software rendering.
     27 // The intent is to have one DriSurface implementation for a
     28 // HardwareDisplayController.
     29 //
     30 // DoubleBufferedSurface is intended to be the software analog to
     31 // EGLNativeSurface while DriSurface is intended to provide the glue
     32 // necessary to initialize and display the surface to the screen.
     33 //
     34 // The typical usage pattern is:
     35 // -----------------------------------------------------------------------------
     36 // HardwareDisplayController controller;
     37 // // Initialize controller
     38 //
     39 // DriSurface* surface = new DriSurface(controller);
     40 // surface.Initialize();
     41 // controller.BindSurfaceToController(surface);
     42 //
     43 // while (true) {
     44 //   SkCanvas* canvas = surface->GetDrawableForWidget();
     45 //   DrawStuff(canvas);
     46 //   controller.SchedulePageFlip();
     47 //
     48 //   Wait for page flip event. The DRM page flip handler will call
     49 //   surface.SwapBuffers();
     50 // }
     51 //
     52 // delete surface;
     53 // -----------------------------------------------------------------------------
     54 // In the above example the wait consists of reading a DRM pageflip event from
     55 // the graphics card file descriptor. This is done by calling |drmHandleEvent|,
     56 // which will read and process the event. |drmHandleEvent| will call a callback
     57 // registered by |SchedulePageFlip| which will update the internal state.
     58 //
     59 // |SchedulePageFlip| can also be used to limit drawing to the screen's vsync
     60 // since page flips only happen on vsync. In a threaded environment a message
     61 // loop would listen on the graphics card file descriptor for an event and
     62 // |drmHandleEvent| would be called from the message loop. The event handler
     63 // would also be responsible for updating the renderer's state and signal that
     64 // it is OK to start drawing the next frame.
     65 //
     66 // The following example will illustrate the system state transitions in one
     67 // iteration of the above loop.
     68 //
     69 // 1. Both buffers contain the same image with b[0] being the front buffer
     70 // (star will represent the frontbuffer).
     71 // -------  -------
     72 // |     |  |     |
     73 // |     |  |     |
     74 // |     |  |     |
     75 // |     |  |     |
     76 // -------  -------
     77 // b[0]*    b[1]
     78 //
     79 // 2. Call |GetBackbuffer| to get a SkCanvas wrapper for the backbuffer and draw
     80 // to it.
     81 // -------  -------
     82 // |     |  |     |
     83 // |     |  |  d  |
     84 // |     |  |     |
     85 // |     |  |     |
     86 // -------  -------
     87 // b[0]*    b[1]
     88 //
     89 // 3. Call |SchedulePageFlip| to display the backbuffer. At this point we can't
     90 // modify b[0] because it is the frontbuffer and we can't modify b[1] since it
     91 // has been scheduled for pageflip. If we do draw in b[1] it is possible that
     92 // the pageflip and draw happen at the same time and we could get tearing.
     93 //
     94 // 4. The pageflip callback is called which will call |SwapSurfaces|. Before
     95 // |SwapSurfaces| is called the state is as following from the hardware's
     96 // perspective:
     97 // -------  -------
     98 // |     |  |     |
     99 // |     |  |  d  |
    100 // |     |  |     |
    101 // |     |  |     |
    102 // -------  -------
    103 // b[0]     b[1]*
    104 //
    105 // 5. |SwapSurfaces| will update out internal reference to the front buffer and
    106 // synchronize the damaged area such that both buffers are identical. The
    107 // damaged area is used from the SkCanvas clip.
    108 // -------  -------
    109 // |     |  |     |
    110 // |  d  |  |  d  |
    111 // |     |  |     |
    112 // |     |  |     |
    113 // -------  -------
    114 // b[0]     b[1]*
    115 //
    116 // The synchronization consists of copying the damaged area from the frontbuffer
    117 // to the backbuffer.
    118 //
    119 // At this point we're back to step 1 and can start a new draw iteration.
    120 class GFX_EXPORT DriSurface {
    121  public:
    122   DriSurface(HardwareDisplayController* controller);
    123 
    124   virtual ~DriSurface();
    125 
    126   // Used to allocate all necessary buffers for this surface. If the
    127   // initialization succeeds, the device is ready to be used for drawing
    128   // operations.
    129   // Returns true if the initialization is successful, false otherwise.
    130   bool Initialize();
    131 
    132   // Returns the ID of the current backbuffer.
    133   uint32_t GetFramebufferId() const;
    134 
    135   // Synchronizes and swaps the back buffer with the front buffer.
    136   void SwapBuffers();
    137 
    138   // Get a Skia canvas for a backbuffer.
    139   SkCanvas* GetDrawableForWidget();
    140 
    141  private:
    142   friend class HardwareDisplayController;
    143 
    144   // Used to create the backing buffers.
    145   virtual DriSkBitmap* CreateBuffer();
    146 
    147   // Stores DRM information for this output device (connector, encoder, last
    148   // CRTC state).
    149   HardwareDisplayController* controller_;
    150 
    151   // The actual buffers used for painting.
    152   scoped_ptr<DriSkBitmap> bitmaps_[2];
    153 
    154   // BitmapDevice for the current backbuffer.
    155   skia::RefPtr<SkBitmapDevice> skia_device_;
    156 
    157   // Canvas for the current backbuffer.
    158   skia::RefPtr<SkCanvas> skia_canvas_;
    159 
    160   // Keeps track of which bitmap is |buffers_| is the frontbuffer.
    161   int front_buffer_;
    162 
    163   DISALLOW_COPY_AND_ASSIGN(DriSurface);
    164 };
    165 
    166 }  // namespace gfx
    167 
    168 #endif  // UI_GFX_OZONE_DRI_DRI_SURFACE_H_
    169