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
|
|
|
|
2011-11-30 14:13:48 +00:00
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2013-09-06 14:16:12 +00:00
|
|
|
#include "SkBlurMask.h"
|
2012-04-25 15:52:27 +00:00
|
|
|
#include "SkBlurMaskFilter.h"
|
2013-09-06 14:16:12 +00:00
|
|
|
#include "SkColorPriv.h"
|
2011-11-30 14:13:48 +00:00
|
|
|
#include "SkGradientShader.h"
|
2015-01-23 13:58:07 +00:00
|
|
|
#include "SkImage.h"
|
2016-02-29 19:41:52 +00:00
|
|
|
#include "SkImage_Base.h"
|
2016-06-08 00:21:10 +00:00
|
|
|
#include "SkMathPriv.h"
|
2015-09-24 15:47:49 +00:00
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkSurface.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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2018-01-02 20:40:29 +00:00
|
|
|
auto surface(sk_tool_utils::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 };
|
|
|
|
|
|
|
|
SkScalar radius = 4 * SkMaxScalar(wScalar, hScalar);
|
|
|
|
|
|
|
|
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),
|
|
|
|
SkShader::kRepeat_TileMode,
|
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,
|
|
|
|
const SkRect& dstR, const SkPaint* paint) {
|
|
|
|
canvas->drawBitmapRect(bm, srcR, dstR, paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bitmapsubsetproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
|
|
|
|
const SkRect& dstR, const SkPaint* paint) {
|
|
|
|
if (!bm.bounds().contains(srcR)) {
|
|
|
|
bitmapproc(canvas, nullptr, bm, srcR, dstR, paint);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkBitmap subset;
|
|
|
|
if (bm.extractSubset(&subset, srcR)) {
|
|
|
|
canvas->drawBitmapRect(subset, dstR, paint);
|
|
|
|
}
|
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,
|
2016-08-26 20:04:14 +00:00
|
|
|
const SkRect& dstR, const SkPaint* paint) {
|
|
|
|
canvas->drawImageRect(image, srcR, dstR, paint);
|
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,
|
|
|
|
const SkIRect& srcR, const SkRect& dstR, const SkPaint* paint) {
|
|
|
|
if (!image->bounds().contains(srcR)) {
|
|
|
|
imageproc(canvas, image, bm, srcR, dstR, paint);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sk_sp<SkImage> subset = image->makeSubset(srcR)) {
|
|
|
|
canvas->drawImageRect(subset, dstR, paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&,
|
|
|
|
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 {
|
2015-09-24 15:47:49 +00:00
|
|
|
if (!fImage) {
|
|
|
|
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;
|
|
|
|
paint.setAlpha(0x20);
|
2016-07-14 16:33:42 +00:00
|
|
|
canvas->drawImageRect(fImage, SkRect::MakeIWH(gSize, gSize), &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.setTextSize(titleHeight);
|
|
|
|
blackPaint.setAntiAlias(true);
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&blackPaint);
|
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);
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(title, 0,
|
2011-11-30 14:13:48 +00:00
|
|
|
titleHeight, blackPaint);
|
|
|
|
|
|
|
|
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);
|
2016-08-26 20:04:14 +00:00
|
|
|
fProc(canvas, fImage.get(), fLargeBitmap, srcRect, dstRect, 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);
|
|
|
|
blackPaint.setTextSize(SK_Scalar1 * 10);
|
|
|
|
SkScalar baseline = dstRect.height() +
|
|
|
|
blackPaint.getTextSize() + SK_Scalar1 * 3;
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(label,
|
2011-11-30 14:13:48 +00:00
|
|
|
0, baseline,
|
|
|
|
blackPaint);
|
|
|
|
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;
|
|
|
|
SkBitmap bm;
|
|
|
|
|
|
|
|
bm = make_chessbm(5, 5);
|
2015-03-16 17:08:34 +00:00
|
|
|
paint.setFilterQuality(kLow_SkFilterQuality);
|
2012-04-25 15:52:27 +00:00
|
|
|
|
|
|
|
srcRect.setXYWH(1, 1, 3, 3);
|
2016-04-04 17:02:58 +00:00
|
|
|
paint.setMaskFilter(SkBlurMaskFilter::Make(
|
2014-04-28 16:25:35 +00:00
|
|
|
kNormal_SkBlurStyle,
|
2013-09-06 14:16:12 +00:00
|
|
|
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
|
2016-08-26 20:04:14 +00:00
|
|
|
SkBlurMaskFilter::kHighQuality_BlurFlag));
|
|
|
|
|
|
|
|
sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm));
|
|
|
|
fProc(canvas, image.get(), bm, srcRect, dstRect, &paint);
|
2012-04-25 15:52:27 +00:00
|
|
|
}
|
2011-11-30 14:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-01-23 13:58:07 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
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"); )
|