blob: a9804ac5e41a6db55fbbe196d9f3cc03ec42d720 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.comad687ce2009-06-16 01:50:25 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkDevice.h"
12#include "SkPaint.h"
reed@android.com83f7bc32009-07-17 02:42:41 +000013#include "SkShader.h"
reed@android.comad687ce2009-06-16 01:50:25 +000014
15static SkBitmap createBitmap(int n) {
16 SkBitmap bitmap;
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000017 bitmap.allocN32Pixels(n, n);
junov@google.comdbfac8a2012-12-06 21:47:40 +000018 bitmap.eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comae933ce2012-08-23 18:19:56 +000019
reed@android.comad687ce2009-06-16 01:50:25 +000020 SkCanvas canvas(bitmap);
21 SkRect r;
22 r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n));
23 SkPaint paint;
24 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +000025
reed@android.comad687ce2009-06-16 01:50:25 +000026 paint.setColor(SK_ColorRED);
27 canvas.drawOval(r, paint);
28 paint.setColor(SK_ColorBLUE);
29 paint.setStrokeWidth(SkIntToScalar(n)/15);
30 paint.setStyle(SkPaint::kStroke_Style);
31 canvas.drawLine(0, 0, r.fRight, r.fBottom, paint);
32 canvas.drawLine(0, r.fBottom, r.fRight, 0, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000033
reed@android.comad687ce2009-06-16 01:50:25 +000034 return bitmap;
35}
36
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000037class MipMapView : public SampleView {
reed@android.comad687ce2009-06-16 01:50:25 +000038 SkBitmap fBitmap;
39 enum {
reed@android.comf2b98d62010-12-20 18:26:13 +000040 N = 64
reed@android.comad687ce2009-06-16 01:50:25 +000041 };
reed@google.com7b185902012-03-12 21:13:48 +000042 bool fOnce;
reed@android.comad687ce2009-06-16 01:50:25 +000043public:
44 MipMapView() {
reed@google.com7b185902012-03-12 21:13:48 +000045 fOnce = false;
46 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000047
reed@google.com7b185902012-03-12 21:13:48 +000048 void init() {
49 if (fOnce) {
50 return;
51 }
52 fOnce = true;
53
reed@android.comad687ce2009-06-16 01:50:25 +000054 fBitmap = createBitmap(N);
rmistry@google.comae933ce2012-08-23 18:19:56 +000055
reed@android.comad687ce2009-06-16 01:50:25 +000056 fWidth = N;
reed@android.comad687ce2009-06-16 01:50:25 +000057 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000058
reed@android.comad687ce2009-06-16 01:50:25 +000059protected:
60 // overrides from SkEventSink
61 virtual bool onQuery(SkEvent* evt) {
62 if (SampleCode::TitleQ(*evt)) {
reed@android.comf2b98d62010-12-20 18:26:13 +000063 SampleCode::TitleR(evt, "MipMaps");
reed@android.comad687ce2009-06-16 01:50:25 +000064 return true;
65 }
66 return this->INHERITED::onQuery(evt);
67 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000068
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000069 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.com7b185902012-03-12 21:13:48 +000070 this->init();
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +000071
reed@google.comd94697c2013-07-24 14:31:33 +000072 static const SkPaint::FilterLevel gLevel[] = {
73 SkPaint::kNone_FilterLevel,
74 SkPaint::kLow_FilterLevel,
75 SkPaint::kMedium_FilterLevel,
76 SkPaint::kHigh_FilterLevel,
77 };
reed@android.comad687ce2009-06-16 01:50:25 +000078
79 SkPaint paint;
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +000080
reed@google.comd94697c2013-07-24 14:31:33 +000081 for (size_t i = 0; i < SK_ARRAY_COUNT(gLevel); ++i) {
reed@google.comd9763a52013-07-24 17:10:05 +000082 SkScalar x = 10.0f + i * 100;
83 SkScalar y = 10.0f;
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +000084
reed@google.comd94697c2013-07-24 14:31:33 +000085 paint.setFilterLevel(gLevel[i]);
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +000086
reed@google.comd94697c2013-07-24 14:31:33 +000087 canvas->drawBitmap(fBitmap, x, y, &paint);
88 }
reed@android.comad687ce2009-06-16 01:50:25 +000089 this->inval(NULL);
90 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000091
reed@android.comad687ce2009-06-16 01:50:25 +000092private:
reed@android.com44177402009-11-23 21:07:51 +000093 int fWidth;
reed@android.comad687ce2009-06-16 01:50:25 +000094
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000095 typedef SampleView INHERITED;
reed@android.comad687ce2009-06-16 01:50:25 +000096};
97
98//////////////////////////////////////////////////////////////////////////////
99
100static SkView* MyFactory() { return new MipMapView; }
101static SkViewRegister reg(MyFactory);