skia2/tests/RecorderTest.cpp
Mike Reed 4f23dec742 Reland "Add new virts, hide old ones"
This reverts commit 8f924ac0ce.

Reason for revert: suppressions landed for fuchsia images to rebaseline

Original change's description:
> Revert "Add new virts, hide old ones"
>
> This reverts commit c56e2e5aa6.
>
> Reason for revert: suspected of breaking chrome roll
>
> Original change's description:
> > Add new virts, hide old ones
> >
> > Add virtuals for the draw methods that now take sampling/filtermode.
> >
> > drawImage
> > drawImageRect
> > drawImageLattice
> > drawAtlas
> >
> > Add a flag that can remove the older virtuals, once each client has
> > stopped overriding them. In that situation, the older public methods
> > will simplify extract the sampling from the paint, and call the new
> > public methods.
> >
> > Bug: skia:11105, skia:7650
> > Change-Id: I8b0029727295caa983e8148fc743a55cfbecd043
> > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/347022
> > Commit-Queue: Mike Reed <reed@google.com>
> > Reviewed-by: Florin Malita <fmalita@chromium.org>
> > Reviewed-by: Brian Salomon <bsalomon@google.com>
>
> TBR=bsalomon@google.com,fmalita@chromium.org,reed@google.com
>
> Change-Id: I0a90952c11a180d918126ea06a630f4a0bf9b49b
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: skia:11105
> Bug: skia:7650
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/348194
> Reviewed-by: Derek Sollenberger <djsollen@google.com>
> Commit-Queue: Derek Sollenberger <djsollen@google.com>

TBR=djsollen@google.com,bsalomon@google.com,fmalita@chromium.org,reed@google.com

# Not skipping CQ checks because this is a reland.

Bug: skia:11105
Bug: skia:7650
Change-Id: Ia2b4537a2d330460b7554278d2c05075cf27162a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/348876
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
2020-12-30 15:21:01 +00:00

154 lines
4.4 KiB
C++

/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "tests/Test.h"
#include "include/core/SkPictureRecorder.h"
#include "include/core/SkShader.h"
#include "include/core/SkSurface.h"
#include "src/core/SkRecord.h"
#include "src/core/SkRecorder.h"
#include "src/core/SkRecords.h"
#define COUNT(T) + 1
static const int kRecordTypes = SK_RECORD_TYPES(COUNT);
#undef COUNT
// Tallies the types of commands it sees into a histogram.
class Tally {
public:
Tally() { sk_bzero(&fHistogram, sizeof(fHistogram)); }
template <typename T>
void operator()(const T&) { ++fHistogram[T::kType]; }
template <typename T>
int count() const { return fHistogram[T::kType]; }
void apply(const SkRecord& record) {
for (int i = 0; i < record.count(); i++) {
record.visit(i, *this);
}
}
private:
int fHistogram[kRecordTypes];
};
DEF_TEST(Recorder, r) {
SkRecord record;
SkRecorder recorder(&record, 1920, 1080);
recorder.drawRect(SkRect::MakeWH(10, 10), SkPaint());
Tally tally;
tally.apply(record);
REPORTER_ASSERT(r, 1 == tally.count<SkRecords::DrawRect>());
}
// Regression test for leaking refs held by optional arguments.
DEF_TEST(Recorder_RefLeaking, r) {
// We use SaveLayer to test:
// - its SkRect argument is optional and SkRect is POD. Just testing that that works.
// - its SkPaint argument is optional and SkPaint is not POD. The bug was here.
SkRect bounds = SkRect::MakeWH(320, 240);
SkPaint paint;
paint.setShader(SkShaders::Empty());
REPORTER_ASSERT(r, paint.getShader()->unique());
{
SkRecord record;
SkRecorder recorder(&record, 1920, 1080);
recorder.saveLayer(&bounds, &paint);
REPORTER_ASSERT(r, !paint.getShader()->unique());
}
REPORTER_ASSERT(r, paint.getShader()->unique());
}
DEF_TEST(Recorder_drawImage_takeReference, reporter) {
sk_sp<SkImage> image;
{
auto surface(SkSurface::MakeRasterN32Premul(100, 100));
surface->getCanvas()->clear(SK_ColorGREEN);
image = surface->makeImageSnapshot();
}
#ifdef SK_SUPPORT_LEGACY_ONDRAWIMAGERECT
{
SkRecord record;
SkRecorder recorder(&record, 100, 100);
// DrawImage is supposed to take a reference
recorder.drawImage(image, 0, 0);
REPORTER_ASSERT(reporter, !image->unique());
Tally tally;
tally.apply(record);
REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImage>());
}
REPORTER_ASSERT(reporter, image->unique());
{
SkRecord record;
SkRecorder recorder(&record, 100, 100);
// DrawImageRect is supposed to take a reference
recorder.drawImageRect(image, SkRect::MakeWH(100, 100), nullptr);
REPORTER_ASSERT(reporter, !image->unique());
Tally tally;
tally.apply(record);
REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImageRect>());
}
REPORTER_ASSERT(reporter, image->unique());
#endif
{
SkRecord record;
SkRecorder recorder(&record, 100, 100);
// DrawImage is supposed to take a reference
recorder.drawImage(image.get(), 0, 0, SkSamplingOptions());
REPORTER_ASSERT(reporter, !image->unique());
Tally tally;
tally.apply(record);
REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImage2>());
}
REPORTER_ASSERT(reporter, image->unique());
{
SkRecord record;
SkRecorder recorder(&record, 100, 100);
// DrawImageRect is supposed to take a reference
recorder.drawImageRect(image.get(), SkRect::MakeWH(100, 100), SkRect::MakeWH(100, 100),
SkSamplingOptions(), nullptr, SkCanvas::kFast_SrcRectConstraint);
REPORTER_ASSERT(reporter, !image->unique());
Tally tally;
tally.apply(record);
REPORTER_ASSERT(reporter, 1 == tally.count<SkRecords::DrawImageRect2>());
}
REPORTER_ASSERT(reporter, image->unique());
}
// skbug.com/10997
DEF_TEST(Recorder_boundsOverflow, reporter) {
SkRect bigBounds = {SK_ScalarMin, SK_ScalarMin, SK_ScalarMax, SK_ScalarMax};
SkRecord record;
SkRecorder recorder(&record, bigBounds);
REPORTER_ASSERT(reporter, recorder.imageInfo().width() > 0 &&
recorder.imageInfo().height() > 0);
}