2015-08-14 13:30:50 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkMatrix.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkShader.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkTileMode.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "src/core/SkMathPriv.h"
|
2015-08-14 13:30:50 +00:00
|
|
|
|
2018-01-04 20:28:39 +00:00
|
|
|
static sk_sp<SkImage> makebm(int w, int h) {
|
2015-08-14 13:30:50 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
|
2018-01-04 20:28:39 +00:00
|
|
|
auto surface(SkSurface::MakeRaster(info));
|
2015-08-14 13:30:50 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
|
|
|
|
const SkScalar wScalar = SkIntToScalar(w);
|
|
|
|
const SkScalar hScalar = SkIntToScalar(h);
|
|
|
|
|
|
|
|
const SkPoint pt = { wScalar / 2, hScalar / 2 };
|
|
|
|
|
|
|
|
const SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar);
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
|
2015-08-14 13:30:50 +00:00
|
|
|
SK_ColorGREEN, SK_ColorMAGENTA,
|
|
|
|
SK_ColorBLUE, SK_ColorCYAN,
|
|
|
|
SK_ColorRED};
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar pos[] = {0,
|
2015-08-14 13:30:50 +00:00
|
|
|
SK_Scalar1 / 6,
|
|
|
|
2 * SK_Scalar1 / 6,
|
|
|
|
3 * SK_Scalar1 / 6,
|
|
|
|
4 * SK_Scalar1 / 6,
|
|
|
|
5 * SK_Scalar1 / 6,
|
|
|
|
SK_Scalar1};
|
|
|
|
|
|
|
|
SkASSERT(SK_ARRAY_COUNT(colors) == SK_ARRAY_COUNT(pos));
|
|
|
|
SkPaint paint;
|
|
|
|
SkRect rect = SkRect::MakeWH(wScalar, hScalar);
|
|
|
|
SkMatrix mat = SkMatrix::I();
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
2016-03-09 17:50:50 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeRadial(
|
2015-08-14 13:30:50 +00:00
|
|
|
pt, radius,
|
|
|
|
colors, pos,
|
|
|
|
SK_ARRAY_COUNT(colors),
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kRepeat,
|
2016-03-09 17:50:50 +00:00
|
|
|
0, &mat));
|
2015-08-14 13:30:50 +00:00
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
rect.inset(wScalar / 8, hScalar / 8);
|
|
|
|
mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
|
|
|
|
}
|
2016-03-17 17:51:11 +00:00
|
|
|
return surface->makeImageSnapshot();
|
2015-08-14 13:30:50 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr int gSize = 1024;
|
|
|
|
constexpr int gSurfaceSize = 2048;
|
2015-08-14 13:30:50 +00:00
|
|
|
|
2016-12-21 16:14:46 +00:00
|
|
|
// This GM calls drawImageRect several times using the same texture. This is intended to exercise
|
|
|
|
// combining GrDrawOps during these calls.
|
2015-08-14 13:30:50 +00:00
|
|
|
class DrawMiniBitmapRectGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
DrawMiniBitmapRectGM(bool antiAlias) : fAA(antiAlias) {
|
|
|
|
fName.set("drawminibitmaprect");
|
|
|
|
if (fAA) {
|
|
|
|
fName.appendf("_aa");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override { return fName; }
|
|
|
|
|
|
|
|
SkISize onISize() override { return SkISize::Make(gSize, gSize); }
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == fImage) {
|
2018-01-04 20:28:39 +00:00
|
|
|
fImage = makebm(gSurfaceSize, gSurfaceSize);
|
2015-08-14 13:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
|
2016-09-01 18:24:54 +00:00
|
|
|
const int kMaxSrcRectSize = 1 << (SkNextLog2(gSurfaceSize) + 2);
|
2015-08-14 13:30:50 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr int kPadX = 30;
|
|
|
|
constexpr int kPadY = 40;
|
2015-08-14 13:30:50 +00:00
|
|
|
|
|
|
|
int rowCount = 0;
|
|
|
|
canvas->translate(SkIntToScalar(kPadX), SkIntToScalar(kPadY));
|
|
|
|
canvas->save();
|
|
|
|
SkRandom random;
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(fAA);
|
|
|
|
for (int w = 1; w <= kMaxSrcRectSize; w *= 3) {
|
|
|
|
for (int h = 1; h <= kMaxSrcRectSize; h *= 3) {
|
|
|
|
|
|
|
|
const SkIRect srcRect =
|
|
|
|
SkIRect::MakeXYWH((gSurfaceSize - w) / 2, (gSurfaceSize - h) / 2, w, h);
|
|
|
|
canvas->save();
|
|
|
|
switch (random.nextU() % 3) {
|
|
|
|
case 0:
|
|
|
|
canvas->rotate(random.nextF() * 10.f);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
canvas->rotate(-random.nextF() * 10.f);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// rect stays rect
|
|
|
|
break;
|
|
|
|
}
|
2016-03-17 17:51:11 +00:00
|
|
|
canvas->drawImageRect(fImage.get(), srcRect, dstRect, &paint,
|
2015-08-14 13:30:50 +00:00
|
|
|
SkCanvas::kFast_SrcRectConstraint);
|
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
|
|
|
|
++rowCount;
|
|
|
|
if ((dstRect.width() + 2 * kPadX) * rowCount > gSize) {
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
|
|
|
|
canvas->save();
|
|
|
|
rowCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-03-17 17:51:11 +00:00
|
|
|
bool fAA;
|
|
|
|
sk_sp<SkImage> fImage;
|
|
|
|
SkString fName;
|
2015-08-14 13:30:50 +00:00
|
|
|
|
|
|
|
typedef skiagm::GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM( return new DrawMiniBitmapRectGM(true); )
|
|
|
|
DEF_GM( return new DrawMiniBitmapRectGM(false); )
|