blob: 6813c66fc736c59e36ed4eb4d702db6a464e977f [file] [log] [blame]
[email protected]a9c88d12012-01-29 03:45:171// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]fb13c762010-07-24 02:22:072// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]542bdfe2010-11-30 03:55:475#include <deque>
[email protected]fb13c762010-07-24 02:22:076#include <stdlib.h>
7
[email protected]1e1cb3b2011-11-10 02:07:418#include "base/bind.h"
9#include "base/logging.h"
[email protected]dc454a7c2011-08-12 22:01:1310#include "base/memory/scoped_ptr.h"
[email protected]fb13c762010-07-24 02:22:0711#include "media/base/video_frame.h"
[email protected]28dc4212011-02-08 20:44:0012#include "remoting/base/base_mock_objects.h"
[email protected]fb13c762010-07-24 02:22:0713#include "remoting/base/codec_test.h"
[email protected]8ea7a1672010-10-04 19:48:4214#include "remoting/base/decoder.h"
[email protected]fb13c762010-07-24 02:22:0715#include "remoting/base/encoder.h"
[email protected]c3af26f332010-10-06 22:46:0016#include "remoting/base/util.h"
[email protected]fb13c762010-07-24 02:22:0717#include "testing/gtest/include/gtest/gtest.h"
18
[email protected]1d65f482012-07-28 06:40:4619namespace {
20
21const int kBytesPerPixel = 4;
[email protected]fb13c762010-07-24 02:22:0722
23// Some sample rects for testing.
[email protected]1d65f482012-07-28 06:40:4624std::vector<std::vector<SkIRect> > MakeTestRectLists(const SkISize& size) {
25 std::vector<std::vector<SkIRect> > rect_lists;
26 std::vector<SkIRect> rects;
27 rects.push_back(SkIRect::MakeXYWH(0, 0, size.width(), size.height()));
28 rect_lists.push_back(rects);
29 rects.clear();
30 rects.push_back(SkIRect::MakeXYWH(0, 0, size.width() / 2, size.height() / 2));
31 rect_lists.push_back(rects);
32 rects.clear();
33 rects.push_back(SkIRect::MakeXYWH(size.width() / 2, size.height() / 2,
34 size.width() / 2, size.height() / 2));
35 rect_lists.push_back(rects);
36 rects.clear();
37 rects.push_back(SkIRect::MakeXYWH(16, 16, 16, 16));
38 rects.push_back(SkIRect::MakeXYWH(128, 64, 32, 32));
39 rect_lists.push_back(rects);
40 return rect_lists;
41}
42
43} // namespace
[email protected]fb13c762010-07-24 02:22:0744
45namespace remoting {
46
[email protected]fb13c762010-07-24 02:22:0747// A class to test the message output of the encoder.
48class EncoderMessageTester {
49 public:
50 EncoderMessageTester()
51 : begin_rect_(0),
52 rect_data_(0),
53 end_rect_(0),
[email protected]66db1af92010-07-27 01:42:4654 added_rects_(0),
[email protected]fb13c762010-07-24 02:22:0755 state_(kWaitingForBeginRect),
56 strict_(false) {
57 }
58
59 ~EncoderMessageTester() {
60 EXPECT_EQ(begin_rect_, end_rect_);
[email protected]df10bf12010-09-28 22:19:4861 EXPECT_GT(begin_rect_, 0);
[email protected]fb13c762010-07-24 02:22:0762 EXPECT_EQ(kWaitingForBeginRect, state_);
[email protected]df10bf12010-09-28 22:19:4863 if (strict_) {
64 EXPECT_EQ(added_rects_, begin_rect_);
[email protected]66db1af92010-07-27 01:42:4665 }
[email protected]fb13c762010-07-24 02:22:0766 }
67
[email protected]137e58f12010-12-07 19:35:4368 // Test that we received the correct packet.
69 void ReceivedPacket(VideoPacket* packet) {
[email protected]fb13c762010-07-24 02:22:0770 if (state_ == kWaitingForBeginRect) {
[email protected]137e58f12010-12-07 19:35:4371 EXPECT_TRUE((packet->flags() & VideoPacket::FIRST_PACKET) != 0);
[email protected]fb13c762010-07-24 02:22:0772 state_ = kWaitingForRectData;
73 ++begin_rect_;
74
75 if (strict_) {
[email protected]dc454a7c2011-08-12 22:01:1376 SkIRect rect = rects_.front();
[email protected]fb13c762010-07-24 02:22:0777 rects_.pop_front();
[email protected]dc454a7c2011-08-12 22:01:1378 EXPECT_EQ(rect.fLeft, packet->format().x());
79 EXPECT_EQ(rect.fTop, packet->format().y());
[email protected]137e58f12010-12-07 19:35:4380 EXPECT_EQ(rect.width(), packet->format().width());
81 EXPECT_EQ(rect.height(), packet->format().height());
[email protected]fb13c762010-07-24 02:22:0782 }
83 } else {
[email protected]137e58f12010-12-07 19:35:4384 EXPECT_FALSE((packet->flags() & VideoPacket::FIRST_PACKET) != 0);
[email protected]fb13c762010-07-24 02:22:0785 }
86
87 if (state_ == kWaitingForRectData) {
[email protected]137e58f12010-12-07 19:35:4388 if (packet->has_data()) {
[email protected]fb13c762010-07-24 02:22:0789 ++rect_data_;
90 }
91
[email protected]137e58f12010-12-07 19:35:4392 if ((packet->flags() & VideoPacket::LAST_PACKET) != 0) {
[email protected]fb13c762010-07-24 02:22:0793 // Expect that we have received some data.
94 EXPECT_GT(rect_data_, 0);
95 rect_data_ = 0;
96 state_ = kWaitingForBeginRect;
97 ++end_rect_;
98 }
[email protected]5bc71832010-12-09 01:34:0899
100 if ((packet->flags() & VideoPacket::LAST_PARTITION) != 0) {
101 // LAST_PARTITION must always be marked with LAST_PACKET.
102 EXPECT_TRUE((packet->flags() & VideoPacket::LAST_PACKET) != 0);
103 }
[email protected]fb13c762010-07-24 02:22:07104 }
105 }
106
107 void set_strict(bool strict) {
108 strict_ = strict;
109 }
110
[email protected]dc454a7c2011-08-12 22:01:13111 void AddRects(const SkIRect* rects, int count) {
[email protected]fb13c762010-07-24 02:22:07112 rects_.insert(rects_.begin() + rects_.size(), rects, rects + count);
[email protected]66db1af92010-07-27 01:42:46113 added_rects_ += count;
[email protected]fb13c762010-07-24 02:22:07114 }
115
116 private:
117 enum State {
118 kWaitingForBeginRect,
119 kWaitingForRectData,
120 };
121
122 int begin_rect_;
123 int rect_data_;
124 int end_rect_;
[email protected]66db1af92010-07-27 01:42:46125 int added_rects_;
[email protected]fb13c762010-07-24 02:22:07126 State state_;
127 bool strict_;
128
[email protected]dc454a7c2011-08-12 22:01:13129 std::deque<SkIRect> rects_;
[email protected]fb13c762010-07-24 02:22:07130
131 DISALLOW_COPY_AND_ASSIGN(EncoderMessageTester);
132};
133
134class DecoderTester {
135 public:
[email protected]1d65f482012-07-28 06:40:46136 DecoderTester(Decoder* decoder, const SkISize& screen_size,
137 const SkISize& view_size)
138 : screen_size_(screen_size),
139 view_size_(view_size),
140 strict_(false),
[email protected]137e58f12010-12-07 19:35:43141 decoder_(decoder) {
[email protected]1d65f482012-07-28 06:40:46142 image_data_.reset(new uint8[
143 view_size_.width() * view_size_.height() * kBytesPerPixel]);
[email protected]55d3688e2012-02-24 23:05:56144 EXPECT_TRUE(image_data_.get());
[email protected]1d65f482012-07-28 06:40:46145 decoder_->Initialize(screen_size_);
[email protected]fb13c762010-07-24 02:22:07146 }
147
[email protected]137e58f12010-12-07 19:35:43148 void Reset() {
[email protected]a9c88d12012-01-29 03:45:17149 expected_region_.setEmpty();
150 update_region_.setEmpty();
[email protected]137e58f12010-12-07 19:35:43151 }
[email protected]cee5dba2010-12-07 03:19:48152
[email protected]1d65f482012-07-28 06:40:46153 void ResetRenderedData() {
154 memset(image_data_.get(), 0,
155 view_size_.width() * view_size_.height() * kBytesPerPixel);
156 }
157
[email protected]137e58f12010-12-07 19:35:43158 void ReceivedPacket(VideoPacket* packet) {
159 Decoder::DecodeResult result = decoder_->DecodePacket(packet);
[email protected]cee5dba2010-12-07 03:19:48160
[email protected]137e58f12010-12-07 19:35:43161 ASSERT_NE(Decoder::DECODE_ERROR, result);
162
163 if (result == Decoder::DECODE_DONE) {
[email protected]1d65f482012-07-28 06:40:46164 RenderFrame();
[email protected]cee5dba2010-12-07 03:19:48165 }
[email protected]fb13c762010-07-24 02:22:07166 }
167
[email protected]1d65f482012-07-28 06:40:46168 void RenderFrame() {
169 decoder_->RenderFrame(view_size_,
170 SkIRect::MakeSize(view_size_),
171 image_data_.get(),
172 view_size_.width() * kBytesPerPixel,
173 &update_region_);
174 }
175
[email protected]36b967f2012-07-27 18:35:32176 void ReceivedScopedPacket(scoped_ptr<VideoPacket> packet) {
177 ReceivedPacket(packet.get());
178 }
179
[email protected]fb13c762010-07-24 02:22:07180 void set_strict(bool strict) {
181 strict_ = strict;
182 }
183
184 void set_capture_data(scoped_refptr<CaptureData> data) {
185 capture_data_ = data;
186 }
187
[email protected]dc454a7c2011-08-12 22:01:13188 void AddRects(const SkIRect* rects, int count) {
[email protected]a9c88d12012-01-29 03:45:17189 SkRegion new_rects;
190 new_rects.setRects(rects, count);
[email protected]36b967f2012-07-27 18:35:32191 AddRegion(new_rects);
192 }
193
194 void AddRegion(const SkRegion& region) {
195 expected_region_.op(region, SkRegion::kUnion_Op);
[email protected]fb13c762010-07-24 02:22:07196 }
197
[email protected]137e58f12010-12-07 19:35:43198 void VerifyResults() {
[email protected]fb13c762010-07-24 02:22:07199 if (!strict_)
200 return;
[email protected]66db1af92010-07-27 01:42:46201
[email protected]137e58f12010-12-07 19:35:43202 ASSERT_TRUE(capture_data_.get());
203
[email protected]a9c88d12012-01-29 03:45:17204 // Test the content of the update region.
205 EXPECT_EQ(expected_region_, update_region_);
206 for (SkRegion::Iterator i(update_region_); !i.done(); i.next()) {
[email protected]1d65f482012-07-28 06:40:46207 const int stride = view_size_.width() * kBytesPerPixel;
[email protected]55d3688e2012-02-24 23:05:56208 EXPECT_EQ(stride, capture_data_->data_planes().strides[0]);
[email protected]a9c88d12012-01-29 03:45:17209 const int offset = stride * i.rect().top() +
210 kBytesPerPixel * i.rect().left();
[email protected]66db1af92010-07-27 01:42:46211 const uint8* original = capture_data_->data_planes().data[0] + offset;
[email protected]55d3688e2012-02-24 23:05:56212 const uint8* decoded = image_data_.get() + offset;
[email protected]a9c88d12012-01-29 03:45:17213 const int row_size = kBytesPerPixel * i.rect().width();
214 for (int y = 0; y < i.rect().height(); ++y) {
[email protected]66db1af92010-07-27 01:42:46215 EXPECT_EQ(0, memcmp(original, decoded, row_size))
216 << "Row " << y << " is different";
217 original += stride;
218 decoded += stride;
219 }
[email protected]fb13c762010-07-24 02:22:07220 }
221 }
222
[email protected]36b967f2012-07-27 18:35:32223 // The error at each pixel is the root mean square of the errors in
224 // the R, G, and B components, each normalized to [0, 1]. This routine
225 // checks that the maximum and mean pixel errors do not exceed given limits.
[email protected]1d65f482012-07-28 06:40:46226void VerifyResultsApprox(const uint8* expected_view_data,
227 double max_error_limit, double mean_error_limit) {
[email protected]36b967f2012-07-27 18:35:32228 double max_error = 0.0;
229 double sum_error = 0.0;
230 int error_num = 0;
231 for (SkRegion::Iterator i(update_region_); !i.done(); i.next()) {
[email protected]1d65f482012-07-28 06:40:46232 const int stride = view_size_.width() * kBytesPerPixel;
[email protected]36b967f2012-07-27 18:35:32233 const int offset = stride * i.rect().top() +
234 kBytesPerPixel * i.rect().left();
[email protected]1d65f482012-07-28 06:40:46235 const uint8* expected = expected_view_data + offset;
236 const uint8* actual = image_data_.get() + offset;
[email protected]36b967f2012-07-27 18:35:32237 for (int y = 0; y < i.rect().height(); ++y) {
238 for (int x = 0; x < i.rect().width(); ++x) {
[email protected]1d65f482012-07-28 06:40:46239 double error = CalculateError(expected, actual);
[email protected]36b967f2012-07-27 18:35:32240 max_error = std::max(max_error, error);
241 sum_error += error;
242 ++error_num;
[email protected]1d65f482012-07-28 06:40:46243 expected += 4;
244 actual += 4;
[email protected]36b967f2012-07-27 18:35:32245 }
246 }
247 }
248 EXPECT_LE(max_error, max_error_limit);
249 double mean_error = sum_error / error_num;
250 EXPECT_LE(mean_error, mean_error_limit);
251 LOG(INFO) << "Max error: " << max_error;
252 LOG(INFO) << "Mean error: " << mean_error;
253 }
254
255 double CalculateError(const uint8* original, const uint8* decoded) {
256 double error_sum_squares = 0.0;
257 for (int i = 0; i < 3; i++) {
258 double error = static_cast<double>(*original++) -
259 static_cast<double>(*decoded++);
260 error /= 255.0;
261 error_sum_squares += error * error;
262 }
263 original++;
264 decoded++;
265 return sqrt(error_sum_squares / 3.0);
266 }
267
[email protected]137e58f12010-12-07 19:35:43268 private:
[email protected]1d65f482012-07-28 06:40:46269 SkISize screen_size_;
270 SkISize view_size_;
[email protected]fb13c762010-07-24 02:22:07271 bool strict_;
[email protected]a9c88d12012-01-29 03:45:17272 SkRegion expected_region_;
273 SkRegion update_region_;
[email protected]fb13c762010-07-24 02:22:07274 Decoder* decoder_;
[email protected]55d3688e2012-02-24 23:05:56275 scoped_array<uint8> image_data_;
[email protected]fb13c762010-07-24 02:22:07276 scoped_refptr<CaptureData> capture_data_;
[email protected]fb13c762010-07-24 02:22:07277
278 DISALLOW_COPY_AND_ASSIGN(DecoderTester);
279};
280
[email protected]8ea7a1672010-10-04 19:48:42281// The EncoderTester provides a hook for retrieving the data, and passing the
282// message to other subprograms for validaton.
[email protected]fb13c762010-07-24 02:22:07283class EncoderTester {
284 public:
[email protected]137e58f12010-12-07 19:35:43285 EncoderTester(EncoderMessageTester* message_tester)
[email protected]fb13c762010-07-24 02:22:07286 : message_tester_(message_tester),
[email protected]fb13c762010-07-24 02:22:07287 decoder_tester_(NULL),
288 data_available_(0) {
289 }
290
291 ~EncoderTester() {
292 EXPECT_GT(data_available_, 0);
293 }
294
[email protected]3361e1f2012-03-20 20:31:44295 void DataAvailable(scoped_ptr<VideoPacket> packet) {
[email protected]fb13c762010-07-24 02:22:07296 ++data_available_;
[email protected]3361e1f2012-03-20 20:31:44297 message_tester_->ReceivedPacket(packet.get());
[email protected]fb13c762010-07-24 02:22:07298
299 // Send the message to the DecoderTester.
300 if (decoder_tester_) {
[email protected]3361e1f2012-03-20 20:31:44301 decoder_tester_->ReceivedPacket(packet.get());
[email protected]cee5dba2010-12-07 03:19:48302 }
[email protected]fb13c762010-07-24 02:22:07303 }
304
[email protected]dc454a7c2011-08-12 22:01:13305 void AddRects(const SkIRect* rects, int count) {
[email protected]fb13c762010-07-24 02:22:07306 message_tester_->AddRects(rects, count);
307 }
308
309 void set_decoder_tester(DecoderTester* decoder_tester) {
310 decoder_tester_ = decoder_tester;
311 }
312
313 private:
314 EncoderMessageTester* message_tester_;
[email protected]fb13c762010-07-24 02:22:07315 DecoderTester* decoder_tester_;
316 int data_available_;
317
318 DISALLOW_COPY_AND_ASSIGN(EncoderTester);
319};
320
[email protected]1d65f482012-07-28 06:40:46321scoped_refptr<CaptureData> PrepareEncodeData(const SkISize& size,
322 media::VideoFrame::Format format,
[email protected]fb13c762010-07-24 02:22:07323 uint8** memory) {
324 // TODO(hclam): Support also YUV format.
[email protected]137e58f12010-12-07 19:35:43325 CHECK_EQ(format, media::VideoFrame::RGB32);
[email protected]1d65f482012-07-28 06:40:46326 int memory_size = size.width() * size.height() * kBytesPerPixel;
[email protected]fb13c762010-07-24 02:22:07327
[email protected]1d65f482012-07-28 06:40:46328 *memory = new uint8[memory_size];
[email protected]fb13c762010-07-24 02:22:07329 srand(0);
[email protected]1d65f482012-07-28 06:40:46330 for (int i = 0; i < memory_size; ++i) {
[email protected]fb13c762010-07-24 02:22:07331 (*memory)[i] = rand() % 256;
332 }
333
334 DataPlanes planes;
335 memset(planes.data, 0, sizeof(planes.data));
336 memset(planes.strides, 0, sizeof(planes.strides));
337 planes.data[0] = *memory;
[email protected]1d65f482012-07-28 06:40:46338 planes.strides[0] = size.width() * kBytesPerPixel;
[email protected]fb13c762010-07-24 02:22:07339
340 scoped_refptr<CaptureData> data =
[email protected]1d65f482012-07-28 06:40:46341 new CaptureData(planes, size, format);
[email protected]fb13c762010-07-24 02:22:07342 return data;
343}
344
345static void TestEncodingRects(Encoder* encoder,
346 EncoderTester* tester,
347 scoped_refptr<CaptureData> data,
[email protected]dc454a7c2011-08-12 22:01:13348 const SkIRect* rects, int count) {
349 data->mutable_dirty_region().setEmpty();
[email protected]88552a92010-08-06 22:50:00350 for (int i = 0; i < count; ++i) {
[email protected]dc454a7c2011-08-12 22:01:13351 data->mutable_dirty_region().op(rects[i], SkRegion::kUnion_Op);
[email protected]88552a92010-08-06 22:50:00352 }
[email protected]fb13c762010-07-24 02:22:07353 tester->AddRects(rects, count);
354
[email protected]1e1cb3b2011-11-10 02:07:41355 encoder->Encode(data, true, base::Bind(
356 &EncoderTester::DataAvailable, base::Unretained(tester)));
[email protected]fb13c762010-07-24 02:22:07357}
358
359void TestEncoder(Encoder* encoder, bool strict) {
[email protected]1d65f482012-07-28 06:40:46360 SkISize kSize = SkISize::Make(320, 240);
361
[email protected]fb13c762010-07-24 02:22:07362 EncoderMessageTester message_tester;
363 message_tester.set_strict(strict);
364
[email protected]137e58f12010-12-07 19:35:43365 EncoderTester tester(&message_tester);
[email protected]fb13c762010-07-24 02:22:07366
367 uint8* memory;
368 scoped_refptr<CaptureData> data =
[email protected]1d65f482012-07-28 06:40:46369 PrepareEncodeData(kSize, media::VideoFrame::RGB32, &memory);
[email protected]dc454a7c2011-08-12 22:01:13370 scoped_array<uint8> memory_wrapper(memory);
[email protected]fb13c762010-07-24 02:22:07371
[email protected]1d65f482012-07-28 06:40:46372 std::vector<std::vector<SkIRect> > test_rect_lists = MakeTestRectLists(kSize);
373 for (size_t i = 0; i < test_rect_lists.size(); ++i) {
374 const std::vector<SkIRect>& test_rects = test_rect_lists[i];
375 TestEncodingRects(encoder, &tester, data,
376 &test_rects[0], test_rects.size());
377 }
[email protected]fb13c762010-07-24 02:22:07378}
379
[email protected]a9c88d12012-01-29 03:45:17380static void TestEncodeDecodeRects(Encoder* encoder,
381 EncoderTester* encoder_tester,
382 DecoderTester* decoder_tester,
383 scoped_refptr<CaptureData> data,
384 const SkIRect* rects, int count) {
385 data->mutable_dirty_region().setRects(rects, count);
[email protected]fb13c762010-07-24 02:22:07386 encoder_tester->AddRects(rects, count);
387 decoder_tester->AddRects(rects, count);
[email protected]fb13c762010-07-24 02:22:07388
[email protected]a9c88d12012-01-29 03:45:17389 // Generate random data for the updated region.
[email protected]66db1af92010-07-27 01:42:46390 srand(0);
391 for (int i = 0; i < count; ++i) {
[email protected]f402ba032012-04-11 00:17:16392 CHECK_EQ(data->pixel_format(), media::VideoFrame::RGB32);
393 const int bytes_per_pixel = 4; // Because of RGB32 on previous line.
[email protected]a9c88d12012-01-29 03:45:17394 const int row_size = bytes_per_pixel * rects[i].width();
[email protected]66db1af92010-07-27 01:42:46395 uint8* memory = data->data_planes().data[0] +
[email protected]a9c88d12012-01-29 03:45:17396 data->data_planes().strides[0] * rects[i].top() +
397 bytes_per_pixel * rects[i].left();
398 for (int y = 0; y < rects[i].height(); ++y) {
[email protected]66db1af92010-07-27 01:42:46399 for (int x = 0; x < row_size; ++x)
400 memory[x] = rand() % 256;
401 memory += data->data_planes().strides[0];
402 }
403 }
404
[email protected]1e1cb3b2011-11-10 02:07:41405 encoder->Encode(data, true, base::Bind(&EncoderTester::DataAvailable,
406 base::Unretained(encoder_tester)));
[email protected]137e58f12010-12-07 19:35:43407 decoder_tester->VerifyResults();
408 decoder_tester->Reset();
[email protected]fb13c762010-07-24 02:22:07409}
410
411void TestEncoderDecoder(Encoder* encoder, Decoder* decoder, bool strict) {
[email protected]1d65f482012-07-28 06:40:46412 SkISize kSize = SkISize::Make(320, 240);
413
[email protected]fb13c762010-07-24 02:22:07414 EncoderMessageTester message_tester;
415 message_tester.set_strict(strict);
416
[email protected]137e58f12010-12-07 19:35:43417 EncoderTester encoder_tester(&message_tester);
[email protected]fb13c762010-07-24 02:22:07418
419 uint8* memory;
420 scoped_refptr<CaptureData> data =
[email protected]1d65f482012-07-28 06:40:46421 PrepareEncodeData(kSize, media::VideoFrame::RGB32, &memory);
[email protected]dc454a7c2011-08-12 22:01:13422 scoped_array<uint8> memory_wrapper(memory);
423
[email protected]1d65f482012-07-28 06:40:46424 DecoderTester decoder_tester(decoder, kSize, kSize);
[email protected]fb13c762010-07-24 02:22:07425 decoder_tester.set_strict(strict);
426 decoder_tester.set_capture_data(data);
427 encoder_tester.set_decoder_tester(&decoder_tester);
428
[email protected]1d65f482012-07-28 06:40:46429 std::vector<std::vector<SkIRect> > test_rect_lists = MakeTestRectLists(kSize);
430 for (size_t i = 0; i < test_rect_lists.size(); ++i) {
431 const std::vector<SkIRect> test_rects = test_rect_lists[i];
432 TestEncodeDecodeRects(encoder, &encoder_tester, &decoder_tester, data,
433 &test_rects[0], test_rects.size());
434 }
[email protected]fb13c762010-07-24 02:22:07435}
436
[email protected]36b967f2012-07-27 18:35:32437static void FillWithGradient(uint8* memory, const SkISize& frame_size,
438 const SkIRect& rect) {
439 for (int j = rect.top(); j < rect.bottom(); ++j) {
440 uint8* p = memory + ((j * frame_size.width()) + rect.left()) * 4;
441 for (int i = rect.left(); i < rect.right(); ++i) {
442 *p++ = static_cast<uint8>((255.0 * i) / frame_size.width());
443 *p++ = static_cast<uint8>((164.0 * j) / frame_size.height());
444 *p++ = static_cast<uint8>((82.0 * (i + j)) /
445 (frame_size.width() + frame_size.height()));
446 *p++ = 0;
447 }
448 }
449}
450
451void TestEncoderDecoderGradient(Encoder* encoder,
452 Decoder* decoder,
[email protected]1d65f482012-07-28 06:40:46453 const SkISize& screen_size,
454 const SkISize& view_size,
[email protected]36b967f2012-07-27 18:35:32455 double max_error_limit,
456 double mean_error_limit) {
[email protected]1d65f482012-07-28 06:40:46457 SkIRect screen_rect = SkIRect::MakeSize(screen_size);
458 scoped_array<uint8> screen_data(new uint8[
459 screen_size.width() * screen_size.height() * kBytesPerPixel]);
460 FillWithGradient(screen_data.get(), screen_size, screen_rect);
461
462 SkIRect view_rect = SkIRect::MakeSize(view_size);
463 scoped_array<uint8> expected_view_data(new uint8[
464 view_size.width() * view_size.height() * kBytesPerPixel]);
465 FillWithGradient(expected_view_data.get(), view_size, view_rect);
[email protected]36b967f2012-07-27 18:35:32466
467 DataPlanes planes;
468 memset(planes.data, 0, sizeof(planes.data));
469 memset(planes.strides, 0, sizeof(planes.strides));
[email protected]1d65f482012-07-28 06:40:46470 planes.data[0] = screen_data.get();
471 planes.strides[0] = screen_size.width() * kBytesPerPixel;
[email protected]36b967f2012-07-27 18:35:32472
473 scoped_refptr<CaptureData> capture_data =
[email protected]1d65f482012-07-28 06:40:46474 new CaptureData(planes, screen_size, media::VideoFrame::RGB32);
475 capture_data->mutable_dirty_region().op(screen_rect, SkRegion::kUnion_Op);
[email protected]36b967f2012-07-27 18:35:32476
[email protected]1d65f482012-07-28 06:40:46477 DecoderTester decoder_tester(decoder, screen_size, view_size);
[email protected]36b967f2012-07-27 18:35:32478 decoder_tester.set_capture_data(capture_data);
479 decoder_tester.AddRegion(capture_data->dirty_region());
480
481 encoder->Encode(capture_data, true,
482 base::Bind(&DecoderTester::ReceivedScopedPacket,
483 base::Unretained(&decoder_tester)));
484
[email protected]1d65f482012-07-28 06:40:46485 decoder_tester.VerifyResultsApprox(expected_view_data.get(),
486 max_error_limit, mean_error_limit);
487
488 // Check that the decoder correctly re-renders the frame if its client
489 // invalidates the frame.
490 decoder_tester.ResetRenderedData();
491 decoder->Invalidate(view_size, SkRegion(view_rect));
492 decoder_tester.RenderFrame();
493 decoder_tester.VerifyResultsApprox(expected_view_data.get(),
494 max_error_limit, mean_error_limit);
[email protected]36b967f2012-07-27 18:35:32495}
496
[email protected]fb13c762010-07-24 02:22:07497} // namespace remoting