| 1 |
/* |
| 2 |
* Copyright 2013 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/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkMatrix.h" |
| 13 |
#include "include/core/SkPaint.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkScalar.h" |
| 16 |
#include "include/core/SkShader.h" |
| 17 |
#include "include/core/SkSize.h" |
| 18 |
#include "include/core/SkString.h" |
| 19 |
#include "include/core/SkTileMode.h" |
| 20 |
#include "include/core/SkTypes.h" |
| 21 |
|
| 22 |
namespace { |
| 23 |
|
| 24 |
// This GM draws a 3x3 grid (with the center element excluded) of rectangles |
| 25 |
// filled with a bitmap shader. The bitmap shader is transformed so that the |
| 26 |
// pattern cell is at the center (excluded) region. |
| 27 |
// |
| 28 |
// In Repeat and Mirror mode, this tests that the bitmap shader still draws |
| 29 |
// even though the pattern cell is outside the clip. |
| 30 |
// |
| 31 |
// In Clamp mode, this tests that the clamp is handled properly. For PDF, |
| 32 |
// (and possibly other exported formats) this also "tests" that the image itself |
| 33 |
// is not stored (well, you'll need to open it up with an external tool to |
| 34 |
// verify that). |
| 35 |
|
| 36 |
static SkBitmap create_bitmap() { |
| 37 |
SkBitmap bmp; |
| 38 |
bmp.allocN32Pixels(2, 2); |
| 39 |
uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels()); |
| 40 |
pixels[0] = SkPreMultiplyColor(SK_ColorRED); |
| 41 |
pixels[1] = SkPreMultiplyColor(SK_ColorGREEN); |
| 42 |
pixels[2] = SkPreMultiplyColor(SK_ColorBLACK); |
| 43 |
pixels[3] = SkPreMultiplyColor(SK_ColorBLUE); |
| 44 |
|
| 45 |
return bmp; |
| 46 |
} |
| 47 |
|
| 48 |
constexpr SkScalar RECT_SIZE = 64; |
| 49 |
constexpr SkScalar SLIDE_SIZE = 300; |
| 50 |
|
| 51 |
class ClippedBitmapShadersGM : public skiagm::GM { |
| 52 |
public: |
| 53 |
ClippedBitmapShadersGM(SkTileMode mode, bool hq=false) |
| 54 |
: fMode(mode), fHQ(hq) { |
| 55 |
} |
| 56 |
|
| 57 |
protected: |
| 58 |
SkTileMode fMode; |
| 59 |
bool fHQ; |
| 60 |
|
| 61 |
SkString getName() const override { |
| 62 |
SkString descriptor; |
| 63 |
switch (fMode) { |
| 64 |
case SkTileMode::kRepeat: |
| 65 |
descriptor = "tile"; |
| 66 |
break; |
| 67 |
case SkTileMode::kMirror: |
| 68 |
descriptor = "mirror"; |
| 69 |
break; |
| 70 |
case SkTileMode::kClamp: |
| 71 |
descriptor = "clamp"; |
| 72 |
break; |
| 73 |
case SkTileMode::kDecal: |
| 74 |
descriptor = "decal"; |
| 75 |
break; |
| 76 |
} |
| 77 |
descriptor.prepend("clipped-bitmap-shaders-"); |
| 78 |
if (fHQ) { |
| 79 |
descriptor.append("-hq"); |
| 80 |
} |
| 81 |
return descriptor; |
| 82 |
} |
| 83 |
|
| 84 |
SkISize getISize() override { return {300, 300}; } |
| 85 |
|
| 86 |
void onDraw(SkCanvas* canvas) override { |
| 87 |
SkBitmap bmp = create_bitmap(); |
| 88 |
SkMatrix s; |
| 89 |
s.reset(); |
| 90 |
s.setScale(8, 8); |
| 91 |
s.postTranslate(SLIDE_SIZE / 2, SLIDE_SIZE / 2); |
| 92 |
SkPaint paint; |
| 93 |
paint.setShader(bmp.makeShader(fMode, fMode, |
| 94 |
fHQ ? SkSamplingOptions(SkCubicResampler::Mitchell()) |
| 95 |
: SkSamplingOptions(), |
| 96 |
s)); |
| 97 |
|
| 98 |
SkScalar margin = (SLIDE_SIZE / 3 - RECT_SIZE) / 2; |
| 99 |
for (int i = 0; i < 3; i++) { |
| 100 |
SkScalar yOrigin = SLIDE_SIZE / 3 * i + margin; |
| 101 |
for (int j = 0; j < 3; j++) { |
| 102 |
SkScalar xOrigin = SLIDE_SIZE / 3 * j + margin; |
| 103 |
if (i == 1 && j == 1) { |
| 104 |
continue; // skip center element |
| 105 |
} |
| 106 |
SkRect rect = SkRect::MakeXYWH(xOrigin, yOrigin, |
| 107 |
RECT_SIZE, RECT_SIZE); |
| 108 |
canvas->save(); |
| 109 |
canvas->clipRect(rect); |
| 110 |
canvas->drawRect(rect, paint); |
| 111 |
canvas->restore(); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
private: |
| 117 |
using INHERITED = GM; |
| 118 |
}; |
| 119 |
} // namespace |
| 120 |
|
| 121 |
DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat); ) |
| 122 |
DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror); ) |
| 123 |
DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp); ) |
| 124 |
|
| 125 |
DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat, true); ) |
| 126 |
DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror, true); ) |
| 127 |
DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp, true); ) |