2020-01-14 21:54:17 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2020-01-27 21:11:57 +00:00
|
|
|
#if !defined(SK_BUILD_FOR_GOOGLE3) // Google3 doesn't have etc1.h
|
2020-01-27 21:05:23 +00:00
|
|
|
|
2020-01-14 21:54:17 +00:00
|
|
|
#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"
|
2020-07-16 14:58:58 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2020-07-01 16:55:01 +00:00
|
|
|
#include "include/gpu/GrRecordingContext.h"
|
2020-01-27 21:11:57 +00:00
|
|
|
#include "src/core/SkCompressedDataUtils.h"
|
2020-07-14 21:16:32 +00:00
|
|
|
#include "src/core/SkMipmap.h"
|
2020-01-14 21:54:17 +00:00
|
|
|
#include "src/gpu/GrDataUtils.h"
|
2020-09-14 15:58:06 +00:00
|
|
|
#include "src/gpu/GrImageContextPriv.h"
|
2020-06-29 19:36:12 +00:00
|
|
|
#include "src/gpu/GrRecordingContextPriv.h"
|
2020-01-27 21:11:57 +00:00
|
|
|
#include "src/image/SkImage_Base.h"
|
2020-07-28 13:57:26 +00:00
|
|
|
#include "src/image/SkImage_GpuBase.h"
|
2020-01-14 21:54:17 +00:00
|
|
|
#include "third_party/etc1/etc1.h"
|
2021-02-23 15:45:39 +00:00
|
|
|
#include "tools/gpu/ProxyUtils.h"
|
2020-01-14 21:54:17 +00:00
|
|
|
|
2020-12-09 21:37:04 +00:00
|
|
|
class GrSurfaceDrawContext;
|
2020-01-14 21:54:17 +00:00
|
|
|
|
|
|
|
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 };
|
2020-01-28 19:22:43 +00:00
|
|
|
const float kAnglePerTooth = 2.0f * SK_ScalarPI / (3 * numTeeth);
|
2020-01-14 21:54:17 +00:00
|
|
|
|
|
|
|
float angle = 0.0f;
|
|
|
|
|
|
|
|
SkPath tmp;
|
|
|
|
tmp.setFillType(SkPathFillType::kWinding);
|
|
|
|
|
|
|
|
tmp.moveTo(gen_pt(angle, outerRad));
|
|
|
|
|
2020-01-28 19:22:43 +00:00
|
|
|
for (int i = 0; i < numTeeth; ++i, angle += 3*kAnglePerTooth) {
|
2020-01-14 21:54:17 +00:00
|
|
|
tmp.lineTo(gen_pt(angle+kAnglePerTooth, outerRad));
|
2020-01-28 19:22:43 +00:00
|
|
|
tmp.lineTo(gen_pt(angle+(1.5f*kAnglePerTooth), innerRad));
|
|
|
|
tmp.lineTo(gen_pt(angle+(2.5f*kAnglePerTooth), innerRad));
|
|
|
|
tmp.lineTo(gen_pt(angle+(3.0f*kAnglePerTooth), outerRad));
|
2020-01-14 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmp.close();
|
|
|
|
|
2020-02-07 15:36:46 +00:00
|
|
|
float fInnerRad = 0.1f * std::min(dimensions.fWidth, dimensions.fHeight);
|
2020-01-14 21:54:17 +00:00
|
|
|
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.
|
2020-07-28 13:57:26 +00:00
|
|
|
static sk_sp<SkImage> make_compressed_image(GrDirectContext* dContext,
|
|
|
|
const SkISize dimensions,
|
|
|
|
SkColorType colorType,
|
|
|
|
bool opaque,
|
|
|
|
SkImage::CompressionType compression) {
|
2020-01-27 21:11:57 +00:00
|
|
|
size_t totalSize = SkCompressedDataSize(compression, dimensions, nullptr, true);
|
2020-01-14 21:54:17 +00:00
|
|
|
|
|
|
|
sk_sp<SkData> tmp = SkData::MakeUninitialized(totalSize);
|
|
|
|
char* pixels = (char*) tmp->writable_data();
|
|
|
|
|
2020-07-14 21:16:32 +00:00
|
|
|
int numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
|
2020-01-14 21:54:17 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2020-07-28 13:57:26 +00:00
|
|
|
SkISize levelDims = dimensions;
|
2020-01-14 21:54:17 +00:00
|
|
|
for (int i = 0; i < numMipLevels; ++i) {
|
2020-07-28 13:57:26 +00:00
|
|
|
size_t levelSize = SkCompressedDataSize(compression, levelDims, nullptr, false);
|
2020-01-14 21:54:17 +00:00
|
|
|
|
2020-07-28 13:57:26 +00:00
|
|
|
SkBitmap bm = render_level(levelDims, kColors[i%7], colorType, opaque);
|
2020-01-14 21:54:17 +00:00
|
|
|
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;
|
2020-07-28 13:57:26 +00:00
|
|
|
levelDims = {std::max(1, levelDims.width()/2), std::max(1, levelDims.height()/2)};
|
2020-01-14 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 13:57:26 +00:00
|
|
|
sk_sp<SkImage> image;
|
|
|
|
if (dContext) {
|
|
|
|
image = SkImage::MakeTextureFromCompressed(dContext, std::move(tmp),
|
|
|
|
dimensions.width(),
|
|
|
|
dimensions.height(),
|
|
|
|
compression, GrMipmapped::kYes);
|
|
|
|
} else {
|
|
|
|
image = SkImage::MakeRasterFromCompressed(std::move(tmp),
|
|
|
|
dimensions.width(),
|
|
|
|
dimensions.height(),
|
|
|
|
compression);
|
|
|
|
}
|
|
|
|
return image;
|
2020-01-14 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 |
|
|
|
|
// --------------------------------------
|
|
|
|
//
|
2020-01-28 19:22:43 +00:00
|
|
|
// The nonPowerOfTwo and nonMultipleOfFour cases exercise some compression edge cases.
|
2020-01-27 21:11:57 +00:00
|
|
|
class CompressedTexturesGM : public skiagm::GM {
|
2020-01-14 21:54:17 +00:00
|
|
|
public:
|
2020-01-28 19:22:43 +00:00
|
|
|
enum class Type {
|
|
|
|
kNormal,
|
|
|
|
kNonPowerOfTwo,
|
|
|
|
kNonMultipleOfFour
|
|
|
|
};
|
|
|
|
|
|
|
|
CompressedTexturesGM(Type type) : fType(type) {
|
2020-01-14 21:54:17 +00:00
|
|
|
this->setBGColor(0xFFCCCCCC);
|
2020-01-28 19:22:43 +00:00
|
|
|
|
|
|
|
switch (fType) {
|
|
|
|
case Type::kNonPowerOfTwo:
|
|
|
|
// These dimensions force the top two mip levels to be 1x3 and 1x1
|
|
|
|
fImgDimensions.set(20, 60);
|
|
|
|
break;
|
|
|
|
case Type::kNonMultipleOfFour:
|
|
|
|
// These dimensions force the top three mip levels to be 1x7, 1x3 and 1x1
|
|
|
|
fImgDimensions.set(13, 61); // prime numbers - just bc
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fImgDimensions.set(kBaseTexWidth, kBaseTexHeight);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-14 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
SkString onShortName() override {
|
2020-01-28 19:22:43 +00:00
|
|
|
SkString name("compressed_textures");
|
|
|
|
|
|
|
|
if (fType == Type::kNonPowerOfTwo) {
|
|
|
|
name.append("_npot");
|
|
|
|
} else if (fType == Type::kNonMultipleOfFour) {
|
|
|
|
name.append("_nmof");
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
2020-01-14 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
2020-01-28 19:22:43 +00:00
|
|
|
return SkISize::Make(2*kCellWidth + 3*kPad, 2*kBaseTexHeight + 3*kPad);
|
2020-01-14 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 13:57:26 +00:00
|
|
|
DrawResult onGpuSetup(GrDirectContext* dContext, SkString* errorMsg) override {
|
|
|
|
if (dContext && dContext->abandoned()) {
|
|
|
|
// This isn't a GpuGM so a null 'context' is okay but an abandoned context
|
|
|
|
// if forbidden.
|
|
|
|
return DrawResult::kSkip;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dContext &&
|
|
|
|
dContext->backend() == GrBackendApi::kDirect3D && fType == Type::kNonMultipleOfFour) {
|
|
|
|
// skbug.com/10541 - Are non-multiple-of-four BC1 textures supported in D3D?
|
|
|
|
return DrawResult::kSkip;
|
|
|
|
}
|
|
|
|
|
|
|
|
fOpaqueETC2Image = make_compressed_image(dContext, fImgDimensions,
|
|
|
|
kRGB_565_SkColorType, true,
|
|
|
|
SkImage::CompressionType::kETC2_RGB8_UNORM);
|
|
|
|
|
|
|
|
fOpaqueBC1Image = make_compressed_image(dContext, fImgDimensions,
|
|
|
|
kRGBA_8888_SkColorType, true,
|
|
|
|
SkImage::CompressionType::kBC1_RGB8_UNORM);
|
|
|
|
|
|
|
|
fTransparentBC1Image = make_compressed_image(dContext, fImgDimensions,
|
|
|
|
kRGBA_8888_SkColorType, false,
|
|
|
|
SkImage::CompressionType::kBC1_RGBA8_UNORM);
|
2020-01-14 21:54:17 +00:00
|
|
|
|
2020-07-28 13:57:26 +00:00
|
|
|
if (!fOpaqueETC2Image || !fOpaqueBC1Image || !fTransparentBC1Image) {
|
|
|
|
*errorMsg = "Failed to create compressed images.";
|
|
|
|
return DrawResult::kFail;
|
|
|
|
}
|
2020-01-15 17:56:52 +00:00
|
|
|
|
2020-07-28 13:57:26 +00:00
|
|
|
return DrawResult::kOk;
|
2020-01-14 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 13:57:26 +00:00
|
|
|
void onGpuTeardown() override {
|
|
|
|
fOpaqueETC2Image = nullptr;
|
|
|
|
fOpaqueBC1Image = nullptr;
|
|
|
|
fTransparentBC1Image = nullptr;
|
|
|
|
}
|
2020-01-27 21:11:57 +00:00
|
|
|
|
2020-07-28 13:57:26 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
|
|
|
this->drawCell(canvas, fOpaqueETC2Image.get(), { kPad, kPad });
|
2020-01-14 21:54:17 +00:00
|
|
|
|
2020-07-28 13:57:26 +00:00
|
|
|
this->drawCell(canvas, fOpaqueBC1Image.get(), { 2*kPad + kCellWidth, kPad });
|
2020-01-15 17:56:52 +00:00
|
|
|
|
2020-07-28 13:57:26 +00:00
|
|
|
this->drawCell(canvas, fTransparentBC1Image.get(),
|
2020-01-28 19:22:43 +00:00
|
|
|
{ 2*kPad + kCellWidth, 2*kPad + kBaseTexHeight });
|
2020-01-14 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-07-28 13:57:26 +00:00
|
|
|
void drawCell(SkCanvas* canvas, SkImage* image, SkIVector offset) {
|
2020-01-27 21:11:57 +00:00
|
|
|
|
2020-01-28 19:22:43 +00:00
|
|
|
SkISize levelDimensions = fImgDimensions;
|
2020-07-14 21:16:32 +00:00
|
|
|
int numMipLevels = SkMipmap::ComputeLevelCount(levelDimensions.width(),
|
2020-01-28 19:22:43 +00:00
|
|
|
levelDimensions.height()) + 1;
|
2020-01-14 21:54:17 +00:00
|
|
|
|
2021-02-05 17:55:38 +00:00
|
|
|
SkSamplingOptions sampling(SkCubicResampler::Mitchell());
|
2020-01-14 21:54:17 +00:00
|
|
|
|
2020-01-27 21:11:57 +00:00
|
|
|
bool isCompressed = false;
|
|
|
|
if (image->isTextureBacked()) {
|
2020-09-14 15:58:06 +00:00
|
|
|
const GrCaps* caps = as_IB(image)->context()->priv().caps();
|
2021-02-23 15:45:39 +00:00
|
|
|
GrTextureProxy* proxy = sk_gpu_test::GetTextureImageProxy(image,
|
|
|
|
canvas->recordingContext());
|
2020-01-27 21:11:57 +00:00
|
|
|
isCompressed = caps->isFormatCompressed(proxy->backendFormat());
|
|
|
|
}
|
|
|
|
|
2020-01-31 16:03:32 +00:00
|
|
|
SkPaint redStrokePaint;
|
|
|
|
redStrokePaint.setColor(SK_ColorRED);
|
|
|
|
redStrokePaint.setStyle(SkPaint::kStroke_Style);
|
2020-01-27 21:11:57 +00:00
|
|
|
|
2020-01-14 21:54:17 +00:00
|
|
|
for (int i = 0; i < numMipLevels; ++i) {
|
|
|
|
SkRect r = SkRect::MakeXYWH(offset.fX, offset.fY,
|
2020-01-28 19:22:43 +00:00
|
|
|
levelDimensions.width(), levelDimensions.height());
|
2020-01-14 21:54:17 +00:00
|
|
|
|
2021-01-24 02:14:47 +00:00
|
|
|
canvas->drawImageRect(image, r, sampling);
|
2020-01-31 16:03:32 +00:00
|
|
|
if (!isCompressed) {
|
|
|
|
// Make it obvious which drawImages used decompressed images
|
|
|
|
canvas->drawRect(r, redStrokePaint);
|
|
|
|
}
|
2020-01-14 21:54:17 +00:00
|
|
|
|
|
|
|
if (i == 0) {
|
2020-01-28 19:22:43 +00:00
|
|
|
offset.fX += levelDimensions.width()+1;
|
2020-01-14 21:54:17 +00:00
|
|
|
} else {
|
2020-01-28 19:22:43 +00:00
|
|
|
offset.fY += levelDimensions.height()+1;
|
2020-01-14 21:54:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 15:36:46 +00:00
|
|
|
levelDimensions = {std::max(1, levelDimensions.width()/2),
|
|
|
|
std::max(1, levelDimensions.height()/2)};
|
2020-01-14 21:54:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int kPad = 8;
|
2020-01-28 19:22:43 +00:00
|
|
|
static const int kBaseTexWidth = 64;
|
|
|
|
static const int kCellWidth = 1.5f * kBaseTexWidth;
|
|
|
|
static const int kBaseTexHeight = 64;
|
2020-01-14 21:54:17 +00:00
|
|
|
|
2020-07-28 13:57:26 +00:00
|
|
|
Type fType;
|
|
|
|
SkISize fImgDimensions;
|
|
|
|
|
|
|
|
sk_sp<SkImage> fOpaqueETC2Image;
|
|
|
|
sk_sp<SkImage> fOpaqueBC1Image;
|
|
|
|
sk_sp<SkImage> fTransparentBC1Image;
|
2020-01-14 21:54:17 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2020-01-14 21:54:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-01-28 19:22:43 +00:00
|
|
|
DEF_GM(return new CompressedTexturesGM(CompressedTexturesGM::Type::kNormal);)
|
|
|
|
DEF_GM(return new CompressedTexturesGM(CompressedTexturesGM::Type::kNonPowerOfTwo);)
|
|
|
|
DEF_GM(return new CompressedTexturesGM(CompressedTexturesGM::Type::kNonMultipleOfFour);)
|
2020-01-14 21:54:17 +00:00
|
|
|
|
2020-01-27 21:05:23 +00:00
|
|
|
#endif
|