gfx/thebes/gfxPattern.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 "gfxTypes.h"
michael@0 7 #include "gfxPattern.h"
michael@0 8 #include "gfxASurface.h"
michael@0 9 #include "gfxPlatform.h"
michael@0 10 #include "gfx2DGlue.h"
michael@0 11 #include "gfxGradientCache.h"
michael@0 12
michael@0 13 #include "cairo.h"
michael@0 14
michael@0 15 #include <vector>
michael@0 16
michael@0 17 using namespace mozilla::gfx;
michael@0 18
michael@0 19 gfxPattern::gfxPattern(cairo_pattern_t *aPattern)
michael@0 20 : mGfxPattern(nullptr)
michael@0 21 {
michael@0 22 mPattern = cairo_pattern_reference(aPattern);
michael@0 23 }
michael@0 24
michael@0 25 gfxPattern::gfxPattern(const gfxRGBA& aColor)
michael@0 26 : mGfxPattern(nullptr)
michael@0 27 {
michael@0 28 mPattern = cairo_pattern_create_rgba(aColor.r, aColor.g, aColor.b, aColor.a);
michael@0 29 }
michael@0 30
michael@0 31 // from another surface
michael@0 32 gfxPattern::gfxPattern(gfxASurface *surface)
michael@0 33 : mGfxPattern(nullptr)
michael@0 34 {
michael@0 35 mPattern = cairo_pattern_create_for_surface(surface->CairoSurface());
michael@0 36 }
michael@0 37
michael@0 38 // linear
michael@0 39 gfxPattern::gfxPattern(gfxFloat x0, gfxFloat y0, gfxFloat x1, gfxFloat y1)
michael@0 40 : mGfxPattern(nullptr)
michael@0 41 {
michael@0 42 mPattern = cairo_pattern_create_linear(x0, y0, x1, y1);
michael@0 43 }
michael@0 44
michael@0 45 // radial
michael@0 46 gfxPattern::gfxPattern(gfxFloat cx0, gfxFloat cy0, gfxFloat radius0,
michael@0 47 gfxFloat cx1, gfxFloat cy1, gfxFloat radius1)
michael@0 48 : mGfxPattern(nullptr)
michael@0 49 {
michael@0 50 mPattern = cairo_pattern_create_radial(cx0, cy0, radius0,
michael@0 51 cx1, cy1, radius1);
michael@0 52 }
michael@0 53
michael@0 54 // Azure
michael@0 55 gfxPattern::gfxPattern(SourceSurface *aSurface, const Matrix &aTransform)
michael@0 56 : mPattern(nullptr)
michael@0 57 , mGfxPattern(nullptr)
michael@0 58 , mSourceSurface(aSurface)
michael@0 59 , mTransform(aTransform)
michael@0 60 , mExtend(EXTEND_NONE)
michael@0 61 {
michael@0 62 }
michael@0 63
michael@0 64 gfxPattern::~gfxPattern()
michael@0 65 {
michael@0 66 cairo_pattern_destroy(mPattern);
michael@0 67
michael@0 68 if (mGfxPattern) {
michael@0 69 mGfxPattern->~Pattern();
michael@0 70 }
michael@0 71 }
michael@0 72
michael@0 73 cairo_pattern_t *
michael@0 74 gfxPattern::CairoPattern()
michael@0 75 {
michael@0 76 return mPattern;
michael@0 77 }
michael@0 78
michael@0 79 void
michael@0 80 gfxPattern::AddColorStop(gfxFloat offset, const gfxRGBA& c)
michael@0 81 {
michael@0 82 if (mPattern) {
michael@0 83 mStops = nullptr;
michael@0 84 if (gfxPlatform::GetCMSMode() == eCMSMode_All) {
michael@0 85 gfxRGBA cms;
michael@0 86 qcms_transform *transform = gfxPlatform::GetCMSRGBTransform();
michael@0 87 if (transform)
michael@0 88 gfxPlatform::TransformPixel(c, cms, transform);
michael@0 89
michael@0 90 // Use the original alpha to avoid unnecessary float->byte->float
michael@0 91 // conversion errors
michael@0 92 cairo_pattern_add_color_stop_rgba(mPattern, offset,
michael@0 93 cms.r, cms.g, cms.b, c.a);
michael@0 94 }
michael@0 95 else
michael@0 96 cairo_pattern_add_color_stop_rgba(mPattern, offset, c.r, c.g, c.b, c.a);
michael@0 97 }
michael@0 98 }
michael@0 99
michael@0 100 void
michael@0 101 gfxPattern::SetColorStops(mozilla::RefPtr<mozilla::gfx::GradientStops> aStops)
michael@0 102 {
michael@0 103 mStops = aStops;
michael@0 104 }
michael@0 105
michael@0 106 void
michael@0 107 gfxPattern::CacheColorStops(mozilla::gfx::DrawTarget *aDT)
michael@0 108 {
michael@0 109 if (mPattern) {
michael@0 110 mStops = nullptr;
michael@0 111 nsTArray<GradientStop> stops;
michael@0 112 int count = 0;
michael@0 113 cairo_pattern_get_color_stop_count(mPattern, &count);
michael@0 114 stops.SetLength(count);
michael@0 115 for (int n = 0; n < count; ++n) {
michael@0 116 double offset, r, g, b, a;
michael@0 117 cairo_pattern_get_color_stop_rgba(mPattern, n, &offset, &r, &g, &b, &a);
michael@0 118 stops[n].color = mozilla::gfx::Color(r, g, b, a);
michael@0 119 stops[n].offset = offset;
michael@0 120 }
michael@0 121 mStops = gfxGradientCache::GetOrCreateGradientStops(aDT,
michael@0 122 stops,
michael@0 123 (cairo_pattern_get_extend(mPattern) == CAIRO_EXTEND_REPEAT)
michael@0 124 ? mozilla::gfx::ExtendMode::REPEAT
michael@0 125 : mozilla::gfx::ExtendMode::CLAMP);
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 void
michael@0 130 gfxPattern::SetMatrix(const gfxMatrix& matrix)
michael@0 131 {
michael@0 132 if (mPattern) {
michael@0 133 cairo_matrix_t mat = *reinterpret_cast<const cairo_matrix_t*>(&matrix);
michael@0 134 cairo_pattern_set_matrix(mPattern, &mat);
michael@0 135 } else {
michael@0 136 mTransform = ToMatrix(matrix);
michael@0 137 // Cairo-pattern matrices specify the conversion from DrawTarget to pattern
michael@0 138 // space. Azure pattern matrices specify the conversion from pattern to
michael@0 139 // DrawTarget space.
michael@0 140 mTransform.Invert();
michael@0 141 }
michael@0 142 }
michael@0 143
michael@0 144 gfxMatrix
michael@0 145 gfxPattern::GetMatrix() const
michael@0 146 {
michael@0 147 if (mPattern) {
michael@0 148 cairo_matrix_t mat;
michael@0 149 cairo_pattern_get_matrix(mPattern, &mat);
michael@0 150 return gfxMatrix(*reinterpret_cast<gfxMatrix*>(&mat));
michael@0 151 } else {
michael@0 152 // invert at the higher precision of gfxMatrix
michael@0 153 // cause we need to convert at some point anyways
michael@0 154 gfxMatrix mat = ThebesMatrix(mTransform);
michael@0 155 mat.Invert();
michael@0 156 return mat;
michael@0 157 }
michael@0 158 }
michael@0 159
michael@0 160 gfxMatrix
michael@0 161 gfxPattern::GetInverseMatrix() const
michael@0 162 {
michael@0 163 if (mPattern) {
michael@0 164 cairo_matrix_t mat;
michael@0 165 cairo_pattern_get_matrix(mPattern, &mat);
michael@0 166 cairo_matrix_invert(&mat);
michael@0 167 return gfxMatrix(*reinterpret_cast<gfxMatrix*>(&mat));
michael@0 168 } else {
michael@0 169 return ThebesMatrix(mTransform);
michael@0 170 }
michael@0 171 }
michael@0 172
michael@0 173 Pattern*
michael@0 174 gfxPattern::GetPattern(DrawTarget *aTarget, Matrix *aPatternTransform)
michael@0 175 {
michael@0 176 if (mGfxPattern) {
michael@0 177 mGfxPattern->~Pattern();
michael@0 178 mGfxPattern = nullptr;
michael@0 179 }
michael@0 180
michael@0 181 if (!mPattern) {
michael@0 182 Matrix adjustedMatrix = mTransform;
michael@0 183 if (aPatternTransform)
michael@0 184 AdjustTransformForPattern(adjustedMatrix, aTarget->GetTransform(), aPatternTransform);
michael@0 185 mGfxPattern = new (mSurfacePattern.addr())
michael@0 186 SurfacePattern(mSourceSurface, ToExtendMode(mExtend), adjustedMatrix, mFilter);
michael@0 187 return mGfxPattern;
michael@0 188 }
michael@0 189
michael@0 190 GraphicsExtend extend = (GraphicsExtend)cairo_pattern_get_extend(mPattern);
michael@0 191
michael@0 192 switch (cairo_pattern_get_type(mPattern)) {
michael@0 193 case CAIRO_PATTERN_TYPE_SOLID:
michael@0 194 {
michael@0 195 double r, g, b, a;
michael@0 196 cairo_pattern_get_rgba(mPattern, &r, &g, &b, &a);
michael@0 197
michael@0 198 new (mColorPattern.addr()) ColorPattern(Color(r, g, b, a));
michael@0 199 return mColorPattern.addr();
michael@0 200 }
michael@0 201 case CAIRO_PATTERN_TYPE_SURFACE:
michael@0 202 {
michael@0 203 GraphicsFilter filter = (GraphicsFilter)cairo_pattern_get_filter(mPattern);
michael@0 204 cairo_matrix_t mat;
michael@0 205 cairo_pattern_get_matrix(mPattern, &mat);
michael@0 206 gfxMatrix matrix(*reinterpret_cast<gfxMatrix*>(&mat));
michael@0 207
michael@0 208 cairo_surface_t *surf = nullptr;
michael@0 209 cairo_pattern_get_surface(mPattern, &surf);
michael@0 210
michael@0 211 if (!mSourceSurface) {
michael@0 212 nsRefPtr<gfxASurface> gfxSurf = gfxASurface::Wrap(surf);
michael@0 213 // The underlying surface here will be kept around by the gfxPattern.
michael@0 214 // This function is intended to be used right away.
michael@0 215 mSourceSurface =
michael@0 216 gfxPlatform::GetPlatform()->GetSourceSurfaceForSurface(aTarget, gfxSurf);
michael@0 217 }
michael@0 218
michael@0 219 if (mSourceSurface) {
michael@0 220 Matrix newMat = ToMatrix(matrix);
michael@0 221
michael@0 222 AdjustTransformForPattern(newMat, aTarget->GetTransform(), aPatternTransform);
michael@0 223
michael@0 224 double x, y;
michael@0 225 cairo_surface_get_device_offset(surf, &x, &y);
michael@0 226 newMat.Translate(-x, -y);
michael@0 227 mGfxPattern = new (mSurfacePattern.addr())
michael@0 228 SurfacePattern(mSourceSurface, ToExtendMode(extend), newMat, ToFilter(filter));
michael@0 229 return mGfxPattern;
michael@0 230 }
michael@0 231 break;
michael@0 232 }
michael@0 233 case CAIRO_PATTERN_TYPE_LINEAR:
michael@0 234 {
michael@0 235 double x1, y1, x2, y2;
michael@0 236 cairo_pattern_get_linear_points(mPattern, &x1, &y1, &x2, &y2);
michael@0 237 if (!mStops) {
michael@0 238 int count = 0;
michael@0 239 cairo_pattern_get_color_stop_count(mPattern, &count);
michael@0 240
michael@0 241 std::vector<GradientStop> stops;
michael@0 242
michael@0 243 for (int i = 0; i < count; i++) {
michael@0 244 GradientStop stop;
michael@0 245 double r, g, b, a, offset;
michael@0 246 cairo_pattern_get_color_stop_rgba(mPattern, i, &offset, &r, &g, &b, &a);
michael@0 247
michael@0 248 stop.offset = offset;
michael@0 249 stop.color = Color(Float(r), Float(g), Float(b), Float(a));
michael@0 250 stops.push_back(stop);
michael@0 251 }
michael@0 252
michael@0 253 mStops = aTarget->CreateGradientStops(&stops.front(), count, ToExtendMode(extend));
michael@0 254 }
michael@0 255
michael@0 256 if (mStops) {
michael@0 257 cairo_matrix_t mat;
michael@0 258 cairo_pattern_get_matrix(mPattern, &mat);
michael@0 259 gfxMatrix matrix(*reinterpret_cast<gfxMatrix*>(&mat));
michael@0 260
michael@0 261 Matrix newMat = ToMatrix(matrix);
michael@0 262
michael@0 263 AdjustTransformForPattern(newMat, aTarget->GetTransform(), aPatternTransform);
michael@0 264
michael@0 265 mGfxPattern = new (mLinearGradientPattern.addr())
michael@0 266 LinearGradientPattern(Point(x1, y1), Point(x2, y2), mStops, newMat);
michael@0 267
michael@0 268 return mGfxPattern;
michael@0 269 }
michael@0 270 break;
michael@0 271 }
michael@0 272 case CAIRO_PATTERN_TYPE_RADIAL:
michael@0 273 {
michael@0 274 if (!mStops) {
michael@0 275 int count = 0;
michael@0 276 cairo_pattern_get_color_stop_count(mPattern, &count);
michael@0 277
michael@0 278 std::vector<GradientStop> stops;
michael@0 279
michael@0 280 for (int i = 0; i < count; i++) {
michael@0 281 GradientStop stop;
michael@0 282 double r, g, b, a, offset;
michael@0 283 cairo_pattern_get_color_stop_rgba(mPattern, i, &offset, &r, &g, &b, &a);
michael@0 284
michael@0 285 stop.offset = offset;
michael@0 286 stop.color = Color(Float(r), Float(g), Float(b), Float(a));
michael@0 287 stops.push_back(stop);
michael@0 288 }
michael@0 289
michael@0 290 mStops = aTarget->CreateGradientStops(&stops.front(), count, ToExtendMode(extend));
michael@0 291 }
michael@0 292
michael@0 293 if (mStops) {
michael@0 294 cairo_matrix_t mat;
michael@0 295 cairo_pattern_get_matrix(mPattern, &mat);
michael@0 296 gfxMatrix matrix(*reinterpret_cast<gfxMatrix*>(&mat));
michael@0 297
michael@0 298 Matrix newMat = ToMatrix(matrix);
michael@0 299
michael@0 300 AdjustTransformForPattern(newMat, aTarget->GetTransform(), aPatternTransform);
michael@0 301
michael@0 302 double x1, y1, x2, y2, r1, r2;
michael@0 303 cairo_pattern_get_radial_circles(mPattern, &x1, &y1, &r1, &x2, &y2, &r2);
michael@0 304 mGfxPattern = new (mRadialGradientPattern.addr())
michael@0 305 RadialGradientPattern(Point(x1, y1), Point(x2, y2), r1, r2, mStops, newMat);
michael@0 306
michael@0 307 return mGfxPattern;
michael@0 308 }
michael@0 309 break;
michael@0 310 }
michael@0 311 default:
michael@0 312 /* Reassure the compiler we are handling all the enum values. */
michael@0 313 break;
michael@0 314 }
michael@0 315
michael@0 316 new (mColorPattern.addr()) ColorPattern(Color(0, 0, 0, 0));
michael@0 317 return mColorPattern.addr();
michael@0 318 }
michael@0 319
michael@0 320 void
michael@0 321 gfxPattern::SetExtend(GraphicsExtend extend)
michael@0 322 {
michael@0 323 if (mPattern) {
michael@0 324 mStops = nullptr;
michael@0 325 if (extend == EXTEND_PAD_EDGE) {
michael@0 326 if (cairo_pattern_get_type(mPattern) == CAIRO_PATTERN_TYPE_SURFACE) {
michael@0 327 cairo_surface_t *surf = nullptr;
michael@0 328
michael@0 329 cairo_pattern_get_surface (mPattern, &surf);
michael@0 330 if (surf) {
michael@0 331 switch (cairo_surface_get_type(surf)) {
michael@0 332 case CAIRO_SURFACE_TYPE_WIN32_PRINTING:
michael@0 333 case CAIRO_SURFACE_TYPE_QUARTZ:
michael@0 334 extend = EXTEND_NONE;
michael@0 335 break;
michael@0 336
michael@0 337 case CAIRO_SURFACE_TYPE_WIN32:
michael@0 338 case CAIRO_SURFACE_TYPE_XLIB:
michael@0 339 default:
michael@0 340 extend = EXTEND_PAD;
michael@0 341 break;
michael@0 342 }
michael@0 343 }
michael@0 344 }
michael@0 345
michael@0 346 // if something went wrong, or not a surface pattern, use PAD
michael@0 347 if (extend == EXTEND_PAD_EDGE)
michael@0 348 extend = EXTEND_PAD;
michael@0 349 }
michael@0 350
michael@0 351 cairo_pattern_set_extend(mPattern, (cairo_extend_t)extend);
michael@0 352 } else {
michael@0 353 // This is always a surface pattern and will default to EXTEND_PAD
michael@0 354 // for EXTEND_PAD_EDGE.
michael@0 355 mExtend = extend;
michael@0 356 }
michael@0 357 }
michael@0 358
michael@0 359 bool
michael@0 360 gfxPattern::IsOpaque()
michael@0 361 {
michael@0 362 if (mPattern) {
michael@0 363 switch (cairo_pattern_get_type(mPattern)) {
michael@0 364 case CAIRO_PATTERN_TYPE_SURFACE:
michael@0 365 {
michael@0 366 cairo_surface_t *surf = nullptr;
michael@0 367 cairo_pattern_get_surface(mPattern, &surf);
michael@0 368
michael@0 369 if (cairo_surface_get_content(surf) == CAIRO_CONTENT_COLOR) {
michael@0 370 return true;
michael@0 371 }
michael@0 372 }
michael@0 373 default:
michael@0 374 return false;
michael@0 375 }
michael@0 376 }
michael@0 377
michael@0 378 if (mSourceSurface->GetFormat() == SurfaceFormat::B8G8R8X8) {
michael@0 379 return true;
michael@0 380 }
michael@0 381 return false;
michael@0 382 }
michael@0 383
michael@0 384 gfxPattern::GraphicsExtend
michael@0 385 gfxPattern::Extend() const
michael@0 386 {
michael@0 387 if (mPattern) {
michael@0 388 return (GraphicsExtend)cairo_pattern_get_extend(mPattern);
michael@0 389 } else {
michael@0 390 return mExtend;
michael@0 391 }
michael@0 392 }
michael@0 393
michael@0 394 void
michael@0 395 gfxPattern::SetFilter(GraphicsFilter filter)
michael@0 396 {
michael@0 397 if (mPattern) {
michael@0 398 cairo_pattern_set_filter(mPattern, (cairo_filter_t)(int)filter);
michael@0 399 } else {
michael@0 400 mFilter = ToFilter(filter);
michael@0 401 }
michael@0 402 }
michael@0 403
michael@0 404 GraphicsFilter
michael@0 405 gfxPattern::Filter() const
michael@0 406 {
michael@0 407 if (mPattern) {
michael@0 408 return (GraphicsFilter)cairo_pattern_get_filter(mPattern);
michael@0 409 } else {
michael@0 410 return ThebesFilter(mFilter);
michael@0 411 }
michael@0 412 }
michael@0 413
michael@0 414 bool
michael@0 415 gfxPattern::GetSolidColor(gfxRGBA& aColor)
michael@0 416 {
michael@0 417 return cairo_pattern_get_rgba(mPattern,
michael@0 418 &aColor.r,
michael@0 419 &aColor.g,
michael@0 420 &aColor.b,
michael@0 421 &aColor.a) == CAIRO_STATUS_SUCCESS;
michael@0 422 }
michael@0 423
michael@0 424 already_AddRefed<gfxASurface>
michael@0 425 gfxPattern::GetSurface()
michael@0 426 {
michael@0 427 if (mPattern) {
michael@0 428 cairo_surface_t *surf = nullptr;
michael@0 429
michael@0 430 if (cairo_pattern_get_surface (mPattern, &surf) != CAIRO_STATUS_SUCCESS)
michael@0 431 return nullptr;
michael@0 432
michael@0 433 return gfxASurface::Wrap(surf);
michael@0 434 } else {
michael@0 435 // We should never be trying to get the surface off an Azure gfx Pattern.
michael@0 436 NS_ERROR("Attempt to get surface off an Azure gfxPattern!");
michael@0 437 return nullptr;
michael@0 438 }
michael@0 439 }
michael@0 440
michael@0 441 gfxPattern::GraphicsPatternType
michael@0 442 gfxPattern::GetType() const
michael@0 443 {
michael@0 444 if (mPattern) {
michael@0 445 return (GraphicsPatternType) cairo_pattern_get_type(mPattern);
michael@0 446 } else {
michael@0 447 // We should never be trying to get the type off an Azure gfx Pattern.
michael@0 448 MOZ_ASSERT(0);
michael@0 449 return PATTERN_SURFACE;
michael@0 450 }
michael@0 451 }
michael@0 452
michael@0 453 int
michael@0 454 gfxPattern::CairoStatus()
michael@0 455 {
michael@0 456 if (mPattern) {
michael@0 457 return cairo_pattern_status(mPattern);
michael@0 458 } else {
michael@0 459 // An Azure pattern as this point is never in error status.
michael@0 460 return CAIRO_STATUS_SUCCESS;
michael@0 461 }
michael@0 462 }
michael@0 463
michael@0 464 void
michael@0 465 gfxPattern::AdjustTransformForPattern(Matrix &aPatternTransform,
michael@0 466 const Matrix &aCurrentTransform,
michael@0 467 const Matrix *aOriginalTransform)
michael@0 468 {
michael@0 469 aPatternTransform.Invert();
michael@0 470 if (!aOriginalTransform) {
michael@0 471 // User space is unchanged, so to get from pattern space to user space,
michael@0 472 // just invert the cairo matrix.
michael@0 473 aPatternTransform.NudgeToIntegers();
michael@0 474 return;
michael@0 475 }
michael@0 476 // aPatternTransform now maps from pattern space to the user space defined
michael@0 477 // by *aOriginalTransform.
michael@0 478
michael@0 479 Matrix mat = aCurrentTransform;
michael@0 480 mat.Invert();
michael@0 481 // mat maps from device space to current user space
michael@0 482
michael@0 483 // First, transform from pattern space to original user space. Then transform
michael@0 484 // from original user space to device space. Then transform from
michael@0 485 // device space to current user space.
michael@0 486 aPatternTransform = aPatternTransform * *aOriginalTransform * mat;
michael@0 487 aPatternTransform.NudgeToIntegers();
michael@0 488 }

mercurial