2012-09-18 15:14:33 +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.
|
|
|
|
*/
|
2016-03-09 17:50:50 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkBlendMode.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkShader.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2021-01-13 18:14:23 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
2019-05-01 14:59:30 +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"
|
2022-04-20 18:57:03 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2012-09-18 15:14:33 +00:00
|
|
|
|
2022-04-20 18:57:03 +00:00
|
|
|
static sk_sp<SkImage> make_image(SkCanvas* destCanvas) {
|
2021-01-13 18:14:23 +00:00
|
|
|
auto surf = SkSurface::MakeRasterN32Premul(64, 64);
|
2022-04-20 18:57:03 +00:00
|
|
|
auto tmpCanvas = surf->getCanvas();
|
2012-09-18 15:14:33 +00:00
|
|
|
|
2022-04-20 18:57:03 +00:00
|
|
|
tmpCanvas->drawColor(SK_ColorRED);
|
2012-09-18 15:14:33 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
|
|
|
const SkPoint pts[] = { { 0, 0 }, { 64, 64 } };
|
|
|
|
const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
|
2019-04-03 14:27:45 +00:00
|
|
|
paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp));
|
2022-04-20 18:57:03 +00:00
|
|
|
tmpCanvas->drawCircle(32, 32, 32, paint);
|
2021-01-13 18:14:23 +00:00
|
|
|
|
2022-04-20 18:57:03 +00:00
|
|
|
return ToolUtils::MakeTextureImage(destCanvas, surf->makeImageSnapshot());
|
2012-09-18 15:14:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class DrawBitmapRect2 : public skiagm::GM {
|
|
|
|
bool fUseIRect;
|
|
|
|
public:
|
|
|
|
DrawBitmapRect2(bool useIRect) : fUseIRect(useIRect) {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2012-09-18 15:14:33 +00:00
|
|
|
SkString str;
|
|
|
|
str.printf("bitmaprect_%s", fUseIRect ? "i" : "s");
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2012-09-18 15:14:33 +00:00
|
|
|
return SkISize::Make(640, 480);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2018-08-16 14:17:03 +00:00
|
|
|
canvas->drawColor(0xFFCCCCCC);
|
2012-09-18 15:14:33 +00:00
|
|
|
|
|
|
|
const SkIRect src[] = {
|
|
|
|
{ 0, 0, 32, 32 },
|
|
|
|
{ 0, 0, 80, 80 },
|
|
|
|
{ 32, 32, 96, 96 },
|
|
|
|
{ -32, -32, 32, 32, }
|
|
|
|
};
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setStyle(SkPaint::kStroke_Style);
|
2021-01-22 03:25:21 +00:00
|
|
|
auto sampling = SkSamplingOptions();
|
2012-09-18 15:14:33 +00:00
|
|
|
|
2022-04-20 18:57:03 +00:00
|
|
|
auto image = make_image(canvas);
|
2012-09-18 15:14:33 +00:00
|
|
|
|
|
|
|
SkRect dstR = { 0, 200, 128, 380 };
|
|
|
|
|
|
|
|
canvas->translate(16, 40);
|
2022-06-21 13:49:17 +00:00
|
|
|
for (size_t i = 0; i < std::size(src); i++) {
|
2012-09-18 15:14:33 +00:00
|
|
|
SkRect srcR;
|
|
|
|
srcR.set(src[i]);
|
|
|
|
|
2021-01-22 03:25:21 +00:00
|
|
|
canvas->drawImage(image, 0, 0, sampling, &paint);
|
2012-09-26 13:10:19 +00:00
|
|
|
if (!fUseIRect) {
|
2021-01-22 03:25:21 +00:00
|
|
|
canvas->drawImageRect(image.get(), srcR, dstR, sampling, &paint,
|
2021-01-13 18:14:23 +00:00
|
|
|
SkCanvas::kStrict_SrcRectConstraint);
|
2012-09-18 15:14:33 +00:00
|
|
|
} else {
|
2021-01-22 03:25:21 +00:00
|
|
|
canvas->drawImageRect(image.get(), SkRect::Make(src[i]), dstR, sampling, &paint,
|
|
|
|
SkCanvas::kStrict_SrcRectConstraint);
|
2012-09-18 15:14:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
canvas->drawRect(dstR, paint);
|
|
|
|
canvas->drawRect(srcR, paint);
|
|
|
|
|
|
|
|
canvas->translate(160, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2012-09-18 15:14:33 +00:00
|
|
|
};
|
|
|
|
|
2012-09-26 13:10:19 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-01-27 15:26:51 +00:00
|
|
|
static void make_3x3_bitmap(SkBitmap* bitmap) {
|
|
|
|
const int xSize = 3;
|
|
|
|
const int ySize = 3;
|
2012-09-26 13:10:19 +00:00
|
|
|
|
2015-01-27 15:26:51 +00:00
|
|
|
const SkColor textureData[xSize][ySize] = {
|
2012-12-03 17:35:19 +00:00
|
|
|
{ SK_ColorRED, SK_ColorWHITE, SK_ColorBLUE },
|
|
|
|
{ SK_ColorGREEN, SK_ColorBLACK, SK_ColorCYAN },
|
|
|
|
{ SK_ColorYELLOW, SK_ColorGRAY, SK_ColorMAGENTA }
|
2012-09-26 13:10:19 +00:00
|
|
|
};
|
|
|
|
|
2015-01-27 15:26:51 +00:00
|
|
|
bitmap->allocN32Pixels(xSize, ySize, true);
|
|
|
|
SkCanvas canvas(*bitmap);
|
|
|
|
SkPaint paint;
|
2012-09-26 13:10:19 +00:00
|
|
|
|
2015-01-27 15:26:51 +00:00
|
|
|
for (int y = 0; y < ySize; y++) {
|
|
|
|
for (int x = 0; x < xSize; x++) {
|
|
|
|
paint.setColor(textureData[x][y]);
|
|
|
|
canvas.drawIRect(SkIRect::MakeXYWH(x, y, 1, 1), paint);
|
2012-09-26 13:10:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-14 17:54:12 +00:00
|
|
|
// This GM attempts to make visible any issues drawBitmapRect may have
|
2012-09-26 13:10:19 +00:00
|
|
|
// with partial source rects. In this case the eight pixels on the border
|
|
|
|
// should be half the width/height of the central pixel, i.e.:
|
|
|
|
// __|____|__
|
|
|
|
// | |
|
|
|
|
// __|____|__
|
|
|
|
// | |
|
|
|
|
class DrawBitmapRect3 : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
DrawBitmapRect3() {
|
|
|
|
this->setBGColor(SK_ColorBLACK);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2012-09-26 13:10:19 +00:00
|
|
|
SkString str;
|
|
|
|
str.printf("3x3bitmaprect");
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2012-09-26 13:10:19 +00:00
|
|
|
return SkISize::Make(640, 480);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2012-09-26 13:10:19 +00:00
|
|
|
|
|
|
|
SkBitmap bitmap;
|
|
|
|
make_3x3_bitmap(&bitmap);
|
|
|
|
|
|
|
|
SkRect srcR = { 0.5f, 0.5f, 2.5f, 2.5f };
|
|
|
|
SkRect dstR = { 100, 100, 300, 200 };
|
|
|
|
|
2022-04-20 18:57:03 +00:00
|
|
|
canvas->drawImageRect(ToolUtils::MakeTextureImage(canvas, bitmap.asImage()),
|
|
|
|
srcR, dstR, SkSamplingOptions(),
|
2021-01-24 03:07:05 +00:00
|
|
|
nullptr, SkCanvas::kStrict_SrcRectConstraint);
|
2012-09-26 13:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2012-09-26 13:10:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
2022-04-20 18:57:03 +00:00
|
|
|
static sk_sp<SkImage> make_big_bitmap(SkCanvas* canvas) {
|
2012-09-26 13:10:19 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr int gXSize = 4096;
|
|
|
|
constexpr int gYSize = 4096;
|
|
|
|
constexpr int gBorderWidth = 10;
|
2012-09-26 13:10:19 +00:00
|
|
|
|
2021-01-24 03:07:05 +00:00
|
|
|
SkBitmap bitmap;
|
|
|
|
bitmap.allocN32Pixels(gXSize, gYSize);
|
2012-09-26 13:10:19 +00:00
|
|
|
for (int y = 0; y < gYSize; ++y) {
|
|
|
|
for (int x = 0; x < gXSize; ++x) {
|
2012-09-27 02:01:04 +00:00
|
|
|
if (x <= gBorderWidth || x >= gXSize-gBorderWidth ||
|
2012-09-26 13:10:19 +00:00
|
|
|
y <= gBorderWidth || y >= gYSize-gBorderWidth) {
|
2021-01-24 03:07:05 +00:00
|
|
|
*bitmap.getAddr32(x, y) = SkPreMultiplyColor(0x88FFFFFF);
|
2012-09-26 13:10:19 +00:00
|
|
|
} else {
|
2021-01-24 03:07:05 +00:00
|
|
|
*bitmap.getAddr32(x, y) = SkPreMultiplyColor(0x88FF0000);
|
2012-09-26 13:10:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-24 03:07:05 +00:00
|
|
|
bitmap.setImmutable();
|
2022-04-20 18:57:03 +00:00
|
|
|
return ToolUtils::MakeTextureImage(canvas, bitmap.asImage());
|
2012-09-26 13:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This GM attempts to reveal any issues we may have when the GPU has to
|
|
|
|
// break up a large texture in order to draw it. The XOR transfer mode will
|
|
|
|
// create stripes in the image if there is imprecision in the destination
|
|
|
|
// tile placement.
|
|
|
|
class DrawBitmapRect4 : public skiagm::GM {
|
|
|
|
bool fUseIRect;
|
2021-01-24 03:07:05 +00:00
|
|
|
sk_sp<SkImage> fBigImage;
|
2014-05-30 15:06:24 +00:00
|
|
|
|
2012-09-26 13:10:19 +00:00
|
|
|
public:
|
|
|
|
DrawBitmapRect4(bool useIRect) : fUseIRect(useIRect) {
|
|
|
|
this->setBGColor(0x88444444);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2012-09-26 13:10:19 +00:00
|
|
|
SkString str;
|
|
|
|
str.printf("bigbitmaprect_%s", fUseIRect ? "i" : "s");
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2012-09-26 13:10:19 +00:00
|
|
|
return SkISize::Make(640, 480);
|
|
|
|
}
|
|
|
|
|
2022-04-19 20:55:02 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2022-04-20 18:57:03 +00:00
|
|
|
if (!fBigImage) {
|
|
|
|
fBigImage = make_big_bitmap(canvas);
|
|
|
|
}
|
|
|
|
|
2012-09-26 13:10:19 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAlpha(128);
|
2016-10-06 00:33:02 +00:00
|
|
|
paint.setBlendMode(SkBlendMode::kXor);
|
2021-01-24 03:07:05 +00:00
|
|
|
SkSamplingOptions sampling;
|
2012-09-26 13:10:19 +00:00
|
|
|
|
2012-10-01 14:32:51 +00:00
|
|
|
SkRect srcR1 = { 0.0f, 0.0f, 4096.0f, 2040.0f };
|
|
|
|
SkRect dstR1 = { 10.1f, 10.1f, 629.9f, 400.9f };
|
|
|
|
|
|
|
|
SkRect srcR2 = { 4085.0f, 10.0f, 4087.0f, 12.0f };
|
|
|
|
SkRect dstR2 = { 10, 410, 30, 430 };
|
2012-09-26 13:10:19 +00:00
|
|
|
|
|
|
|
if (!fUseIRect) {
|
2021-01-24 03:07:05 +00:00
|
|
|
canvas->drawImageRect(fBigImage, srcR1, dstR1, sampling, &paint,
|
2015-07-17 14:09:43 +00:00
|
|
|
SkCanvas::kStrict_SrcRectConstraint);
|
2021-01-24 03:07:05 +00:00
|
|
|
canvas->drawImageRect(fBigImage, srcR2, dstR2, sampling, &paint,
|
2015-07-17 14:09:43 +00:00
|
|
|
SkCanvas::kStrict_SrcRectConstraint);
|
2012-09-26 13:10:19 +00:00
|
|
|
} else {
|
2021-01-24 03:07:05 +00:00
|
|
|
canvas->drawImageRect(fBigImage, SkRect::Make(srcR1.roundOut()), dstR1, sampling,
|
|
|
|
&paint, SkCanvas::kStrict_SrcRectConstraint);
|
|
|
|
canvas->drawImageRect(fBigImage, SkRect::Make(srcR2.roundOut()), dstR2, sampling,
|
|
|
|
&paint, SkCanvas::kStrict_SrcRectConstraint);
|
2012-09-26 13:10:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2012-09-26 13:10:19 +00:00
|
|
|
};
|
|
|
|
|
2014-12-01 21:54:01 +00:00
|
|
|
class BitmapRectRounding : public skiagm::GM {
|
|
|
|
SkBitmap fBM;
|
|
|
|
|
|
|
|
public:
|
|
|
|
BitmapRectRounding() {}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2014-12-01 21:54:01 +00:00
|
|
|
SkString str;
|
|
|
|
str.printf("bitmaprect_rounding");
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2014-12-01 21:54:01 +00:00
|
|
|
return SkISize::Make(640, 480);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2014-12-01 21:54:01 +00:00
|
|
|
fBM.allocN32Pixels(10, 10);
|
|
|
|
fBM.eraseColor(SK_ColorBLUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This choice of coordinates and matrix land the bottom edge of the clip (and bitmap dst)
|
|
|
|
// at exactly 1/2 pixel boundary. However, drawBitmapRect may lose precision along the way.
|
|
|
|
// If it does, we may see a red-line at the bottom, instead of the bitmap exactly matching
|
|
|
|
// the clip (in which case we should see all blue).
|
|
|
|
// The correct image should be all blue.
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-12-01 21:54:01 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
|
|
|
|
const SkRect r = SkRect::MakeXYWH(1, 1, 110, 114);
|
|
|
|
canvas->scale(0.9f, 0.9f);
|
|
|
|
|
|
|
|
// the drawRect shows the same problem as clipRect(r) followed by drawcolor(red)
|
|
|
|
canvas->drawRect(r, paint);
|
2021-01-24 03:07:05 +00:00
|
|
|
canvas->drawImageRect(fBM.asImage(), r, SkSamplingOptions());
|
2014-12-01 21:54:01 +00:00
|
|
|
}
|
2015-01-23 19:07:07 +00:00
|
|
|
|
2014-12-01 21:54:01 +00:00
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2014-12-01 21:54:01 +00:00
|
|
|
};
|
2014-12-15 21:42:51 +00:00
|
|
|
DEF_GM( return new BitmapRectRounding; )
|
2014-12-01 21:54:01 +00:00
|
|
|
|
2012-09-18 15:14:33 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-12-10 21:31:59 +00:00
|
|
|
DEF_GM( return new DrawBitmapRect2(false); )
|
|
|
|
DEF_GM( return new DrawBitmapRect2(true); )
|
|
|
|
DEF_GM( return new DrawBitmapRect3(); )
|
2012-09-26 13:10:19 +00:00
|
|
|
|
2012-09-26 15:28:04 +00:00
|
|
|
#ifndef SK_BUILD_FOR_ANDROID
|
2015-12-10 21:31:59 +00:00
|
|
|
DEF_GM( return new DrawBitmapRect4(false); )
|
|
|
|
DEF_GM( return new DrawBitmapRect4(true); )
|
2012-09-26 15:28:04 +00:00
|
|
|
#endif
|