| 1 |
/* |
| 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 |
|
| 8 |
#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 "src/core/SkPaintPriv.h" |
| 13 |
#include "tools/DecodeUtils.h" |
| 14 |
#include "tools/Resources.h" |
| 15 |
#include "tools/timer/Timer.h" |
| 16 |
#include "tools/viewer/Slide.h" |
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
|
| 20 |
static const int kGrid = 100; |
| 21 |
static const int kWidth = 960; |
| 22 |
static const int kHeight = 640; |
| 23 |
|
| 24 |
typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, SkSpan<const SkRSXform>, SkSpan<const SkRect>, |
| 25 |
SkSpan<const SkColor>, const SkRect*, const SkSamplingOptions&, |
| 26 |
const SkPaint*); |
| 27 |
|
| 28 |
static void draw_atlas(SkCanvas* canvas, SkImage* atlas, SkSpan<const SkRSXform> xform, |
| 29 |
SkSpan<const SkRect> tex, |
| 30 |
SkSpan<const SkColor> colors, const SkRect* cull, |
| 31 |
const SkSamplingOptions& sampling, const SkPaint* paint) { |
| 32 |
canvas->drawAtlas(atlas, xform, tex, colors, SkBlendMode::kModulate, |
| 33 |
sampling, cull, paint); |
| 34 |
} |
| 35 |
|
| 36 |
static void draw_atlas_sim(SkCanvas* canvas, SkImage* atlas, SkSpan<const SkRSXform> xform, |
| 37 |
SkSpan<const SkRect> tex, |
| 38 |
SkSpan<const SkColor> colors, const SkRect* cull, |
| 39 |
const SkSamplingOptions& sampling, const SkPaint* paint) { |
| 40 |
size_t N = std::min(xform.size(), tex.size()); |
| 41 |
if (!colors.empty()) { |
| 42 |
N = std::min(N, colors.size()); |
| 43 |
} |
| 44 |
for (size_t i = 0; i < N; ++i) { |
| 45 |
SkMatrix matrix; |
| 46 |
matrix.setRSXform(xform[i]); |
| 47 |
|
| 48 |
canvas->save(); |
| 49 |
canvas->concat(matrix); |
| 50 |
canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex[i].y()), |
| 51 |
sampling, paint, SkCanvas::kFast_SrcRectConstraint); |
| 52 |
canvas->restore(); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
class DrawShipSlide : public Slide { |
| 57 |
public: |
| 58 |
DrawShipSlide(const char name[], DrawAtlasProc proc) : fProc(proc) { |
| 59 |
fName = name; |
| 60 |
} |
| 61 |
|
| 62 |
protected: |
| 63 |
void load(SkScalar, SkScalar) override { |
| 64 |
fAtlas = ToolUtils::GetResourceAsImage("images/ship.png"); |
| 65 |
if (!fAtlas) { |
| 66 |
SkDebugf("\nCould not decode file ship.png. Falling back to penguin mode.\n"); |
| 67 |
fAtlas = ToolUtils::GetResourceAsImage("images/baby_tux.png"); |
| 68 |
if (!fAtlas) { |
| 69 |
SkDebugf("\nCould not decode file baby_tux.png. Did you forget" |
| 70 |
" to set the resourcePath?\n"); |
| 71 |
return; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
SkScalar anchorX = fAtlas->width()*0.5f; |
| 76 |
SkScalar anchorY = fAtlas->height()*0.5f; |
| 77 |
int currIndex = 0; |
| 78 |
for (int x = 0; x < kGrid; x++) { |
| 79 |
for (int y = 0; y < kGrid; y++) { |
| 80 |
float xPos = (x / (kGrid - 1.0f)) * kWidth; |
| 81 |
float yPos = (y / (kGrid - 1.0f)) * kWidth; |
| 82 |
|
| 83 |
fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f, |
| 84 |
SkIntToScalar(fAtlas->width()), |
| 85 |
SkIntToScalar(fAtlas->height())); |
| 86 |
fXform[currIndex] = SkRSXform::MakeFromRadians(0.1f, SK_ScalarPI*0.5f, |
| 87 |
xPos, yPos, anchorX, anchorY); |
| 88 |
currIndex++; |
| 89 |
} |
| 90 |
} |
| 91 |
fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f, |
| 92 |
SkIntToScalar(fAtlas->width()), |
| 93 |
SkIntToScalar(fAtlas->height())); |
| 94 |
fXform[currIndex] = SkRSXform::MakeFromRadians(0.5f, SK_ScalarPI*0.5f, |
| 95 |
kWidth*0.5f, kHeight*0.5f, anchorX, anchorY); |
| 96 |
} |
| 97 |
|
| 98 |
void unload() override { |
| 99 |
fAtlas = nullptr; |
| 100 |
} |
| 101 |
|
| 102 |
void draw(SkCanvas* canvas) override { |
| 103 |
const float kCosDiff = 0.99984769515f; |
| 104 |
const float kSinDiff = 0.01745240643f; |
| 105 |
|
| 106 |
if (!fAtlas) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
SkPaint paint; |
| 111 |
paint.setColor(SK_ColorWHITE); |
| 112 |
|
| 113 |
SkScalar anchorX = fAtlas->width()*0.5f; |
| 114 |
SkScalar anchorY = fAtlas->height()*0.5f; |
| 115 |
for (int i = 0; i < kGrid*kGrid+1; ++i) { |
| 116 |
SkScalar c = fXform[i].fSCos; |
| 117 |
SkScalar s = fXform[i].fSSin; |
| 118 |
|
| 119 |
SkScalar dx = c*anchorX - s*anchorY; |
| 120 |
SkScalar dy = s*anchorX + c*anchorY; |
| 121 |
|
| 122 |
fXform[i].fSCos = kCosDiff*c - kSinDiff*s; |
| 123 |
fXform[i].fSSin = kSinDiff*c + kCosDiff*s; |
| 124 |
|
| 125 |
dx -= fXform[i].fSCos*anchorX - fXform[i].fSSin*anchorY; |
| 126 |
dy -= fXform[i].fSSin*anchorX + fXform[i].fSCos*anchorY; |
| 127 |
fXform[i].fTx += dx; |
| 128 |
fXform[i].fTy += dy; |
| 129 |
} |
| 130 |
|
| 131 |
SkSpan<const SkRSXform> xforms = {&fXform[0], kGrid*kGrid+1}; |
| 132 |
SkSpan<const SkRect> texs = {&fTex[0], kGrid*kGrid+1}; |
| 133 |
fProc(canvas, fAtlas.get(), xforms, texs, {}, nullptr, |
| 134 |
SkSamplingOptions(SkFilterMode::kLinear), &paint); |
| 135 |
} |
| 136 |
|
| 137 |
bool animate(double nanos) override { |
| 138 |
//TODO: use nanos |
| 139 |
//SkScalar angle = SkDoubleToScalar(fmod(1e-9 * nanos * 360 / 24, 360)); |
| 140 |
//fAnimatingDrawable->setSweep(angle); |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
private: |
| 145 |
DrawAtlasProc fProc; |
| 146 |
|
| 147 |
sk_sp<SkImage> fAtlas; |
| 148 |
SkRSXform fXform[kGrid*kGrid+1]; |
| 149 |
SkRect fTex[kGrid*kGrid+1]; |
| 150 |
}; |
| 151 |
|
| 152 |
////////////////////////////////////////////////////////////////////////////// |
| 153 |
|
| 154 |
DEF_SLIDE( return new DrawShipSlide("DrawShip", draw_atlas); ) |
| 155 |
DEF_SLIDE( return new DrawShipSlide("DrawShipSim", draw_atlas_sim); ) |