Have peek32 return uint32_t& to make it harder to look at more than 4 bytes.

BUG=skia:
R=reed@google.com, robertphillips@google.com

Author: mtklein@google.com

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

git-svn-id: http://skia.googlecode.com/svn/trunk@13265 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
commit-bot@chromium.org 2014-01-31 16:22:57 +00:00
parent 0a74106aaa
commit c6d3c444ca
4 changed files with 34 additions and 38 deletions

View File

@ -79,19 +79,9 @@ public:
return p;
}
// return the address of the 4byte int at the specified offset (which must
// be a multiple of 4. This does not allocate any new space, so the returned
// address is only valid for 1 int.
uint32_t* peek32(size_t offset) {
SkASSERT(SkAlign4(offset) == offset);
const int count = SkToInt(offset/4);
SkASSERT(count < fCount);
if (count < this->externalCount()) {
return fExternal + count;
}
return &fInternal[count - this->externalCount()];
}
// Read or write 4 bytes at offset, which must be a multiple of 4 <= size().
uint32_t read32At(size_t offset) { return this->atOffset(offset); }
void write32At(size_t offset, uint32_t val) { this->atOffset(offset) = val; }
bool writeBool(bool value) {
this->write32(value);
@ -215,8 +205,7 @@ public:
/**
* Move the cursor back to offset bytes from the beginning.
* This has the same restrictions as peek32: offset must be <= size() and
* offset must be a multiple of 4.
* offset must be a multiple of 4 no greater than size().
*/
void rewindToOffset(size_t offset) {
SkASSERT(SkAlign4(offset) == offset);
@ -249,6 +238,18 @@ public:
}
private:
uint32_t& atOffset(size_t offset) {
SkASSERT(SkAlign4(offset) == offset);
const int count = SkToInt(offset/4);
SkASSERT(count < fCount);
if (count < this->externalCount()) {
return fExternal[count];
}
return fInternal[count - this->externalCount()];
}
// Number of uint32_t written into fExternal. <= fExternalLimit.
int externalCount() const { return fCount - fInternal.count(); }

View File

@ -197,13 +197,13 @@ bool SkPictureRecord::isDrawingToLayer() const {
* Read the op code from 'offset' in 'writer' and extract the size too.
*/
static DrawType peek_op_and_size(SkWriter32* writer, int32_t offset, uint32_t* size) {
uint32_t* peek = writer->peek32(offset);
uint32_t peek = writer->read32At(offset);
uint32_t op;
UNPACK_8_24(*peek, op, *size);
UNPACK_8_24(peek, op, *size);
if (MASK_24 == *size) {
// size required its own slot right after the op code
*size = *writer->peek32(offset+kUInt32Size);
*size = writer->read32At(offset+kUInt32Size);
}
return (DrawType) op;
}
@ -306,7 +306,7 @@ static bool remove_save_layer1(SkWriter32* writer, int32_t offset,
// back up to the save block
// TODO: add a stack to track save*/restore offsets rather than searching backwards
while (offset > 0) {
offset = *writer->peek32(offset);
offset = writer->read32At(offset);
}
int pattern[] = { SAVE_LAYER, kDRAW_BITMAP_FLAVOR, /* RESTORE */ };
@ -331,8 +331,8 @@ static bool remove_save_layer1(SkWriter32* writer, int32_t offset,
* field alone so the NOOP can be skipped later.
*/
static void convert_command_to_noop(SkWriter32* writer, uint32_t offset) {
uint32_t* ptr = writer->peek32(offset);
*ptr = (*ptr & MASK_24) | (NOOP << 24);
uint32_t command = writer->read32At(offset);
writer->write32At(offset, (command & MASK_24) | (NOOP << 24));
}
/*
@ -353,8 +353,8 @@ static bool merge_savelayer_paint_into_drawbitmp(SkWriter32* writer,
uint32_t slPaintOffset = getPaintOffset(SAVE_LAYER, saveLayerInfo.fSize);
// we have a match, now we need to get the paints involved
uint32_t dbmPaintId = *writer->peek32(dbmInfo.fOffset+dbmPaintOffset);
uint32_t saveLayerPaintId = *writer->peek32(saveLayerInfo.fOffset+slPaintOffset);
uint32_t dbmPaintId = writer->read32At(dbmInfo.fOffset+dbmPaintOffset);
uint32_t saveLayerPaintId = writer->read32At(saveLayerInfo.fOffset+slPaintOffset);
if (0 == saveLayerPaintId) {
// In this case the saveLayer/restore isn't needed at all - just kill the saveLayer
@ -367,9 +367,7 @@ static bool merge_savelayer_paint_into_drawbitmp(SkWriter32* writer,
// In this case just make the DBM* use the saveLayer's paint, kill the saveLayer
// and signal the caller (by returning true) to not add the RESTORE op
convert_command_to_noop(writer, saveLayerInfo.fOffset);
uint32_t* ptr = writer->peek32(dbmInfo.fOffset+dbmPaintOffset);
SkASSERT(0 == *ptr);
*ptr = saveLayerPaintId;
writer->write32At(dbmInfo.fOffset+dbmPaintOffset, saveLayerPaintId);
return true;
}
@ -402,9 +400,7 @@ static bool merge_savelayer_paint_into_drawbitmp(SkWriter32* writer,
// kill the saveLayer and alter the DBMR2R's paint to be the modified one
convert_command_to_noop(writer, saveLayerInfo.fOffset);
uint32_t* ptr = writer->peek32(dbmInfo.fOffset+dbmPaintOffset);
SkASSERT(dbmPaintId == *ptr);
*ptr = data->index();
writer->write32At(dbmInfo.fOffset+dbmPaintOffset, data->index());
return true;
}
@ -425,7 +421,7 @@ static bool remove_save_layer2(SkWriter32* writer, int32_t offset,
// back up to the save block
// TODO: add a stack to track save*/restore offsets rather than searching backwards
while (offset > 0) {
offset = *writer->peek32(offset);
offset = writer->read32At(offset);
}
int pattern[] = { SAVE_LAYER, SAVE, CLIP_RECT, kDRAW_BITMAP_FLAVOR, RESTORE, /* RESTORE */ };
@ -462,7 +458,7 @@ static bool collapse_save_clip_restore(SkWriter32* writer, int32_t offset,
// back up to the save block
while (offset > 0) {
offset = *writer->peek32(offset);
offset = writer->read32At(offset);
}
// now offset points to a save
@ -477,7 +473,7 @@ static bool collapse_save_clip_restore(SkWriter32* writer, int32_t offset,
SkASSERT(kSaveSize == opSize);
// get the save flag (last 4-bytes of the space allocated for the opSize)
SkCanvas::SaveFlags saveFlags = (SkCanvas::SaveFlags) *writer->peek32(offset+4);
SkCanvas::SaveFlags saveFlags = (SkCanvas::SaveFlags) writer->read32At(offset+4);
if (SkCanvas::kMatrixClip_SaveFlag != saveFlags) {
// This function's optimization is only correct for kMatrixClip style saves.
// TODO: set checkMatrix & checkClip booleans here and then check for the
@ -694,9 +690,9 @@ static bool regionOpExpands(SkRegion::Op op) {
void SkPictureRecord::fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset) {
int32_t offset = fRestoreOffsetStack.top();
while (offset > 0) {
uint32_t* peek = fWriter.peek32(offset);
offset = *peek;
*peek = restoreOffset;
uint32_t peek = fWriter.read32At(offset);
fWriter.write32At(offset, restoreOffset);
offset = peek;
}
#ifdef SK_DEBUG

View File

@ -321,5 +321,5 @@ void SkWriteBuffer::writeFlattenable(const SkFlattenable* flattenable) {
flattenable->flatten(*this);
uint32_t objSize = fWriter.bytesWritten() - offset;
// record the obj's size
*fWriter.peek32(offset - sizeof(uint32_t)) = objSize;
fWriter.write32At(offset - sizeof(uint32_t), objSize);
}

View File

@ -102,8 +102,7 @@ static void test1(skiatest::Reporter* reporter, SkWriter32* writer) {
for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) {
REPORTER_ASSERT(reporter, i*4 == writer->bytesWritten());
writer->write32(data[i]);
uint32_t* addr = writer->peek32(i * 4);
REPORTER_ASSERT(reporter, data[i] == *addr);
REPORTER_ASSERT(reporter, data[i] == writer->read32At(i*4));
}
char buffer[sizeof(data)];