Update compressed texturing GM
This CL has some extra headroom for upcoming kBC1_RGBA8_UNORM support. Bug: skia:9680 Change-Id: I866c7fe12657a41575c57dcd001a6a09477fc44a Reviewed-on: https://skia-review.googlesource.com/c/skia/+/264096 Commit-Queue: Robert Phillips <robertphillips@google.com> Reviewed-by: Greg Daniel <egdaniel@google.com>
This commit is contained in:
parent
cb67acbab4
commit
ac9080283e
231
gm/compressed_textures.cpp
Normal file
231
gm/compressed_textures.cpp
Normal file
@ -0,0 +1,231 @@
|
||||
/*
|
||||
* Copyright 2020 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "include/core/SkTypes.h" // IWYU pragma: keep
|
||||
|
||||
#if !defined(SK_BUILD_FOR_GOOGLE3)
|
||||
|
||||
#include "gm/gm.h"
|
||||
#include "include/core/SkBitmap.h"
|
||||
#include "include/core/SkCanvas.h"
|
||||
#include "include/core/SkColor.h"
|
||||
#include "include/core/SkData.h"
|
||||
#include "include/core/SkImage.h"
|
||||
#include "include/core/SkImageInfo.h"
|
||||
#include "include/core/SkPath.h"
|
||||
#include "include/core/SkRect.h"
|
||||
#include "include/core/SkRefCnt.h"
|
||||
#include "include/core/SkSize.h"
|
||||
#include "include/core/SkString.h"
|
||||
#include "src/core/SkMipMap.h"
|
||||
#include "src/gpu/GrDataUtils.h"
|
||||
#include "third_party/etc1/etc1.h"
|
||||
|
||||
class GrContext;
|
||||
class GrRenderTargetContext;
|
||||
|
||||
static SkPoint gen_pt(float angle, const SkVector& scale) {
|
||||
SkScalar s = SkScalarSin(angle);
|
||||
SkScalar c = SkScalarCos(angle);
|
||||
|
||||
return { scale.fX * c, scale.fY * s };
|
||||
}
|
||||
|
||||
// The resulting path will be centered at (0,0) and its size will match 'dimensions'
|
||||
static SkPath make_gear(SkISize dimensions, int numTeeth) {
|
||||
SkVector outerRad{ dimensions.fWidth / 2.0f, dimensions.fHeight / 2.0f };
|
||||
SkVector innerRad{ dimensions.fWidth / 2.5f, dimensions.fHeight / 2.5f };
|
||||
const float kAnglePerTooth = SK_ScalarPI / numTeeth;
|
||||
|
||||
float angle = 0.0f;
|
||||
|
||||
SkPath tmp;
|
||||
tmp.setFillType(SkPathFillType::kWinding);
|
||||
|
||||
tmp.moveTo(gen_pt(angle, outerRad));
|
||||
|
||||
for (int i = 0; i < numTeeth; ++i, angle += 2*kAnglePerTooth) {
|
||||
tmp.lineTo(gen_pt(angle+kAnglePerTooth, outerRad));
|
||||
tmp.lineTo(gen_pt(angle+kAnglePerTooth, innerRad));
|
||||
tmp.lineTo(gen_pt(angle+2*kAnglePerTooth, innerRad));
|
||||
tmp.lineTo(gen_pt(angle+2*kAnglePerTooth, outerRad));
|
||||
}
|
||||
|
||||
tmp.close();
|
||||
|
||||
float fInnerRad = 0.1f * SkTMin(dimensions.fWidth, dimensions.fHeight);
|
||||
if (fInnerRad > 0.5f) {
|
||||
tmp.addCircle(0.0f, 0.0f, fInnerRad, SkPathDirection::kCCW);
|
||||
}
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
// Render one level of a mipmap
|
||||
SkBitmap render_level(SkISize dimensions, SkColor color, SkColorType colorType, bool opaque) {
|
||||
SkPath path = make_gear(dimensions, 9);
|
||||
|
||||
SkImageInfo ii = SkImageInfo::Make(dimensions.width(), dimensions.height(),
|
||||
colorType, opaque ? kOpaque_SkAlphaType
|
||||
: kPremul_SkAlphaType);
|
||||
SkBitmap bm;
|
||||
bm.allocPixels(ii);
|
||||
|
||||
bm.eraseColor(opaque ? SK_ColorBLACK : SK_ColorTRANSPARENT);
|
||||
|
||||
SkCanvas c(bm);
|
||||
|
||||
SkPaint paint;
|
||||
paint.setColor(color | 0xFF000000);
|
||||
paint.setAntiAlias(false);
|
||||
|
||||
c.translate(dimensions.width() / 2.0f, dimensions.height() / 2.0f);
|
||||
c.drawPath(path, paint);
|
||||
|
||||
return bm;
|
||||
}
|
||||
|
||||
// Create the compressed data blob needed to represent a mipmapped 2-color texture of the specified
|
||||
// compression format. In this case 2-color means either opaque black or transparent black plus
|
||||
// one other color.
|
||||
// Note that ETC1/ETC2_RGB8_UNORM only supports 565 opaque textures.
|
||||
static sk_sp<SkData> make_compressed_data(SkISize dimensions,
|
||||
SkColorType colorType,
|
||||
bool opaque,
|
||||
SkImage::CompressionType compression) {
|
||||
size_t totalSize = GrCompressedDataSize(compression, dimensions, nullptr, GrMipMapped::kYes);
|
||||
|
||||
sk_sp<SkData> tmp = SkData::MakeUninitialized(totalSize);
|
||||
char* pixels = (char*) tmp->writable_data();
|
||||
|
||||
int numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
|
||||
|
||||
size_t offset = 0;
|
||||
|
||||
// Use a different color for each mipmap level so we can visually evaluate the draws
|
||||
static const SkColor kColors[] = {
|
||||
SK_ColorRED,
|
||||
SK_ColorGREEN,
|
||||
SK_ColorBLUE,
|
||||
SK_ColorCYAN,
|
||||
SK_ColorMAGENTA,
|
||||
SK_ColorYELLOW,
|
||||
SK_ColorWHITE,
|
||||
};
|
||||
|
||||
for (int i = 0; i < numMipLevels; ++i) {
|
||||
size_t levelSize = GrCompressedDataSize(compression, dimensions, nullptr, GrMipMapped::kNo);
|
||||
|
||||
SkBitmap bm = render_level(dimensions, kColors[i%7], colorType, opaque);
|
||||
if (compression == SkImage::CompressionType::kETC2_RGB8_UNORM) {
|
||||
SkASSERT(bm.colorType() == kRGB_565_SkColorType);
|
||||
SkASSERT(opaque);
|
||||
|
||||
if (etc1_encode_image((unsigned char*)bm.getAddr16(0, 0),
|
||||
bm.width(), bm.height(), 2, bm.rowBytes(),
|
||||
(unsigned char*) &pixels[offset])) {
|
||||
return nullptr;
|
||||
}
|
||||
} else {
|
||||
GrTwoColorBC1Compress(bm.pixmap(), kColors[i%7], &pixels[offset]);
|
||||
}
|
||||
|
||||
offset += levelSize;
|
||||
dimensions = {SkTMax(1, dimensions.width()/2), SkTMax(1, dimensions.height()/2)};
|
||||
}
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
// Basic test of Ganesh's ETC1 and BC1 support
|
||||
// The layout is:
|
||||
// ETC2 BC1
|
||||
// --------------------------------------
|
||||
// RGB8 | kETC2_RGB8_UNORM | kBC1_RGB8_UNORM |
|
||||
// |--------------------------------------|
|
||||
// RGBA8 | | kBC1_RGBA8_UNORM |
|
||||
// --------------------------------------
|
||||
//
|
||||
class CompressedTexturesGM : public skiagm::GpuGM {
|
||||
public:
|
||||
CompressedTexturesGM() {
|
||||
this->setBGColor(0xFFCCCCCC);
|
||||
}
|
||||
|
||||
protected:
|
||||
SkString onShortName() override {
|
||||
return SkString("compressed_textures");
|
||||
}
|
||||
|
||||
SkISize onISize() override {
|
||||
return SkISize::Make(2*kCellWidth + 3*kPad, 2*kTexHeight + 3*kPad);
|
||||
}
|
||||
|
||||
void onOnceBeforeDraw() override {
|
||||
fOpaqueETC2Data = make_compressed_data({ kTexWidth, kTexHeight },
|
||||
kRGB_565_SkColorType, true,
|
||||
SkImage::CompressionType::kETC2_RGB8_UNORM);
|
||||
|
||||
fOpaqueBC1Data = make_compressed_data({ kTexWidth, kTexHeight },
|
||||
kRGBA_8888_SkColorType, true,
|
||||
SkImage::CompressionType::kBC1_RGB8_UNORM);
|
||||
}
|
||||
|
||||
void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
|
||||
this->drawCell(context, canvas, fOpaqueETC2Data,
|
||||
SkImage::CompressionType::kETC2_RGB8_UNORM, { kPad, kPad });
|
||||
|
||||
this->drawCell(context, canvas, fOpaqueBC1Data,
|
||||
SkImage::CompressionType::kBC1_RGB8_UNORM, { 2*kPad + kCellWidth, kPad });
|
||||
}
|
||||
|
||||
private:
|
||||
void drawCell(GrContext* context, SkCanvas* canvas, sk_sp<SkData> data,
|
||||
SkImage::CompressionType compression, SkIVector offset) {
|
||||
|
||||
sk_sp<SkImage> image = SkImage::MakeFromCompressed(context, data,
|
||||
kTexWidth, kTexHeight,
|
||||
compression, GrMipMapped::kYes);
|
||||
SkISize dimensions{ kTexWidth, kTexHeight };
|
||||
|
||||
int numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
|
||||
|
||||
SkPaint paint;
|
||||
paint.setFilterQuality(kHigh_SkFilterQuality); // to force mipmapping
|
||||
|
||||
for (int i = 0; i < numMipLevels; ++i) {
|
||||
SkRect r = SkRect::MakeXYWH(offset.fX, offset.fY,
|
||||
dimensions.width(), dimensions.height());
|
||||
|
||||
canvas->drawImageRect(image, r, &paint);
|
||||
|
||||
if (i == 0) {
|
||||
offset.fX += dimensions.width();
|
||||
} else {
|
||||
offset.fY += dimensions.height();
|
||||
}
|
||||
|
||||
dimensions = {SkTMax(1, dimensions.width()/2), SkTMax(1, dimensions.height()/2)};
|
||||
}
|
||||
}
|
||||
|
||||
static const int kPad = 8;
|
||||
static const int kTexWidth = 64;
|
||||
static const int kCellWidth = 1.5f * kTexWidth;
|
||||
static const int kTexHeight = 64;
|
||||
|
||||
sk_sp<SkData> fOpaqueETC2Data;
|
||||
sk_sp<SkData> fOpaqueBC1Data;
|
||||
|
||||
typedef GM INHERITED;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
DEF_GM(return new CompressedTexturesGM;)
|
||||
|
||||
#endif
|
91
gm/etc1.cpp
91
gm/etc1.cpp
@ -1,91 +0,0 @@
|
||||
/*
|
||||
* Copyright 2017 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "include/core/SkTypes.h" // IWYU pragma: keep
|
||||
|
||||
#if !defined(SK_BUILD_FOR_GOOGLE3)
|
||||
|
||||
#include "gm/gm.h"
|
||||
#include "include/core/SkBitmap.h"
|
||||
#include "include/core/SkCanvas.h"
|
||||
#include "include/core/SkColor.h"
|
||||
#include "include/core/SkData.h"
|
||||
#include "include/core/SkImage.h"
|
||||
#include "include/core/SkImageInfo.h"
|
||||
#include "include/core/SkRect.h"
|
||||
#include "include/core/SkRefCnt.h"
|
||||
#include "include/core/SkSize.h"
|
||||
#include "include/core/SkString.h"
|
||||
#include "third_party/etc1/etc1.h"
|
||||
|
||||
class GrContext;
|
||||
class GrRenderTargetContext;
|
||||
|
||||
// Basic test of Ganesh's ETC1 support
|
||||
class ETC1GM : public skiagm::GpuGM {
|
||||
public:
|
||||
ETC1GM() {
|
||||
this->setBGColor(0xFFCCCCCC);
|
||||
}
|
||||
|
||||
protected:
|
||||
SkString onShortName() override {
|
||||
return SkString("etc1");
|
||||
}
|
||||
|
||||
SkISize onISize() override {
|
||||
return SkISize::Make(kTexWidth + 2*kPad, kTexHeight + 2*kPad);
|
||||
}
|
||||
|
||||
void onOnceBeforeDraw() override {
|
||||
SkBitmap bm;
|
||||
SkImageInfo ii = SkImageInfo::Make(kTexWidth, kTexHeight, kRGB_565_SkColorType,
|
||||
kOpaque_SkAlphaType);
|
||||
bm.allocPixels(ii);
|
||||
|
||||
bm.erase(SK_ColorBLUE, SkIRect::MakeWH(kTexWidth, kTexHeight));
|
||||
|
||||
for (int y = 0; y < kTexHeight; y += 4) {
|
||||
for (int x = 0; x < kTexWidth; x += 4) {
|
||||
bm.erase((x+y) % 8 ? SK_ColorRED : SK_ColorGREEN, SkIRect::MakeXYWH(x, y, 4, 4));
|
||||
}
|
||||
}
|
||||
|
||||
int size = etc1_get_encoded_data_size(bm.width(), bm.height());
|
||||
fETC1Data = SkData::MakeUninitialized(size);
|
||||
|
||||
unsigned char* pixels = (unsigned char*) fETC1Data->writable_data();
|
||||
|
||||
if (etc1_encode_image((unsigned char*) bm.getAddr16(0, 0),
|
||||
bm.width(), bm.height(), 2, bm.rowBytes(), pixels)) {
|
||||
fETC1Data = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
|
||||
auto image = SkImage::MakeFromCompressed(context, fETC1Data,
|
||||
kTexWidth, kTexHeight,
|
||||
SkImage::CompressionType::kETC2_RGB8_UNORM);
|
||||
|
||||
canvas->drawImage(image, 0, 0);
|
||||
}
|
||||
|
||||
private:
|
||||
static const int kPad = 8;
|
||||
static const int kTexWidth = 16;
|
||||
static const int kTexHeight = 20;
|
||||
|
||||
sk_sp<SkData> fETC1Data;
|
||||
|
||||
typedef GM INHERITED;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
DEF_GM(return new ETC1GM;)
|
||||
|
||||
#endif
|
@ -95,6 +95,7 @@ gm_sources = [
|
||||
"$_gm/complexclip_blur_tiled.cpp",
|
||||
"$_gm/composeshader.cpp",
|
||||
"$_gm/compositor_quads.cpp",
|
||||
"$_gm/compressed_textures.cpp",
|
||||
"$_gm/concavepaths.cpp",
|
||||
"$_gm/conicpaths.cpp",
|
||||
"$_gm/constcolorprocessor.cpp",
|
||||
@ -153,7 +154,6 @@ gm_sources = [
|
||||
"$_gm/encode_alpha_jpeg.cpp",
|
||||
"$_gm/encode_platform.cpp",
|
||||
"$_gm/encode_srgb.cpp",
|
||||
"$_gm/etc1.cpp",
|
||||
"$_gm/extractbitmap.cpp",
|
||||
"$_gm/fadefilter.cpp",
|
||||
"$_gm/fatpathfill.cpp",
|
||||
|
@ -134,15 +134,18 @@ struct BC1Block {
|
||||
uint32_t fIndices;
|
||||
};
|
||||
|
||||
// Create a BC1 compressed block that is filled with 'col'
|
||||
static void create_BC1_block(SkColor col, BC1Block* block) {
|
||||
static uint16_t to565(SkColor col) {
|
||||
int r5 = SkMulDiv255Round(31, SkColorGetR(col));
|
||||
int g6 = SkMulDiv255Round(63, SkColorGetG(col));
|
||||
int b5 = SkMulDiv255Round(31, SkColorGetB(col));
|
||||
|
||||
uint16_t c565 = (r5 << 11) | (g6 << 5) | b5;
|
||||
block->fColor0 = c565;
|
||||
block->fColor1 = c565;
|
||||
return (r5 << 11) | (g6 << 5) | b5;
|
||||
}
|
||||
|
||||
// Create a BC1 compressed block that has two colors but is initialized to 'col0'
|
||||
static void create_BC1_block(SkColor col0, SkColor col1, BC1Block* block) {
|
||||
block->fColor0 = to565(col0);
|
||||
block->fColor1 = to565(col1);
|
||||
// This sets all 16 pixels to just use 'fColor0'
|
||||
block->fIndices = 0;
|
||||
}
|
||||
@ -235,7 +238,7 @@ static void fillin_BC1_with_color(SkISize dimensions, const SkColor4f& colorf, c
|
||||
SkColor color = colorf.toSkColor();
|
||||
|
||||
BC1Block block;
|
||||
create_BC1_block(color, &block);
|
||||
create_BC1_block(color, color, &block);
|
||||
|
||||
int numBlocks = num_ETC1_blocks(dimensions.width(), dimensions.height());
|
||||
|
||||
@ -245,6 +248,47 @@ static void fillin_BC1_with_color(SkISize dimensions, const SkColor4f& colorf, c
|
||||
}
|
||||
}
|
||||
|
||||
// Fill in 'dstPixels' with BC1 blocks derived from the 'pixmap'.
|
||||
void GrTwoColorBC1Compress(const SkPixmap& pixmap, SkColor otherColor, char* dstPixels) {
|
||||
BC1Block* dstBlocks = (BC1Block*) dstPixels;
|
||||
SkASSERT(pixmap.colorType() == SkColorType::kRGBA_8888_SkColorType);
|
||||
|
||||
BC1Block block;
|
||||
|
||||
// black -> fColor0, otherColor -> fColor1
|
||||
create_BC1_block(SK_ColorBLACK, otherColor, &block);
|
||||
|
||||
int numXBlocks = num_ETC1_blocks_w(pixmap.width());
|
||||
int numYBlocks = num_ETC1_blocks_w(pixmap.height());
|
||||
|
||||
for (int y = 0; y < numYBlocks; ++y) {
|
||||
for (int x = 0; x < numXBlocks; ++x) {
|
||||
int shift = 0;
|
||||
int offsetX = 4 * x, offsetY = 4 * y;
|
||||
block.fIndices = 0; // init all the pixels to color0
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
if (offsetX + j >= pixmap.width() || offsetY + i >= pixmap.height()) {
|
||||
// This can happen for the topmost levels of a mipmap
|
||||
continue;
|
||||
}
|
||||
|
||||
SkColor tmp = pixmap.getColor(offsetX + j, offsetY + i);
|
||||
if (tmp == SK_ColorTRANSPARENT) {
|
||||
// For RGBA BC1 images color3 is set to transparent black
|
||||
block.fIndices |= 3 << shift;
|
||||
} else if (tmp != SK_ColorBLACK) {
|
||||
block.fIndices |= 1 << shift; // color1
|
||||
}
|
||||
shift += 2;
|
||||
}
|
||||
}
|
||||
|
||||
dstBlocks[y*numXBlocks + x] = block;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t GrComputeTightCombinedBufferSize(size_t bytesPerPixel, SkISize baseDimensions,
|
||||
SkTArray<size_t>* individualMipOffsets, int mipLevelCount) {
|
||||
SkASSERT(individualMipOffsets && !individualMipOffsets->count());
|
||||
|
@ -43,4 +43,12 @@ bool GrConvertPixels(const GrImageInfo& dstInfo, void* dst, size_t dstRB,
|
||||
/** Clears the dst image to a constant color. */
|
||||
bool GrClearImage(const GrImageInfo& dstInfo, void* dst, size_t dstRB, SkColor4f color);
|
||||
|
||||
/**
|
||||
* BC1 compress an image that contains only either opaque black or transparent black and one
|
||||
* other color.
|
||||
* opaque pixmaps -> kBC1_RGB8_UNORM
|
||||
* transparent pixmaps -> kBC1_RGBA8_UNORM
|
||||
*/
|
||||
void GrTwoColorBC1Compress(const SkPixmap& pixmap, SkColor otherColor, char* dstPixels);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user