blob: a906cbedeba9f2a97f80dbbbeb92321afb7ce91a [file] [log] [blame]
reed71c3c762015-06-24 10:29:17 -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
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkRSXform.h"
11#include "SkSurface.h"
12
13class DrawAtlasGM : public skiagm::GM {
reed9ce9d672016-03-17 10:51:11 -070014 static sk_sp<SkImage> MakeAtlas(SkCanvas* caller, const SkRect& target) {
reed71c3c762015-06-24 10:29:17 -070015 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
reede8f30622016-03-23 18:59:25 -070016 auto surface(caller->makeSurface(info));
halcanary96fcdcc2015-08-27 07:41:13 -070017 if (nullptr == surface) {
reede8f30622016-03-23 18:59:25 -070018 surface = SkSurface::MakeRaster(info);
reed71c3c762015-06-24 10:29:17 -070019 }
20 SkCanvas* canvas = surface->getCanvas();
21 // draw red everywhere, but we don't expect to see it in the draw, testing the notion
22 // that drawAtlas draws a subset-region of the atlas.
23 canvas->clear(SK_ColorRED);
24
25 SkPaint paint;
26 paint.setXfermodeMode(SkXfermode::kClear_Mode);
27 SkRect r(target);
28 r.inset(-1, -1);
29 // zero out a place (with a 1-pixel border) to land our drawing.
30 canvas->drawRect(r, paint);
halcanary96fcdcc2015-08-27 07:41:13 -070031 paint.setXfermode(nullptr);
reed71c3c762015-06-24 10:29:17 -070032 paint.setColor(SK_ColorBLUE);
33 paint.setAntiAlias(true);
34 canvas->drawOval(target, paint);
reed9ce9d672016-03-17 10:51:11 -070035 return surface->makeImageSnapshot();
reed71c3c762015-06-24 10:29:17 -070036 }
37
reed9ce9d672016-03-17 10:51:11 -070038 sk_sp<SkImage> fAtlas;
reed71c3c762015-06-24 10:29:17 -070039
40public:
41 DrawAtlasGM() {}
halcanary9d524f22016-03-29 09:03:52 -070042
reed71c3c762015-06-24 10:29:17 -070043protected:
halcanary9d524f22016-03-29 09:03:52 -070044
reed71c3c762015-06-24 10:29:17 -070045 SkString onShortName() override {
46 return SkString("draw-atlas");
47 }
halcanary9d524f22016-03-29 09:03:52 -070048
reed71c3c762015-06-24 10:29:17 -070049 SkISize onISize() override {
50 return SkISize::Make(640, 480);
51 }
halcanary9d524f22016-03-29 09:03:52 -070052
reed71c3c762015-06-24 10:29:17 -070053 void onDraw(SkCanvas* canvas) override {
54 const SkRect target = { 50, 50, 80, 90 };
halcanary96fcdcc2015-08-27 07:41:13 -070055 if (nullptr == fAtlas) {
reed9ce9d672016-03-17 10:51:11 -070056 fAtlas = MakeAtlas(canvas, target);
reed71c3c762015-06-24 10:29:17 -070057 }
58
59 const struct {
60 SkScalar fScale;
61 SkScalar fDegrees;
62 SkScalar fTx;
63 SkScalar fTy;
halcanary9d524f22016-03-29 09:03:52 -070064
reed71c3c762015-06-24 10:29:17 -070065 void apply(SkRSXform* xform) const {
66 const SkScalar rad = SkDegreesToRadians(fDegrees);
67 xform->fSCos = fScale * SkScalarCos(rad);
68 xform->fSSin = fScale * SkScalarSin(rad);
69 xform->fTx = fTx;
70 xform->fTy = fTy;
71 }
72 } rec[] = {
73 { 1, 0, 10, 10 }, // just translate
74 { 2, 0, 110, 10 }, // scale + translate
75 { 1, 30, 210, 10 }, // rotate + translate
76 { 2, -30, 310, 30 }, // scale + rotate + translate
77 };
78
79 const int N = SK_ARRAY_COUNT(rec);
80 SkRSXform xform[N];
81 SkRect tex[N];
82 SkColor colors[N];
83
84 for (int i = 0; i < N; ++i) {
85 rec[i].apply(&xform[i]);
86 tex[i] = target;
87 colors[i] = 0x80FF0000 + (i * 40 * 256);
88 }
89
90 SkPaint paint;
91 paint.setFilterQuality(kLow_SkFilterQuality);
92 paint.setAntiAlias(true);
93
reed9ce9d672016-03-17 10:51:11 -070094 canvas->drawAtlas(fAtlas.get(), xform, tex, N, nullptr, &paint);
reed71c3c762015-06-24 10:29:17 -070095 canvas->translate(0, 100);
reed9ce9d672016-03-17 10:51:11 -070096 canvas->drawAtlas(fAtlas.get(), xform, tex, colors, N, SkXfermode::kSrcIn_Mode, nullptr, &paint);
reed71c3c762015-06-24 10:29:17 -070097 }
halcanary9d524f22016-03-29 09:03:52 -070098
reed71c3c762015-06-24 10:29:17 -070099private:
100 typedef GM INHERITED;
101};
102DEF_GM( return new DrawAtlasGM; )