2011-11-30 14:13:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2015-01-23 13:58:07 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkBlurTypes.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFilterQuality.h"
|
|
|
|
#include "include/core/SkFont.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/SkMaskFilter.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/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/effects/SkGradientShader.h"
|
2020-07-10 18:33:22 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkBlurMask.h"
|
|
|
|
#include "src/core/SkMathPriv.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2011-11-30 14:13:48 +00:00
|
|
|
|
2012-04-25 15:52:27 +00:00
|
|
|
static SkBitmap make_chessbm(int w, int h) {
|
|
|
|
SkBitmap bm;
|
2014-01-25 16:46:20 +00:00
|
|
|
bm.allocN32Pixels(w, h);
|
2012-04-25 15:52:27 +00:00
|
|
|
|
|
|
|
for (int y = 0; y < bm.height(); y++) {
|
|
|
|
uint32_t* p = bm.getAddr32(0, y);
|
|
|
|
for (int x = 0; x < bm.width(); x++) {
|
|
|
|
p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
|
|
|
|
}
|
|
|
|
}
|
2021-01-24 02:14:47 +00:00
|
|
|
bm.setImmutable();
|
2012-04-25 15:52:27 +00:00
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
2016-07-14 16:33:42 +00:00
|
|
|
// Creates a bitmap and a matching image.
|
2016-03-17 17:51:11 +00:00
|
|
|
static sk_sp<SkImage> makebm(SkCanvas* origCanvas, SkBitmap* resultBM, int w, int h) {
|
2015-09-24 15:47:49 +00:00
|
|
|
SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
auto surface(ToolUtils::makeSurface(origCanvas, info));
|
2015-09-24 15:47:49 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
2011-11-30 14:13:48 +00:00
|
|
|
|
2015-09-24 15:47:49 +00:00
|
|
|
canvas->clear(SK_ColorTRANSPARENT);
|
2011-11-30 14:13:48 +00:00
|
|
|
|
|
|
|
SkScalar wScalar = SkIntToScalar(w);
|
|
|
|
SkScalar hScalar = SkIntToScalar(h);
|
|
|
|
|
|
|
|
SkPoint pt = { wScalar / 2, hScalar / 2 };
|
|
|
|
|
2020-02-05 18:34:09 +00:00
|
|
|
SkScalar radius = 4 * std::max(wScalar, hScalar);
|
2011-11-30 14:13:48 +00:00
|
|
|
|
|
|
|
SkColor colors[] = { SK_ColorRED, SK_ColorYELLOW,
|
|
|
|
SK_ColorGREEN, SK_ColorMAGENTA,
|
|
|
|
SK_ColorBLUE, SK_ColorCYAN,
|
|
|
|
SK_ColorRED};
|
|
|
|
|
|
|
|
SkScalar pos[] = {0,
|
|
|
|
SK_Scalar1 / 6,
|
|
|
|
2 * SK_Scalar1 / 6,
|
|
|
|
3 * SK_Scalar1 / 6,
|
|
|
|
4 * SK_Scalar1 / 6,
|
|
|
|
5 * SK_Scalar1 / 6,
|
|
|
|
SK_Scalar1};
|
|
|
|
|
|
|
|
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(
|
2014-04-28 14:55:39 +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-09-24 15:47:49 +00:00
|
|
|
canvas->drawRect(rect, paint);
|
2011-11-30 14:13:48 +00:00
|
|
|
rect.inset(wScalar / 8, hScalar / 8);
|
|
|
|
mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
|
|
|
|
}
|
2015-09-24 15:47:49 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
auto image = surface->makeImageSnapshot();
|
2015-09-24 15:47:49 +00:00
|
|
|
|
|
|
|
SkBitmap tempBM;
|
|
|
|
|
2018-02-07 20:51:00 +00:00
|
|
|
image->asLegacyBitmap(&tempBM);
|
2015-09-24 15:47:49 +00:00
|
|
|
|
2014-05-02 14:22:38 +00:00
|
|
|
// Let backends know we won't change this, so they don't have to deep copy it defensively.
|
2015-09-24 15:47:49 +00:00
|
|
|
tempBM.setImmutable();
|
|
|
|
*resultBM = tempBM;
|
2015-01-23 13:58:07 +00:00
|
|
|
|
2015-09-24 15:47:49 +00:00
|
|
|
return image;
|
2011-11-30 14:13:48 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 20:04:14 +00:00
|
|
|
static void bitmapproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
|
2021-01-24 02:14:47 +00:00
|
|
|
const SkRect& dstR, const SkSamplingOptions& sampling,
|
|
|
|
const SkPaint* paint) {
|
|
|
|
canvas->drawImageRect(bm.asImage(), SkRect::Make(srcR), dstR, sampling, paint,
|
|
|
|
SkCanvas::kStrict_SrcRectConstraint);
|
2016-08-26 20:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void bitmapsubsetproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
|
2021-01-24 02:14:47 +00:00
|
|
|
const SkRect& dstR, const SkSamplingOptions& sampling,
|
|
|
|
const SkPaint* paint) {
|
2016-08-26 20:04:14 +00:00
|
|
|
if (!bm.bounds().contains(srcR)) {
|
2021-01-24 02:14:47 +00:00
|
|
|
bitmapproc(canvas, nullptr, bm, srcR, dstR, sampling, paint);
|
2016-08-26 20:04:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkBitmap subset;
|
|
|
|
if (bm.extractSubset(&subset, srcR)) {
|
2021-01-24 02:14:47 +00:00
|
|
|
canvas->drawImageRect(subset.asImage(), dstR, sampling, paint);
|
2016-08-26 20:04:14 +00:00
|
|
|
}
|
2015-01-23 13:58:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 14:09:43 +00:00
|
|
|
static void imageproc(SkCanvas* canvas, SkImage* image, const SkBitmap&, const SkIRect& srcR,
|
2021-01-24 02:14:47 +00:00
|
|
|
const SkRect& dstR, const SkSamplingOptions& sampling, const SkPaint* paint) {
|
|
|
|
canvas->drawImageRect(image, SkRect::Make(srcR), dstR, sampling, paint,
|
|
|
|
SkCanvas::kStrict_SrcRectConstraint);
|
2015-01-23 13:58:07 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 20:04:14 +00:00
|
|
|
static void imagesubsetproc(SkCanvas* canvas, SkImage* image, const SkBitmap& bm,
|
2021-01-24 02:14:47 +00:00
|
|
|
const SkIRect& srcR, const SkRect& dstR,
|
|
|
|
const SkSamplingOptions& sampling, const SkPaint* paint) {
|
2016-08-26 20:04:14 +00:00
|
|
|
if (!image->bounds().contains(srcR)) {
|
2021-01-24 02:14:47 +00:00
|
|
|
imageproc(canvas, image, bm, srcR, dstR, sampling, paint);
|
2016-08-26 20:04:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:33:22 +00:00
|
|
|
auto direct = GrAsDirectContext(canvas->recordingContext());
|
|
|
|
if (sk_sp<SkImage> subset = image->makeSubset(srcR, direct)) {
|
2021-01-24 02:14:47 +00:00
|
|
|
canvas->drawImageRect(subset, dstR, sampling, paint);
|
2016-08-26 20:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&,
|
2021-01-24 02:14:47 +00:00
|
|
|
const SkSamplingOptions&, const SkPaint*);
|
2015-01-23 13:58:07 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr int gSize = 1024;
|
|
|
|
constexpr int gBmpSize = 2048;
|
2011-11-30 14:13:48 +00:00
|
|
|
|
2015-01-23 13:58:07 +00:00
|
|
|
class DrawBitmapRectGM : public skiagm::GM {
|
2011-11-30 14:13:48 +00:00
|
|
|
public:
|
2015-01-23 13:58:07 +00:00
|
|
|
DrawBitmapRectGM(DrawRectRectProc proc, const char suffix[]) : fProc(proc) {
|
|
|
|
fName.set("drawbitmaprect");
|
|
|
|
if (suffix) {
|
|
|
|
fName.append(suffix);
|
|
|
|
}
|
2011-11-30 14:13:48 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
DrawRectRectProc* fProc;
|
|
|
|
SkBitmap fLargeBitmap;
|
|
|
|
sk_sp<SkImage> fImage;
|
|
|
|
SkString fName;
|
2011-11-30 14:13:48 +00:00
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override { return fName; }
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(gSize, gSize); }
|
2012-08-23 18:19:56 +00:00
|
|
|
|
2015-09-24 15:47:49 +00:00
|
|
|
void setupImage(SkCanvas* canvas) {
|
2016-03-17 17:51:11 +00:00
|
|
|
fImage = makebm(canvas, &fLargeBitmap, gBmpSize, gBmpSize);
|
2015-01-23 13:58:07 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2020-07-10 15:27:43 +00:00
|
|
|
if (!fImage || !fImage->isValid(canvas->recordingContext())) {
|
2015-09-24 15:47:49 +00:00
|
|
|
this->setupImage(canvas);
|
|
|
|
}
|
|
|
|
|
2012-01-05 18:45:27 +00:00
|
|
|
SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
|
2016-09-01 18:24:54 +00:00
|
|
|
const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
|
2011-11-30 14:13:48 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
const int kPadX = 30;
|
|
|
|
const int kPadY = 40;
|
2011-11-30 14:13:48 +00:00
|
|
|
SkPaint paint;
|
2019-02-15 21:13:57 +00:00
|
|
|
paint.setAlphaf(0.125f);
|
2021-01-24 02:14:47 +00:00
|
|
|
canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), SkSamplingOptions(), &paint);
|
2011-11-30 14:13:48 +00:00
|
|
|
canvas->translate(SK_Scalar1 * kPadX / 2,
|
|
|
|
SK_Scalar1 * kPadY / 2);
|
|
|
|
SkPaint blackPaint;
|
|
|
|
SkScalar titleHeight = SK_Scalar1 * 24;
|
|
|
|
blackPaint.setColor(SK_ColorBLACK);
|
|
|
|
blackPaint.setAntiAlias(true);
|
2019-01-07 14:31:58 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface(), titleHeight);
|
2019-01-07 14:31:58 +00:00
|
|
|
|
2011-11-30 14:13:48 +00:00
|
|
|
SkString title;
|
2015-01-23 13:58:07 +00:00
|
|
|
title.printf("Bitmap size: %d x %d", gBmpSize, gBmpSize);
|
2019-01-07 14:31:58 +00:00
|
|
|
canvas->drawString(title, 0, titleHeight, font, blackPaint);
|
2011-11-30 14:13:48 +00:00
|
|
|
|
|
|
|
canvas->translate(0, SK_Scalar1 * kPadY / 2 + titleHeight);
|
|
|
|
int rowCount = 0;
|
|
|
|
canvas->save();
|
|
|
|
for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
|
|
|
|
for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
|
|
|
|
|
2015-01-23 13:58:07 +00:00
|
|
|
SkIRect srcRect = SkIRect::MakeXYWH((gBmpSize - w) / 2, (gBmpSize - h) / 2, w, h);
|
2021-01-24 02:14:47 +00:00
|
|
|
fProc(canvas, fImage.get(), fLargeBitmap, srcRect, dstRect, SkSamplingOptions(),
|
|
|
|
nullptr);
|
2011-11-30 14:13:48 +00:00
|
|
|
|
|
|
|
SkString label;
|
|
|
|
label.appendf("%d x %d", w, h);
|
|
|
|
blackPaint.setAntiAlias(true);
|
|
|
|
blackPaint.setStyle(SkPaint::kFill_Style);
|
2019-01-07 14:31:58 +00:00
|
|
|
font.setSize(SK_Scalar1 * 10);
|
|
|
|
SkScalar baseline = dstRect.height() + font.getSize() + SK_Scalar1 * 3;
|
|
|
|
canvas->drawString(label, 0, baseline, font, blackPaint);
|
2011-11-30 14:13:48 +00:00
|
|
|
blackPaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
blackPaint.setStrokeWidth(SK_Scalar1);
|
|
|
|
blackPaint.setAntiAlias(false);
|
|
|
|
canvas->drawRect(dstRect, blackPaint);
|
|
|
|
|
|
|
|
canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
|
|
|
|
++rowCount;
|
|
|
|
if ((dstRect.width() + kPadX) * rowCount > gSize) {
|
|
|
|
canvas->restore();
|
|
|
|
canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
|
|
|
|
canvas->save();
|
|
|
|
rowCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-25 15:52:27 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
// test the following code path:
|
|
|
|
// SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
|
|
|
|
SkIRect srcRect;
|
|
|
|
SkPaint paint;
|
2021-01-24 02:14:47 +00:00
|
|
|
SkBitmap bm = make_chessbm(5, 5);
|
2012-04-25 15:52:27 +00:00
|
|
|
|
|
|
|
srcRect.setXYWH(1, 1, 3, 3);
|
2018-03-14 17:01:17 +00:00
|
|
|
paint.setMaskFilter(SkMaskFilter::MakeBlur(
|
2014-04-28 16:25:35 +00:00
|
|
|
kNormal_SkBlurStyle,
|
2018-03-14 17:01:17 +00:00
|
|
|
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5))));
|
2016-08-26 20:04:14 +00:00
|
|
|
|
2021-01-24 02:14:47 +00:00
|
|
|
fProc(canvas, bm.asImage().get(), bm, srcRect, dstRect,
|
|
|
|
SkSamplingOptions(SkFilterMode::kLinear), &paint);
|
2012-04-25 15:52:27 +00:00
|
|
|
}
|
2011-11-30 14:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2011-11-30 14:13:48 +00:00
|
|
|
};
|
|
|
|
|
2016-08-26 20:04:14 +00:00
|
|
|
DEF_GM( return new DrawBitmapRectGM(bitmapproc , nullptr); )
|
|
|
|
DEF_GM( return new DrawBitmapRectGM(bitmapsubsetproc, "-subset"); )
|
|
|
|
DEF_GM( return new DrawBitmapRectGM(imageproc , "-imagerect"); )
|
|
|
|
DEF_GM( return new DrawBitmapRectGM(imagesubsetproc , "-imagerect-subset"); )
|