| 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/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkColorType.h" |
| 13 |
#include "include/core/SkFont.h" |
| 14 |
#include "include/core/SkFontTypes.h" |
| 15 |
#include "include/core/SkImageInfo.h" |
| 16 |
#include "include/core/SkPaint.h" |
| 17 |
#include "include/core/SkRect.h" |
| 18 |
#include "include/core/SkScalar.h" |
| 19 |
#include "include/core/SkSize.h" |
| 20 |
#include "include/core/SkString.h" |
| 21 |
#include "include/core/SkTypeface.h" |
| 22 |
#include "include/core/SkTypes.h" |
| 23 |
#include "tools/ToolUtils.h" |
| 24 |
#include "tools/fonts/FontToolUtils.h" |
| 25 |
|
| 26 |
#include <string.h> |
| 27 |
|
| 28 |
namespace { |
| 29 |
|
| 30 |
static const char* color_type_name(SkColorType colorType) { |
| 31 |
switch (colorType) { |
| 32 |
case kUnknown_SkColorType: return "unknown"; |
| 33 |
case kAlpha_8_SkColorType: return "A8"; |
| 34 |
case kRGB_565_SkColorType: return "565"; |
| 35 |
case kARGB_4444_SkColorType: return "4444"; |
| 36 |
case kRGBA_8888_SkColorType: return "8888"; |
| 37 |
case kRGB_888x_SkColorType: return "888x"; |
| 38 |
case kBGRA_8888_SkColorType: return "8888"; |
| 39 |
case kRGBA_1010102_SkColorType: return "1010102"; |
| 40 |
case kRGB_101010x_SkColorType: return "101010x"; |
| 41 |
case kBGRA_1010102_SkColorType: return "bgra1010102"; |
| 42 |
case kBGR_101010x_SkColorType: return "bgr101010x"; |
| 43 |
case kBGR_101010x_XR_SkColorType: return "bgr101010x_xr"; |
| 44 |
case kBGRA_10101010_XR_SkColorType: return "bgra10101010_xr"; |
| 45 |
case kRGBA_10x6_SkColorType: return "10101010"; |
| 46 |
case kGray_8_SkColorType: return "G8"; |
| 47 |
case kRGBA_F16Norm_SkColorType: return "F16Norm"; |
| 48 |
case kRGB_F16F16F16x_SkColorType: return "F16F16F16x"; |
| 49 |
case kRGBA_F16_SkColorType: return "F16"; |
| 50 |
case kRGBA_F32_SkColorType: return "F32"; |
| 51 |
case kR8G8_unorm_SkColorType: return "R8G8_unorm"; |
| 52 |
case kA16_unorm_SkColorType: return "A16_unorm"; |
| 53 |
case kR16_unorm_SkColorType: return "R16_unorm"; |
| 54 |
case kR16G16_unorm_SkColorType: return "R16G16_unorm"; |
| 55 |
case kA16_float_SkColorType: return "A16_float"; |
| 56 |
case kR16G16_float_SkColorType: return "R16G16_float"; |
| 57 |
case kR16G16B16A16_unorm_SkColorType: return "R16G16B16A16_unorm"; |
| 58 |
case kSRGBA_8888_SkColorType: return "SRGBA_8888"; |
| 59 |
case kR8_unorm_SkColorType: return "R8_unorm"; |
| 60 |
} |
| 61 |
return ""; |
| 62 |
} |
| 63 |
|
| 64 |
constexpr SkColorType gColorTypes[] = { |
| 65 |
kRGB_565_SkColorType, |
| 66 |
kARGB_4444_SkColorType, |
| 67 |
kN32_SkColorType, |
| 68 |
}; |
| 69 |
|
| 70 |
#define NUM_CONFIGS std::size(gColorTypes) |
| 71 |
|
| 72 |
static void draw_checks(SkCanvas* canvas, int width, int height) { |
| 73 |
SkPaint paint; |
| 74 |
paint.setColor(SK_ColorRED); |
| 75 |
canvas->drawRect(SkRect::MakeIWH(width/2, height/2), paint); |
| 76 |
paint.setColor(SK_ColorGREEN); |
| 77 |
canvas->drawRect({ SkIntToScalar(width/2), 0, SkIntToScalar(width), SkIntToScalar(height/2) }, |
| 78 |
paint); |
| 79 |
paint.setColor(SK_ColorBLUE); |
| 80 |
canvas->drawRect({ 0, SkIntToScalar(height/2), SkIntToScalar(width/2), SkIntToScalar(height) }, |
| 81 |
paint); |
| 82 |
paint.setColor(SK_ColorYELLOW); |
| 83 |
canvas->drawRect({ SkIntToScalar(width/2), SkIntToScalar(height/2), SkIntToScalar(width), |
| 84 |
SkIntToScalar(height) }, paint); |
| 85 |
} |
| 86 |
|
| 87 |
class BitmapCopyGM : public skiagm::GM { |
| 88 |
SkBitmap fDst[NUM_CONFIGS]; |
| 89 |
|
| 90 |
void onOnceBeforeDraw() override { this->setBGColor(0xFFDDDDDD); } |
| 91 |
|
| 92 |
SkString getName() const override { return SkString("bitmapcopy"); } |
| 93 |
|
| 94 |
SkISize getISize() override { return {540, 330}; } |
| 95 |
|
| 96 |
void onDraw(SkCanvas* canvas) override { |
| 97 |
SkPaint paint; |
| 98 |
SkScalar horizMargin = 10; |
| 99 |
SkScalar vertMargin = 10; |
| 100 |
|
| 101 |
SkBitmap src; |
| 102 |
src.allocN32Pixels(40, 40, kOpaque_SkAlphaType); |
| 103 |
SkCanvas canvasTmp(src); |
| 104 |
|
| 105 |
draw_checks(&canvasTmp, 40, 40); |
| 106 |
|
| 107 |
for (unsigned i = 0; i < NUM_CONFIGS; ++i) { |
| 108 |
ToolUtils::copy_to(&fDst[i], gColorTypes[i], src); |
| 109 |
} |
| 110 |
|
| 111 |
canvas->clear(0xFFDDDDDD); |
| 112 |
paint.setAntiAlias(true); |
| 113 |
|
| 114 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 115 |
|
| 116 |
SkScalar width = SkIntToScalar(40); |
| 117 |
SkScalar height = SkIntToScalar(40); |
| 118 |
if (font.getSpacing() > height) { |
| 119 |
height = font.getSpacing(); |
| 120 |
} |
| 121 |
for (unsigned i = 0; i < NUM_CONFIGS; i++) { |
| 122 |
const char* name = color_type_name(src.colorType()); |
| 123 |
SkScalar textWidth = font.measureText(name, strlen(name), SkTextEncoding::kUTF8); |
| 124 |
if (textWidth > width) { |
| 125 |
width = textWidth; |
| 126 |
} |
| 127 |
} |
| 128 |
SkScalar horizOffset = width + horizMargin; |
| 129 |
SkScalar vertOffset = height + vertMargin; |
| 130 |
canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 131 |
|
| 132 |
for (unsigned i = 0; i < NUM_CONFIGS; i++) { |
| 133 |
canvas->save(); |
| 134 |
// Draw destination config name |
| 135 |
const char* name = color_type_name(fDst[i].colorType()); |
| 136 |
SkScalar textWidth = font.measureText(name, strlen(name), SkTextEncoding::kUTF8); |
| 137 |
SkScalar x = (width - textWidth) / SkScalar(2); |
| 138 |
SkScalar y = font.getSpacing() / SkScalar(2); |
| 139 |
canvas->drawSimpleText(name, strlen(name), SkTextEncoding::kUTF8, x, y, font, paint); |
| 140 |
|
| 141 |
// Draw destination bitmap |
| 142 |
canvas->translate(0, vertOffset); |
| 143 |
x = (width - 40) / SkScalar(2); |
| 144 |
canvas->drawImage(fDst[i].asImage(), x, 0, SkSamplingOptions(), &paint); |
| 145 |
canvas->restore(); |
| 146 |
|
| 147 |
canvas->translate(horizOffset, 0); |
| 148 |
} |
| 149 |
} |
| 150 |
}; |
| 151 |
} // namespace |
| 152 |
|
| 153 |
////////////////////////////////////////////////////////////////////////////// |
| 154 |
|
| 155 |
DEF_GM( return new BitmapCopyGM; ) |