blob: 1f757f5b6fc02a98673296e1cba9ea212e65e97a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed76113a92015-02-02 12:55:02 -08007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColorFilter.h"
11#include "include/core/SkColorPriv.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkGraphics.h"
14#include "include/core/SkPath.h"
15#include "include/core/SkRegion.h"
16#include "include/core/SkShader.h"
17#include "include/core/SkTime.h"
18#include "include/core/SkTypeface.h"
19#include "include/effects/SkGradientShader.h"
20#include "samplecode/Sample.h"
21#include "src/utils/SkUTF.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/core/SkStream.h"
24#include "src/core/SkOSFile.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000025
Hal Canary41248072019-07-11 16:32:53 -040026static constexpr int INT_SIZE = 64;
27static constexpr float SCALAR_SIZE = (float)INT_SIZE;
reed@google.com3cec4d72011-07-06 13:59:47 +000028
reed@google.com33535f32012-09-25 15:37:50 +000029static void make_bitmap(SkBitmap* bitmap) {
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +000030 bitmap->allocN32Pixels(INT_SIZE, INT_SIZE);
reed@google.com33535f32012-09-25 15:37:50 +000031 SkCanvas canvas(*bitmap);
reed@google.com3cec4d72011-07-06 13:59:47 +000032
reed@android.comfead49e2009-10-15 18:51:46 +000033 canvas.drawColor(SK_ColorRED);
34 SkPaint paint;
35 paint.setAntiAlias(true);
reed@google.com33535f32012-09-25 15:37:50 +000036 const SkPoint pts[] = { { 0, 0 }, { SCALAR_SIZE, SCALAR_SIZE } };
reed@android.comfead49e2009-10-15 18:51:46 +000037 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
Mike Reedfae8fce2019-04-03 10:27:45 -040038 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp));
reed@google.com33535f32012-09-25 15:37:50 +000039 canvas.drawCircle(SCALAR_SIZE/2, SCALAR_SIZE/2, SCALAR_SIZE/2, paint);
40}
41
reed@google.com33535f32012-09-25 15:37:50 +000042static void bounce(SkScalar* value, SkScalar* delta, SkScalar min, SkScalar max) {
43 *value += *delta;
44 if (*value < min) {
45 *value = min;
46 *delta = - *delta;
47 } else if (*value > max) {
48 *value = max;
49 *delta = - *delta;
50 }
51}
52
53static void bounce_pt(SkPoint* pt, SkVector* vec, const SkRect& limit) {
54 bounce(&pt->fX, &vec->fX, limit.fLeft, limit.fRight);
55 bounce(&pt->fY, &vec->fY, limit.fTop, limit.fBottom);
reed@android.comfead49e2009-10-15 18:51:46 +000056}
reed@android.com8a1c16f2008-12-17 15:59:43 +000057
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040058class BitmapRectView : public Sample {
Hal Canary41248072019-07-11 16:32:53 -040059 SkPoint fSrcPt = {0, 0};
60 SkPoint fSrcVec = {0.866025f, 0.5f};
reed@google.com33535f32012-09-25 15:37:50 +000061
Hal Canary41248072019-07-11 16:32:53 -040062 SkRect fSrcLimit = {-SCALAR_SIZE/4, -SCALAR_SIZE/4,
63 SCALAR_SIZE*5/4, SCALAR_SIZE*5/4};
64 SkRect fDstR[2] = {{10, 100, 260, 400}, {322.5, 100, 572.5, 400}};
65 SkBitmap fBitmap;
reed@google.com33535f32012-09-25 15:37:50 +000066
Hal Canary8a027312019-07-03 10:55:44 -040067 SkString name() override { return SkString("BitmapRect"); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000068
Hal Canary41248072019-07-11 16:32:53 -040069 void onOnceBeforeDraw() override {
70 this->setBGColor(SK_ColorGRAY);
71 make_bitmap(&fBitmap);
72 }
73
mtklein36352bf2015-03-25 18:17:31 -070074 void onDrawContent(SkCanvas* canvas) override {
Hal Canary41248072019-07-11 16:32:53 -040075 SkRect srcR = {fSrcPt.fX - 16, fSrcPt.fY - 16,
76 fSrcPt.fX + 16, fSrcPt.fY + 16};
reed@android.comfead49e2009-10-15 18:51:46 +000077
Hal Canary41248072019-07-11 16:32:53 -040078 SkPaint paint(SkColors::kYellow);
reed@android.comfead49e2009-10-15 18:51:46 +000079 paint.setStyle(SkPaint::kStroke_Style);
reed@google.com3cec4d72011-07-06 13:59:47 +000080
reed@google.com33535f32012-09-25 15:37:50 +000081 canvas->translate(20, 20);
reed@android.comfead49e2009-10-15 18:51:46 +000082
Hal Canary41248072019-07-11 16:32:53 -040083 canvas->drawBitmap(fBitmap, 0, 0, &paint);
reed@google.com33535f32012-09-25 15:37:50 +000084 canvas->drawRect(srcR, paint);
reed@android.comf2b98d62010-12-20 18:26:13 +000085
reed@google.com33535f32012-09-25 15:37:50 +000086 for (int i = 0; i < 2; ++i) {
reed93a12152015-03-16 10:08:34 -070087 paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
Hal Canary41248072019-07-11 16:32:53 -040088 canvas->drawBitmapRect(fBitmap, srcR, fDstR[i], &paint,
reed84984ef2015-07-17 07:09:43 -070089 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com33535f32012-09-25 15:37:50 +000090 canvas->drawRect(fDstR[i], paint);
reed@android.comfead49e2009-10-15 18:51:46 +000091 }
reed76113a92015-02-02 12:55:02 -080092 }
reed@google.com33535f32012-09-25 15:37:50 +000093
Hal Canary41248072019-07-11 16:32:53 -040094 bool onAnimate(double nanos) override {
95 bounce_pt(&fSrcPt, &fSrcVec, fSrcLimit);
reed76113a92015-02-02 12:55:02 -080096 return true;
reed@android.com8a1c16f2008-12-17 15:59:43 +000097 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000098};
99
Hal Canary41248072019-07-11 16:32:53 -0400100static constexpr int BIG_H = 120;
101
reed@google.com33535f32012-09-25 15:37:50 +0000102static void make_big_bitmap(SkBitmap* bm) {
103 static const char gText[] =
104 "We the people, in order to form a more perfect union, establish justice,"
105 " ensure domestic tranquility, provide for the common defense, promote the"
106 " general welfare and ensure the blessings of liberty to ourselves and our"
107 " posterity, do ordain and establish this constitution for the United"
108 " States of America.";
109
Mike Reed91919132019-01-02 12:21:01 -0500110 SkFont font;
111 font.setSize(SkIntToScalar(BIG_H));
reed@google.com33535f32012-09-25 15:37:50 +0000112
Ben Wagner51e15a62019-05-07 15:38:46 -0400113 const int BIG_W = SkScalarRoundToInt(font.measureText(gText, strlen(gText), SkTextEncoding::kUTF8));
reed@google.com33535f32012-09-25 15:37:50 +0000114
commit-bot@chromium.orga8c18312014-02-17 02:55:57 +0000115 bm->allocN32Pixels(BIG_W, BIG_H);
reed@google.com33535f32012-09-25 15:37:50 +0000116 bm->eraseColor(SK_ColorWHITE);
117
118 SkCanvas canvas(*bm);
119
Ben Wagner51e15a62019-05-07 15:38:46 -0400120 canvas.drawSimpleText(gText, strlen(gText), SkTextEncoding::kUTF8, 0, font.getSize()*4/5, font, SkPaint());
reed@google.com33535f32012-09-25 15:37:50 +0000121}
122
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400123class BitmapRectView2 : public Sample {
reed@google.com33535f32012-09-25 15:37:50 +0000124 SkBitmap fBitmap;
Hal Canary41248072019-07-11 16:32:53 -0400125 SkRect fSrcR = {0, 0, 3 * BIG_H, BIG_H};
126 SkRect fLimitR;
127 SkScalar fDX = 1;
128 SkRect fDstR[2] = {{20, 20, 620, 220}, {20, 270, 620, 470}};
reed@google.com33535f32012-09-25 15:37:50 +0000129
Hal Canary8a027312019-07-03 10:55:44 -0400130 SkString name() override { return SkString("BigBitmapRect"); }
Ben Wagner4d516112018-06-07 13:11:37 -0400131
132 void onOnceBeforeDraw() override {
reed@google.com33535f32012-09-25 15:37:50 +0000133 this->setBGColor(SK_ColorGRAY);
Hal Canary41248072019-07-11 16:32:53 -0400134 make_big_bitmap(&fBitmap);
135 fLimitR = SkRect::Make(fBitmap.dimensions());
reed@google.com33535f32012-09-25 15:37:50 +0000136 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000137
mtklein36352bf2015-03-25 18:17:31 -0700138 void onDrawContent(SkCanvas* canvas) override {
reed@google.com33535f32012-09-25 15:37:50 +0000139 SkPaint paint;
140 paint.setStyle(SkPaint::kStroke_Style);
141 paint.setColor(SK_ColorYELLOW);
142
143 for (int i = 0; i < 2; ++i) {
reed93a12152015-03-16 10:08:34 -0700144 paint.setFilterQuality(1 == i ? kLow_SkFilterQuality : kNone_SkFilterQuality);
reede47829b2015-08-06 10:02:53 -0700145 canvas->drawBitmapRect(fBitmap, fSrcR, fDstR[i], &paint,
reed84984ef2015-07-17 07:09:43 -0700146 SkCanvas::kStrict_SrcRectConstraint);
reed@google.com33535f32012-09-25 15:37:50 +0000147 canvas->drawRect(fDstR[i], paint);
148 }
reed76113a92015-02-02 12:55:02 -0800149 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +0000150
Hal Canary41248072019-07-11 16:32:53 -0400151 bool onAnimate(double nanos) override {
152 SkScalar width = fSrcR.width();
153 bounce(&fSrcR.fLeft, &fDX, fLimitR.fLeft, fLimitR.fRight - width);
154 fSrcR.fRight = fSrcR.fLeft + width;
reed76113a92015-02-02 12:55:02 -0800155 return true;
reed@google.com33535f32012-09-25 15:37:50 +0000156 }
reed@google.com33535f32012-09-25 15:37:50 +0000157};
158
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400159DEF_SAMPLE( return new BitmapRectView(); )
160DEF_SAMPLE( return new BitmapRectView2(); )