2014-05-29 22:01:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 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"
|
2014-06-18 21:32:48 +00:00
|
|
|
|
|
|
|
#include "Resources.h"
|
2014-05-29 22:01:08 +00:00
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkBlurMaskFilter.h"
|
|
|
|
#include "SkCanvas.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
#include "SkColorPriv.h"
|
2014-05-29 22:01:08 +00:00
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkRandom.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
|
|
|
|
// Intended to exercise pixel snapping observed with scaled images (and
|
|
|
|
// with non-scaled images, but for a different reason): Bug 1145
|
|
|
|
|
|
|
|
class SubpixelTranslateView : public SampleView {
|
|
|
|
public:
|
|
|
|
SubpixelTranslateView(const char imageFilename[],
|
|
|
|
float horizontalVelocity,
|
|
|
|
float verticalVelocity)
|
2014-07-01 19:35:49 +00:00
|
|
|
: fHorizontalVelocity(horizontalVelocity),
|
2014-05-29 22:01:08 +00:00
|
|
|
fVerticalVelocity(verticalVelocity) {
|
2014-07-01 19:35:49 +00:00
|
|
|
SkString resourcePath = GetResourcePath(imageFilename);
|
2014-06-18 21:32:48 +00:00
|
|
|
SkImageDecoder* codec = NULL;
|
|
|
|
SkFILEStream stream(resourcePath.c_str());
|
2014-05-29 22:01:08 +00:00
|
|
|
if (stream.isValid()) {
|
|
|
|
codec = SkImageDecoder::Factory(&stream);
|
|
|
|
}
|
|
|
|
if (codec) {
|
|
|
|
stream.rewind();
|
2014-06-13 00:40:00 +00:00
|
|
|
codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
|
2014-05-29 22:01:08 +00:00
|
|
|
SkDELETE(codec);
|
|
|
|
} else {
|
|
|
|
fBM.allocN32Pixels(1, 1);
|
|
|
|
*(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
|
|
|
|
}
|
|
|
|
fCurPos = SkPoint::Make(0,0);
|
|
|
|
fSize = 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkBitmap fBM;
|
2014-05-30 13:45:36 +00:00
|
|
|
SkScalar fSize;
|
2014-05-29 22:01:08 +00:00
|
|
|
float fHorizontalVelocity, fVerticalVelocity;
|
|
|
|
|
|
|
|
SkPoint fCurPos;
|
|
|
|
|
|
|
|
// overrides from SkEventSink
|
2015-01-09 18:06:39 +00:00
|
|
|
bool onQuery(SkEvent* evt) SK_OVERRIDE {
|
2014-05-29 22:01:08 +00:00
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, "SubpixelTranslate");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
|
|
|
|
2015-01-09 18:06:39 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
|
2014-05-29 22:01:08 +00:00
|
|
|
|
2015-03-16 17:08:34 +00:00
|
|
|
static const SkFilterQuality gQualitys[] = {
|
|
|
|
kNone_SkFilterQuality,
|
|
|
|
kLow_SkFilterQuality,
|
|
|
|
kMedium_SkFilterQuality,
|
|
|
|
kHigh_SkFilterQuality
|
2014-05-29 22:01:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setTextSize(48);
|
2015-01-29 18:26:37 +00:00
|
|
|
paint.setSubpixelText(true);
|
2014-05-29 22:01:08 +00:00
|
|
|
|
|
|
|
paint.setAntiAlias(true);
|
2015-03-16 17:08:34 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
|
|
|
|
paint.setFilterQuality(gQualitys[i]);
|
2014-05-29 22:01:08 +00:00
|
|
|
SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY, fSize, fSize );
|
|
|
|
canvas->drawBitmapRect( fBM, r, &paint );
|
|
|
|
}
|
|
|
|
|
2015-03-16 17:08:34 +00:00
|
|
|
canvas->drawText( "AA Scaled", strlen("AA Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize/2, paint );
|
2014-05-29 22:01:08 +00:00
|
|
|
|
|
|
|
paint.setAntiAlias(false);
|
2015-03-16 17:08:34 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
|
|
|
|
paint.setFilterQuality(gQualitys[i]);
|
2014-05-29 22:01:08 +00:00
|
|
|
SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize );
|
|
|
|
canvas->drawBitmapRect( fBM, r, &paint );
|
|
|
|
}
|
2015-03-16 17:08:34 +00:00
|
|
|
canvas->drawText( "Scaled", strlen("Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize + 10 + fSize/2, paint );
|
2014-05-29 22:01:08 +00:00
|
|
|
|
|
|
|
paint.setAntiAlias(true);
|
2015-03-16 17:08:34 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
|
|
|
|
paint.setFilterQuality(gQualitys[i]);
|
2014-05-29 22:01:08 +00:00
|
|
|
canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint );
|
|
|
|
}
|
|
|
|
|
2015-03-16 17:08:34 +00:00
|
|
|
canvas->drawText( "AA No Scale", strlen("AA No Scale"), fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fSize/2, paint );
|
2014-05-29 22:01:08 +00:00
|
|
|
|
|
|
|
paint.setAntiAlias(false);
|
2015-03-16 17:08:34 +00:00
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
|
|
|
|
paint.setFilterQuality(gQualitys[i]);
|
2014-05-29 22:01:08 +00:00
|
|
|
canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10, &paint );
|
|
|
|
}
|
|
|
|
|
2015-03-16 17:08:34 +00:00
|
|
|
canvas->drawText( "No Scale", strlen("No Scale"), fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10 + fSize/2, paint );
|
2014-05-29 22:01:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
fCurPos.fX += fHorizontalVelocity;
|
|
|
|
fCurPos.fY += fVerticalVelocity;
|
|
|
|
this->inval(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef SampleView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-05-30 13:45:36 +00:00
|
|
|
static SkView* MyFactory() { return new SubpixelTranslateView("mandrill_256.png", .05f, .05f); }
|
2014-05-29 22:01:08 +00:00
|
|
|
static SkViewRegister reg(MyFactory);
|