| 1 |
/* |
| 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 |
*/ |
| 7 |
|
| 8 |
#include "gm/gm.h" |
| 9 |
#include "include/core/SkBitmap.h" |
| 10 |
#include "include/core/SkBlendMode.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkColor.h" |
| 13 |
#include "include/core/SkImage.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkPoint.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkRefCnt.h" |
| 18 |
#include "include/core/SkSamplingOptions.h" |
| 19 |
#include "include/core/SkSize.h" |
| 20 |
#include "include/core/SkString.h" |
| 21 |
#include "include/core/SkSurface.h" |
| 22 |
#include "include/core/SkTileMode.h" |
| 23 |
#include "include/core/SkTypes.h" |
| 24 |
#include "include/effects/SkGradient.h" |
| 25 |
#include "tools/GpuToolUtils.h" |
| 26 |
#include "tools/ToolUtils.h" |
| 27 |
|
| 28 |
#include <cstddef> |
| 29 |
#include <iterator> |
| 30 |
|
| 31 |
static sk_sp<SkImage> make_image(SkCanvas* destCanvas) { |
| 32 |
auto surf = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(64, 64)); |
| 33 |
auto tmpCanvas = surf->getCanvas(); |
| 34 |
|
| 35 |
tmpCanvas->drawColor(SK_ColorRED); |
| 36 |
SkPaint paint; |
| 37 |
paint.setAntiAlias(true); |
| 38 |
const SkPoint pts[] = { { 0, 0 }, { 64, 64 } }; |
| 39 |
const SkColor4f colors[] = { SkColors::kWhite, SkColors::kBlue }; |
| 40 |
paint.setShader(SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {}})); |
| 41 |
tmpCanvas->drawCircle(32, 32, 32, paint); |
| 42 |
|
| 43 |
return ToolUtils::MakeTextureImage(destCanvas, surf->makeImageSnapshot()); |
| 44 |
} |
| 45 |
|
| 46 |
class DrawBitmapRect2 : public skiagm::GM { |
| 47 |
bool fUseIRect; |
| 48 |
public: |
| 49 |
DrawBitmapRect2(bool useIRect) : fUseIRect(useIRect) { |
| 50 |
} |
| 51 |
|
| 52 |
protected: |
| 53 |
SkString getName() const override { |
| 54 |
SkString str; |
| 55 |
str.printf("bitmaprect_%s", fUseIRect ? "i" : "s"); |
| 56 |
return str; |
| 57 |
} |
| 58 |
|
| 59 |
SkISize getISize() override { return SkISize::Make(640, 480); } |
| 60 |
|
| 61 |
void onDraw(SkCanvas* canvas) override { |
| 62 |
canvas->drawColor(0xFFCCCCCC); |
| 63 |
|
| 64 |
const SkIRect src[] = { |
| 65 |
{ 0, 0, 32, 32 }, |
| 66 |
{ 0, 0, 80, 80 }, |
| 67 |
{ 32, 32, 96, 96 }, |
| 68 |
{ -32, -32, 32, 32, } |
| 69 |
}; |
| 70 |
|
| 71 |
SkPaint paint; |
| 72 |
paint.setStyle(SkPaint::kStroke_Style); |
| 73 |
auto sampling = SkSamplingOptions(); |
| 74 |
|
| 75 |
auto image = make_image(canvas); |
| 76 |
|
| 77 |
SkRect dstR = { 0, 200, 128, 380 }; |
| 78 |
|
| 79 |
canvas->translate(16, 40); |
| 80 |
for (size_t i = 0; i < std::size(src); i++) { |
| 81 |
SkRect srcR; |
| 82 |
srcR.set(src[i]); |
| 83 |
|
| 84 |
canvas->drawImage(image, 0, 0, sampling, &paint); |
| 85 |
if (!fUseIRect) { |
| 86 |
canvas->drawImageRect(image.get(), srcR, dstR, sampling, &paint, |
| 87 |
SkCanvas::kStrict_SrcRectConstraint); |
| 88 |
} else { |
| 89 |
canvas->drawImageRect(image.get(), SkRect::Make(src[i]), dstR, sampling, &paint, |
| 90 |
SkCanvas::kStrict_SrcRectConstraint); |
| 91 |
} |
| 92 |
|
| 93 |
canvas->drawRect(dstR, paint); |
| 94 |
canvas->drawRect(srcR, paint); |
| 95 |
|
| 96 |
canvas->translate(160, 0); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
private: |
| 101 |
using INHERITED = skiagm::GM; |
| 102 |
}; |
| 103 |
|
| 104 |
////////////////////////////////////////////////////////////////////////////// |
| 105 |
|
| 106 |
static void make_3x3_bitmap(SkBitmap* bitmap) { |
| 107 |
const int xSize = 3; |
| 108 |
const int ySize = 3; |
| 109 |
|
| 110 |
const SkColor textureData[xSize][ySize] = { |
| 111 |
{ SK_ColorRED, SK_ColorWHITE, SK_ColorBLUE }, |
| 112 |
{ SK_ColorGREEN, SK_ColorBLACK, SK_ColorCYAN }, |
| 113 |
{ SK_ColorYELLOW, SK_ColorGRAY, SK_ColorMAGENTA } |
| 114 |
}; |
| 115 |
|
| 116 |
bitmap->allocN32Pixels(xSize, ySize, true); |
| 117 |
SkCanvas canvas(*bitmap); |
| 118 |
SkPaint paint; |
| 119 |
|
| 120 |
for (int y = 0; y < ySize; y++) { |
| 121 |
for (int x = 0; x < xSize; x++) { |
| 122 |
paint.setColor(textureData[x][y]); |
| 123 |
canvas.drawIRect(SkIRect::MakeXYWH(x, y, 1, 1), paint); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
// This GM attempts to make visible any issues drawBitmapRect may have |
| 129 |
// with partial source rects. In this case the eight pixels on the border |
| 130 |
// should be half the width/height of the central pixel, i.e.: |
| 131 |
// __|____|__ |
| 132 |
// | | |
| 133 |
// __|____|__ |
| 134 |
// | | |
| 135 |
class DrawBitmapRect3 : public skiagm::GM { |
| 136 |
public: |
| 137 |
DrawBitmapRect3() { |
| 138 |
this->setBGColor(SK_ColorBLACK); |
| 139 |
} |
| 140 |
|
| 141 |
protected: |
| 142 |
SkString getName() const override { |
| 143 |
SkString str; |
| 144 |
str.printf("3x3bitmaprect"); |
| 145 |
return str; |
| 146 |
} |
| 147 |
|
| 148 |
SkISize getISize() override { return SkISize::Make(640, 480); } |
| 149 |
|
| 150 |
void onDraw(SkCanvas* canvas) override { |
| 151 |
|
| 152 |
SkBitmap bitmap; |
| 153 |
make_3x3_bitmap(&bitmap); |
| 154 |
|
| 155 |
SkRect srcR = { 0.5f, 0.5f, 2.5f, 2.5f }; |
| 156 |
SkRect dstR = { 100, 100, 300, 200 }; |
| 157 |
|
| 158 |
canvas->drawImageRect(ToolUtils::MakeTextureImage(canvas, bitmap.asImage()), |
| 159 |
srcR, dstR, SkSamplingOptions(), |
| 160 |
nullptr, SkCanvas::kStrict_SrcRectConstraint); |
| 161 |
} |
| 162 |
|
| 163 |
private: |
| 164 |
using INHERITED = skiagm::GM; |
| 165 |
}; |
| 166 |
|
| 167 |
////////////////////////////////////////////////////////////////////////////// |
| 168 |
static sk_sp<SkImage> make_big_bitmap(SkCanvas* canvas) { |
| 169 |
|
| 170 |
constexpr int gXSize = 4096; |
| 171 |
constexpr int gYSize = 4096; |
| 172 |
constexpr int gBorderWidth = 10; |
| 173 |
|
| 174 |
SkBitmap bitmap; |
| 175 |
bitmap.allocN32Pixels(gXSize, gYSize); |
| 176 |
for (int y = 0; y < gYSize; ++y) { |
| 177 |
for (int x = 0; x < gXSize; ++x) { |
| 178 |
if (x <= gBorderWidth || x >= gXSize-gBorderWidth || |
| 179 |
y <= gBorderWidth || y >= gYSize-gBorderWidth) { |
| 180 |
*bitmap.getAddr32(x, y) = SkPreMultiplyColor(0x88FFFFFF); |
| 181 |
} else { |
| 182 |
*bitmap.getAddr32(x, y) = SkPreMultiplyColor(0x88FF0000); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
bitmap.setImmutable(); |
| 187 |
return ToolUtils::MakeTextureImage(canvas, bitmap.asImage()); |
| 188 |
} |
| 189 |
|
| 190 |
// This GM attempts to reveal any issues we may have when the GPU has to |
| 191 |
// break up a large texture in order to draw it. The XOR transfer mode will |
| 192 |
// create stripes in the image if there is imprecision in the destination |
| 193 |
// tile placement. |
| 194 |
class DrawBitmapRect4 : public skiagm::GM { |
| 195 |
bool fUseIRect; |
| 196 |
sk_sp<SkImage> fBigImage; |
| 197 |
|
| 198 |
public: |
| 199 |
DrawBitmapRect4(bool useIRect) : fUseIRect(useIRect) { |
| 200 |
this->setBGColor(0x88444444); |
| 201 |
} |
| 202 |
|
| 203 |
protected: |
| 204 |
SkString getName() const override { |
| 205 |
SkString str; |
| 206 |
str.printf("bigbitmaprect_%s", fUseIRect ? "i" : "s"); |
| 207 |
return str; |
| 208 |
} |
| 209 |
|
| 210 |
SkISize getISize() override { return SkISize::Make(640, 480); } |
| 211 |
|
| 212 |
void onDraw(SkCanvas* canvas) override { |
| 213 |
if (!fBigImage) { |
| 214 |
fBigImage = make_big_bitmap(canvas); |
| 215 |
} |
| 216 |
|
| 217 |
SkPaint paint; |
| 218 |
paint.setAlpha(128); |
| 219 |
paint.setBlendMode(SkBlendMode::kXor); |
| 220 |
SkSamplingOptions sampling; |
| 221 |
|
| 222 |
SkRect srcR1 = { 0.0f, 0.0f, 4096.0f, 2040.0f }; |
| 223 |
SkRect dstR1 = { 10.1f, 10.1f, 629.9f, 400.9f }; |
| 224 |
|
| 225 |
SkRect srcR2 = { 4085.0f, 10.0f, 4087.0f, 12.0f }; |
| 226 |
SkRect dstR2 = { 10, 410, 30, 430 }; |
| 227 |
|
| 228 |
if (!fUseIRect) { |
| 229 |
canvas->drawImageRect(fBigImage, srcR1, dstR1, sampling, &paint, |
| 230 |
SkCanvas::kStrict_SrcRectConstraint); |
| 231 |
canvas->drawImageRect(fBigImage, srcR2, dstR2, sampling, &paint, |
| 232 |
SkCanvas::kStrict_SrcRectConstraint); |
| 233 |
} else { |
| 234 |
canvas->drawImageRect(fBigImage, SkRect::Make(srcR1.roundOut()), dstR1, sampling, |
| 235 |
&paint, SkCanvas::kStrict_SrcRectConstraint); |
| 236 |
canvas->drawImageRect(fBigImage, SkRect::Make(srcR2.roundOut()), dstR2, sampling, |
| 237 |
&paint, SkCanvas::kStrict_SrcRectConstraint); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
private: |
| 242 |
using INHERITED = skiagm::GM; |
| 243 |
}; |
| 244 |
|
| 245 |
class BitmapRectRounding : public skiagm::GM { |
| 246 |
SkBitmap fBM; |
| 247 |
|
| 248 |
public: |
| 249 |
BitmapRectRounding() {} |
| 250 |
|
| 251 |
protected: |
| 252 |
SkString getName() const override { |
| 253 |
SkString str; |
| 254 |
str.printf("bitmaprect_rounding"); |
| 255 |
return str; |
| 256 |
} |
| 257 |
|
| 258 |
SkISize getISize() override { return SkISize::Make(640, 480); } |
| 259 |
|
| 260 |
void onOnceBeforeDraw() override { |
| 261 |
fBM.allocN32Pixels(10, 10); |
| 262 |
fBM.eraseColor(SK_ColorBLUE); |
| 263 |
} |
| 264 |
|
| 265 |
// This choice of coordinates and matrix land the bottom edge of the clip (and bitmap dst) |
| 266 |
// at exactly 1/2 pixel boundary. However, drawBitmapRect may lose precision along the way. |
| 267 |
// If it does, we may see a red-line at the bottom, instead of the bitmap exactly matching |
| 268 |
// the clip (in which case we should see all blue). |
| 269 |
// The correct image should be all blue. |
| 270 |
void onDraw(SkCanvas* canvas) override { |
| 271 |
SkPaint paint; |
| 272 |
paint.setColor(SK_ColorRED); |
| 273 |
|
| 274 |
const SkRect r = SkRect::MakeXYWH(1, 1, 110, 114); |
| 275 |
canvas->scale(0.9f, 0.9f); |
| 276 |
|
| 277 |
// the drawRect shows the same problem as clipRect(r) followed by drawcolor(red) |
| 278 |
canvas->drawRect(r, paint); |
| 279 |
canvas->drawImageRect(fBM.asImage(), r, SkSamplingOptions()); |
| 280 |
} |
| 281 |
|
| 282 |
private: |
| 283 |
using INHERITED = skiagm::GM; |
| 284 |
}; |
| 285 |
DEF_GM( return new BitmapRectRounding; ) |
| 286 |
|
| 287 |
////////////////////////////////////////////////////////////////////////////// |
| 288 |
|
| 289 |
DEF_GM( return new DrawBitmapRect2(false); ) |
| 290 |
DEF_GM( return new DrawBitmapRect2(true); ) |
| 291 |
DEF_GM( return new DrawBitmapRect3(); ) |
| 292 |
|
| 293 |
#ifndef SK_BUILD_FOR_ANDROID |
| 294 |
DEF_GM( return new DrawBitmapRect4(false); ) |
| 295 |
DEF_GM( return new DrawBitmapRect4(true); ) |
| 296 |
#endif |