2016-03-17 13:58:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkAutoPixmapStorage_DEFINED
|
|
|
|
#define SkAutoPixmapStorage_DEFINED
|
|
|
|
|
2017-03-27 17:35:15 +00:00
|
|
|
#include "SkMalloc.h"
|
2016-03-17 13:58:39 +00:00
|
|
|
#include "SkPixmap.h"
|
|
|
|
|
2017-12-31 20:23:54 +00:00
|
|
|
class SkAutoPixmapStorage : public SkPixmap {
|
2016-03-17 13:58:39 +00:00
|
|
|
public:
|
|
|
|
SkAutoPixmapStorage();
|
|
|
|
~SkAutoPixmapStorage();
|
|
|
|
|
2017-06-12 17:05:49 +00:00
|
|
|
/**
|
|
|
|
* Leave the moved-from object in a free-but-valid state.
|
|
|
|
*/
|
|
|
|
SkAutoPixmapStorage& operator=(SkAutoPixmapStorage&& other);
|
|
|
|
|
2016-03-17 13:58:39 +00:00
|
|
|
/**
|
|
|
|
* Try to allocate memory for the pixels needed to match the specified Info. On success
|
|
|
|
* return true and fill out the pixmap to point to that memory. The storage will be freed
|
|
|
|
* when this object is destroyed, or if another call to tryAlloc() or alloc() is made.
|
|
|
|
*
|
|
|
|
* On failure, return false and reset() the pixmap to empty.
|
|
|
|
*/
|
|
|
|
bool tryAlloc(const SkImageInfo&);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate memory for the pixels needed to match the specified Info and fill out the pixmap
|
|
|
|
* to point to that memory. The storage will be freed when this object is destroyed,
|
|
|
|
* or if another call to tryAlloc() or alloc() is made.
|
|
|
|
*
|
2017-08-17 18:05:04 +00:00
|
|
|
* If the memory cannot be allocated, calls SK_ABORT().
|
2016-03-17 13:58:39 +00:00
|
|
|
*/
|
|
|
|
void alloc(const SkImageInfo&);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the size and optionally the rowBytes that would be allocated by SkAutoPixmapStorage if
|
|
|
|
* alloc/tryAlloc was called.
|
|
|
|
*/
|
|
|
|
static size_t AllocSize(const SkImageInfo& info, size_t* rowBytes);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an SkData object wrapping the allocated pixels memory, and resets the pixmap.
|
|
|
|
* If the storage hasn't been allocated, the result is NULL.
|
|
|
|
*/
|
|
|
|
const SkData* SK_WARN_UNUSED_RESULT detachPixelsAsData();
|
|
|
|
|
|
|
|
// We wrap these so we can clear our internal storage
|
|
|
|
|
|
|
|
void reset() {
|
|
|
|
this->freeStorage();
|
|
|
|
this->INHERITED::reset();
|
|
|
|
}
|
2017-07-18 14:53:11 +00:00
|
|
|
void reset(const SkImageInfo& info, const void* addr, size_t rb) {
|
2016-03-17 13:58:39 +00:00
|
|
|
this->freeStorage();
|
2017-07-18 14:53:11 +00:00
|
|
|
this->INHERITED::reset(info, addr, rb);
|
2016-03-17 13:58:39 +00:00
|
|
|
}
|
2017-09-07 12:25:00 +00:00
|
|
|
|
2016-03-17 13:58:39 +00:00
|
|
|
bool SK_WARN_UNUSED_RESULT reset(const SkMask& mask) {
|
|
|
|
this->freeStorage();
|
|
|
|
return this->INHERITED::reset(mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void* fStorage;
|
|
|
|
|
|
|
|
void freeStorage() {
|
|
|
|
sk_free(fStorage);
|
|
|
|
fStorage = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef SkPixmap INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|