Home | History | Annotate | Download | only in surface
      1 // Copyright (c) 2012 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_SURFACE_TRANSPORT_DIB_H_
      6 #define UI_SURFACE_TRANSPORT_DIB_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "ui/surface/surface_export.h"
     10 
     11 #if !defined(TOOLKIT_GTK)
     12 #include "base/memory/shared_memory.h"
     13 #endif
     14 
     15 #if defined(OS_WIN)
     16 #include <windows.h>
     17 #elif defined(TOOLKIT_GTK)
     18 #include "ui/base/x/x11_util.h"
     19 #include "ui/gfx/x/x11_types.h"
     20 #endif
     21 
     22 class SkCanvas;
     23 
     24 // -----------------------------------------------------------------------------
     25 // A TransportDIB is a block of memory that is used to transport pixels
     26 // between processes: from the renderer process to the browser, and
     27 // between renderer and plugin processes.
     28 // -----------------------------------------------------------------------------
     29 class SURFACE_EXPORT TransportDIB {
     30  public:
     31   ~TransportDIB();
     32 
     33   // Two typedefs are defined. A Handle is the type which can be sent over
     34   // the wire so that the remote side can map the transport DIB. The Id typedef
     35   // is sufficient to identify the transport DIB when you know that the remote
     36   // side already may have it mapped.
     37 #if defined(OS_WIN)
     38   typedef HANDLE Handle;
     39   // On Windows, the Id type includes a sequence number (epoch) to solve an ABA
     40   // issue:
     41   //   1) Process A creates a transport DIB with HANDLE=1 and sends to B.
     42   //   2) Process B maps the transport DIB and caches 1 -> DIB.
     43   //   3) Process A closes the transport DIB and creates a new one. The new DIB
     44   //      is also assigned HANDLE=1.
     45   //   4) Process A sends the Handle to B, but B incorrectly believes that it
     46   //      already has it cached.
     47   struct HandleAndSequenceNum {
     48     HandleAndSequenceNum()
     49         : handle(NULL),
     50           sequence_num(0) {
     51     }
     52 
     53     HandleAndSequenceNum(HANDLE h, uint32 seq_num)
     54         : handle(h),
     55           sequence_num(seq_num) {
     56     }
     57 
     58     bool operator==(const HandleAndSequenceNum& other) const {
     59       return other.handle == handle && other.sequence_num == sequence_num;
     60     }
     61 
     62     bool operator<(const HandleAndSequenceNum& other) const {
     63       // Use the lexicographic order on the tuple <handle, sequence_num>.
     64       if (other.handle != handle)
     65         return other.handle < handle;
     66       return other.sequence_num < sequence_num;
     67     }
     68 
     69     HANDLE handle;
     70     uint32 sequence_num;
     71   };
     72   typedef HandleAndSequenceNum Id;
     73 
     74   // Returns a default, invalid handle, that is meant to indicate a missing
     75   // Transport DIB.
     76   static Handle DefaultHandleValue() { return NULL; }
     77 
     78   // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE
     79   // ACTUALLY USED AS A REAL HANDLE.
     80   static Handle GetFakeHandleForTest() {
     81     static int fake_handle = 10;
     82     return reinterpret_cast<Handle>(fake_handle++);
     83   }
     84 #elif defined(TOOLKIT_GTK)
     85   typedef int Handle;  // These two ints are SysV IPC shared memory keys
     86   struct Id {
     87     // Ensure that default initialized Ids are invalid.
     88     Id() : shmkey(-1) {
     89     }
     90 
     91     bool operator<(const Id& other) const {
     92       return shmkey < other.shmkey;
     93     }
     94 
     95     bool operator==(const Id& other) const {
     96       return shmkey == other.shmkey;
     97     }
     98 
     99     int shmkey;
    100   };
    101 
    102   // Returns a default, invalid handle, that is meant to indicate a missing
    103   // Transport DIB.
    104   static Handle DefaultHandleValue() { return -1; }
    105 
    106   // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE
    107   // ACTUALLY USED AS A REAL HANDLE.
    108   static Handle GetFakeHandleForTest() {
    109     static int fake_handle = 10;
    110     return fake_handle++;
    111   }
    112 #else  // OS_POSIX
    113   typedef base::SharedMemoryHandle Handle;
    114   // On POSIX, the inode number of the backing file is used as an id.
    115 #if defined(OS_ANDROID)
    116   typedef base::SharedMemoryHandle Id;
    117 #else
    118   typedef base::SharedMemoryId Id;
    119 #endif
    120 
    121   // Returns a default, invalid handle, that is meant to indicate a missing
    122   // Transport DIB.
    123   static Handle DefaultHandleValue() { return Handle(); }
    124 
    125   // Returns a value that is ONLY USEFUL FOR TESTS WHERE IT WON'T BE
    126   // ACTUALLY USED AS A REAL HANDLE.
    127   static Handle GetFakeHandleForTest() {
    128     static int fake_handle = 10;
    129     return Handle(fake_handle++, false);
    130   }
    131 #endif
    132 
    133   // Create a new TransportDIB, returning NULL on failure.
    134   //
    135   // The size is the minimum size in bytes of the memory backing the transport
    136   // DIB (we may actually allocate more than that to give us better reuse when
    137   // cached).
    138   //
    139   // The sequence number is used to uniquely identify the transport DIB. It
    140   // should be unique for all transport DIBs ever created in the same
    141   // renderer.
    142   static TransportDIB* Create(size_t size, uint32 sequence_num);
    143 
    144   // Map the referenced transport DIB.  The caller owns the returned object.
    145   // Returns NULL on failure.
    146   static TransportDIB* Map(Handle transport_dib);
    147 
    148   // Create a new |TransportDIB| with a handle to the shared memory. This
    149   // always returns a valid pointer. The DIB is not mapped.
    150   static TransportDIB* CreateWithHandle(Handle handle);
    151 
    152   // Returns true if the handle is valid.
    153   static bool is_valid_handle(Handle dib);
    154 
    155   // Returns true if the ID refers to a valid dib.
    156   static bool is_valid_id(Id id);
    157 
    158   // Returns a canvas using the memory of this TransportDIB. The returned
    159   // pointer will be owned by the caller. The bitmap will be of the given size,
    160   // which should fit inside this memory.
    161   //
    162   // On POSIX, this |TransportDIB| will be mapped if not already. On Windows,
    163   // this |TransportDIB| will NOT be mapped and should not be mapped prior,
    164   // because PlatformCanvas will map the file internally.
    165   //
    166   // Will return NULL on allocation failure. This could be because the image
    167   // is too large to map into the current process' address space.
    168   SkCanvas* GetPlatformCanvas(int w, int h);
    169 
    170   // Map the DIB into the current process if it is not already. This is used to
    171   // map a DIB that has already been created. Returns true if the DIB is mapped.
    172   bool Map();
    173 
    174   // Return a pointer to the shared memory.
    175   void* memory() const;
    176 
    177   // Return the maximum size of the shared memory. This is not the amount of
    178   // data which is valid, you have to know that via other means, this is simply
    179   // the maximum amount that /could/ be valid.
    180   size_t size() const { return size_; }
    181 
    182   // Return the identifier which can be used to refer to this shared memory
    183   // on the wire.
    184   Id id() const;
    185 
    186   // Return a handle to the underlying shared memory. This can be sent over the
    187   // wire to give this transport DIB to another process.
    188   Handle handle() const;
    189 
    190 #if defined(TOOLKIT_GTK)
    191   // Map the shared memory into the X server and return an id for the shared
    192   // segment.
    193   XID MapToX(XDisplay* connection);
    194 
    195   void IncreaseInFlightCounter() { inflight_counter_++; }
    196   // Decreases the inflight counter, and deletes the transport DIB if it is
    197   // detached.
    198   void DecreaseInFlightCounter();
    199 
    200   // Deletes this transport DIB and detaches the shared memory once the
    201   // |inflight_counter_| is zero.
    202   void Detach();
    203 #endif
    204 
    205  private:
    206   TransportDIB();
    207 
    208   // Verifies that the dib can hold a canvas of the requested dimensions.
    209   bool VerifyCanvasSize(int w, int h);
    210 
    211 #if defined(TOOLKIT_GTK)
    212   Id key_;  // SysV shared memory id
    213   void* address_;  // mapped address
    214   XSharedMemoryId x_shm_;  // X id for the shared segment
    215   XDisplay* display_;  // connection to the X server
    216   size_t inflight_counter_;  // How many requests to the X server are in flight
    217   bool detached_;  // If true, delete the transport DIB when it is idle
    218 #else
    219   explicit TransportDIB(base::SharedMemoryHandle dib);
    220   base::SharedMemory shared_memory_;
    221   uint32 sequence_num_;
    222 #endif
    223   size_t size_;  // length, in bytes
    224 
    225   DISALLOW_COPY_AND_ASSIGN(TransportDIB);
    226 };
    227 
    228 #endif  // UI_SURFACE_TRANSPORT_DIB_H_
    229