2014-04-11 18:33:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2015-11-19 15:23:49 +00:00
|
|
|
#include "RecordTestUtils.h"
|
2014-07-05 20:37:53 +00:00
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkImageInfo.h"
|
2014-04-08 17:31:08 +00:00
|
|
|
#include "SkRecord.h"
|
|
|
|
#include "SkRecords.h"
|
2015-11-19 15:23:49 +00:00
|
|
|
#include "SkShader.h"
|
|
|
|
#include "Test.h"
|
2018-06-11 14:45:26 +00:00
|
|
|
#include <new>
|
2015-11-19 15:23:49 +00:00
|
|
|
|
2014-04-08 17:31:08 +00:00
|
|
|
|
2014-04-08 23:31:35 +00:00
|
|
|
// Sums the area of any DrawRect command it sees.
|
2014-04-08 17:31:08 +00:00
|
|
|
class AreaSummer {
|
|
|
|
public:
|
2014-04-08 23:31:35 +00:00
|
|
|
AreaSummer() : fArea(0) {}
|
2014-04-08 17:31:08 +00:00
|
|
|
|
|
|
|
template <typename T> void operator()(const T&) { }
|
|
|
|
|
2014-05-07 21:16:09 +00:00
|
|
|
void operator()(const SkRecords::DrawRect& draw) {
|
|
|
|
fArea += (int)(draw.rect.width() * draw.rect.height());
|
|
|
|
}
|
|
|
|
|
2014-04-08 23:31:35 +00:00
|
|
|
int area() const { return fArea; }
|
|
|
|
|
2014-04-22 16:57:20 +00:00
|
|
|
void apply(const SkRecord& record) {
|
2015-08-19 16:51:00 +00:00
|
|
|
for (int i = 0; i < record.count(); i++) {
|
2016-03-22 18:46:53 +00:00
|
|
|
record.visit(i, *this);
|
2014-04-22 16:57:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-08 17:31:08 +00:00
|
|
|
private:
|
2014-04-08 23:31:35 +00:00
|
|
|
int fArea;
|
2014-04-08 17:31:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Scales out the bottom-right corner of any DrawRect command it sees by 2x.
|
|
|
|
struct Stretch {
|
|
|
|
template <typename T> void operator()(T*) {}
|
2014-05-07 21:16:09 +00:00
|
|
|
void operator()(SkRecords::DrawRect* draw) {
|
|
|
|
draw->rect.fRight *= 2;
|
|
|
|
draw->rect.fBottom *= 2;
|
|
|
|
}
|
2014-04-22 16:57:20 +00:00
|
|
|
|
|
|
|
void apply(SkRecord* record) {
|
2015-08-19 16:51:00 +00:00
|
|
|
for (int i = 0; i < record->count(); i++) {
|
2016-03-22 18:46:53 +00:00
|
|
|
record->mutate(i, *this);
|
2014-04-22 16:57:20 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-08 17:31:08 +00:00
|
|
|
};
|
|
|
|
|
2015-09-28 17:33:02 +00:00
|
|
|
#define APPEND(record, type, ...) new (record.append<type>()) type{__VA_ARGS__}
|
2014-07-05 20:37:53 +00:00
|
|
|
|
2014-04-08 17:31:08 +00:00
|
|
|
// Basic tests for the low-level SkRecord code.
|
|
|
|
DEF_TEST(Record, r) {
|
|
|
|
SkRecord record;
|
|
|
|
|
|
|
|
// Add a simple DrawRect command.
|
|
|
|
SkRect rect = SkRect::MakeWH(10, 10);
|
|
|
|
SkPaint paint;
|
2014-07-05 20:37:53 +00:00
|
|
|
APPEND(record, SkRecords::DrawRect, paint, rect);
|
2014-04-08 17:31:08 +00:00
|
|
|
|
|
|
|
// Its area should be 100.
|
2014-04-08 23:31:35 +00:00
|
|
|
AreaSummer summer;
|
2014-04-22 16:57:20 +00:00
|
|
|
summer.apply(record);
|
2014-04-08 23:31:35 +00:00
|
|
|
REPORTER_ASSERT(r, summer.area() == 100);
|
|
|
|
|
|
|
|
// Scale 2x.
|
|
|
|
Stretch stretch;
|
2014-04-22 16:57:20 +00:00
|
|
|
stretch.apply(&record);
|
2014-04-08 23:31:35 +00:00
|
|
|
|
|
|
|
// Now its area should be 100 + 400.
|
2014-04-22 16:57:20 +00:00
|
|
|
summer.apply(record);
|
2014-04-08 23:31:35 +00:00
|
|
|
REPORTER_ASSERT(r, summer.area() == 500);
|
2014-04-08 17:31:08 +00:00
|
|
|
}
|
2014-07-05 20:37:53 +00:00
|
|
|
|
2015-11-19 15:23:49 +00:00
|
|
|
DEF_TEST(Record_defrag, r) {
|
|
|
|
SkRecord record;
|
|
|
|
APPEND(record, SkRecords::Save);
|
|
|
|
APPEND(record, SkRecords::ClipRect);
|
|
|
|
APPEND(record, SkRecords::NoOp);
|
|
|
|
APPEND(record, SkRecords::DrawRect);
|
|
|
|
APPEND(record, SkRecords::NoOp);
|
|
|
|
APPEND(record, SkRecords::NoOp);
|
|
|
|
APPEND(record, SkRecords::Restore);
|
|
|
|
REPORTER_ASSERT(r, record.count() == 7);
|
|
|
|
|
|
|
|
record.defrag();
|
|
|
|
REPORTER_ASSERT(r, record.count() == 4);
|
|
|
|
assert_type<SkRecords::Save >(r, record, 0);
|
|
|
|
assert_type<SkRecords::ClipRect>(r, record, 1);
|
|
|
|
assert_type<SkRecords::DrawRect>(r, record, 2);
|
|
|
|
assert_type<SkRecords::Restore >(r, record, 3);
|
|
|
|
}
|
|
|
|
|
2014-07-05 20:37:53 +00:00
|
|
|
#undef APPEND
|
|
|
|
|
2014-08-28 21:10:05 +00:00
|
|
|
template <typename T>
|
|
|
|
static bool is_aligned(const T* p) {
|
|
|
|
return (((uintptr_t)p) & (sizeof(T) - 1)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(Record_Alignment, r) {
|
|
|
|
SkRecord record;
|
|
|
|
REPORTER_ASSERT(r, is_aligned(record.alloc<uint8_t>()));
|
|
|
|
REPORTER_ASSERT(r, is_aligned(record.alloc<uint16_t>()));
|
|
|
|
REPORTER_ASSERT(r, is_aligned(record.alloc<uint32_t>()));
|
|
|
|
REPORTER_ASSERT(r, is_aligned(record.alloc<void*>()));
|
|
|
|
|
2014-11-13 20:41:14 +00:00
|
|
|
// It's not clear if we care that 8-byte values are aligned on 32-bit machines.
|
|
|
|
if (sizeof(void*) == 8) {
|
|
|
|
REPORTER_ASSERT(r, is_aligned(record.alloc<double>()));
|
|
|
|
REPORTER_ASSERT(r, is_aligned(record.alloc<uint64_t>()));
|
|
|
|
}
|
2014-08-28 21:10:05 +00:00
|
|
|
}
|