2013-07-25 21:34:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
2013-09-06 14:16:12 +00:00
|
|
|
#include "SkBlurMask.h"
|
2013-08-20 16:51:20 +00:00
|
|
|
#include "SkBlurMaskFilter.h"
|
2013-09-06 14:16:12 +00:00
|
|
|
#include "SkCanvas.h"
|
2013-07-25 21:34:00 +00:00
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrContext.h"
|
|
|
|
#endif
|
|
|
|
|
2013-11-08 15:09:22 +00:00
|
|
|
// Create a black&white checked texture with 2 1-pixel rings
|
|
|
|
// around the outside edge. The inner ring is red and the outer ring is blue.
|
|
|
|
static void make_ringed_bitmap(SkBitmap* result, int width, int height) {
|
|
|
|
SkASSERT(0 == width % 2 && 0 == height % 2);
|
2013-11-09 07:02:23 +00:00
|
|
|
|
2013-11-08 15:09:22 +00:00
|
|
|
static const SkPMColor kRed = SkPreMultiplyColor(SK_ColorRED);
|
|
|
|
static const SkPMColor kBlue = SkPreMultiplyColor(SK_ColorBLUE);
|
|
|
|
static const SkPMColor kBlack = SkPreMultiplyColor(SK_ColorBLACK);
|
|
|
|
static const SkPMColor kWhite = SkPreMultiplyColor(SK_ColorWHITE);
|
2013-07-25 21:34:00 +00:00
|
|
|
|
2013-10-21 14:00:07 +00:00
|
|
|
result->setConfig(SkBitmap::kARGB_8888_Config, width, height, 0,
|
|
|
|
kOpaque_SkAlphaType);
|
2013-07-25 21:34:00 +00:00
|
|
|
result->allocPixels();
|
|
|
|
SkAutoLockPixels lock(*result);
|
|
|
|
|
2013-07-26 07:00:58 +00:00
|
|
|
SkPMColor* scanline = result->getAddr32(0, 0);
|
2013-07-25 21:34:00 +00:00
|
|
|
for (int x = 0; x < width; ++x) {
|
2013-11-08 15:09:22 +00:00
|
|
|
scanline[x] = kBlue;
|
2013-07-25 21:34:00 +00:00
|
|
|
}
|
2013-11-08 15:09:22 +00:00
|
|
|
scanline = result->getAddr32(0, 1);
|
|
|
|
scanline[0] = kBlue;
|
|
|
|
for (int x = 1; x < width - 1; ++x) {
|
|
|
|
scanline[x] = kRed;
|
|
|
|
}
|
|
|
|
scanline[width-1] = kBlue;
|
2013-07-25 21:34:00 +00:00
|
|
|
|
2013-11-08 15:09:22 +00:00
|
|
|
for (int y = 2; y < height/2; ++y) {
|
2013-07-25 21:34:00 +00:00
|
|
|
scanline = result->getAddr32(0, y);
|
2013-11-08 15:09:22 +00:00
|
|
|
scanline[0] = kBlue;
|
|
|
|
scanline[1] = kRed;
|
|
|
|
for (int x = 2; x < width/2; ++x) {
|
|
|
|
scanline[x] = kBlack;
|
2013-07-25 21:34:00 +00:00
|
|
|
}
|
2013-11-08 15:09:22 +00:00
|
|
|
for (int x = width/2; x < width-2; ++x) {
|
|
|
|
scanline[x] = kWhite;
|
2013-07-25 21:34:00 +00:00
|
|
|
}
|
2013-11-08 15:09:22 +00:00
|
|
|
scanline[width-2] = kRed;
|
|
|
|
scanline[width-1] = kBlue;
|
2013-07-25 21:34:00 +00:00
|
|
|
}
|
|
|
|
|
2013-11-08 15:09:22 +00:00
|
|
|
for (int y = height/2; y < height-2; ++y) {
|
2013-07-25 21:34:00 +00:00
|
|
|
scanline = result->getAddr32(0, y);
|
2013-11-08 15:09:22 +00:00
|
|
|
scanline[0] = kBlue;
|
|
|
|
scanline[1] = kRed;
|
|
|
|
for (int x = 2; x < width/2; ++x) {
|
|
|
|
scanline[x] = kWhite;
|
2013-07-25 21:34:00 +00:00
|
|
|
}
|
2013-11-08 15:09:22 +00:00
|
|
|
for (int x = width/2; x < width-2; ++x) {
|
|
|
|
scanline[x] = kBlack;
|
2013-07-25 21:34:00 +00:00
|
|
|
}
|
2013-11-08 15:09:22 +00:00
|
|
|
scanline[width-2] = kRed;
|
|
|
|
scanline[width-1] = kBlue;
|
|
|
|
}
|
|
|
|
|
|
|
|
scanline = result->getAddr32(0, height-2);
|
|
|
|
scanline[0] = kBlue;
|
|
|
|
for (int x = 1; x < width - 1; ++x) {
|
|
|
|
scanline[x] = kRed;
|
2013-07-25 21:34:00 +00:00
|
|
|
}
|
2013-11-08 15:09:22 +00:00
|
|
|
scanline[width-1] = kBlue;
|
2013-07-25 21:34:00 +00:00
|
|
|
|
|
|
|
scanline = result->getAddr32(0, height-1);
|
|
|
|
for (int x = 0; x < width; ++x) {
|
2013-11-08 15:09:22 +00:00
|
|
|
scanline[x] = kBlue;
|
2013-07-25 21:34:00 +00:00
|
|
|
}
|
|
|
|
result->setImmutable();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This GM exercises the drawBitmapRectToRect "bleed" flag
|
|
|
|
class BleedGM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
BleedGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual SkString onShortName() SK_OVERRIDE {
|
|
|
|
return SkString("bleed");
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SkISize onISize() SK_OVERRIDE {
|
|
|
|
return SkISize::Make(kWidth, kHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void onOnceBeforeDraw() SK_OVERRIDE {
|
2013-11-08 15:09:22 +00:00
|
|
|
make_ringed_bitmap(&fBitmapSmall, kSmallTextureSize, kSmallTextureSize);
|
2013-07-25 21:34:00 +00:00
|
|
|
|
|
|
|
// To exercise the GPU's tiling path we need a texture
|
|
|
|
// too big for the GPU to handle in one go
|
2013-11-08 15:09:22 +00:00
|
|
|
make_ringed_bitmap(&fBitmapBig, 2*kMaxTextureSize, 2*kMaxTextureSize);
|
2013-07-25 21:34:00 +00:00
|
|
|
}
|
|
|
|
|
2013-08-20 12:09:32 +00:00
|
|
|
// Draw only the center of the small bitmap
|
2013-08-21 07:01:29 +00:00
|
|
|
void drawCase1(SkCanvas* canvas, int transX, int transY,
|
2013-11-08 15:09:22 +00:00
|
|
|
SkCanvas::DrawBitmapRectFlags flags, SkPaint::FilterLevel filter) {
|
|
|
|
SkRect src = SkRect::MakeXYWH(2, 2,
|
|
|
|
kSmallTextureSize-4,
|
|
|
|
kSmallTextureSize-4);
|
2013-08-20 12:09:32 +00:00
|
|
|
SkRect dst = SkRect::MakeXYWH(0, 0, SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
|
2013-07-26 07:00:58 +00:00
|
|
|
|
2013-08-20 12:09:32 +00:00
|
|
|
SkPaint paint;
|
2013-11-08 15:09:22 +00:00
|
|
|
paint.setFilterLevel(filter);
|
2013-08-20 12:09:32 +00:00
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(transX), SkIntToScalar(transY));
|
|
|
|
canvas->drawBitmapRectToRect(fBitmapSmall, &src, dst, &paint, flags);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw almost all of the large bitmap
|
2013-08-21 07:01:29 +00:00
|
|
|
void drawCase2(SkCanvas* canvas, int transX, int transY,
|
2013-11-08 15:09:22 +00:00
|
|
|
SkCanvas::DrawBitmapRectFlags flags, SkPaint::FilterLevel filter) {
|
|
|
|
SkRect src = SkRect::MakeXYWH(2, 2,
|
|
|
|
SkIntToScalar(fBitmapBig.width()-4),
|
|
|
|
SkIntToScalar(fBitmapBig.height()-4));
|
2013-08-20 12:09:32 +00:00
|
|
|
SkRect dst = SkRect::MakeXYWH(0, 0, SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
|
2013-07-25 21:34:00 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
2013-11-08 15:09:22 +00:00
|
|
|
paint.setFilterLevel(filter);
|
2013-07-25 21:34:00 +00:00
|
|
|
|
2013-08-20 12:09:32 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(transX), SkIntToScalar(transY));
|
|
|
|
canvas->drawBitmapRectToRect(fBitmapBig, &src, dst, &paint, flags);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2013-07-25 21:34:00 +00:00
|
|
|
|
2013-08-20 12:09:32 +00:00
|
|
|
// Draw ~1/4 of the large bitmap
|
2013-08-21 07:01:29 +00:00
|
|
|
void drawCase3(SkCanvas* canvas, int transX, int transY,
|
2013-11-08 15:09:22 +00:00
|
|
|
SkCanvas::DrawBitmapRectFlags flags, SkPaint::FilterLevel filter) {
|
|
|
|
SkRect src = SkRect::MakeXYWH(2, 2,
|
|
|
|
SkIntToScalar(fBitmapBig.width()/2-2),
|
|
|
|
SkIntToScalar(fBitmapBig.height()/2-2));
|
2013-08-20 12:09:32 +00:00
|
|
|
SkRect dst = SkRect::MakeXYWH(0, 0, SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
|
2013-07-25 21:34:00 +00:00
|
|
|
|
2013-08-20 12:09:32 +00:00
|
|
|
SkPaint paint;
|
2013-11-08 15:09:22 +00:00
|
|
|
paint.setFilterLevel(filter);
|
2013-07-25 21:34:00 +00:00
|
|
|
|
2013-08-20 12:09:32 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(transX), SkIntToScalar(transY));
|
|
|
|
canvas->drawBitmapRectToRect(fBitmapBig, &src, dst, &paint, flags);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2013-08-20 16:51:20 +00:00
|
|
|
// Draw the center of the small bitmap with a mask filter
|
2013-08-21 07:01:29 +00:00
|
|
|
void drawCase4(SkCanvas* canvas, int transX, int transY,
|
2013-11-08 15:09:22 +00:00
|
|
|
SkCanvas::DrawBitmapRectFlags flags, SkPaint::FilterLevel filter) {
|
|
|
|
SkRect src = SkRect::MakeXYWH(2, 2,
|
|
|
|
kSmallTextureSize-4,
|
|
|
|
kSmallTextureSize-4);
|
2013-08-20 16:51:20 +00:00
|
|
|
SkRect dst = SkRect::MakeXYWH(0, 0, SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
|
|
|
|
|
|
|
|
SkPaint paint;
|
2013-11-08 15:09:22 +00:00
|
|
|
paint.setFilterLevel(filter);
|
2013-09-06 14:16:12 +00:00
|
|
|
SkMaskFilter* mf = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
|
|
|
|
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3)));
|
2013-08-20 16:51:20 +00:00
|
|
|
paint.setMaskFilter(mf)->unref();
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(transX), SkIntToScalar(transY));
|
|
|
|
canvas->drawBitmapRectToRect(fBitmapSmall, &src, dst, &paint, flags);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2013-08-20 12:09:32 +00:00
|
|
|
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
|
2013-07-25 21:34:00 +00:00
|
|
|
|
2013-08-20 12:09:32 +00:00
|
|
|
canvas->clear(SK_ColorGRAY);
|
2013-07-25 21:34:00 +00:00
|
|
|
|
2013-11-08 15:09:22 +00:00
|
|
|
// Currently there are no test cases with medium filtering since medium uses mip-mapping and
|
|
|
|
// these draws are always upscaling.
|
2013-11-09 07:02:23 +00:00
|
|
|
|
2013-08-20 12:09:32 +00:00
|
|
|
// First draw a column with no bleeding, tiling, or filtering
|
2013-11-08 15:09:22 +00:00
|
|
|
this->drawCase1(canvas, kCol0X, kRow0Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kNone_FilterLevel);
|
|
|
|
this->drawCase2(canvas, kCol0X, kRow1Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kNone_FilterLevel);
|
|
|
|
this->drawCase3(canvas, kCol0X, kRow2Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kNone_FilterLevel);
|
|
|
|
this->drawCase4(canvas, kCol0X, kRow3Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kNone_FilterLevel);
|
|
|
|
|
|
|
|
// Then draw a column with no bleeding or tiling but with low filtering
|
|
|
|
this->drawCase1(canvas, kCol1X, kRow0Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
|
|
|
|
this->drawCase2(canvas, kCol1X, kRow1Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
|
|
|
|
this->drawCase3(canvas, kCol1X, kRow2Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
|
|
|
|
this->drawCase4(canvas, kCol1X, kRow3Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
|
|
|
|
|
|
|
|
// Then draw a column with no bleeding or tiling but with high filtering
|
|
|
|
this->drawCase1(canvas, kCol2X, kRow0Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
|
|
|
|
this->drawCase2(canvas, kCol2X, kRow1Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
|
|
|
|
this->drawCase3(canvas, kCol2X, kRow2Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
|
|
|
|
this->drawCase4(canvas, kCol2X, kRow3Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
|
2013-11-09 07:02:23 +00:00
|
|
|
|
2013-08-20 12:09:32 +00:00
|
|
|
#if SK_SUPPORT_GPU
|
2013-11-21 06:21:58 +00:00
|
|
|
GrContext* ctx = canvas->getGrContext();
|
2013-08-20 12:09:32 +00:00
|
|
|
int oldMaxTextureSize = 0;
|
|
|
|
if (NULL != ctx) {
|
|
|
|
// shrink the max texture size so all our textures can be reasonably sized
|
|
|
|
oldMaxTextureSize = ctx->getMaxTextureSize();
|
|
|
|
ctx->setMaxTextureSizeOverride(kMaxTextureSize);
|
|
|
|
}
|
|
|
|
#endif
|
2013-07-25 21:34:00 +00:00
|
|
|
|
2013-11-08 15:09:22 +00:00
|
|
|
// Then draw a column with no bleeding but with tiling and low filtering
|
|
|
|
this->drawCase1(canvas, kCol3X, kRow0Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
|
|
|
|
this->drawCase2(canvas, kCol3X, kRow1Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
|
|
|
|
this->drawCase3(canvas, kCol3X, kRow2Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
|
|
|
|
this->drawCase4(canvas, kCol3X, kRow3Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
|
|
|
|
|
|
|
|
// Then draw a column with no bleeding but with tiling and high filtering
|
|
|
|
this->drawCase1(canvas, kCol4X, kRow0Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
|
|
|
|
this->drawCase2(canvas, kCol4X, kRow1Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
|
|
|
|
this->drawCase3(canvas, kCol4X, kRow2Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
|
|
|
|
this->drawCase4(canvas, kCol4X, kRow3Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
|
2013-11-09 07:02:23 +00:00
|
|
|
|
2013-11-08 15:09:22 +00:00
|
|
|
// Then draw a column with bleeding, tiling, and low filtering
|
|
|
|
this->drawCase1(canvas, kCol5X, kRow0Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
|
|
|
|
this->drawCase2(canvas, kCol5X, kRow1Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
|
|
|
|
this->drawCase3(canvas, kCol5X, kRow2Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
|
|
|
|
this->drawCase4(canvas, kCol5X, kRow3Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
|
2013-11-09 07:02:23 +00:00
|
|
|
|
2013-11-08 15:09:22 +00:00
|
|
|
// Finally draw a column with bleeding, tiling, and high filtering
|
|
|
|
this->drawCase1(canvas, kCol6X, kRow0Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
|
|
|
|
this->drawCase2(canvas, kCol6X, kRow1Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
|
|
|
|
this->drawCase3(canvas, kCol6X, kRow2Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
|
|
|
|
this->drawCase4(canvas, kCol6X, kRow3Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
|
2013-07-25 21:34:00 +00:00
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
if (NULL != ctx) {
|
|
|
|
ctx->setMaxTextureSizeOverride(oldMaxTextureSize);
|
2013-07-26 07:00:58 +00:00
|
|
|
}
|
2013-07-25 21:34:00 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-11-08 15:09:22 +00:00
|
|
|
static const int kBlockSize = 70;
|
|
|
|
static const int kBlockSpacing = 5;
|
2013-08-20 12:09:32 +00:00
|
|
|
|
|
|
|
static const int kCol0X = kBlockSpacing;
|
|
|
|
static const int kCol1X = 2*kBlockSpacing + kBlockSize;
|
|
|
|
static const int kCol2X = 3*kBlockSpacing + 2*kBlockSize;
|
|
|
|
static const int kCol3X = 4*kBlockSpacing + 3*kBlockSize;
|
2013-11-08 15:09:22 +00:00
|
|
|
static const int kCol4X = 5*kBlockSpacing + 4*kBlockSize;
|
|
|
|
static const int kCol5X = 6*kBlockSpacing + 5*kBlockSize;
|
|
|
|
static const int kCol6X = 7*kBlockSpacing + 6*kBlockSize;
|
|
|
|
static const int kWidth = 8*kBlockSpacing + 7*kBlockSize;
|
2013-08-20 12:09:32 +00:00
|
|
|
|
|
|
|
static const int kRow0Y = kBlockSpacing;
|
|
|
|
static const int kRow1Y = 2*kBlockSpacing + kBlockSize;
|
|
|
|
static const int kRow2Y = 3*kBlockSpacing + 2*kBlockSize;
|
2013-08-20 16:51:20 +00:00
|
|
|
static const int kRow3Y = 4*kBlockSpacing + 3*kBlockSize;
|
|
|
|
static const int kHeight = 5*kBlockSpacing + 4*kBlockSize;
|
2013-07-25 21:34:00 +00:00
|
|
|
|
2013-11-08 15:09:22 +00:00
|
|
|
static const int kSmallTextureSize = 6;
|
2013-07-25 21:34:00 +00:00
|
|
|
static const int kMaxTextureSize = 32;
|
|
|
|
|
|
|
|
SkBitmap fBitmapSmall;
|
|
|
|
SkBitmap fBitmapBig;
|
|
|
|
|
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM( return new BleedGM(); )
|