Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkBitmapCache.h" | 9 #include "SkBitmapCache.h" |
| 10 #include "SkImageCacherator.h" | 10 #include "SkImageCacherator.h" |
| 11 #include "SkMallocPixelRef.h" | |
| 12 #include "SkNextID.h" | |
| 11 #include "SkPixelRef.h" | 13 #include "SkPixelRef.h" |
| 12 | 14 |
| 13 #if SK_SUPPORT_GPU | 15 #if SK_SUPPORT_GPU |
| 14 #include "GrContext.h" | 16 #include "GrContext.h" |
| 15 #include "GrGpuResourcePriv.h" | 17 #include "GrGpuResourcePriv.h" |
| 16 #include "GrResourceKey.h" | 18 #include "GrResourceKey.h" |
| 17 #include "GrTextureAccess.h" | 19 #include "GrTextureAccess.h" |
| 18 #include "SkGr.h" | 20 #include "SkGr.h" |
| 19 #include "SkGrPriv.h" | 21 #include "SkGrPriv.h" |
| 20 #endif | 22 #endif |
| 21 | 23 |
| 22 SkImageCacherator* SkImageCacherator::NewFromGenerator(SkImageGenerator* gen) { | 24 SkImageCacherator* SkImageCacherator::NewFromGenerator(SkImageGenerator* gen, |
| 25 const SkIRect* subset) { | |
| 23 if (!gen) { | 26 if (!gen) { |
| 24 return nullptr; | 27 return nullptr; |
| 25 } | 28 } |
| 26 return SkNEW_ARGS(SkImageCacherator, (gen)); | 29 const SkImageInfo& info = gen->getInfo(); |
| 30 if (info.isEmpty()) { | |
| 31 return nullptr; | |
| 32 } | |
| 33 | |
| 34 uint32_t uniqueID = gen->uniqueID(); | |
| 35 const SkIRect bounds = SkIRect::MakeWH(info.width(), info.height()); | |
| 36 if (subset) { | |
| 37 if (!bounds.contains(*subset)) { | |
| 38 return nullptr; | |
| 39 } | |
| 40 if (*subset != bounds) { | |
| 41 // we need a different uniqueID since we really are a subset of the raw generator | |
| 42 uniqueID = SkNextID::ImageID(); | |
| 43 } | |
| 44 } else { | |
| 45 subset = &bounds; | |
| 46 } | |
| 47 | |
| 48 return SkNEW_ARGS(SkImageCacherator, (gen, | |
| 49 gen->getInfo().makeWH(subset->width(), subset->height()), | |
| 50 SkIPoint::Make(subset->x(), subset->y( )), | |
| 51 uniqueID)); | |
| 27 } | 52 } |
| 28 | 53 |
| 29 SkImageCacherator::SkImageCacherator(SkImageGenerator* gen) : fGenerator(gen) {} | 54 SkImageCacherator::SkImageCacherator(SkImageGenerator* gen, const SkImageInfo& i nfo, |
| 55 const SkIPoint& origin, uint32_t uniqueID) | |
| 56 : fGenerator(gen) | |
| 57 , fInfo(info) | |
| 58 , fOrigin(origin) | |
| 59 , fUniqueID(uniqueID) | |
| 60 {} | |
| 30 | 61 |
| 31 SkImageCacherator::~SkImageCacherator() { | 62 SkImageCacherator::~SkImageCacherator() { |
| 32 SkDELETE(fGenerator); | 63 SkDELETE(fGenerator); |
| 33 } | 64 } |
| 34 | 65 |
| 35 static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) { | 66 static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) { |
| 36 SkASSERT(bitmap.getGenerationID() == expectedID); | 67 SkASSERT(bitmap.getGenerationID() == expectedID); |
| 37 SkASSERT(bitmap.isImmutable()); | 68 SkASSERT(bitmap.isImmutable()); |
| 38 SkASSERT(bitmap.getPixels()); | 69 SkASSERT(bitmap.getPixels()); |
| 39 return true; | 70 return true; |
| 40 } | 71 } |
| 41 | 72 |
| 42 static bool generate_bitmap(SkImageGenerator* generator, SkBitmap* bitmap) { | 73 static bool generate_bitmap(SkBitmap* bitmap, const SkImageInfo& info, const SkI Point& origin, |
| 43 if (!bitmap->tryAllocPixels(generator->getInfo()) || | 74 SkImageGenerator* generator) { |
| 44 !generator->getPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBy tes())) | 75 const size_t rowBytes = info.minRowBytes(); |
| 45 { | 76 if (!bitmap->tryAllocPixels(info, rowBytes)) { |
| 46 bitmap->reset(); | |
| 47 return false; | 77 return false; |
| 48 } | 78 } |
| 79 SkASSERT(bitmap->rowBytes() == rowBytes); | |
| 80 | |
| 81 const SkImageInfo& genInfo = generator->getInfo(); | |
| 82 if (info.dimensions() == genInfo.dimensions()) { | |
| 83 SkASSERT(origin.x() == 0 && origin.y() == 0); | |
| 84 // fast-case, no copy needed | |
| 85 if (!generator->getPixels(bitmap->info(), bitmap->getPixels(), rowBytes) ) { | |
| 86 bitmap->reset(); | |
| 87 return false; | |
| 88 } | |
| 89 } else { | |
| 90 // need to handle subsetting | |
|
scroggo
2015/08/19 20:58:20
Should this assert that the dimensions are actuall
reed1
2015/08/19 21:04:09
I think the caller has done that, but it'd be fine
| |
| 91 SkBitmap full; | |
| 92 if (!full.tryAllocPixels(genInfo)) { | |
| 93 return false; | |
| 94 } | |
| 95 if (!generator->getPixels(full.info(), full.getPixels(), full.rowBytes() )) { | |
| 96 bitmap->reset(); | |
| 97 return false; | |
| 98 } | |
| 99 full.readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), | |
|
scroggo
2015/08/19 20:58:20
Is there a reason you did not call extractSubset?
reed1
2015/08/19 21:04:09
Definitely. This is a generator, not a cacher, and
| |
| 100 origin.x(), origin.y()); | |
| 101 } | |
| 49 return true; | 102 return true; |
| 50 } | 103 } |
| 51 | 104 |
| 52 //////////////////////////////////////////////////////////////////////////////// ////////////////// | 105 //////////////////////////////////////////////////////////////////////////////// ////////////////// |
| 53 | 106 |
| 54 bool SkImageCacherator::tryLockAsBitmap(SkBitmap* bitmap) { | 107 bool SkImageCacherator::tryLockAsBitmap(SkBitmap* bitmap) { |
| 55 const uint32_t uniqueID = fGenerator->uniqueID(); | 108 if (SkBitmapCache::Find(fUniqueID, bitmap)) { |
| 56 | 109 return check_output_bitmap(*bitmap, fUniqueID); |
| 57 if (SkBitmapCache::Find(uniqueID, bitmap)) { | |
| 58 return check_output_bitmap(*bitmap, uniqueID); | |
| 59 } | 110 } |
| 60 if (!generate_bitmap(fGenerator, bitmap)) { | 111 if (!generate_bitmap(bitmap, fInfo, fOrigin, fGenerator)) { |
| 61 return false; | 112 return false; |
| 62 } | 113 } |
| 63 | 114 |
| 64 bitmap->pixelRef()->setImmutableWithID(uniqueID); | 115 bitmap->pixelRef()->setImmutableWithID(fUniqueID); |
| 65 SkBitmapCache::Add(uniqueID, *bitmap); | 116 SkBitmapCache::Add(fUniqueID, *bitmap); |
| 66 return true; | 117 return true; |
| 67 } | 118 } |
| 68 | 119 |
| 69 bool SkImageCacherator::lockAsBitmap(SkBitmap* bitmap) { | 120 bool SkImageCacherator::lockAsBitmap(SkBitmap* bitmap) { |
| 70 const uint32_t uniqueID = fGenerator->uniqueID(); | |
| 71 | |
| 72 if (this->tryLockAsBitmap(bitmap)) { | 121 if (this->tryLockAsBitmap(bitmap)) { |
| 73 return check_output_bitmap(*bitmap, uniqueID); | 122 return check_output_bitmap(*bitmap, fUniqueID); |
| 74 } | 123 } |
| 75 | 124 |
| 76 #if SK_SUPPORT_GPU | 125 #if SK_SUPPORT_GPU |
| 77 // Try to get a texture and read it back to raster (and then cache that with our ID) | 126 // Try to get a texture and read it back to raster (and then cache that with our ID) |
| 78 | 127 |
| 79 SkAutoTUnref<GrTexture> tex(fGenerator->generateTexture(nullptr, kUntiled_Sk ImageUsageType)); | 128 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(), fInfo.height()); |
| 129 SkAutoTUnref<GrTexture> tex(fGenerator->generateTexture(nullptr, kUntiled_Sk ImageUsageType, | |
| 130 &subset)); | |
| 80 if (!tex) { | 131 if (!tex) { |
| 81 bitmap->reset(); | 132 bitmap->reset(); |
| 82 return false; | 133 return false; |
| 83 } | 134 } |
| 84 | 135 |
| 85 const SkImageInfo& info = this->info(); | 136 if (!bitmap->tryAllocPixels(fInfo)) { |
| 86 if (!bitmap->tryAllocPixels(info)) { | |
| 87 bitmap->reset(); | 137 bitmap->reset(); |
| 88 return false; | 138 return false; |
| 89 } | 139 } |
| 90 | 140 |
| 91 const uint32_t pixelOpsFlags = 0; | 141 const uint32_t pixelOpsFlags = 0; |
| 92 if (!tex->readPixels(0, 0, bitmap->width(), bitmap->height(), SkImageInfo2Gr PixelConfig(info), | 142 if (!tex->readPixels(0, 0, bitmap->width(), bitmap->height(), SkImageInfo2Gr PixelConfig(fInfo), |
| 93 bitmap->getPixels(), bitmap->rowBytes(), pixelOpsFlags) ) { | 143 bitmap->getPixels(), bitmap->rowBytes(), pixelOpsFlags) ) { |
| 94 bitmap->reset(); | 144 bitmap->reset(); |
| 95 return false; | 145 return false; |
| 96 } | 146 } |
| 97 | 147 |
| 98 bitmap->pixelRef()->setImmutableWithID(uniqueID); | 148 bitmap->pixelRef()->setImmutableWithID(fUniqueID); |
| 99 SkBitmapCache::Add(uniqueID, *bitmap); | 149 SkBitmapCache::Add(fUniqueID, *bitmap); |
| 100 return check_output_bitmap(*bitmap, uniqueID); | 150 return check_output_bitmap(*bitmap, fUniqueID); |
| 101 #else | 151 #else |
| 102 return false; | 152 return false; |
| 103 #endif | 153 #endif |
| 104 } | 154 } |
| 105 | 155 |
| 106 //////////////////////////////////////////////////////////////////////////////// ////////////////// | 156 //////////////////////////////////////////////////////////////////////////////// ////////////////// |
| 107 | 157 |
| 108 GrTexture* SkImageCacherator::tryLockAsTexture(GrContext* ctx, SkImageUsageType usage) { | 158 GrTexture* SkImageCacherator::tryLockAsTexture(GrContext* ctx, SkImageUsageType usage) { |
| 109 #if SK_SUPPORT_GPU | 159 #if SK_SUPPORT_GPU |
| 110 const uint32_t uniqueID = fGenerator->uniqueID(); | |
| 111 const SkImageInfo& info = this->info(); | |
| 112 | |
| 113 GrUniqueKey key; | 160 GrUniqueKey key; |
| 114 GrMakeKeyFromImageID(&key, uniqueID, info.width(), info.height(), SkIPoint:: Make(0, 0), | 161 GrMakeKeyFromImageID(&key, fUniqueID, fInfo.width(), fInfo.height(), SkIPoin t::Make(0, 0), |
| 115 *ctx->caps(), usage); | 162 *ctx->caps(), usage); |
| 116 GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(key); | 163 GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(key); |
| 117 if (tex) { | 164 if (tex) { |
| 118 return tex; // we got a cache hit! | 165 return tex; // we got a cache hit! |
| 119 } | 166 } |
| 120 | 167 |
| 121 tex = fGenerator->generateTexture(ctx, usage); | 168 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(), fInfo.height()); |
| 169 tex = fGenerator->generateTexture(ctx, usage, &subset); | |
| 122 if (tex) { | 170 if (tex) { |
| 123 tex->resourcePriv().setUniqueKey(key); | 171 tex->resourcePriv().setUniqueKey(key); |
| 124 } | 172 } |
| 125 return tex; | 173 return tex; |
| 126 #else | 174 #else |
| 127 return nullptr; | 175 return nullptr; |
| 128 #endif | 176 #endif |
| 129 } | 177 } |
| 130 | 178 |
| 131 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, SkImageUsageType usa ge) { | 179 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, SkImageUsageType usa ge) { |
| 132 #if SK_SUPPORT_GPU | 180 #if SK_SUPPORT_GPU |
| 133 if (!ctx) { | 181 if (!ctx) { |
| 134 return nullptr; | 182 return nullptr; |
| 135 } | 183 } |
| 136 if (GrTexture* tex = this->tryLockAsTexture(ctx, usage)) { | 184 if (GrTexture* tex = this->tryLockAsTexture(ctx, usage)) { |
| 137 return tex; | 185 return tex; |
| 138 } | 186 } |
| 139 | 187 |
| 140 // Try to get a bitmap and then upload/cache it as a texture | 188 // Try to get a bitmap and then upload/cache it as a texture |
| 141 | 189 |
| 142 SkBitmap bitmap; | 190 SkBitmap bitmap; |
| 143 if (!generate_bitmap(fGenerator, &bitmap)) { | 191 if (!generate_bitmap(&bitmap, fInfo, fOrigin, fGenerator)) { |
| 144 return nullptr; | 192 return nullptr; |
| 145 } | 193 } |
| 146 return GrRefCachedBitmapTexture(ctx, bitmap, usage); | 194 return GrRefCachedBitmapTexture(ctx, bitmap, usage); |
| 147 #else | 195 #else |
| 148 return nullptr; | 196 return nullptr; |
| 149 #endif | 197 #endif |
| 150 } | 198 } |
| 151 | 199 |
| OLD | NEW |