gfx/2d/SourceSurfaceSkia.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 20; 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/. */
     7 #include "Logging.h"
     8 #include "SourceSurfaceSkia.h"
     9 #include "skia/SkBitmap.h"
    10 #include "skia/SkDevice.h"
    11 #include "HelpersSkia.h"
    12 #include "DrawTargetSkia.h"
    13 #include "DataSurfaceHelpers.h"
    15 namespace mozilla {
    16 namespace gfx {
    18 SourceSurfaceSkia::SourceSurfaceSkia()
    19   : mDrawTarget(nullptr), mLocked(false)
    20 {
    21 }
    23 SourceSurfaceSkia::~SourceSurfaceSkia()
    24 {
    25   MaybeUnlock();
    26   if (mDrawTarget) {
    27     mDrawTarget->SnapshotDestroyed();
    28     mDrawTarget = nullptr;
    29   }
    30 }
    32 IntSize
    33 SourceSurfaceSkia::GetSize() const
    34 {
    35   return mSize;
    36 }
    38 SurfaceFormat
    39 SourceSurfaceSkia::GetFormat() const
    40 {
    41   return mFormat;
    42 }
    44 bool
    45 SourceSurfaceSkia::InitFromCanvas(SkCanvas* aCanvas,
    46                                   SurfaceFormat aFormat,
    47                                   DrawTargetSkia* aOwner)
    48 {
    49   SkISize size = aCanvas->getDeviceSize();
    51   mBitmap = (SkBitmap)aCanvas->getDevice()->accessBitmap(false);
    52   mFormat = aFormat;
    54   mSize = IntSize(size.fWidth, size.fHeight);
    55   mStride = mBitmap.rowBytes();
    56   mDrawTarget = aOwner;
    58   return true;
    59 }
    61 bool 
    62 SourceSurfaceSkia::InitFromData(unsigned char* aData,
    63                                 const IntSize &aSize,
    64                                 int32_t aStride,
    65                                 SurfaceFormat aFormat)
    66 {
    67   SkBitmap temp;
    68   temp.setConfig(GfxFormatToSkiaConfig(aFormat), aSize.width, aSize.height, aStride);
    69   temp.setPixels(aData);
    71   if (!temp.copyTo(&mBitmap, GfxFormatToSkiaColorType(aFormat))) {
    72     return false;
    73   }
    75   if (aFormat == SurfaceFormat::B8G8R8X8) {
    76     mBitmap.setAlphaType(kIgnore_SkAlphaType);
    77   }
    79   mSize = aSize;
    80   mFormat = aFormat;
    81   mStride = mBitmap.rowBytes();
    82   return true;
    83 }
    85 unsigned char*
    86 SourceSurfaceSkia::GetData()
    87 {
    88   if (!mLocked) {
    89     mBitmap.lockPixels();
    90     mLocked = true;
    91   }
    93   unsigned char *pixels = (unsigned char *)mBitmap.getPixels();
    94   return pixels;
    95 }
    97 void
    98 SourceSurfaceSkia::DrawTargetWillChange()
    99 {
   100   if (mDrawTarget) {
   101     MaybeUnlock();
   103     mDrawTarget = nullptr;
   104     SkBitmap temp = mBitmap;
   105     mBitmap.reset();
   106     temp.copyTo(&mBitmap, temp.colorType());
   107   }
   108 }
   110 void
   111 SourceSurfaceSkia::MaybeUnlock()
   112 {
   113   if (mLocked) {
   114     mBitmap.unlockPixels();
   115     mLocked = false;
   116   }
   117 }
   119 }
   120 }

mercurial