11cab2921ab279367f8206cdadc9259d12e603548Derek Sollenberger
21cab2921ab279367f8206cdadc9259d12e603548Derek Sollenberger/*
31cab2921ab279367f8206cdadc9259d12e603548Derek Sollenberger * Copyright 2011 Google Inc.
41cab2921ab279367f8206cdadc9259d12e603548Derek Sollenberger *
51cab2921ab279367f8206cdadc9259d12e603548Derek Sollenberger * Use of this source code is governed by a BSD-style license that can be
61cab2921ab279367f8206cdadc9259d12e603548Derek Sollenberger * found in the LICENSE file.
71cab2921ab279367f8206cdadc9259d12e603548Derek Sollenberger */
80b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SampleCode.h"
90b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkView.h"
100b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkCanvas.h"
110b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkDevice.h"
120b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkPaint.h"
130b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger#include "SkShader.h"
140b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
150b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenbergerstatic SkBitmap createBitmap(int n) {
160b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    SkBitmap bitmap;
170b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    bitmap.setConfig(SkBitmap::kARGB_8888_Config, n, n);
180b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    bitmap.allocPixels();
190b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    bitmap.eraseColor(SK_ColorGREEN);
200b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
210b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    SkCanvas canvas(bitmap);
220b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    SkRect r;
230b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    r.set(0, 0, SkIntToScalar(n), SkIntToScalar(n));
240b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    SkPaint paint;
250b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    paint.setAntiAlias(true);
260b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
270b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    paint.setColor(SK_ColorRED);
280b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    canvas.drawOval(r, paint);
290b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    paint.setColor(SK_ColorBLUE);
300b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    paint.setStrokeWidth(SkIntToScalar(n)/15);
310b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    paint.setStyle(SkPaint::kStroke_Style);
320b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    canvas.drawLine(0, 0, r.fRight, r.fBottom, paint);
330b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    canvas.drawLine(0, r.fBottom, r.fRight, 0, paint);
340b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
350b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    return bitmap;
360b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger}
370b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
380b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenbergerclass AARectView : public SampleView {
390b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    SkBitmap fBitmap;
400b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    enum {
410b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        N = 64
420b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    };
430b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenbergerpublic:
440b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    AARectView() {
450b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        fBitmap = createBitmap(N);
460b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
470b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        fWidth = N;
480b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    }
490b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
500b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenbergerprotected:
510b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    // overrides from SkEventSink
520b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    virtual bool onQuery(SkEvent* evt) {
530b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        if (SampleCode::TitleQ(*evt)) {
540b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger            SampleCode::TitleR(evt, "AA Rects");
550b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger            return true;
560b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        }
570b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        return this->INHERITED::onQuery(evt);
580b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    }
590b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
600b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    virtual void onDrawContent(SkCanvas* canvas) {
610b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
620b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
630b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        SkPaint bluePaint;
640b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        bluePaint.setARGB(0xff, 0x0, 0x0, 0xff);
650b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        SkPaint bmpPaint;
660b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        SkShader* bmpShader = SkShader::CreateBitmapShader(fBitmap, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode);
670b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        bmpPaint.setShader(bmpShader);
680b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        bmpShader->unref();
690b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
700b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        bluePaint.setStrokeWidth(3);
710b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        bmpPaint.setStrokeWidth(3);
720b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
730b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        SkPaint paints[] = { bluePaint, bmpPaint };
740b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
750b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        SkRect rect;
760b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
770b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        SkScalar dx = SkIntToScalar(80);
780b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        SkScalar dy = SkIntToScalar(100);
790b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        SkMatrix matrix;
800b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        for (size_t p = 0; p < SK_ARRAY_COUNT(paints); ++p) {
810b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger            for (int stroke = 0; stroke < 2; ++stroke) {
820b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                paints[p].setStyle(stroke ? SkPaint::kStroke_Style : SkPaint::kFill_Style);
830b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                for (int a = 0; a < 3; ++ a) {
840b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                    paints[p].setAntiAlias(a > 0);
850b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                    paints[p].setAlpha(a > 1 ? 0x80 : 0xff);
860b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
870b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                    canvas->save();
880b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        rect = SkRect::MakeLTRB(SkFloatToScalar(0.f),
890b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                SkFloatToScalar(0.f),
900b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                SkFloatToScalar(40.f),
910b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                SkFloatToScalar(40.f));
920b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->drawRect(rect, paints[p]);
930b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->translate(dx, 0);
940b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
950b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
960b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                SkFloatToScalar(0.5f),
970b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                SkFloatToScalar(40.5f),
980b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                SkFloatToScalar(40.5f));
990b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->drawRect(rect, paints[p]);
1000b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->translate(dx, 0);
1010b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
1020b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
1030b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                SkFloatToScalar(0.5f),
1040b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                SkFloatToScalar(40.f),
1050b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                SkFloatToScalar(40.f));
1060b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->drawRect(rect, paints[p]);
1070b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->translate(dx, 0);
1080b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
1090b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        rect = SkRect::MakeLTRB(SkFloatToScalar(0.75f),
1100b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                SkFloatToScalar(0.75f),
1110b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                SkFloatToScalar(40.75f),
1120b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                SkFloatToScalar(40.75f));
1130b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->drawRect(rect, paints[p]);
1140b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->translate(dx, 0);
1150b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
1160b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->save();
1170b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            canvas->translate(SkFloatToScalar(.33f), SkFloatToScalar(.67f));
1180b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            rect = SkRect::MakeLTRB(SkFloatToScalar(0.0f),
1190b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(0.0f),
1200b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(40.0f),
1210b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(40.0f));
1220b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            canvas->drawRect(rect, paints[p]);
1230b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->restore();
1240b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->translate(dx, 0);
1250b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
1260b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->save();
1270b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            matrix.setRotate(SkFloatToScalar(45.f));
1280b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            canvas->concat(matrix);
1290b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            canvas->translate(SkFloatToScalar(20.0f / sqrtf(2.f)),
1300b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                SkFloatToScalar(20.0f / sqrtf(2.f)));
1310b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            rect = SkRect::MakeLTRB(SkFloatToScalar(-20.0f),
1320b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(-20.0f),
1330b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(20.0f),
1340b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(20.0f));
1350b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            canvas->drawRect(rect, paints[p]);
1360b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->restore();
1370b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->translate(dx, 0);
1380b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
1390b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->save();
1400b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            canvas->rotate(SkFloatToScalar(90.f));
1410b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            rect = SkRect::MakeLTRB(SkFloatToScalar(0.0f),
1420b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(0.0f),
1430b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(40.0f),
1440b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(-40.0f));
1450b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            canvas->drawRect(rect, paints[p]);
1460b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->restore();
1470b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->translate(dx, 0);
1480b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
1490b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->save();
1500b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            canvas->rotate(SkFloatToScalar(90.f));
1510b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
1520b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(0.5f),
1530b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(40.5f),
1540b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(-40.5f));
1550b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            canvas->drawRect(rect, paints[p]);
1560b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->restore();
1570b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->translate(dx, 0);
1580b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
1590b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->save();
1600b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            matrix.setScale(SkFloatToScalar(-1.f), SkFloatToScalar(-1.f));
1610b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            canvas->concat(matrix);
1620b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            rect = SkRect::MakeLTRB(SkFloatToScalar(0.5f),
1630b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(0.5f),
1640b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(-40.5f),
1650b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(-40.5f));
1660b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            canvas->drawRect(rect, paints[p]);
1670b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->restore();
1680b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->translate(dx, 0);
1690b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
1700b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->save();
1710b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            matrix.setScale(SkFloatToScalar(2.1f), SkFloatToScalar(4.1f));
1720b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            canvas->concat(matrix);
1730b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            rect = SkRect::MakeLTRB(SkFloatToScalar(0.1f),
1740b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(0.1f),
1750b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(19.1f),
1760b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                                                    SkFloatToScalar(9.1f));
1770b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                            canvas->drawRect(rect, paints[p]);
1780b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->restore();
1790b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                        canvas->translate(dx, 0);
1800b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
1810b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                    canvas->restore();
1820b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                    canvas->translate(0, dy);
1830b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger                }
1840b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger            }
1850b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger        }
1860b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    }
1870b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
1880b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenbergerprivate:
1890b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    int fWidth;
1900b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
1910b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger    typedef SampleView INHERITED;
1920b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger};
1930b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
1940b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger//////////////////////////////////////////////////////////////////////////////
1950b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
1960b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenbergerstatic SkView* MyFactory() { return new AARectView; }
1970b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenbergerstatic SkViewRegister reg(MyFactory);
1980b15698a8c76bb8abc1b555c1d91892669b4118fDerek Sollenberger
199