blob: 8fdc9ebc55010325adf59d6a5b62b3d2228aac98 [file] [log] [blame]
jvanverth2f5bb3a2015-10-05 11:05:07 -07001/*
2 * Copyright 2015 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 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkFont.h"
10#include "include/core/SkRSXform.h"
11#include "include/core/SkSurface.h"
12#include "samplecode/Sample.h"
13#include "tools/Resources.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "tools/timer/Timer.h"
jvanverth2f5bb3a2015-10-05 11:05:07 -070015
16#include <stdio.h>
17
18static const int kGrid = 100;
19static const int kWidth = 960;
20static const int kHeight = 640;
21
jvanverth9011bcf2015-10-07 10:43:05 -070022typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRect[],
23const SkColor[], int, const SkRect*, const SkPaint*);
jvanverth2f5bb3a2015-10-05 11:05:07 -070024
jvanverth9011bcf2015-10-07 10:43:05 -070025static void draw_atlas(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
26 const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
27 const SkPaint* paint) {
Mike Reed7d954ad2016-10-28 15:42:34 -040028 canvas->drawAtlas(atlas, xform, tex, colors, count, SkBlendMode::kModulate, cull, paint);
jvanverth9011bcf2015-10-07 10:43:05 -070029}
30
31static void draw_atlas_sim(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
32 const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
33 const SkPaint* paint) {
34 for (int i = 0; i < count; ++i) {
35 SkMatrix matrix;
36 matrix.setRSXform(xform[i]);
halcanary9d524f22016-03-29 09:03:52 -070037
jvanverth9011bcf2015-10-07 10:43:05 -070038 canvas->save();
39 canvas->concat(matrix);
40 canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex[i].y()), paint,
41 SkCanvas::kFast_SrcRectConstraint);
42 canvas->restore();
43 }
44}
45
46
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040047class DrawShipView : public Sample {
jvanverth2f5bb3a2015-10-05 11:05:07 -070048public:
jvanverth9011bcf2015-10-07 10:43:05 -070049 DrawShipView(const char name[], DrawAtlasProc proc) : fName(name), fProc(proc) {
Hal Canaryc465d132017-12-08 10:21:31 -050050 fAtlas = GetResourceAsImage("images/ship.png");
jvanverth2f5bb3a2015-10-05 11:05:07 -070051 if (!fAtlas) {
52 SkDebugf("\nCould not decode file ship.png. Falling back to penguin mode.\n");
Hal Canaryc465d132017-12-08 10:21:31 -050053 fAtlas = GetResourceAsImage("images/baby_tux.png");
jvanverth2f5bb3a2015-10-05 11:05:07 -070054 if (!fAtlas) {
55 SkDebugf("\nCould not decode file baby_tux.png. Did you forget"
56 " to set the resourcePath?\n");
57 return;
58 }
59 }
halcanary9d524f22016-03-29 09:03:52 -070060
jvanverth2f5bb3a2015-10-05 11:05:07 -070061 SkScalar anchorX = fAtlas->width()*0.5f;
62 SkScalar anchorY = fAtlas->height()*0.5f;
63 int currIndex = 0;
64 for (int x = 0; x < kGrid; x++) {
65 for (int y = 0; y < kGrid; y++) {
66 float xPos = (x / (kGrid - 1.0f)) * kWidth;
67 float yPos = (y / (kGrid - 1.0f)) * kWidth;
halcanary9d524f22016-03-29 09:03:52 -070068
jvanverth2f5bb3a2015-10-05 11:05:07 -070069 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f,
70 SkIntToScalar(fAtlas->width()),
71 SkIntToScalar(fAtlas->height()));
jvanverth629162d2015-11-08 08:07:24 -080072 fXform[currIndex] = SkRSXform::MakeFromRadians(0.1f, SK_ScalarPI*0.5f,
jvanverth2f5bb3a2015-10-05 11:05:07 -070073 xPos, yPos, anchorX, anchorY);
74 currIndex++;
75 }
76 }
77 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f,
78 SkIntToScalar(fAtlas->width()),
79 SkIntToScalar(fAtlas->height()));
jvanverth629162d2015-11-08 08:07:24 -080080 fXform[currIndex] = SkRSXform::MakeFromRadians(0.5f, SK_ScalarPI*0.5f,
jvanverth2f5bb3a2015-10-05 11:05:07 -070081 kWidth*0.5f, kHeight*0.5f, anchorX, anchorY);
halcanary9d524f22016-03-29 09:03:52 -070082
jvanverth2f5bb3a2015-10-05 11:05:07 -070083 }
84
85 ~DrawShipView() override {}
halcanary9d524f22016-03-29 09:03:52 -070086
jvanverth2f5bb3a2015-10-05 11:05:07 -070087protected:
Hal Canary8a027312019-07-03 10:55:44 -040088 SkString name() override { return SkString(fName); }
halcanary9d524f22016-03-29 09:03:52 -070089
jvanverth2f5bb3a2015-10-05 11:05:07 -070090 void onDrawContent(SkCanvas* canvas) override {
91 const float kCosDiff = 0.99984769515f;
92 const float kSinDiff = 0.01745240643f;
halcanary9d524f22016-03-29 09:03:52 -070093
jvanverth2f5bb3a2015-10-05 11:05:07 -070094 if (!fAtlas) {
95 return;
96 }
halcanary9d524f22016-03-29 09:03:52 -070097
jvanverth2f5bb3a2015-10-05 11:05:07 -070098 SkPaint paint;
99 paint.setFilterQuality(kLow_SkFilterQuality);
100 paint.setColor(SK_ColorWHITE);
Mike Reed89126e42019-01-03 12:59:14 -0500101
jvanverth2f5bb3a2015-10-05 11:05:07 -0700102 SkScalar anchorX = fAtlas->width()*0.5f;
103 SkScalar anchorY = fAtlas->height()*0.5f;
104 for (int i = 0; i < kGrid*kGrid+1; ++i) {
105 SkScalar c = fXform[i].fSCos;
106 SkScalar s = fXform[i].fSSin;
halcanary9d524f22016-03-29 09:03:52 -0700107
jvanverth2f5bb3a2015-10-05 11:05:07 -0700108 SkScalar dx = c*anchorX - s*anchorY;
109 SkScalar dy = s*anchorX + c*anchorY;
halcanary9d524f22016-03-29 09:03:52 -0700110
jvanverth2f5bb3a2015-10-05 11:05:07 -0700111 fXform[i].fSCos = kCosDiff*c - kSinDiff*s;
112 fXform[i].fSSin = kSinDiff*c + kCosDiff*s;
halcanary9d524f22016-03-29 09:03:52 -0700113
jvanverth2f5bb3a2015-10-05 11:05:07 -0700114 dx -= fXform[i].fSCos*anchorX - fXform[i].fSSin*anchorY;
115 dy -= fXform[i].fSSin*anchorX + fXform[i].fSCos*anchorY;
116 fXform[i].fTx += dx;
117 fXform[i].fTy += dy;
118 }
halcanary9d524f22016-03-29 09:03:52 -0700119
reed9ce9d672016-03-17 10:51:11 -0700120 fProc(canvas, fAtlas.get(), fXform, fTex, nullptr, kGrid*kGrid+1, nullptr, &paint);
jvanverth2f5bb3a2015-10-05 11:05:07 -0700121 }
122
Hal Canary41248072019-07-11 16:32:53 -0400123 bool onAnimate(double nanos) override {
Ben Wagneracf98df2019-07-12 12:51:44 -0400124 //TODO: use nanos
125 //SkScalar angle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 24, 360));
126 //fAnimatingDrawable->setSweep(angle);
jvanverth2f5bb3a2015-10-05 11:05:07 -0700127 return true;
128 }
jvanverth2f5bb3a2015-10-05 11:05:07 -0700129
130private:
jvanverth9011bcf2015-10-07 10:43:05 -0700131 const char* fName;
132 DrawAtlasProc fProc;
halcanary9d524f22016-03-29 09:03:52 -0700133
reed9ce9d672016-03-17 10:51:11 -0700134 sk_sp<SkImage> fAtlas;
jvanverth2f5bb3a2015-10-05 11:05:07 -0700135 SkRSXform fXform[kGrid*kGrid+1];
136 SkRect fTex[kGrid*kGrid+1];
halcanary9d524f22016-03-29 09:03:52 -0700137
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400138 typedef Sample INHERITED;
jvanverth2f5bb3a2015-10-05 11:05:07 -0700139};
140
141//////////////////////////////////////////////////////////////////////////////
142
jvanverth9011bcf2015-10-07 10:43:05 -0700143DEF_SAMPLE( return new DrawShipView("DrawShip", draw_atlas); )
144DEF_SAMPLE( return new DrawShipView("DrawShipSim", draw_atlas_sim); )