File: TestWindowGfx.cpp

package info (click to toggle)
firefox 146.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,260 kB
  • sloc: cpp: 7,587,892; javascript: 6,509,455; ansic: 3,755,295; python: 1,410,813; xml: 629,201; asm: 438,677; java: 186,096; sh: 62,697; makefile: 18,086; objc: 13,087; perl: 12,811; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (493 lines) | stat: -rw-r--r-- 14,835 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <stdint.h>

#include "Image.h"
#include "ImageFactory.h"
#include "imgITools.h"
#include "mozilla/Base64.h"
#include "mozilla/Encoding.h"
#include "mozilla/gtest/MozAssertions.h"
#include "mozilla/Preferences.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "mozilla/SystemPrincipal.h"
#include "mozilla/UniquePtr.h"
#include "nsIChannel.h"
#include "nsIInputStream.h"
#include "nsILoadInfo.h"
#include "nsISVGPaintContext.h"
#include "nsMimeTypes.h"
#include "nsStreamUtils.h"
#include "nsWindowGfx.h"
#include "SystemPrincipal.h"

#include "gtest/gtest.h"

using namespace mozilla;
using namespace mozilla::image;

const char* SVG_GREEN_CIRCLE =
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"100\" height=\"100\" viewBox=\"0 0 100 100\" version=\"1.1\"> \
  <circle fill=\"#00FF00\" stroke=\"#FF0000\" stroke-width=\"20\" cx=\"50\" cy=\"50\" r=\"40\" /> \
</svg> \
";

const char* SVG_UNSIZED_CIRCLE =
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 100 100\" version=\"1.1\"> \
  <circle fill=\"#00FF00\" stroke=\"#FF0000\" stroke-width=\"20\" cx=\"50\" cy=\"50\" r=\"40\" /> \
</svg> \
";

const char* SVG_CONTEXT_CIRCLE =
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \
<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"100\" height=\"100\" viewBox=\"0 0 100 100\" version=\"1.1\"> \
  <circle fill=\"context-fill\" stroke=\"context-stroke\" stroke-width=\"20\" cx=\"50\" cy=\"50\" r=\"40\" /> \
</svg> \
";

// Circle's radius is 40 but the total radius includes half the stroke width.
#define CIRCLE_TOTAL_AREA (M_PI * 50 * 50)
// The fill area's radius is the circle's radius minus half the stroke width.
#define CIRCLE_FILL_AREA (M_PI * 30 * 30)
#define CIRCLE_STROKE_AREA (CIRCLE_TOTAL_AREA - CIRCLE_FILL_AREA)

// Allow 2% margin of error to allow for blending
#define ASSERT_NEARLY(val1, val2)   \
  {                                 \
    ASSERT_GT(val1, (val2) * 0.98); \
    ASSERT_LT(val1, (val2) * 1.02); \
  }

class SvgPaintContext : public nsISVGPaintContext {
 public:
  NS_DECL_ISUPPORTS

  nsCString mStrokeColor;
  nsCString mFillColor;

  SvgPaintContext(const char* aStroke, const char* aFill)
      : mStrokeColor(aStroke), mFillColor(aFill) {}

  NS_IMETHODIMP GetStrokeColor(nsACString& color) override {
    color = mStrokeColor;
    return NS_OK;
  }

  NS_IMETHODIMP GetStrokeOpacity(float* opacity) override {
    *opacity = 1.0;
    return NS_OK;
  }

  NS_IMETHODIMP GetFillColor(nsACString& color) override {
    color = mFillColor;
    return NS_OK;
  }

  NS_IMETHODIMP GetFillOpacity(float* opacity) override {
    *opacity = 1.0;
    return NS_OK;
  }

 private:
  virtual ~SvgPaintContext() {};
};

NS_IMPL_ISUPPORTS(SvgPaintContext, nsISVGPaintContext);

class ImageLoadListener : public IProgressObserver {
 public:
  NS_INLINE_DECL_REFCOUNTING(ImageLoadListener, override)

  virtual void OnLoadComplete(bool aLastPart) override { mIsLoaded = true; }

  // Other notifications are ignored.
  virtual void Notify(int32_t aType,
                      const nsIntRect* aRect = nullptr) override {}
  virtual void SetHasImage() override {}
  virtual bool NotificationsDeferred() const override { return false; }
  virtual void MarkPendingNotify() override {}
  virtual void ClearPendingNotify() override {}

  boolean mIsLoaded{};

 private:
  virtual ~ImageLoadListener() {};
};

void LoadImage(const char* aData, imgIContainer** aImage) {
  nsCString svgUri;
  nsresult rv = Base64Encode(aData, strlen(aData), svgUri);
  ASSERT_NS_SUCCEEDED(rv);
  svgUri.Insert("data:" IMAGE_SVG_XML ";base64,", 0);

  nsCOMPtr<nsIURI> uri;
  rv = NS_NewURI(getter_AddRefs(uri), svgUri, UTF_8_ENCODING, nullptr);
  ASSERT_NS_SUCCEEDED(rv);

  nsCOMPtr<nsIPrincipal> principal = SystemPrincipal::Get();
  nsCOMPtr<nsIChannel> channel;
  rv = NS_NewChannel(getter_AddRefs(channel), uri, principal,
                     nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
                     nsContentPolicyType::TYPE_IMAGE);

  RefPtr<ImageLoadListener> listener = new ImageLoadListener();
  RefPtr<ProgressTracker> tracker = new ProgressTracker();
  tracker->AddObserver(listener);
  RefPtr<Image> image = ImageFactory::CreateImage(
      channel, tracker, nsCString(IMAGE_SVG_XML), uri, false, 0);
  ASSERT_FALSE(image->HasError());

  nsCOMPtr<nsIInputStream> stream;
  rv = channel->Open(getter_AddRefs(stream));
  ASSERT_NS_SUCCEEDED(rv);

  uint64_t size;
  rv = stream->Available(&size);
  ASSERT_NS_SUCCEEDED(rv);
  ASSERT_EQ(size, strlen(aData));

  rv = image->OnImageDataAvailable(channel, stream, 0, size);
  ASSERT_NS_SUCCEEDED(rv);

  // Let the Image know we've sent all the data.
  rv = image->OnImageDataComplete(channel, NS_OK, true);
  ASSERT_NS_SUCCEEDED(rv);

  // The final load event from the SVG document is dispatched asynchronously so
  // wait for that to happen.
  MOZ_ALWAYS_TRUE(
      SpinEventLoopUntil("windows:widget:TEST(TestWindowGfx, CreateIcon)"_ns,
                         [&listener]() { return listener->mIsLoaded; }));

  image.forget(aImage);
}

void ConvertToRaster(imgIContainer* vectorImage, imgIContainer** aImage) {
  // First we encode it as a png image.
  nsCOMPtr<imgITools> imgTools =
      do_CreateInstance("@mozilla.org/image/tools;1");

  nsCOMPtr<nsIInputStream> stream;
  nsresult rv = imgTools->EncodeImage(vectorImage, "image/png"_ns, u""_ns,
                                      getter_AddRefs(stream));
  ASSERT_NS_SUCCEEDED(rv);

  uint64_t size;
  rv = stream->Available(&size);

  // And then we load the image again as a raster imgIContainer
  RefPtr<image::Image> image =
      ImageFactory::CreateAnonymousImage("image/png"_ns, size);
  RefPtr<ProgressTracker> tracker = image->GetProgressTracker();
  ASSERT_FALSE(image->HasError());

  rv = image->OnImageDataAvailable(nullptr, stream, 0, size);
  ASSERT_NS_SUCCEEDED(rv);

  // Let the Image know we've sent all the data.
  rv = image->OnImageDataComplete(nullptr, NS_OK, true);
  tracker->SyncNotifyProgress(FLAG_LOAD_COMPLETE);
  ASSERT_NS_SUCCEEDED(rv);

  image.forget(aImage);
}

void CountPixels(ICONINFO& ii, BITMAP& bm, double* redCount, double* greenCount,
                 double* blueCount) {
  BITMAPINFOHEADER bi;
  bi.biSize = sizeof(BITMAPINFOHEADER);
  bi.biWidth = bm.bmWidth;
  bi.biHeight = bm.bmHeight;
  bi.biPlanes = 1;
  bi.biBitCount = 32;
  bi.biCompression = BI_RGB;
  bi.biSizeImage = 0;
  bi.biXPelsPerMeter = 0;
  bi.biYPelsPerMeter = 0;
  bi.biClrUsed = 0;
  bi.biClrImportant = 0;

  auto stride = GDI_WIDTHBYTES(bm.bmWidth * 32);
  auto dataLength = stride * bm.bmHeight;

  UniquePtr<uint8_t[]> bitmapData(new uint8_t[dataLength]);

  *redCount = 0;
  *greenCount = 0;
  *blueCount = 0;

  int lines =
      GetDIBits(::GetDC(nullptr), ii.hbmColor, 0, (UINT)bm.bmHeight,
                (void*)bitmapData.get(), (BITMAPINFO*)&bi, DIB_RGB_COLORS);
  if (lines != bm.bmHeight) {
    return;
  }

  for (long y = 0; y < bm.bmHeight; y++) {
    auto index = stride * y;

    for (long x = 0; x < bm.bmWidth; x++) {
      // Pixels are in BGRA format.
      double blue = bitmapData[index++] / 255.0;
      double green = bitmapData[index++] / 255.0;
      double red = bitmapData[index++] / 255.0;
      double alpha = bitmapData[index++] / 255.0;

      *redCount += red * alpha;
      *greenCount += green * alpha;
      *blueCount += blue * alpha;
    }
  }
}

// Tests that we can scale down an image
TEST(TestWindowGfx, CreateIcon_ScaledDown)
{
  auto Test = [](imgIContainer* image) {
    HICON icon;
    nsresult rv =
        nsWindowGfx::CreateIcon(image, nullptr, false, LayoutDeviceIntPoint(),
                                LayoutDeviceIntSize(50, 50), &icon);
    ASSERT_NS_SUCCEEDED(rv);

    ICONINFO ii;
    BOOL fResult = ::GetIconInfo(icon, &ii);
    ASSERT_TRUE(fResult);

    BITMAP bm;
    fResult = ::GetObject(ii.hbmColor, sizeof(bm), &bm) == sizeof(bm);
    ASSERT_TRUE(fResult);

    ASSERT_EQ(bm.bmWidth, 50);
    ASSERT_EQ(bm.bmHeight, 50);

    double redCount, greenCount, blueCount;
    CountPixels(ii, bm, &redCount, &greenCount, &blueCount);

    // We've scaled the image down to a quarter of its size.
    double fillArea = CIRCLE_FILL_AREA / 4;
    double strokeArea = CIRCLE_STROKE_AREA / 4;

    ASSERT_NEARLY(redCount, strokeArea);
    ASSERT_NEARLY(greenCount, fillArea);
    ASSERT_EQ(blueCount, 0.0);

    if (ii.hbmMask) DeleteObject(ii.hbmMask);
    if (ii.hbmColor) DeleteObject(ii.hbmColor);

    ::DestroyIcon(icon);
  };

  nsCOMPtr<imgIContainer> vectorImage;
  LoadImage(SVG_GREEN_CIRCLE, getter_AddRefs(vectorImage));

  Test(vectorImage);

  nsCOMPtr<imgIContainer> rasterImage;
  ConvertToRaster(vectorImage, getter_AddRefs(rasterImage));

  Test(rasterImage);
}

// Tests that we can scale up an image
TEST(TestWindowGfx, CreateIcon_ScaledUp)
{
  auto Test = [](imgIContainer* image) {
    HICON icon;
    nsresult rv =
        nsWindowGfx::CreateIcon(image, nullptr, false, LayoutDeviceIntPoint(),
                                LayoutDeviceIntSize(200, 200), &icon);
    ASSERT_NS_SUCCEEDED(rv);

    ICONINFO ii;
    BOOL fResult = ::GetIconInfo(icon, &ii);
    ASSERT_TRUE(fResult);

    BITMAP bm;
    fResult = ::GetObject(ii.hbmColor, sizeof(bm), &bm) == sizeof(bm);
    ASSERT_TRUE(fResult);

    ASSERT_EQ(bm.bmWidth, 200);
    ASSERT_EQ(bm.bmHeight, 200);

    double redCount, greenCount, blueCount;
    CountPixels(ii, bm, &redCount, &greenCount, &blueCount);

    // We've scaled the image up to four times its size.
    double fillArea = CIRCLE_FILL_AREA * 4;
    double strokeArea = CIRCLE_STROKE_AREA * 4;

    ASSERT_NEARLY(redCount, strokeArea);
    ASSERT_NEARLY(greenCount, fillArea);
    ASSERT_EQ(blueCount, 0.0);

    if (ii.hbmMask) DeleteObject(ii.hbmMask);
    if (ii.hbmColor) DeleteObject(ii.hbmColor);

    ::DestroyIcon(icon);
  };

  nsCOMPtr<imgIContainer> vectorImage;
  LoadImage(SVG_GREEN_CIRCLE, getter_AddRefs(vectorImage));

  Test(vectorImage);

  nsCOMPtr<imgIContainer> rasterImage;
  ConvertToRaster(vectorImage, getter_AddRefs(rasterImage));

  Test(rasterImage);
}

// Tests that we can render an image at its intrinsic size
TEST(TestWindowGfx, CreateIcon_Intrinsic)
{
  auto Test = [](imgIContainer* image) {
    HICON icon;
    nsresult rv =
        nsWindowGfx::CreateIcon(image, nullptr, false, LayoutDeviceIntPoint(),
                                LayoutDeviceIntSize(), &icon);
    ASSERT_NS_SUCCEEDED(rv);

    ICONINFO ii;
    BOOL fResult = ::GetIconInfo(icon, &ii);
    ASSERT_TRUE(fResult);

    BITMAP bm;
    fResult = ::GetObject(ii.hbmColor, sizeof(bm), &bm) == sizeof(bm);
    ASSERT_TRUE(fResult);

    ASSERT_EQ(bm.bmWidth, 100);
    ASSERT_EQ(bm.bmHeight, 100);

    double redCount, greenCount, blueCount;
    CountPixels(ii, bm, &redCount, &greenCount, &blueCount);

    ASSERT_NEARLY(redCount, CIRCLE_STROKE_AREA);
    ASSERT_NEARLY(greenCount, CIRCLE_FILL_AREA);
    ASSERT_EQ(blueCount, 0.0);

    if (ii.hbmMask) DeleteObject(ii.hbmMask);
    if (ii.hbmColor) DeleteObject(ii.hbmColor);

    ::DestroyIcon(icon);
  };

  nsCOMPtr<imgIContainer> vectorImage;
  LoadImage(SVG_GREEN_CIRCLE, getter_AddRefs(vectorImage));

  Test(vectorImage);

  nsCOMPtr<imgIContainer> rasterImage;
  ConvertToRaster(vectorImage, getter_AddRefs(rasterImage));

  Test(rasterImage);
}

// If an SVG has no intrinsic size and we don't provide one we fail.
TEST(TestWindowGfx, CreateIcon_SVG_NoSize)
{
  nsCOMPtr<imgIContainer> image;
  LoadImage(SVG_UNSIZED_CIRCLE, getter_AddRefs(image));

  HICON icon;
  nsresult rv =
      nsWindowGfx::CreateIcon(image, nullptr, false, LayoutDeviceIntPoint(),
                              LayoutDeviceIntSize(), &icon);
  ASSERT_EQ(rv, NS_ERROR_FAILURE);
}

// But we can still render an SVG with no intrinsic size as long as we provide
// one.
TEST(TestWindowGfx, CreateIcon_SVG_NoIntrinsic)
{
  nsCOMPtr<imgIContainer> image;
  LoadImage(SVG_UNSIZED_CIRCLE, getter_AddRefs(image));

  HICON icon;
  nsresult rv =
      nsWindowGfx::CreateIcon(image, nullptr, false, LayoutDeviceIntPoint(),
                              LayoutDeviceIntSize(200, 200), &icon);
  ASSERT_NS_SUCCEEDED(rv);

  ICONINFO ii;
  BOOL fResult = ::GetIconInfo(icon, &ii);
  ASSERT_TRUE(fResult);

  BITMAP bm;
  fResult = ::GetObject(ii.hbmColor, sizeof(bm), &bm) == sizeof(bm);
  ASSERT_TRUE(fResult);

  ASSERT_EQ(bm.bmWidth, 200);
  ASSERT_EQ(bm.bmHeight, 200);

  double redCount, greenCount, blueCount;
  CountPixels(ii, bm, &redCount, &greenCount, &blueCount);

  // We've scaled the image up to four times its size.
  double fillArea = CIRCLE_FILL_AREA * 4;
  double strokeArea = CIRCLE_STROKE_AREA * 4;

  ASSERT_NEARLY(redCount, strokeArea);
  ASSERT_NEARLY(greenCount, fillArea);
  ASSERT_EQ(blueCount, 0.0);

  if (ii.hbmMask) DeleteObject(ii.hbmMask);
  if (ii.hbmColor) DeleteObject(ii.hbmColor);

  ::DestroyIcon(icon);
}

// Tests that we can set SVG context-fill and context-stroke
TEST(TestWindowGfx, CreateIcon_SVG_Context)
{
  // Normally the context properties don't work for content documents including
  // data URIs.
  Preferences::SetBool("svg.context-properties.content.enabled", true);
  // This test breaks if color management is enabled and an earlier gtest may
  // have enabled it.
  gfxPlatform::SetCMSModeOverride(CMSMode::Off);

  nsCOMPtr<imgIContainer> image;
  LoadImage(SVG_CONTEXT_CIRCLE, getter_AddRefs(image));

  nsCOMPtr<nsISVGPaintContext> paintContext =
      new SvgPaintContext("#00FF00", "#0000FF");

  HICON icon;
  nsresult rv = nsWindowGfx::CreateIcon(image, paintContext, false,
                                        LayoutDeviceIntPoint(),
                                        LayoutDeviceIntSize(200, 200), &icon);
  ASSERT_NS_SUCCEEDED(rv);

  ICONINFO ii;
  BOOL fResult = ::GetIconInfo(icon, &ii);
  ASSERT_TRUE(fResult);

  BITMAP bm;
  fResult = ::GetObject(ii.hbmColor, sizeof(bm), &bm) == sizeof(bm);
  ASSERT_TRUE(fResult);

  ASSERT_EQ(bm.bmWidth, 200);
  ASSERT_EQ(bm.bmHeight, 200);

  double redCount, greenCount, blueCount;
  CountPixels(ii, bm, &redCount, &greenCount, &blueCount);

  // We've scaled the image up to four times its size.
  double fillArea = CIRCLE_FILL_AREA * 4;
  double strokeArea = CIRCLE_STROKE_AREA * 4;

  ASSERT_NEARLY(greenCount, strokeArea);
  ASSERT_NEARLY(blueCount, fillArea);
  ASSERT_EQ(redCount, 0.0);

  if (ii.hbmMask) DeleteObject(ii.hbmMask);
  if (ii.hbmColor) DeleteObject(ii.hbmColor);

  ::DestroyIcon(icon);
}