gfx/layers/ImageDataSerializer.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.

michael@0 1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "ImageDataSerializer.h"
michael@0 7 #include "gfx2DGlue.h" // for SurfaceFormatToImageFormat
michael@0 8 #include "gfxImageSurface.h" // for gfxImageSurface
michael@0 9 #include "gfxPoint.h" // for gfxIntSize
michael@0 10 #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
michael@0 11 #include "mozilla/gfx/2D.h" // for DataSourceSurface, Factory
michael@0 12 #include "mozilla/gfx/Tools.h" // for GetAlignedStride, etc
michael@0 13 #include "mozilla/mozalloc.h" // for operator delete, etc
michael@0 14
michael@0 15 namespace mozilla {
michael@0 16 namespace layers {
michael@0 17
michael@0 18 using namespace gfx;
michael@0 19
michael@0 20 // The Data is layed out as follows:
michael@0 21 //
michael@0 22 // +-------------------+ -++ --+ <-- ImageDataSerializerBase::mData pointer
michael@0 23 // | SurfaceBufferInfo | | |
michael@0 24 // +-------------------+ --+ | offset
michael@0 25 // | ... | |
michael@0 26 // +-------------------+ ------+
michael@0 27 // | |
michael@0 28 // | data |
michael@0 29 // | |
michael@0 30 // +-------------------+
michael@0 31
michael@0 32 // Structure written at the beginning of the data blob containing the image
michael@0 33 // (as shown in the figure above). It contains the necessary informations to
michael@0 34 // read the image in the blob.
michael@0 35 namespace {
michael@0 36 struct SurfaceBufferInfo
michael@0 37 {
michael@0 38 uint32_t width;
michael@0 39 uint32_t height;
michael@0 40 SurfaceFormat format;
michael@0 41
michael@0 42 static uint32_t GetOffset()
michael@0 43 {
michael@0 44 return GetAlignedStride<16>(sizeof(SurfaceBufferInfo));
michael@0 45 }
michael@0 46 };
michael@0 47 } // anonymous namespace
michael@0 48
michael@0 49 static SurfaceBufferInfo*
michael@0 50 GetBufferInfo(uint8_t* aData, size_t aDataSize)
michael@0 51 {
michael@0 52 return aDataSize >= sizeof(SurfaceBufferInfo)
michael@0 53 ? reinterpret_cast<SurfaceBufferInfo*>(aData)
michael@0 54 : nullptr;
michael@0 55 }
michael@0 56
michael@0 57 void
michael@0 58 ImageDataSerializer::InitializeBufferInfo(IntSize aSize,
michael@0 59 SurfaceFormat aFormat)
michael@0 60 {
michael@0 61 SurfaceBufferInfo* info = GetBufferInfo(mData, mDataSize);
michael@0 62 MOZ_ASSERT(info); // OK to assert here, this method is client-side-only
michael@0 63 info->width = aSize.width;
michael@0 64 info->height = aSize.height;
michael@0 65 info->format = aFormat;
michael@0 66 Validate();
michael@0 67 }
michael@0 68
michael@0 69 static inline uint32_t
michael@0 70 ComputeStride(SurfaceFormat aFormat, uint32_t aWidth)
michael@0 71 {
michael@0 72 return GetAlignedStride<4>(BytesPerPixel(aFormat) * aWidth);
michael@0 73 }
michael@0 74
michael@0 75 uint32_t
michael@0 76 ImageDataSerializerBase::ComputeMinBufferSize(IntSize aSize,
michael@0 77 SurfaceFormat aFormat)
michael@0 78 {
michael@0 79 uint32_t bufsize = aSize.height * ComputeStride(aFormat, aSize.width);
michael@0 80 return SurfaceBufferInfo::GetOffset()
michael@0 81 + GetAlignedStride<16>(bufsize);
michael@0 82 }
michael@0 83
michael@0 84 void
michael@0 85 ImageDataSerializerBase::Validate()
michael@0 86 {
michael@0 87 mIsValid = false;
michael@0 88 if (!mData) {
michael@0 89 return;
michael@0 90 }
michael@0 91 SurfaceBufferInfo* info = GetBufferInfo(mData, mDataSize);
michael@0 92 if (!info) {
michael@0 93 return;
michael@0 94 }
michael@0 95 size_t requiredSize =
michael@0 96 ComputeMinBufferSize(IntSize(info->width, info->height), info->format);
michael@0 97 mIsValid = requiredSize <= mDataSize;
michael@0 98 }
michael@0 99
michael@0 100 uint8_t*
michael@0 101 ImageDataSerializerBase::GetData()
michael@0 102 {
michael@0 103 MOZ_ASSERT(IsValid());
michael@0 104 return mData + SurfaceBufferInfo::GetOffset();
michael@0 105 }
michael@0 106
michael@0 107 uint32_t
michael@0 108 ImageDataSerializerBase::GetStride() const
michael@0 109 {
michael@0 110 MOZ_ASSERT(IsValid());
michael@0 111 SurfaceBufferInfo* info = GetBufferInfo(mData, mDataSize);
michael@0 112 return ComputeStride(GetFormat(), info->width);
michael@0 113 }
michael@0 114
michael@0 115 IntSize
michael@0 116 ImageDataSerializerBase::GetSize() const
michael@0 117 {
michael@0 118 MOZ_ASSERT(IsValid());
michael@0 119 SurfaceBufferInfo* info = GetBufferInfo(mData, mDataSize);
michael@0 120 return IntSize(info->width, info->height);
michael@0 121 }
michael@0 122
michael@0 123 SurfaceFormat
michael@0 124 ImageDataSerializerBase::GetFormat() const
michael@0 125 {
michael@0 126 MOZ_ASSERT(IsValid());
michael@0 127 return GetBufferInfo(mData, mDataSize)->format;
michael@0 128 }
michael@0 129
michael@0 130 TemporaryRef<gfxImageSurface>
michael@0 131 ImageDataSerializerBase::GetAsThebesSurface()
michael@0 132 {
michael@0 133 MOZ_ASSERT(IsValid());
michael@0 134 IntSize size = GetSize();
michael@0 135 return new gfxImageSurface(GetData(),
michael@0 136 gfxIntSize(size.width, size.height),
michael@0 137 GetStride(),
michael@0 138 SurfaceFormatToImageFormat(GetFormat()));
michael@0 139 }
michael@0 140
michael@0 141 TemporaryRef<DrawTarget>
michael@0 142 ImageDataSerializerBase::GetAsDrawTarget(gfx::BackendType aBackend)
michael@0 143 {
michael@0 144 MOZ_ASSERT(IsValid());
michael@0 145 return gfx::Factory::CreateDrawTargetForData(aBackend,
michael@0 146 GetData(), GetSize(),
michael@0 147 GetStride(), GetFormat());
michael@0 148 }
michael@0 149
michael@0 150 TemporaryRef<gfx::DataSourceSurface>
michael@0 151 ImageDataSerializerBase::GetAsSurface()
michael@0 152 {
michael@0 153 MOZ_ASSERT(IsValid());
michael@0 154 return Factory::CreateWrappingDataSourceSurface(GetData(),
michael@0 155 GetStride(),
michael@0 156 GetSize(),
michael@0 157 GetFormat());
michael@0 158 }
michael@0 159
michael@0 160 } // namespace layers
michael@0 161 } // namespace mozilla

mercurial