2013-10-24 15:20:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkBitmap.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkBlendMode.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2013-10-24 15:20:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This GM checks that bitmap pixels are unpremultiplied before being exported
|
|
|
|
* to other formats. If unpremultiplication is implemented properly, this
|
|
|
|
* GM should come out completely white. If not, this GM looks like a row of two
|
|
|
|
* greyscale gradients above a row of grey lines.
|
|
|
|
* This tests both the ARGB4444 and ARGB8888 bitmap configurations.
|
|
|
|
*/
|
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr int SLIDE_SIZE = 256;
|
2013-10-24 15:20:00 +00:00
|
|
|
|
2014-02-17 21:21:46 +00:00
|
|
|
static void init_bitmap(SkColorType ct, SkBitmap* bitmap) {
|
|
|
|
bitmap->allocPixels(SkImageInfo::Make(SLIDE_SIZE, SLIDE_SIZE, ct,
|
|
|
|
kPremul_SkAlphaType));
|
2013-10-24 15:20:00 +00:00
|
|
|
bitmap->eraseColor(SK_ColorWHITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkBitmap make_argb8888_gradient() {
|
|
|
|
SkBitmap bitmap;
|
2014-04-11 17:15:40 +00:00
|
|
|
init_bitmap(kN32_SkColorType, &bitmap);
|
2013-10-24 15:20:00 +00:00
|
|
|
for (int y = 0; y < SLIDE_SIZE; y++) {
|
|
|
|
uint32_t* dst = bitmap.getAddr32(0, y);
|
|
|
|
for (int x = 0; x < SLIDE_SIZE; x++) {
|
2018-11-02 20:34:01 +00:00
|
|
|
dst[x] = SkPackARGB32(y, y, y, y);
|
2013-10-24 15:20:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkBitmap make_argb4444_gradient() {
|
|
|
|
SkBitmap bitmap;
|
2014-02-17 21:21:46 +00:00
|
|
|
init_bitmap(kARGB_4444_SkColorType, &bitmap);
|
2018-11-02 20:34:01 +00:00
|
|
|
// Using draw rather than readPixels to suppress dither
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
|
|
|
SkCanvas{ bitmap }.drawBitmap(make_argb8888_gradient(), 0, 0, &paint);
|
2013-10-24 15:20:00 +00:00
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkBitmap make_argb8888_stripes() {
|
|
|
|
SkBitmap bitmap;
|
2014-04-11 17:15:40 +00:00
|
|
|
init_bitmap(kN32_SkColorType, &bitmap);
|
2013-10-24 15:20:00 +00:00
|
|
|
uint8_t rowColor = 0;
|
|
|
|
for (int y = 0; y < SLIDE_SIZE; y++) {
|
|
|
|
uint32_t* dst = bitmap.getAddr32(0, y);
|
|
|
|
for (int x = 0; x < SLIDE_SIZE; x++) {
|
|
|
|
dst[x] = SkPackARGB32(rowColor, rowColor,
|
|
|
|
rowColor, rowColor);
|
|
|
|
}
|
|
|
|
if (rowColor == 0) {
|
|
|
|
rowColor = 255;
|
|
|
|
} else {
|
|
|
|
rowColor = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkBitmap make_argb4444_stripes() {
|
|
|
|
SkBitmap bitmap;
|
2014-02-17 21:21:46 +00:00
|
|
|
init_bitmap(kARGB_4444_SkColorType, &bitmap);
|
2018-11-02 20:34:01 +00:00
|
|
|
// Using draw rather than readPixels to suppress dither
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setBlendMode(SkBlendMode::kSrc);
|
|
|
|
SkCanvas{ bitmap }.drawBitmap(make_argb8888_stripes(), 0, 0, &paint);
|
2013-10-24 15:20:00 +00:00
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class BitmapPremulGM : public GM {
|
|
|
|
public:
|
|
|
|
BitmapPremulGM() {
|
|
|
|
this->setBGColor(SK_ColorWHITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2013-10-24 15:20:00 +00:00
|
|
|
return SkString("bitmap_premul");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2013-10-24 15:20:00 +00:00
|
|
|
return SkISize::Make(SLIDE_SIZE * 2, SLIDE_SIZE * 2);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-10-24 15:20:00 +00:00
|
|
|
SkScalar slideSize = SkIntToScalar(SLIDE_SIZE);
|
|
|
|
canvas->drawBitmap(make_argb8888_gradient(), 0, 0);
|
|
|
|
canvas->drawBitmap(make_argb4444_gradient(), slideSize, 0);
|
|
|
|
canvas->drawBitmap(make_argb8888_stripes(), 0, slideSize);
|
|
|
|
canvas->drawBitmap(make_argb4444_stripes(), slideSize, slideSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2013-10-24 15:20:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM( return new BitmapPremulGM; )
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|