pull mipmap class into its own (private) header

BUG=
R=scroggo@google.com

Review URL: https://codereview.chromium.org/19462007

git-svn-id: http://skia.googlecode.com/svn/trunk@10161 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@google.com 2013-07-18 19:53:31 +00:00
parent 12d064236a
commit eed6f1b76b
5 changed files with 338 additions and 0 deletions

View File

@ -107,6 +107,7 @@
'<(skia_src_path)/core/SkMath.cpp',
'<(skia_src_path)/core/SkMatrix.cpp',
'<(skia_src_path)/core/SkMetaData.cpp',
'<(skia_src_path)/core/SkMipMap.cpp',
'<(skia_src_path)/core/SkOrderedReadBuffer.cpp',
'<(skia_src_path)/core/SkOrderedWriteBuffer.cpp',
'<(skia_src_path)/core/SkPackBits.cpp',

View File

@ -77,6 +77,7 @@
'../tests/Matrix44Test.cpp',
'../tests/MemsetTest.cpp',
'../tests/MetaDataTest.cpp',
'../tests/MipMapTest.cpp',
'../tests/OSPathTest.cpp',
'../tests/PackBitsTest.cpp',
'../tests/PaintTest.cpp',

239
src/core/SkMipMap.cpp Normal file
View File

@ -0,0 +1,239 @@
#include "SkMipMap.h"
#include "SkBitmap.h"
#include "SkColorPriv.h"
static void downsampleby2_proc32(SkBitmap* dst, int x, int y,
const SkBitmap& src) {
x <<= 1;
y <<= 1;
const SkPMColor* p = src.getAddr32(x, y);
const SkPMColor* baseP = p;
SkPMColor c, ag, rb;
c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
if (x < src.width() - 1) {
p += 1;
}
c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
p = baseP;
if (y < src.height() - 1) {
p += src.rowBytes() >> 2;
}
c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
if (x < src.width() - 1) {
p += 1;
}
c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
*dst->getAddr32(x >> 1, y >> 1) =
((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
}
static inline uint32_t expand16(U16CPU c) {
return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16);
}
// returns dirt in the top 16bits, but we don't care, since we only
// store the low 16bits.
static inline U16CPU pack16(uint32_t c) {
return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE);
}
static void downsampleby2_proc16(SkBitmap* dst, int x, int y,
const SkBitmap& src) {
x <<= 1;
y <<= 1;
const uint16_t* p = src.getAddr16(x, y);
const uint16_t* baseP = p;
SkPMColor c;
c = expand16(*p);
if (x < src.width() - 1) {
p += 1;
}
c += expand16(*p);
p = baseP;
if (y < src.height() - 1) {
p += src.rowBytes() >> 1;
}
c += expand16(*p);
if (x < src.width() - 1) {
p += 1;
}
c += expand16(*p);
*dst->getAddr16(x >> 1, y >> 1) = (uint16_t)pack16(c >> 2);
}
static uint32_t expand4444(U16CPU c) {
return (c & 0xF0F) | ((c & ~0xF0F) << 12);
}
static U16CPU collaps4444(uint32_t c) {
return (c & 0xF0F) | ((c >> 12) & ~0xF0F);
}
static void downsampleby2_proc4444(SkBitmap* dst, int x, int y,
const SkBitmap& src) {
x <<= 1;
y <<= 1;
const uint16_t* p = src.getAddr16(x, y);
const uint16_t* baseP = p;
uint32_t c;
c = expand4444(*p);
if (x < src.width() - 1) {
p += 1;
}
c += expand4444(*p);
p = baseP;
if (y < src.height() - 1) {
p += src.rowBytes() >> 1;
}
c += expand4444(*p);
if (x < src.width() - 1) {
p += 1;
}
c += expand4444(*p);
*dst->getAddr16(x >> 1, y >> 1) = (uint16_t)collaps4444(c >> 2);
}
static bool isPos32Bits(const Sk64& value) {
return !value.isNeg() && value.is32();
}
SkMipMap::Level* SkMipMap::AllocLevels(int levelCount, size_t pixelSize) {
if (levelCount < 0) {
return NULL;
}
Sk64 size;
size.setMul(levelCount + 1, sizeof(Level));
size.add(SkToS32(pixelSize));
if (!isPos32Bits(size)) {
return NULL;
}
return (Level*)sk_malloc_throw(size.get32());
}
SkMipMap* SkMipMap::Build(const SkBitmap& src) {
void (*proc)(SkBitmap* dst, int x, int y, const SkBitmap& src);
const SkBitmap::Config config = src.getConfig();
switch (config) {
case SkBitmap::kARGB_8888_Config:
proc = downsampleby2_proc32;
break;
case SkBitmap::kRGB_565_Config:
proc = downsampleby2_proc16;
break;
case SkBitmap::kARGB_4444_Config:
proc = downsampleby2_proc4444;
break;
case SkBitmap::kIndex8_Config:
case SkBitmap::kA8_Config:
default:
return NULL; // don't build mipmaps for these configs
}
SkAutoLockPixels alp(src);
if (!src.readyToDraw()) {
return NULL;
}
// whip through our loop to compute the exact size needed
size_t size = 0;
int countLevels = 0;
{
int width = src.width();
int height = src.height();
for (;;) {
width >>= 1;
height >>= 1;
if (0 == width || 0 == height) {
break;
}
size += SkBitmap::ComputeRowBytes(config, width) * height;
countLevels += 1;
}
}
if (0 == countLevels) {
return NULL;
}
Level* levels = SkMipMap::AllocLevels(countLevels, size);
if (NULL == levels) {
return NULL;
}
uint8_t* baseAddr = (uint8_t*)&levels[countLevels];
uint8_t* addr = baseAddr;
int width = src.width();
int height = src.height();
uint32_t rowBytes;
SkBitmap srcBM(src);
for (int i = 0; i < countLevels; ++i) {
width >>= 1;
height >>= 1;
rowBytes = SkToU32(SkBitmap::ComputeRowBytes(config, width));
levels[i].fPixels = addr;
levels[i].fWidth = width;
levels[i].fHeight = height;
levels[i].fRowBytes = rowBytes;
SkBitmap dstBM;
dstBM.setConfig(config, width, height, rowBytes);
dstBM.setPixels(addr);
srcBM.lockPixels();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
proc(&dstBM, x, y, srcBM);
}
}
srcBM.unlockPixels();
srcBM = dstBM;
addr += height * rowBytes;
}
SkASSERT(addr == baseAddr + size);
return SkNEW_ARGS(SkMipMap, (levels, countLevels));
}
static SkFixed compute_level(SkScalar scale) {
SkFixed s = SkAbs32(SkScalarToFixed(SkScalarInvert(scale)));
if (s < SK_Fixed1) {
return 0;
}
int clz = SkCLZ(s);
SkASSERT(clz >= 1 && clz <= 15);
return SkIntToFixed(15 - clz) + ((unsigned)(s << (clz + 1)) >> 16);
}
bool SkMipMap::extractLevel(SkScalar scale, Level* levelPtr) const {
if (scale >= SK_Scalar1) {
return false;
}
int level = compute_level(scale) >> 16;
SkASSERT(level >= 0);
if (level <= 0) {
return false;
}
if (level > fCount) {
level = fCount;
}
if (levelPtr) {
*levelPtr = fLevels[level - 1];
}
return true;
}

38
src/core/SkMipMap.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef SkMipMap_DEFINED
#define SkMipMap_DEFINED
#include "SkRefCnt.h"
#include "SkScalar.h"
class SkBitmap;
class SkMipMap : public SkRefCnt {
public:
static SkMipMap* Build(const SkBitmap& src);
struct Level {
void* fPixels;
uint32_t fRowBytes;
uint32_t fWidth, fHeight;
};
bool extractLevel(SkScalar scale, Level*) const;
private:
Level* fLevels;
int fCount;
// we take ownership of levels, and will free it with sk_free()
SkMipMap(Level* levels, int count) : fLevels(levels), fCount(count) {
SkASSERT(levels);
SkASSERT(count > 0);
}
virtual ~SkMipMap() {
sk_free(fLevels);
}
static Level* AllocLevels(int levelCount, size_t pixelSize);
};
#endif

59
tests/MipMapTest.cpp Normal file
View File

@ -0,0 +1,59 @@
/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Test.h"
#include "SkMipMap.h"
#include "SkBitmap.h"
#include "SkRandom.h"
static void make_bitmap(SkBitmap* bm, SkRandom& rand) {
// for now, Build needs a min size of 2, otherwise it will return NULL.
// should fix that to support 1 X N, where N > 1 to return non-null.
int w = 2 + rand.nextU() % 1000;
int h = 2 + rand.nextU() % 1000;
bm->setConfig(SkBitmap::kARGB_8888_Config, w, h);
bm->allocPixels();
bm->eraseColor(SK_ColorWHITE);
}
static void TestMipMap(skiatest::Reporter* reporter) {
SkBitmap bm;
SkRandom rand;
for (int i = 0; i < 500; ++i) {
make_bitmap(&bm, rand);
SkAutoTUnref<SkMipMap> mm(SkMipMap::Build(bm));
REPORTER_ASSERT(reporter, !mm->extractLevel(SK_Scalar1, NULL));
REPORTER_ASSERT(reporter, !mm->extractLevel(SK_Scalar1 * 2, NULL));
SkMipMap::Level prevLevel;
prevLevel.fPixels = NULL; // sentinel
SkScalar scale = SK_Scalar1;
for (int j = 0; j < 30; ++j) {
scale = scale * 2 / 3;
SkMipMap::Level level;
if (mm->extractLevel(scale, &level)) {
REPORTER_ASSERT(reporter, level.fPixels);
REPORTER_ASSERT(reporter, level.fWidth > 0);
REPORTER_ASSERT(reporter, level.fHeight > 0);
REPORTER_ASSERT(reporter, level.fRowBytes >= level.fWidth * 4);
if (prevLevel.fPixels) {
REPORTER_ASSERT(reporter, level.fWidth <= prevLevel.fWidth);
REPORTER_ASSERT(reporter, level.fHeight <= prevLevel.fHeight);
}
prevLevel = level;
}
}
}
}
#include "TestClassDef.h"
DEFINE_TESTCLASS("MipMap", MipMapTestClass, TestMipMap)