[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / ui / surface / transport_dib.h
blobc2321448df3a99240c05cdadb83de498e56f94d6
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.
5 #ifndef UI_SURFACE_TRANSPORT_DIB_H_
6 #define UI_SURFACE_TRANSPORT_DIB_H_
8 #include "base/basictypes.h"
9 #include "base/memory/shared_memory.h"
10 #include "ui/surface/surface_export.h"
12 #if defined(OS_WIN)
13 #include <windows.h>
14 #endif
16 class SkCanvas;
18 // -----------------------------------------------------------------------------
19 // A TransportDIB is a block of memory that is used to transport pixels
20 // between processes: from the renderer process to the browser, and
21 // between renderer and plugin processes.
22 // -----------------------------------------------------------------------------
23 class SURFACE_EXPORT TransportDIB {
24 public:
25 ~TransportDIB();
27 // A Handle is the type which can be sent over the wire so that the remote
28 // side can map the transport DIB.
29 #if defined(OS_WIN)
30 typedef HANDLE Handle;
31 #else // OS_POSIX
32 typedef base::SharedMemoryHandle Handle;
33 #endif
35 // Returns a default, invalid handle, that is meant to indicate a missing
36 // Transport DIB.
37 static Handle DefaultHandleValue() {
38 return base::SharedMemory::NULLHandle();
41 // Create a new TransportDIB, returning NULL on failure.
43 // The size is the minimum size in bytes of the memory backing the transport
44 // DIB (we may actually allocate more than that to give us better reuse when
45 // cached).
47 // The sequence number is used to uniquely identify the transport DIB. It
48 // should be unique for all transport DIBs ever created in the same
49 // renderer.
50 static TransportDIB* Create(size_t size, uint32 sequence_num);
52 // Map the referenced transport DIB. The caller owns the returned object.
53 // Returns NULL on failure.
54 static TransportDIB* Map(Handle transport_dib);
56 // Create a new |TransportDIB| with a handle to the shared memory. This
57 // always returns a valid pointer. The DIB is not mapped.
58 static TransportDIB* CreateWithHandle(Handle handle);
60 // Returns true if the handle is valid.
61 static bool is_valid_handle(Handle dib);
63 // Returns a canvas using the memory of this TransportDIB. The returned
64 // pointer will be owned by the caller. The bitmap will be of the given size,
65 // which should fit inside this memory.
67 // On POSIX, this |TransportDIB| will be mapped if not already. On Windows,
68 // this |TransportDIB| will NOT be mapped and should not be mapped prior,
69 // because PlatformCanvas will map the file internally.
71 // Will return NULL on allocation failure. This could be because the image
72 // is too large to map into the current process' address space.
73 SkCanvas* GetPlatformCanvas(int w, int h);
75 // Map the DIB into the current process if it is not already. This is used to
76 // map a DIB that has already been created. Returns true if the DIB is mapped.
77 bool Map();
79 // Return a pointer to the shared memory.
80 void* memory() const;
82 // Return the maximum size of the shared memory. This is not the amount of
83 // data which is valid, you have to know that via other means, this is simply
84 // the maximum amount that /could/ be valid.
85 size_t size() const { return size_; }
87 // Returns a pointer to the SharedMemory object that backs the transport dib.
88 base::SharedMemory* shared_memory();
90 private:
91 TransportDIB();
93 // Verifies that the dib can hold a canvas of the requested dimensions.
94 bool VerifyCanvasSize(int w, int h);
96 explicit TransportDIB(base::SharedMemoryHandle dib);
97 base::SharedMemory shared_memory_;
98 uint32 sequence_num_;
99 size_t size_; // length, in bytes
101 DISALLOW_COPY_AND_ASSIGN(TransportDIB);
104 #endif // UI_SURFACE_TRANSPORT_DIB_H_