Skip to content

Instantly share code, notes, and snippets.

@HalCanary
Last active September 25, 2015 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HalCanary/960d8196d0d40e4e7b02 to your computer and use it in GitHub Desktop.
Save HalCanary/960d8196d0d40e4e7b02 to your computer and use it in GitHub Desktop.
class SkImmutablePixmap {
public:
SkImmutablePixmap(const SkImageInfo& info,
const void* addr,
size_t rowBytes,
SkColorTable* ctable = NULL)
: fPixels(addr), fCTable(ctable), fRowBytes(rowBytes), fInfo(info) {
SkASSERT((kIndex_8_SkColorType == info.colorType())
== (ctable != nullptr));
}
virtual ~SkImmutablePixmap() {}
const void* const fPixels;
const SkColorTable* const fCTable;
const size_t fRowBytes;
const SkImageInfo fInfo;
};
class SkImage_Base : public SkImage {
// ..............
virtual SkImmutablePixmap* getPixmap() const;
// ..............
};
// default implementation
SkImage_Base::SkImmutablePixmap* getPixmap() const {
SkBitmap bitmap;
if (!this->getROPixels(&bitmap)) {
return nullptr;
}
SkAutoLockPixels autoLockPixels(bitmap);
class SkImmutablePixmap_Bitmap : public SkImmutablePixmap {
SkImmutablePixmap_Bitmap(const SkBitmap& bm)
: SkImmutablePixmap(bm.info(),
bm.getPixels(),
bm.rowBytes(),
bm.colorTable())
, fBitmap(bm) {
SkASSERT(bm.getPixels()); // is locked
fBitmap.lock(); // hold that lock
}
virtual ~SkImmutablePixmap_Bitmap() { fBitmap.unlock(); }
private:
SkBitmap fBitmap;
};
return new SkImmutablePixmap_Bitmap(bitmap);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment