gfx/ipc/SharedDIBSurface.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "SharedDIBSurface.h"
     8 #include "cairo.h"
    10 namespace mozilla {
    11 namespace gfx {
    13 static const cairo_user_data_key_t SHAREDDIB_KEY = {0};
    15 static const long kBytesPerPixel = 4;
    17 bool
    18 SharedDIBSurface::Create(HDC adc, uint32_t aWidth, uint32_t aHeight,
    19                          bool aTransparent)
    20 {
    21   nsresult rv = mSharedDIB.Create(adc, aWidth, aHeight, aTransparent);
    22   if (NS_FAILED(rv) || !mSharedDIB.IsValid())
    23     return false;
    25   InitSurface(aWidth, aHeight, aTransparent);
    26   return true;
    27 }
    29 bool
    30 SharedDIBSurface::Attach(Handle aHandle, uint32_t aWidth, uint32_t aHeight,
    31                          bool aTransparent)
    32 {
    33   nsresult rv = mSharedDIB.Attach(aHandle, aWidth, aHeight, aTransparent);
    34   if (NS_FAILED(rv) || !mSharedDIB.IsValid())
    35     return false;
    37   InitSurface(aWidth, aHeight, aTransparent);
    38   return true;
    39 }
    41 void
    42 SharedDIBSurface::InitSurface(uint32_t aWidth, uint32_t aHeight,
    43                               bool aTransparent)
    44 {
    45   long stride = long(aWidth * kBytesPerPixel);
    46   unsigned char* data = reinterpret_cast<unsigned char*>(mSharedDIB.GetBits());
    48   gfxImageFormat format = aTransparent ? gfxImageFormat::ARGB32 : gfxImageFormat::RGB24;
    50   gfxImageSurface::InitWithData(data, gfxIntSize(aWidth, aHeight),
    51                                 stride, format);
    53   cairo_surface_set_user_data(mSurface, &SHAREDDIB_KEY, this, nullptr);
    54 }
    56 bool
    57 SharedDIBSurface::IsSharedDIBSurface(gfxASurface* aSurface)
    58 {
    59   return aSurface &&
    60     aSurface->GetType() == gfxSurfaceType::Image &&
    61     aSurface->GetData(&SHAREDDIB_KEY);
    62 }
    64 } // namespace gfx
    65 } // namespace mozilla

mercurial