Flutter Engine
The Flutter Engine
screenshot.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TESTS_INTEGRATION_UTILS_SCREENSHOT_H_
6#define FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TESTS_INTEGRATION_UTILS_SCREENSHOT_H_
7
8#include <lib/zx/vmo.h>
9#include <zircon/status.h>
10
11#include <cmath>
12#include <iostream>
13#include <map>
14#include <tuple>
15#include <vector>
16
17namespace fuchsia_test_utils {
18// Represents a Pixel in BGRA format.
19// Uses the sRGB color space.
20struct Pixel {
21 uint8_t blue = 0;
22 uint8_t green = 0;
23 uint8_t red = 0;
24 uint8_t alpha = 0;
25
26 Pixel(uint8_t blue, uint8_t green, uint8_t red, uint8_t alpha)
27 : blue(blue), green(green), red(red), alpha(alpha) {}
28
29 bool operator==(const Pixel& rhs) const {
30 return blue == rhs.blue && green == rhs.green && red == rhs.red &&
31 alpha == rhs.alpha;
32 }
33
34 inline bool operator!=(const Pixel& rhs) const { return !(*this == rhs); }
35
36 bool operator<(const Pixel& other) const {
37 return std::tie(blue, green, red, alpha) <
38 std::tie(other.blue, other.green, other.red, other.alpha);
39 }
40};
41
42std::ostream& operator<<(std::ostream& stream, const Pixel& pixel);
43
44// Helper class to get information about a screenshot returned by
45// |fuchsia.ui.composition.Screenshot| protocol.
47 public:
48 // BGRA format.
49 inline static const Pixel kBlack = Pixel(0, 0, 0, 255);
50 inline static const Pixel kBlue = Pixel(255, 0, 0, 255);
51 inline static const Pixel kRed = Pixel(0, 0, 255, 255);
52 inline static const Pixel kMagenta = Pixel(255, 0, 255, 255);
53 inline static const Pixel kGreen = Pixel(0, 255, 0, 255);
54
55 // Params:-
56 // |screenshot_vmo| - The VMO returned by
57 // fuchsia.ui.composition.Screenshot.Take representing the screenshot data.
58 // |width|, |height| - Width and height of the physical display in pixels as
59 // returned by |fuchsia.ui.display.singleton.Info|.
60 // |rotation| - The display rotation value in degrees. The width and the
61 // height of the screenshot are flipped if this value is 90 or 270 degrees,
62 // as the screenshot shows how content is seen by the user.
63 Screenshot(const zx::vmo& screenshot_vmo,
64 uint64_t width,
65 uint64_t height,
66 int rotation);
67
68 // Returns the |Pixel| located at (x,y) coordinates. |x| and |y| should range
69 // from [0,width_) and [0,height_) respectively.
70 //
71 // (0,0)________________width_____________(w-1,0)
72 // | | |
73 // | | y |h
74 // | x | |e
75 // |-----------------------X |i
76 // | |g
77 // | |h
78 // | |t
79 // |_________________________________|
80 // (0,h-1) screenshot (w-1,h-1)
81 //
82 // Clients should only use this function to get the pixel data.
83 Pixel GetPixelAt(uint64_t x, uint64_t y) const;
84
85 // Counts the frequencies of each color in a screenshot.
86 std::map<Pixel, uint32_t> Histogram() const;
87
88 // Returns a 2D vector of size |height_ * width_|. Each value in the vector
89 // corresponds to a pixel in the screenshot.
90 std::vector<std::vector<Pixel>> screenshot() const { return screenshot_; }
91
92 uint64_t width() const { return width_; }
93
94 uint64_t height() const { return height_; }
95
96 private:
97 // Populates |screenshot_| by converting the linear array of bytes in
98 // |screenshot_vmo| of size |kBytesPerPixel * width_ * height_| to a 2D vector
99 // of |Pixel|s of size |height_ * width_|.
100 void ExtractScreenshotFromVMO(uint8_t* screenshot_vmo);
101
102 // Returns the |Pixel|s in the |row_index| row of the screenshot.
103 std::vector<Pixel> GetPixelsInRow(uint8_t* screenshot_vmo, size_t row_index);
104
105 uint64_t width_ = 0;
106 uint64_t height_ = 0;
107 std::vector<std::vector<Pixel>> screenshot_;
108};
109
110} // namespace fuchsia_test_utils
111
112#endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_FLUTTER_TESTS_INTEGRATION_UTILS_SCREENSHOT_H_
static const Pixel kGreen
Definition: screenshot.h:53
Screenshot(const zx::vmo &screenshot_vmo, uint64_t width, uint64_t height, int rotation)
Definition: screenshot.cc:21
static const Pixel kRed
Definition: screenshot.h:51
static const Pixel kBlue
Definition: screenshot.h:50
Pixel GetPixelAt(uint64_t x, uint64_t y) const
Definition: screenshot.cc:53
std::map< Pixel, uint32_t > Histogram() const
Definition: screenshot.cc:59
std::vector< std::vector< Pixel > > screenshot() const
Definition: screenshot.h:90
static const Pixel kBlack
Definition: screenshot.h:49
static const Pixel kMagenta
Definition: screenshot.h:52
double y
double x
std::ostream & operator<<(std::ostream &stream, const Pixel &pixel)
Definition: screenshot.cc:46
bool operator<(const Pixel &other) const
Definition: screenshot.h:36
bool operator==(const Pixel &rhs) const
Definition: screenshot.h:29
bool operator!=(const Pixel &rhs) const
Definition: screenshot.h:34
Pixel(uint8_t blue, uint8_t green, uint8_t red, uint8_t alpha)
Definition: screenshot.h:26