Home | History | Annotate | Download | only in gpu
      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.
      4 
      5 #include "content/common/gpu/surface_handle_types_mac.h"
      6 
      7 #include "base/logging.h"
      8 
      9 namespace content {
     10 namespace {
     11 
     12 // The type of the handle is stored in the upper 64 bits.
     13 const uint64 kTypeMask = 0xFFFFFFFFull << 32;
     14 
     15 const uint64 kTypeIOSurface = 0x01010101ull << 32;
     16 const uint64 kTypeCAContext = 0x02020202ull << 32;
     17 
     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;
     22 
     23 }  // namespace
     24 
     25 SurfaceHandleType GetSurfaceHandleType(uint64 surface_handle) {
     26   switch(surface_handle & kTypeMask) {
     27     case kTypeIOSurface:
     28       return kSurfaceHandleTypeIOSurface;
     29     case kTypeCAContext:
     30       return kSurfaceHandleTypeCAContext;
     31   }
     32   return kSurfaceHandleTypeInvalid;
     33 }
     34 
     35 IOSurfaceID IOSurfaceIDFromSurfaceHandle(uint64 surface_handle) {
     36   DCHECK_EQ(kSurfaceHandleTypeIOSurface, GetSurfaceHandleType(surface_handle));
     37   return static_cast<uint32>(surface_handle) ^ kXORMaskIOSurface;
     38 }
     39 
     40 CAContextID CAContextIDFromSurfaceHandle(uint64 surface_handle) {
     41   DCHECK_EQ(kSurfaceHandleTypeCAContext, GetSurfaceHandleType(surface_handle));
     42   return static_cast<uint32>(surface_handle) ^ kXORMaskCAContext;
     43 }
     44 
     45 uint64 SurfaceHandleFromIOSurfaceID(IOSurfaceID io_surface_id) {
     46   return kTypeIOSurface | (io_surface_id ^ kXORMaskIOSurface);
     47 }
     48 
     49 uint64 SurfaceHandleFromCAContextID(CAContextID ca_context_id) {
     50   return kTypeCAContext | (ca_context_id ^ kXORMaskCAContext);
     51 }
     52 
     53 }  //  namespace content
     54