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
3 // found in the LICENSE file.
5 #include "ui/accelerated_widget_mac/surface_handle_types.h"
7 #include "base/logging.h"
12 // The type of the handle is stored in the upper 64 bits.
13 const uint64 kTypeMask
= 0xFFFFFFFFull
<< 32;
15 const uint64 kTypeIOSurface
= 0x01010101ull
<< 32;
16 const uint64 kTypeCAContext
= 0x02020202ull
<< 32;
18 // To make it a bit less likely that we'll just cast off the top bits of the
19 // handle to get the ID, XOR lower bits with a type-specific mask.
20 const uint32 kXORMaskIOSurface
= 0x01010101;
21 const uint32 kXORMaskCAContext
= 0x02020202;
25 SurfaceHandleType
GetSurfaceHandleType(uint64 surface_handle
) {
26 switch(surface_handle
& kTypeMask
) {
28 return kSurfaceHandleTypeIOSurface
;
30 return kSurfaceHandleTypeCAContext
;
32 return kSurfaceHandleTypeInvalid
;
35 IOSurfaceID
IOSurfaceIDFromSurfaceHandle(uint64 surface_handle
) {
36 DCHECK_EQ(kSurfaceHandleTypeIOSurface
, GetSurfaceHandleType(surface_handle
));
37 return static_cast<uint32
>(surface_handle
) ^ kXORMaskIOSurface
;
40 CAContextID
CAContextIDFromSurfaceHandle(uint64 surface_handle
) {
41 DCHECK_EQ(kSurfaceHandleTypeCAContext
, GetSurfaceHandleType(surface_handle
));
42 return static_cast<uint32
>(surface_handle
) ^ kXORMaskCAContext
;
45 uint64
SurfaceHandleFromIOSurfaceID(IOSurfaceID io_surface_id
) {
46 return kTypeIOSurface
| (io_surface_id
^ kXORMaskIOSurface
);
49 uint64
SurfaceHandleFromCAContextID(CAContextID ca_context_id
) {
50 return kTypeCAContext
| (ca_context_id
^ kXORMaskCAContext
);