2011-12-02 19:11:17 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2018-12-13 15:08:05 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkFont.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2011-12-02 19:11:17 +00:00
|
|
|
|
2019-05-01 14:59:30 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
namespace {
|
2011-12-02 19:11:17 +00:00
|
|
|
|
2018-01-25 19:07:47 +00:00
|
|
|
static const char* color_type_name(SkColorType colorType) {
|
|
|
|
switch (colorType) {
|
2019-09-19 20:05:48 +00:00
|
|
|
case kUnknown_SkColorType: return "unknown";
|
|
|
|
case kAlpha_8_SkColorType: return "A8";
|
|
|
|
case kRGB_565_SkColorType: return "565";
|
|
|
|
case kARGB_4444_SkColorType: return "4444";
|
|
|
|
case kRGBA_8888_SkColorType: return "8888";
|
|
|
|
case kRGB_888x_SkColorType: return "888x";
|
|
|
|
case kBGRA_8888_SkColorType: return "8888";
|
|
|
|
case kRGBA_1010102_SkColorType: return "1010102";
|
|
|
|
case kRGB_101010x_SkColorType: return "101010x";
|
2020-02-11 18:19:08 +00:00
|
|
|
case kBGRA_1010102_SkColorType: return "bgra1010102";
|
|
|
|
case kBGR_101010x_SkColorType: return "bgr101010x";
|
2019-09-19 20:05:48 +00:00
|
|
|
case kGray_8_SkColorType: return "G8";
|
|
|
|
case kRGBA_F16Norm_SkColorType: return "F16Norm";
|
|
|
|
case kRGBA_F16_SkColorType: return "F16";
|
|
|
|
case kRGBA_F32_SkColorType: return "F32";
|
|
|
|
case kR8G8_unorm_SkColorType: return "R8G8_unorm";
|
|
|
|
case kA16_unorm_SkColorType: return "A16_unorm";
|
|
|
|
case kR16G16_unorm_SkColorType: return "R16G16_unorm";
|
|
|
|
case kA16_float_SkColorType: return "A16_float";
|
|
|
|
case kR16G16_float_SkColorType: return "R16G16_float";
|
|
|
|
case kR16G16B16A16_unorm_SkColorType: return "R16G16B16A16_unorm";
|
2018-01-25 19:07:47 +00:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2011-12-02 19:11:17 +00:00
|
|
|
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkColorType gColorTypes[] = {
|
2014-02-23 03:59:35 +00:00
|
|
|
kRGB_565_SkColorType,
|
|
|
|
kARGB_4444_SkColorType,
|
2014-04-11 17:15:40 +00:00
|
|
|
kN32_SkColorType,
|
2011-12-02 19:11:17 +00:00
|
|
|
};
|
|
|
|
|
2014-02-23 03:59:35 +00:00
|
|
|
#define NUM_CONFIGS SK_ARRAY_COUNT(gColorTypes)
|
2011-12-02 19:11:17 +00:00
|
|
|
|
|
|
|
static void draw_checks(SkCanvas* canvas, int width, int height) {
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorRED);
|
2017-02-22 18:21:42 +00:00
|
|
|
canvas->drawRect(SkRect::MakeIWH(width/2, height/2), paint);
|
2011-12-02 19:11:17 +00:00
|
|
|
paint.setColor(SK_ColorGREEN);
|
2017-02-22 18:21:42 +00:00
|
|
|
canvas->drawRect({ SkIntToScalar(width/2), 0, SkIntToScalar(width), SkIntToScalar(height/2) },
|
|
|
|
paint);
|
2011-12-02 19:11:17 +00:00
|
|
|
paint.setColor(SK_ColorBLUE);
|
2017-02-22 18:21:42 +00:00
|
|
|
canvas->drawRect({ 0, SkIntToScalar(height/2), SkIntToScalar(width/2), SkIntToScalar(height) },
|
|
|
|
paint);
|
2011-12-02 19:11:17 +00:00
|
|
|
paint.setColor(SK_ColorYELLOW);
|
2017-02-22 18:21:42 +00:00
|
|
|
canvas->drawRect({ SkIntToScalar(width/2), SkIntToScalar(height/2), SkIntToScalar(width),
|
|
|
|
SkIntToScalar(height) }, paint);
|
2011-12-02 19:11:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
class BitmapCopyGM : public skiagm::GM {
|
2011-12-02 19:11:17 +00:00
|
|
|
SkBitmap fDst[NUM_CONFIGS];
|
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
void onOnceBeforeDraw() override { this->setBGColor(0xFFDDDDDD); }
|
2011-12-02 19:11:17 +00:00
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
SkString onShortName() override { return SkString("bitmapcopy"); }
|
2011-12-02 19:11:17 +00:00
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
SkISize onISize() override { return {540, 330}; }
|
2011-12-02 19:11:17 +00:00
|
|
|
|
2019-07-18 15:51:19 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2011-12-02 19:11:17 +00:00
|
|
|
SkPaint paint;
|
2014-02-13 17:14:46 +00:00
|
|
|
SkScalar horizMargin = 10;
|
|
|
|
SkScalar vertMargin = 10;
|
2011-12-02 19:11:17 +00:00
|
|
|
|
2014-02-13 17:14:46 +00:00
|
|
|
SkBitmap src;
|
2017-04-28 20:06:34 +00:00
|
|
|
src.allocN32Pixels(40, 40, kOpaque_SkAlphaType);
|
2014-02-13 17:14:46 +00:00
|
|
|
SkCanvas canvasTmp(src);
|
2013-08-08 07:01:20 +00:00
|
|
|
|
2013-08-07 17:03:09 +00:00
|
|
|
draw_checks(&canvasTmp, 40, 40);
|
2011-12-02 19:11:17 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < NUM_CONFIGS; ++i) {
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::copy_to(&fDst[i], gColorTypes[i], src);
|
2011-12-02 19:11:17 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 14:17:03 +00:00
|
|
|
canvas->clear(0xFFDDDDDD);
|
2011-12-02 19:11:17 +00:00
|
|
|
paint.setAntiAlias(true);
|
2018-12-13 15:08:05 +00:00
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
SkFont font(ToolUtils::create_portable_typeface());
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2011-12-02 19:11:17 +00:00
|
|
|
SkScalar width = SkIntToScalar(40);
|
|
|
|
SkScalar height = SkIntToScalar(40);
|
2018-12-13 15:08:05 +00:00
|
|
|
if (font.getSpacing() > height) {
|
|
|
|
height = font.getSpacing();
|
2011-12-02 19:11:17 +00:00
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < NUM_CONFIGS; i++) {
|
2018-01-25 19:07:47 +00:00
|
|
|
const char* name = color_type_name(src.colorType());
|
2019-05-07 19:38:46 +00:00
|
|
|
SkScalar textWidth = font.measureText(name, strlen(name), SkTextEncoding::kUTF8);
|
2011-12-02 19:11:17 +00:00
|
|
|
if (textWidth > width) {
|
|
|
|
width = textWidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SkScalar horizOffset = width + horizMargin;
|
|
|
|
SkScalar vertOffset = height + vertMargin;
|
|
|
|
canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < NUM_CONFIGS; i++) {
|
|
|
|
canvas->save();
|
|
|
|
// Draw destination config name
|
2018-01-25 19:07:47 +00:00
|
|
|
const char* name = color_type_name(fDst[i].colorType());
|
2019-05-07 19:38:46 +00:00
|
|
|
SkScalar textWidth = font.measureText(name, strlen(name), SkTextEncoding::kUTF8);
|
2011-12-02 19:11:17 +00:00
|
|
|
SkScalar x = (width - textWidth) / SkScalar(2);
|
2018-12-13 15:08:05 +00:00
|
|
|
SkScalar y = font.getSpacing() / SkScalar(2);
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(name, strlen(name), SkTextEncoding::kUTF8, x, y, font, paint);
|
2011-12-02 19:11:17 +00:00
|
|
|
|
|
|
|
// Draw destination bitmap
|
|
|
|
canvas->translate(0, vertOffset);
|
|
|
|
x = (width - 40) / SkScalar(2);
|
2021-01-24 03:07:05 +00:00
|
|
|
canvas->drawImage(fDst[i].asImage(), x, 0, SkSamplingOptions(), &paint);
|
2011-12-02 19:11:17 +00:00
|
|
|
canvas->restore();
|
|
|
|
|
|
|
|
canvas->translate(horizOffset, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-07-18 15:51:19 +00:00
|
|
|
} // namespace
|
2011-12-02 19:11:17 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-01-23 15:22:01 +00:00
|
|
|
DEF_GM( return new BitmapCopyGM; )
|