| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_OZONE_PLATFORM_DRI_DRI_SURFACE_H_ | 5 #ifndef UI_OZONE_PLATFORM_DRI_DRI_SURFACE_H_ |
| 6 #define UI_OZONE_PLATFORM_DRI_DRI_SURFACE_H_ | 6 #define UI_OZONE_PLATFORM_DRI_DRI_SURFACE_H_ |
| 7 | 7 |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "ui/gfx/geometry/size.h" | 10 #include "ui/gfx/geometry/size.h" |
| 11 #include "ui/gfx/skia_util.h" | 11 #include "ui/gfx/skia_util.h" |
| 12 #include "ui/ozone/ozone_export.h" | 12 #include "ui/ozone/ozone_export.h" |
| 13 #include "ui/ozone/platform/dri/scanout_surface.h" |
| 13 | 14 |
| 14 class SkCanvas; | 15 class SkCanvas; |
| 15 | 16 |
| 16 namespace ui { | 17 namespace ui { |
| 17 | 18 |
| 18 class DriBuffer; | 19 class DriBuffer; |
| 19 class DriWrapper; | 20 class DriWrapper; |
| 20 | 21 |
| 21 // DriSurface is used to represent a surface that can be scanned out | 22 // An implementation of ScanoutSurface which uses dumb buffers (used for |
| 22 // to a monitor. It will store the internal state associated with the drawing | 23 // software rendering). |
| 23 // surface associated with it. DriSurface also performs all the needed | 24 class OZONE_EXPORT DriSurface : public ScanoutSurface { |
| 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(dri_wrapper, size); | |
| 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 OZONE_EXPORT DriSurface { | |
| 121 public: | 25 public: |
| 122 DriSurface(DriWrapper* dri, const gfx::Size& size); | 26 DriSurface(DriWrapper* dri, const gfx::Size& size); |
| 123 virtual ~DriSurface(); | 27 virtual ~DriSurface(); |
| 124 | 28 |
| 125 // Used to allocate all necessary buffers for this surface. If the | |
| 126 // initialization succeeds, the device is ready to be used for drawing | |
| 127 // operations. | |
| 128 // Returns true if the initialization is successful, false otherwise. | |
| 129 bool Initialize(); | |
| 130 | |
| 131 // Returns the ID of the current backbuffer. | |
| 132 uint32_t GetFramebufferId() const; | |
| 133 | |
| 134 // Returns the handle for the current backbuffer. | |
| 135 uint32_t GetHandle() const; | |
| 136 | |
| 137 // Synchronizes and swaps the back buffer with the front buffer. | |
| 138 void SwapBuffers(); | |
| 139 | |
| 140 // Get a Skia canvas for a backbuffer. | 29 // Get a Skia canvas for a backbuffer. |
| 141 SkCanvas* GetDrawableForWidget(); | 30 SkCanvas* GetDrawableForWidget(); |
| 142 | 31 |
| 143 const gfx::Size& size() const { return size_; } | 32 // ScanoutSurface: |
| 33 virtual bool Initialize() OVERRIDE; |
| 34 virtual uint32_t GetFramebufferId() const OVERRIDE; |
| 35 virtual uint32_t GetHandle() const OVERRIDE; |
| 36 virtual void SwapBuffers() OVERRIDE; |
| 37 virtual gfx::Size Size() const OVERRIDE; |
| 144 | 38 |
| 145 private: | 39 private: |
| 146 DriBuffer* frontbuffer() const { return bitmaps_[front_buffer_].get(); } | 40 DriBuffer* frontbuffer() const { return bitmaps_[front_buffer_].get(); } |
| 147 DriBuffer* backbuffer() const { return bitmaps_[front_buffer_ ^ 1].get(); } | 41 DriBuffer* backbuffer() const { return bitmaps_[front_buffer_ ^ 1].get(); } |
| 148 | 42 |
| 149 // Used to create the backing buffers. | 43 // Used to create the backing buffers. |
| 150 virtual DriBuffer* CreateBuffer(); | 44 virtual DriBuffer* CreateBuffer(); |
| 151 | 45 |
| 152 // Stores the connection to the graphics card. Pointer not owned by this | 46 // Stores the connection to the graphics card. Pointer not owned by this |
| 153 // class. | 47 // class. |
| 154 DriWrapper* dri_; | 48 DriWrapper* dri_; |
| 155 | 49 |
| 156 // The actual buffers used for painting. | 50 // The actual buffers used for painting. |
| 157 scoped_ptr<DriBuffer> bitmaps_[2]; | 51 scoped_ptr<DriBuffer> bitmaps_[2]; |
| 158 | 52 |
| 159 // Keeps track of which bitmap is |buffers_| is the frontbuffer. | 53 // Keeps track of which bitmap is |buffers_| is the frontbuffer. |
| 160 int front_buffer_; | 54 int front_buffer_; |
| 161 | 55 |
| 162 // Surface size. | 56 // Surface size. |
| 163 gfx::Size size_; | 57 gfx::Size size_; |
| 164 | 58 |
| 165 DISALLOW_COPY_AND_ASSIGN(DriSurface); | 59 DISALLOW_COPY_AND_ASSIGN(DriSurface); |
| 166 }; | 60 }; |
| 167 | 61 |
| 168 } // namespace ui | 62 } // namespace ui |
| 169 | 63 |
| 170 #endif // UI_OZONE_PLATFORM_DRI_DRI_SURFACE_H_ | 64 #endif // UI_OZONE_PLATFORM_DRI_DRI_SURFACE_H_ |
| OLD | NEW |