skia2/tests/LayerDrawLooperTest.cpp
mtklein 7c591161d0 Revert of Make SkSmallAllocator obey the RAII invariants and be expandable (patchset #6 id:100001 of https://codereview.chromium.org/2488523003/ )
Reason for revert:
Crashing Mac Perf and Test bots.

This is a flaky but extremely likely crash.  I've only seen one Mac Perf or Test bot that had this patch that didn't crash.

This should be easy to reproduce like this:
  $ gn gen out --args=is_debug=false
  $ ninja -C out dm
  $ out/dm -m xfermodes3 --config gpu

This is crashing every time I run it on my laptop, and never when I revert this CL.

Building in release and running --config gpu probably don't matter.

Original issue's description:
> Make SkSmallAllocator obey the RAII invariants and move to heap structures when needed.
>
> The biggest change is to the API which allowed code to bypass the
> destruction invariants. This destruction bypass feature was needed in
> only one use, and is totally encapsulated using createWithIniterT.
>
> BUG=skia:
> GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2488523003
>
> Committed: https://skia.googlesource.com/skia/+/d5dc657b8c3ac916f98005dafdedafe02f023449

TBR=bungeman@google.com,herb@google.com
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:

Review-Url: https://codereview.chromium.org/2485853005
2016-11-09 14:50:31 -08:00

170 lines
6.2 KiB
C++

/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkBitmap.h"
#include "SkBitmapDevice.h"
#include "SkCanvas.h"
#include "SkDraw.h"
#include "SkLayerDrawLooper.h"
#include "SkMatrix.h"
#include "SkPaint.h"
#include "SkRect.h"
#include "SkRefCnt.h"
#include "SkScalar.h"
#include "SkSmallAllocator.h"
#include "SkXfermode.h"
#include "Test.h"
static SkBitmap make_bm(int w, int h) {
SkBitmap bm;
bm.allocN32Pixels(w, h);
return bm;
}
// TODO: can this be derived from SkBaseDevice?
class FakeDevice : public SkBitmapDevice {
public:
FakeDevice() : INHERITED(make_bm(100, 100), SkSurfaceProps(0, kUnknown_SkPixelGeometry)) {
}
void drawRect(const SkDraw& draw, const SkRect& r, const SkPaint& paint) override {
fLastMatrix = *draw.fMatrix;
this->INHERITED::drawRect(draw, r, paint);
}
SkMatrix fLastMatrix;
private:
typedef SkBitmapDevice INHERITED;
};
static void test_frontToBack(skiatest::Reporter* reporter) {
SkLayerDrawLooper::Builder looperBuilder;
SkLayerDrawLooper::LayerInfo layerInfo;
// Add the front layer, with the defaults.
(void)looperBuilder.addLayer(layerInfo);
// Add the back layer, with some layer info set.
layerInfo.fOffset.set(10.0f, 20.0f);
layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit;
SkPaint* layerPaint = looperBuilder.addLayer(layerInfo);
layerPaint->setBlendMode(SkBlendMode::kSrc);
FakeDevice device;
SkCanvas canvas(&device);
SkPaint paint;
auto looper(looperBuilder.detach());
SkSmallAllocator<1, 32> allocator;
void* buffer = allocator.reserveT<SkDrawLooper::Context>(looper->contextSize());
SkDrawLooper::Context* context = looper->createContext(&canvas, buffer);
// The back layer should come first.
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrc);
canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX());
REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY());
paint.reset();
// Then the front layer.
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrcOver);
canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX());
REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY());
// Only two layers were added, so that should be the end.
REPORTER_ASSERT(reporter, !context->next(&canvas, &paint));
}
static void test_backToFront(skiatest::Reporter* reporter) {
SkLayerDrawLooper::Builder looperBuilder;
SkLayerDrawLooper::LayerInfo layerInfo;
// Add the back layer, with the defaults.
(void)looperBuilder.addLayerOnTop(layerInfo);
// Add the front layer, with some layer info set.
layerInfo.fOffset.set(10.0f, 20.0f);
layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit;
SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo);
layerPaint->setBlendMode(SkBlendMode::kSrc);
FakeDevice device;
SkCanvas canvas(&device);
SkPaint paint;
auto looper(looperBuilder.detach());
SkSmallAllocator<1, 32> allocator;
void* buffer = allocator.reserveT<SkDrawLooper::Context>(looper->contextSize());
SkDrawLooper::Context* context = looper->createContext(&canvas, buffer);
// The back layer should come first.
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrcOver);
canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX());
REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY());
paint.reset();
// Then the front layer.
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrc);
canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX());
REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY());
// Only two layers were added, so that should be the end.
REPORTER_ASSERT(reporter, !context->next(&canvas, &paint));
}
static void test_mixed(skiatest::Reporter* reporter) {
SkLayerDrawLooper::Builder looperBuilder;
SkLayerDrawLooper::LayerInfo layerInfo;
// Add the back layer, with the defaults.
(void)looperBuilder.addLayer(layerInfo);
// Add the front layer, with some layer info set.
layerInfo.fOffset.set(10.0f, 20.0f);
layerInfo.fPaintBits |= SkLayerDrawLooper::kXfermode_Bit;
SkPaint* layerPaint = looperBuilder.addLayerOnTop(layerInfo);
layerPaint->setBlendMode(SkBlendMode::kSrc);
FakeDevice device;
SkCanvas canvas(&device);
SkPaint paint;
sk_sp<SkDrawLooper> looper(looperBuilder.detach());
SkSmallAllocator<1, 32> allocator;
void* buffer = allocator.reserveT<SkDrawLooper::Context>(looper->contextSize());
SkDrawLooper::Context* context = looper->createContext(&canvas, buffer);
// The back layer should come first.
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrcOver);
canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateX());
REPORTER_ASSERT(reporter, 0.0f == device.fLastMatrix.getTranslateY());
paint.reset();
// Then the front layer.
REPORTER_ASSERT(reporter, context->next(&canvas, &paint));
REPORTER_ASSERT(reporter, paint.getBlendMode() == SkBlendMode::kSrc);
canvas.drawRect(SkRect::MakeWH(50.0f, 50.0f), paint);
REPORTER_ASSERT(reporter, 10.0f == device.fLastMatrix.getTranslateX());
REPORTER_ASSERT(reporter, 20.0f == device.fLastMatrix.getTranslateY());
// Only two layers were added, so that should be the end.
REPORTER_ASSERT(reporter, !context->next(&canvas, &paint));
}
DEF_TEST(LayerDrawLooper, reporter) {
test_frontToBack(reporter);
test_backToFront(reporter);
test_mixed(reporter);
}