Home | History | Annotate | Download | only in ozone
      1 // Copyright (c) 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_SURFACE_LNUX_FACTORY_OZONE_H_
      6 #define UI_GFX_OZONE_SURFACE_LNUX_FACTORY_OZONE_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/native_library.h"
     10 #include "ui/gfx/gfx_export.h"
     11 #include "ui/gfx/native_widget_types.h"
     12 #include "ui/gfx/rect.h"
     13 
     14 class SkCanvas;
     15 
     16 namespace gfx {
     17 class Screen;
     18 class VSyncProvider;
     19 
     20 // The Ozone interface allows external implementations to hook into Chromium to
     21 // provide a system specific implementation. The Ozone interface supports two
     22 // drawing modes: 1) accelerated drawing through EGL and 2) software drawing
     23 // through Skia.
     24 //
     25 // The following functionality is specific to the drawing mode and may not have
     26 // any meaningful implementation in the other mode. An implementation must
     27 // provide functionality for at least one mode.
     28 //
     29 // 1) Accelerated Drawing (EGL path):
     30 //
     31 // The following functions are specific to EGL:
     32 //  - GetNativeDisplay
     33 //  - LoadEGLGLES2Bindings
     34 //  - GetEGLSurfaceProperties (optional if the properties match the default
     35 //  Chromium ones).
     36 //
     37 // 2) Software Drawing (Skia):
     38 //
     39 // The following function is specific to the software path:
     40 //  - GetCanvasForWidget
     41 //
     42 // The accelerated path can optionally provide support for the software drawing
     43 // path.
     44 //
     45 // The remaining functions are not covered since they are needed in both drawing
     46 // modes (See comments bellow for descriptions).
     47 class GFX_EXPORT SurfaceFactoryOzone {
     48  public:
     49   // Describes the state of the hardware after initialization.
     50   enum HardwareState {
     51     UNINITIALIZED,
     52     INITIALIZED,
     53     FAILED,
     54   };
     55 
     56   typedef void*(*GLGetProcAddressProc)(const char* name);
     57   typedef base::Callback<void(base::NativeLibrary)> AddGLLibraryCallback;
     58   typedef base::Callback<void(GLGetProcAddressProc)>
     59       SetGLGetProcAddressProcCallback;
     60 
     61   SurfaceFactoryOzone();
     62   virtual ~SurfaceFactoryOzone();
     63 
     64   // Returns the instance
     65   static SurfaceFactoryOzone* GetInstance();
     66 
     67   // Returns a display spec as in |CreateDisplayFromSpec| for the default
     68   // native surface.
     69   virtual const char* DefaultDisplaySpec();
     70 
     71   // Sets the implementation delegate. Ownership is retained by the caller.
     72   static void SetInstance(SurfaceFactoryOzone* impl);
     73 
     74   // TODO(rjkroege): decide how to separate screen/display stuff from SFOz
     75   // This method implements gfx::Screen, particularly useful in Desktop Aura.
     76   virtual gfx::Screen* CreateDesktopScreen();
     77 
     78   // Configures the display hardware. Must be called from within the GPU
     79   // process before the sandbox has been activated.
     80   virtual HardwareState InitializeHardware() = 0;
     81 
     82   // Cleans up display hardware state. Call this from within the GPU process.
     83   // This method must be safe to run inside of the sandbox.
     84   virtual void ShutdownHardware() = 0;
     85 
     86   // Returns native platform display handle. This is used to obtain the EGL
     87   // display connection for the native display.
     88   virtual intptr_t GetNativeDisplay();
     89 
     90   // Obtains an AcceleratedWidget backed by a native Linux framebuffer.
     91   // The  returned AcceleratedWidget is an opaque token that must realized
     92   // before it can be used to create a GL surface.
     93   virtual gfx::AcceleratedWidget GetAcceleratedWidget() = 0;
     94 
     95   // Realizes an AcceleratedWidget so that the returned AcceleratedWidget
     96   // can be used to to create a GLSurface. This method may only be called in
     97   // a process that has a valid GL context.
     98   virtual gfx::AcceleratedWidget RealizeAcceleratedWidget(
     99       gfx::AcceleratedWidget w) = 0;
    100 
    101   // Sets up GL bindings for the native surface. Takes two callback parameters
    102   // that allow Ozone to register the GL bindings.
    103   virtual bool LoadEGLGLES2Bindings(
    104       AddGLLibraryCallback add_gl_library,
    105       SetGLGetProcAddressProcCallback set_gl_get_proc_address) = 0;
    106 
    107   // If possible attempts to resize the given AcceleratedWidget instance and if
    108   // a resize action was performed returns true, otherwise false (native
    109   // hardware may only support a single fixed size).
    110   virtual bool AttemptToResizeAcceleratedWidget(
    111       gfx::AcceleratedWidget w,
    112       const gfx::Rect& bounds) = 0;
    113 
    114   // Called after the appropriate GL swap buffers command. Used if extra work
    115   // is needed to perform the actual buffer swap.
    116   virtual bool SchedulePageFlip(gfx::AcceleratedWidget w);
    117 
    118   // Returns a SkCanvas for the backing buffers. Drawing to the canvas will draw
    119   // to the native surface. The canvas is intended for use when no EGL
    120   // acceleration is possible. Its implementation is optional when an EGL
    121   // backend is provided for rendering.
    122   virtual SkCanvas* GetCanvasForWidget(gfx::AcceleratedWidget w);
    123 
    124   // Returns a gfx::VsyncProvider for the provided AcceleratedWidget. Note
    125   // that this may be called after we have entered the sandbox so if there are
    126   // operations (e.g. opening a file descriptor providing vsync events) that
    127   // must be done outside of the sandbox, they must have been completed
    128   // in InitializeHardware. Returns NULL on error.
    129   virtual gfx::VSyncProvider* GetVSyncProvider(gfx::AcceleratedWidget w) = 0;
    130 
    131   // Returns an array of EGL properties, which can be used in any EGL function
    132   // used to select a display configuration. Note that all properties should be
    133   // immediately followed by the corresponding desired value and array should be
    134   // terminated with EGL_NONE. Ownership of the array is not transferred to
    135   // caller. desired_list contains list of desired EGL properties and values.
    136   virtual const int32* GetEGLSurfaceProperties(const int32* desired_list);
    137 
    138   // Create a default SufaceFactoryOzone implementation useful for tests.
    139   static SurfaceFactoryOzone* CreateTestHelper();
    140 
    141  private:
    142   static SurfaceFactoryOzone* impl_; // not owned
    143 };
    144 
    145 }  // namespace gfx
    146 
    147 #endif  // UI_GFX_OZONE_SURFACE_LNUX_FACTORY_OZONE_H_
    148