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"
|
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"
|
2015-09-24 15:47:49 +00:00
|
|
|
#include "SkShader.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "SkGr.h"
|
|
|
|
#endif
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bm.unlockPixels();
|
|
|
|
return bm;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
SkAutoTUnref<SkSurface> surface(origCanvas->newSurface(info));
|
|
|
|
if (nullptr == surface) {
|
|
|
|
// picture canvas will return null, so fall-back to raster
|
|
|
|
surface.reset(SkSurface::NewRaster(info));
|
|
|
|
}
|
2015-01-23 13:58:07 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
2016-02-29 19:41:52 +00:00
|
|
|
if (GrTexture* texture = as_IB(image)->peekTexture()) {
|
|
|
|
GrWrapTextureInBitmap(texture, image->width(), image->height(), image->isOpaque(), &tempBM);
|
2015-09-24 15:47:49 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
image->asLegacyBitmap(&tempBM, SkImage::kRO_LegacyBitmapMode);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-07-17 14:09:43 +00:00
|
|
|
static void canvasproc(SkCanvas* canvas, SkImage*, const SkBitmap& bm, const SkIRect& srcR,
|
2015-01-23 13:58:07 +00:00
|
|
|
const SkRect& dstR) {
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->drawBitmapRect(bm, srcR, dstR, nullptr);
|
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,
|
2015-01-23 13:58:07 +00:00
|
|
|
const SkRect& dstR) {
|
2015-08-27 14:41:13 +00:00
|
|
|
canvas->drawImageRect(image, srcR, dstR, nullptr);
|
2015-01-23 13:58:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-17 14:09:43 +00:00
|
|
|
typedef void DrawRectRectProc(SkCanvas*, SkImage*, const SkBitmap&, const SkIRect&, const SkRect&);
|
2015-01-23 13:58:07 +00:00
|
|
|
|
2011-11-30 14:13:48 +00:00
|
|
|
static const int gSize = 1024;
|
2015-01-23 13:58:07 +00:00
|
|
|
static const 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)};
|
2015-01-23 13:58:07 +00:00
|
|
|
static const int kMaxSrcRectSize = 1 << (SkNextLog2(gBmpSize) + 2);
|
2011-11-30 14:13:48 +00:00
|
|
|
|
|
|
|
static const int kPadX = 30;
|
|
|
|
static const int kPadY = 40;
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAlpha(0x20);
|
2015-08-06 17:02:53 +00:00
|
|
|
canvas->drawBitmapRect(fLargeBitmap, 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);
|
2011-11-30 14:13:48 +00:00
|
|
|
canvas->drawText(title.c_str(), title.size(), 0,
|
|
|
|
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-03-17 17:51:11 +00:00
|
|
|
fProc(canvas, fImage.get(), fLargeBitmap, srcRect, dstRect);
|
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;
|
|
|
|
canvas->drawText(label.c_str(), label.size(),
|
|
|
|
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);
|
|
|
|
SkMaskFilter* mf = SkBlurMaskFilter::Create(
|
2014-04-28 16:25:35 +00:00
|
|
|
kNormal_SkBlurStyle,
|
2013-09-06 14:16:12 +00:00
|
|
|
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
|
2012-04-25 15:52:27 +00:00
|
|
|
SkBlurMaskFilter::kHighQuality_BlurFlag |
|
|
|
|
SkBlurMaskFilter::kIgnoreTransform_BlurFlag);
|
|
|
|
paint.setMaskFilter(mf)->unref();
|
2015-07-17 14:09:43 +00:00
|
|
|
canvas->drawBitmapRect(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
|
|
|
};
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
DEF_GM( return new DrawBitmapRectGM(canvasproc, nullptr); )
|
2015-01-23 13:58:07 +00:00
|
|
|
DEF_GM( return new DrawBitmapRectGM(imageproc, "-imagerect"); )
|