Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Unified Diff: src/core/SkImageCacherator.cpp

Issue 2453473004: Avoid separate allocation of SkImageCacherator (Closed)
Patch Set: pass validator to ctor Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/core/SkImageCacherator.cpp
diff --git a/src/core/SkImageCacherator.cpp b/src/core/SkImageCacherator.cpp
index 496ca74d9a1870a9195256b9b8cc43cc5f5dc235..6858a4c09e04f5cc0e2a7a016042eb2aa4e54933 100644
--- a/src/core/SkImageCacherator.cpp
+++ b/src/core/SkImageCacherator.cpp
@@ -32,49 +32,55 @@
// see skbug.com/ 4971, 5128, ...
//#define SK_SUPPORT_COMPRESSED_TEXTURES_IN_CACHERATOR
-SkImageCacherator* SkImageCacherator::NewFromGenerator(SkImageGenerator* gen,
- const SkIRect* subset) {
+SkImageCacherator::Validator::Validator(SkImageGenerator* gen, const SkIRect* subset)
+ // We are required to take ownership of gen, regardless of whether we instantiate a cacherator
+ // or not. On instantiation, the client is responsible for transferring ownership.
+ : fGenerator(gen) {
+
if (!gen) {
- return nullptr;
+ return;
}
- // We are required to take ownership of gen, regardless of if we return a cacherator or not
- SkAutoTDelete<SkImageGenerator> genHolder(gen);
-
const SkImageInfo& info = gen->getInfo();
if (info.isEmpty()) {
- return nullptr;
+ fGenerator.reset();
+ return;
}
- uint32_t uniqueID = gen->uniqueID();
+ fUniqueID = gen->uniqueID();
const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height());
if (subset) {
if (!bounds.contains(*subset)) {
- return nullptr;
+ fGenerator.reset();
+ return;
}
if (*subset != bounds) {
// we need a different uniqueID since we really are a subset of the raw generator
- uniqueID = SkNextID::ImageID();
+ fUniqueID = SkNextID::ImageID();
}
} else {
subset = &bounds;
}
- // Now that we know we can hand-off the generator (to be owned by the cacherator) we can
- // release our holder. (we DONT want to delete it here anymore)
- genHolder.release();
+ fInfo = info.makeWH(subset->width(), subset->height());
+ fOrigin = SkIPoint::Make(subset->x(), subset->y());
+}
- return new SkImageCacherator(gen, gen->getInfo().makeWH(subset->width(), subset->height()),
- SkIPoint::Make(subset->x(), subset->y()), uniqueID);
+SkImageCacherator* SkImageCacherator::NewFromGenerator(SkImageGenerator* gen,
+ const SkIRect* subset) {
+ Validator validator(gen, subset);
+
+ return validator ? new SkImageCacherator(&validator) : nullptr;
}
-SkImageCacherator::SkImageCacherator(SkImageGenerator* gen, const SkImageInfo& info,
- const SkIPoint& origin, uint32_t uniqueID)
- : fNotThreadSafeGenerator(gen)
- , fInfo(info)
- , fOrigin(origin)
- , fUniqueID(uniqueID)
-{}
+SkImageCacherator::SkImageCacherator(Validator* validator)
+ : fNotThreadSafeGenerator(validator->fGenerator.release()) // we take ownership
+ , fInfo(validator->fInfo)
+ , fOrigin(validator->fOrigin)
+ , fUniqueID(validator->fUniqueID)
+{
+ SkASSERT(fNotThreadSafeGenerator);
+}
SkData* SkImageCacherator::refEncoded(GrContext* ctx) {
ScopedGenerator generator(this);

Powered by Google App Engine
This is Rietveld 408576698