11803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org/*
21803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org * Copyright 2014 Google Inc.
31803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org *
41803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
51803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org * found in the LICENSE file.
61803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org */
71803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
81803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org#include "gm.h"
9bcbc1788b478b1e54079318ad073e8490aa66faetfarina
10bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "Resources.h"
111803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org#include "SampleCode.h"
121803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org#include "SkBlurMaskFilter.h"
131803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org#include "SkCanvas.h"
14bcbc1788b478b1e54079318ad073e8490aa66faetfarina#include "SkColorPriv.h"
151803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org#include "SkImageDecoder.h"
161803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org#include "SkRandom.h"
171803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org#include "SkStream.h"
181803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
191803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org// Intended to exercise pixel snapping observed with scaled images (and
201803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org// with non-scaled images, but for a different reason):  Bug 1145
211803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
221803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.orgclass SubpixelTranslateView : public SampleView {
231803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.orgpublic:
241803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org    SubpixelTranslateView(const char imageFilename[],
251803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org                          float horizontalVelocity,
261803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org                          float verticalVelocity)
27c846f4a96bcde1ffeaf17afc1469db6f2cbd1ad3tfarina      : fHorizontalVelocity(horizontalVelocity),
281803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        fVerticalVelocity(verticalVelocity) {
29c846f4a96bcde1ffeaf17afc1469db6f2cbd1ad3tfarina      SkString resourcePath = GetResourcePath(imageFilename);
302880df2609eba09b555ca37be04b6ad89290c765Tom Hudson      SkImageDecoder* codec = nullptr;
31bcbc1788b478b1e54079318ad073e8490aa66faetfarina      SkFILEStream stream(resourcePath.c_str());
321803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org      if (stream.isValid()) {
331803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org          codec = SkImageDecoder::Factory(&stream);
341803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org      }
351803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org      if (codec) {
361803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org          stream.rewind();
37bfefc7c95fc0e8ebd5000c68f6d16e1a3ea0e71ereed          codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
382880df2609eba09b555ca37be04b6ad89290c765Tom Hudson          delete codec;
391803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org      } else {
401803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org          fBM.allocN32Pixels(1, 1);
411803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org          *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
421803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org      }
431803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org      fCurPos = SkPoint::Make(0,0);
441803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org      fSize = 200;
451803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org    }
461803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
471803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.orgprotected:
481803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org    SkBitmap fBM;
49687a26defaa28ce1ede534bf199bbbfc92cee5a3reed@google.com    SkScalar fSize;
501803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org    float fHorizontalVelocity, fVerticalVelocity;
511803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
521803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org    SkPoint fCurPos;
531803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
541803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org    // overrides from SkEventSink
5536352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    bool onQuery(SkEvent* evt) override {
561803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        if (SampleCode::TitleQ(*evt)) {
571803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org            SampleCode::TitleR(evt, "SubpixelTranslate");
581803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org            return true;
591803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        }
601803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        return this->INHERITED::onQuery(evt);
611803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org    }
621803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
6336352bf5e38f45a70ee4f4fc132a38048d38206dmtklein    void onDrawContent(SkCanvas* canvas) override {
641803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
6593a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed        static const SkFilterQuality gQualitys[] = {
6693a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed            kNone_SkFilterQuality,
6793a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed            kLow_SkFilterQuality,
6893a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed            kMedium_SkFilterQuality,
6993a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed            kHigh_SkFilterQuality
701803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        };
711803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
721803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        SkPaint paint;
731803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        paint.setTextSize(48);
74f75a130c459596c23595d1bf0654863ab5fe3541humper        paint.setSubpixelText(true);
751803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
761803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        paint.setAntiAlias(true);
7793a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed        for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
7893a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed            paint.setFilterQuality(gQualitys[i]);
791803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org            SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY, fSize, fSize );
801803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org            canvas->drawBitmapRect( fBM, r, &paint );
811803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        }
821803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
8393a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed        canvas->drawText( "AA Scaled", strlen("AA Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize/2, paint );
841803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
851803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        paint.setAntiAlias(false);
8693a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed        for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
8793a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed            paint.setFilterQuality(gQualitys[i]);
881803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org            SkRect r = SkRect::MakeXYWH( fCurPos.fX + i * (fSize + 10), fCurPos.fY + fSize + 10, fSize, fSize );
891803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org            canvas->drawBitmapRect( fBM, r, &paint );
901803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        }
9193a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed        canvas->drawText( "Scaled", strlen("Scaled"), fCurPos.fX + SK_ARRAY_COUNT(gQualitys) * (fSize + 10), fCurPos.fY + fSize + 10 + fSize/2, paint );
921803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
931803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        paint.setAntiAlias(true);
9493a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed        for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
9593a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed            paint.setFilterQuality(gQualitys[i]);
961803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org            canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10), &paint );
971803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        }
981803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
9993a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed        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 );
1001803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
1011803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        paint.setAntiAlias(false);
10293a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed        for (size_t i = 0; i < SK_ARRAY_COUNT(gQualitys); ++i) {
10393a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed            paint.setFilterQuality(gQualitys[i]);
1041803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org            canvas->drawBitmap( fBM, fCurPos.fX + i * (fBM.width() + 10), fCurPos.fY + 2*(fSize + 10) + fBM.height() + 10, &paint );
1051803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        }
1061803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
10793a1215fe0ab007ce941c721f1fd3e9dcb5d4754reed        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 );
1081803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
1091803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
1101803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        fCurPos.fX += fHorizontalVelocity;
1111803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org        fCurPos.fY += fVerticalVelocity;
1122880df2609eba09b555ca37be04b6ad89290c765Tom Hudson        this->inval(nullptr);
1131803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org    }
1141803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
1151803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.orgprivate:
1161803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org    typedef SampleView INHERITED;
1171803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org};
1181803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
1191803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org//////////////////////////////////////////////////////////////////////////////
1201803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.org
121687a26defaa28ce1ede534bf199bbbfc92cee5a3reed@google.comstatic SkView* MyFactory() { return new SubpixelTranslateView("mandrill_256.png", .05f, .05f); }
1221803f4eb341dd0209dde8cc7ee0fa4b409fb2704commit-bot@chromium.orgstatic SkViewRegister reg(MyFactory);
123