Tue, 06 Jan 2015 21:39:09 +0100
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 "SourceSurfaceCairo.h" |
michael@0 | 7 | #include "DrawTargetCairo.h" |
michael@0 | 8 | #include "HelpersCairo.h" |
michael@0 | 9 | #include "DataSourceSurfaceWrapper.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "cairo.h" |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | namespace gfx { |
michael@0 | 15 | |
michael@0 | 16 | static SurfaceFormat |
michael@0 | 17 | CairoFormatToSurfaceFormat(cairo_format_t format) |
michael@0 | 18 | { |
michael@0 | 19 | switch (format) |
michael@0 | 20 | { |
michael@0 | 21 | case CAIRO_FORMAT_ARGB32: |
michael@0 | 22 | return SurfaceFormat::B8G8R8A8; |
michael@0 | 23 | case CAIRO_FORMAT_RGB24: |
michael@0 | 24 | return SurfaceFormat::B8G8R8X8; |
michael@0 | 25 | case CAIRO_FORMAT_A8: |
michael@0 | 26 | return SurfaceFormat::A8; |
michael@0 | 27 | default: |
michael@0 | 28 | return SurfaceFormat::B8G8R8A8; |
michael@0 | 29 | } |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | SourceSurfaceCairo::SourceSurfaceCairo(cairo_surface_t* aSurface, |
michael@0 | 33 | const IntSize& aSize, |
michael@0 | 34 | const SurfaceFormat& aFormat, |
michael@0 | 35 | DrawTargetCairo* aDrawTarget /* = nullptr */) |
michael@0 | 36 | : mSize(aSize) |
michael@0 | 37 | , mFormat(aFormat) |
michael@0 | 38 | , mSurface(aSurface) |
michael@0 | 39 | , mDrawTarget(aDrawTarget) |
michael@0 | 40 | { |
michael@0 | 41 | cairo_surface_reference(mSurface); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | SourceSurfaceCairo::~SourceSurfaceCairo() |
michael@0 | 45 | { |
michael@0 | 46 | cairo_surface_destroy(mSurface); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | IntSize |
michael@0 | 50 | SourceSurfaceCairo::GetSize() const |
michael@0 | 51 | { |
michael@0 | 52 | return mSize; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | SurfaceFormat |
michael@0 | 56 | SourceSurfaceCairo::GetFormat() const |
michael@0 | 57 | { |
michael@0 | 58 | return mFormat; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | TemporaryRef<DataSourceSurface> |
michael@0 | 62 | SourceSurfaceCairo::GetDataSurface() |
michael@0 | 63 | { |
michael@0 | 64 | RefPtr<DataSourceSurface> dataSurf; |
michael@0 | 65 | |
michael@0 | 66 | if (cairo_surface_get_type(mSurface) == CAIRO_SURFACE_TYPE_IMAGE) { |
michael@0 | 67 | dataSurf = new DataSourceSurfaceCairo(mSurface); |
michael@0 | 68 | } else { |
michael@0 | 69 | cairo_surface_t* imageSurf = cairo_image_surface_create(GfxFormatToCairoFormat(mFormat), |
michael@0 | 70 | mSize.width, mSize.height); |
michael@0 | 71 | |
michael@0 | 72 | // Fill the new image surface with the contents of our surface. |
michael@0 | 73 | cairo_t* ctx = cairo_create(imageSurf); |
michael@0 | 74 | cairo_set_source_surface(ctx, mSurface, 0, 0); |
michael@0 | 75 | cairo_paint(ctx); |
michael@0 | 76 | cairo_destroy(ctx); |
michael@0 | 77 | |
michael@0 | 78 | dataSurf = new DataSourceSurfaceCairo(imageSurf); |
michael@0 | 79 | cairo_surface_destroy(imageSurf); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | // We also need to make sure that the returned surface has |
michael@0 | 83 | // surface->GetType() == SurfaceType::DATA. |
michael@0 | 84 | dataSurf = new DataSourceSurfaceWrapper(dataSurf); |
michael@0 | 85 | |
michael@0 | 86 | return dataSurf; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | cairo_surface_t* |
michael@0 | 90 | SourceSurfaceCairo::GetSurface() const |
michael@0 | 91 | { |
michael@0 | 92 | return mSurface; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | void |
michael@0 | 96 | SourceSurfaceCairo::DrawTargetWillChange() |
michael@0 | 97 | { |
michael@0 | 98 | if (mDrawTarget) { |
michael@0 | 99 | mDrawTarget = nullptr; |
michael@0 | 100 | |
michael@0 | 101 | // We're about to lose our version of the surface, so make a copy of it. |
michael@0 | 102 | cairo_surface_t* surface = cairo_surface_create_similar(mSurface, |
michael@0 | 103 | GfxFormatToCairoContent(mFormat), |
michael@0 | 104 | mSize.width, mSize.height); |
michael@0 | 105 | cairo_t* ctx = cairo_create(surface); |
michael@0 | 106 | cairo_pattern_t* pat = cairo_pattern_create_for_surface(mSurface); |
michael@0 | 107 | cairo_set_source(ctx, pat); |
michael@0 | 108 | cairo_paint(ctx); |
michael@0 | 109 | cairo_destroy(ctx); |
michael@0 | 110 | cairo_pattern_destroy(pat); |
michael@0 | 111 | |
michael@0 | 112 | // Swap in this new surface. |
michael@0 | 113 | cairo_surface_destroy(mSurface); |
michael@0 | 114 | mSurface = surface; |
michael@0 | 115 | } |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | DataSourceSurfaceCairo::DataSourceSurfaceCairo(cairo_surface_t* imageSurf) |
michael@0 | 119 | : mImageSurface(imageSurf) |
michael@0 | 120 | { |
michael@0 | 121 | cairo_surface_reference(mImageSurface); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | DataSourceSurfaceCairo::~DataSourceSurfaceCairo() |
michael@0 | 125 | { |
michael@0 | 126 | cairo_surface_destroy(mImageSurface); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | unsigned char * |
michael@0 | 130 | DataSourceSurfaceCairo::GetData() |
michael@0 | 131 | { |
michael@0 | 132 | return cairo_image_surface_get_data(mImageSurface); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | int32_t |
michael@0 | 136 | DataSourceSurfaceCairo::Stride() |
michael@0 | 137 | { |
michael@0 | 138 | return cairo_image_surface_get_stride(mImageSurface); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | IntSize |
michael@0 | 142 | DataSourceSurfaceCairo::GetSize() const |
michael@0 | 143 | { |
michael@0 | 144 | IntSize size; |
michael@0 | 145 | size.width = cairo_image_surface_get_width(mImageSurface); |
michael@0 | 146 | size.height = cairo_image_surface_get_height(mImageSurface); |
michael@0 | 147 | |
michael@0 | 148 | return size; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | SurfaceFormat |
michael@0 | 152 | DataSourceSurfaceCairo::GetFormat() const |
michael@0 | 153 | { |
michael@0 | 154 | return CairoFormatToSurfaceFormat(cairo_image_surface_get_format(mImageSurface)); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | cairo_surface_t* |
michael@0 | 158 | DataSourceSurfaceCairo::GetSurface() const |
michael@0 | 159 | { |
michael@0 | 160 | return mImageSurface; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | } |
michael@0 | 164 | } |