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" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 } | 46 } |
47 | 47 |
48 return SkNEW_ARGS(SkImageCacherator, (gen, | 48 return SkNEW_ARGS(SkImageCacherator, (gen, |
49 gen->getInfo().makeWH(subset->width(),
subset->height()), | 49 gen->getInfo().makeWH(subset->width(),
subset->height()), |
50 SkIPoint::Make(subset->x(), subset->y(
)), | 50 SkIPoint::Make(subset->x(), subset->y(
)), |
51 uniqueID)); | 51 uniqueID)); |
52 } | 52 } |
53 | 53 |
54 SkImageCacherator::SkImageCacherator(SkImageGenerator* gen, const SkImageInfo& i
nfo, | 54 SkImageCacherator::SkImageCacherator(SkImageGenerator* gen, const SkImageInfo& i
nfo, |
55 const SkIPoint& origin, uint32_t uniqueID) | 55 const SkIPoint& origin, uint32_t uniqueID) |
56 : fGenerator(gen) | 56 : fNotThreadSafeGenerator(gen) |
57 , fInfo(info) | 57 , fInfo(info) |
58 , fOrigin(origin) | 58 , fOrigin(origin) |
59 , fUniqueID(uniqueID) | 59 , fUniqueID(uniqueID) |
60 {} | 60 {} |
61 | 61 |
62 SkImageCacherator::~SkImageCacherator() { | 62 SkImageCacherator::~SkImageCacherator() { |
63 SkDELETE(fGenerator); | 63 SkDELETE(fNotThreadSafeGenerator); |
| 64 } |
| 65 |
| 66 SkData* SkImageCacherator::refEncoded() { |
| 67 ScopedGenerator generator(this); |
| 68 return generator->refEncodedData(); |
64 } | 69 } |
65 | 70 |
66 static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) { | 71 static bool check_output_bitmap(const SkBitmap& bitmap, uint32_t expectedID) { |
67 SkASSERT(bitmap.getGenerationID() == expectedID); | 72 SkASSERT(bitmap.getGenerationID() == expectedID); |
68 SkASSERT(bitmap.isImmutable()); | 73 SkASSERT(bitmap.isImmutable()); |
69 SkASSERT(bitmap.getPixels()); | 74 SkASSERT(bitmap.getPixels()); |
70 return true; | 75 return true; |
71 } | 76 } |
72 | 77 |
73 static bool generate_bitmap(SkBitmap* bitmap, const SkImageInfo& info, const SkI
Point& origin, | 78 bool SkImageCacherator::generateBitmap(SkBitmap* bitmap) { |
74 SkImageGenerator* generator) { | 79 const size_t rowBytes = fInfo.minRowBytes(); |
75 const size_t rowBytes = info.minRowBytes(); | 80 if (!bitmap->tryAllocPixels(fInfo, rowBytes)) { |
76 if (!bitmap->tryAllocPixels(info, rowBytes)) { | |
77 return false; | 81 return false; |
78 } | 82 } |
79 SkASSERT(bitmap->rowBytes() == rowBytes); | 83 SkASSERT(bitmap->rowBytes() == rowBytes); |
80 | 84 |
| 85 ScopedGenerator generator(this); |
81 const SkImageInfo& genInfo = generator->getInfo(); | 86 const SkImageInfo& genInfo = generator->getInfo(); |
82 if (info.dimensions() == genInfo.dimensions()) { | 87 if (fInfo.dimensions() == genInfo.dimensions()) { |
83 SkASSERT(origin.x() == 0 && origin.y() == 0); | 88 SkASSERT(fOrigin.x() == 0 && fOrigin.y() == 0); |
84 // fast-case, no copy needed | 89 // fast-case, no copy needed |
85 if (!generator->getPixels(bitmap->info(), bitmap->getPixels(), rowBytes)
) { | 90 if (!generator->getPixels(bitmap->info(), bitmap->getPixels(), rowBytes)
) { |
86 bitmap->reset(); | 91 bitmap->reset(); |
87 return false; | 92 return false; |
88 } | 93 } |
89 } else { | 94 } else { |
90 // need to handle subsetting | 95 // need to handle subsetting |
91 SkBitmap full; | 96 SkBitmap full; |
92 if (!full.tryAllocPixels(genInfo)) { | 97 if (!full.tryAllocPixels(genInfo)) { |
93 return false; | 98 return false; |
94 } | 99 } |
95 if (!generator->getPixels(full.info(), full.getPixels(), full.rowBytes()
)) { | 100 if (!generator->getPixels(full.info(), full.getPixels(), full.rowBytes()
)) { |
96 bitmap->reset(); | 101 bitmap->reset(); |
97 return false; | 102 return false; |
98 } | 103 } |
99 full.readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), | 104 full.readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), |
100 origin.x(), origin.y()); | 105 fOrigin.x(), fOrigin.y()); |
101 } | 106 } |
102 return true; | 107 return true; |
103 } | 108 } |
104 | 109 |
105 ////////////////////////////////////////////////////////////////////////////////
////////////////// | 110 ////////////////////////////////////////////////////////////////////////////////
////////////////// |
106 | 111 |
107 bool SkImageCacherator::tryLockAsBitmap(SkBitmap* bitmap) { | 112 bool SkImageCacherator::tryLockAsBitmap(SkBitmap* bitmap) { |
108 if (SkBitmapCache::Find(fUniqueID, bitmap)) { | 113 if (SkBitmapCache::Find(fUniqueID, bitmap)) { |
109 return check_output_bitmap(*bitmap, fUniqueID); | 114 return check_output_bitmap(*bitmap, fUniqueID); |
110 } | 115 } |
111 if (!generate_bitmap(bitmap, fInfo, fOrigin, fGenerator)) { | 116 |
| 117 if (!this->generateBitmap(bitmap)) { |
112 return false; | 118 return false; |
113 } | 119 } |
114 | 120 |
115 bitmap->pixelRef()->setImmutableWithID(fUniqueID); | 121 bitmap->pixelRef()->setImmutableWithID(fUniqueID); |
116 SkBitmapCache::Add(fUniqueID, *bitmap); | 122 SkBitmapCache::Add(fUniqueID, *bitmap); |
117 return true; | 123 return true; |
118 } | 124 } |
119 | 125 |
120 bool SkImageCacherator::lockAsBitmap(SkBitmap* bitmap) { | 126 bool SkImageCacherator::lockAsBitmap(SkBitmap* bitmap) { |
121 if (this->tryLockAsBitmap(bitmap)) { | 127 if (this->tryLockAsBitmap(bitmap)) { |
122 return check_output_bitmap(*bitmap, fUniqueID); | 128 return check_output_bitmap(*bitmap, fUniqueID); |
123 } | 129 } |
124 | 130 |
125 #if SK_SUPPORT_GPU | 131 #if SK_SUPPORT_GPU |
126 // Try to get a texture and read it back to raster (and then cache that with
our ID) | 132 // Try to get a texture and read it back to raster (and then cache that with
our ID) |
| 133 SkAutoTUnref<GrTexture> tex; |
127 | 134 |
128 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(),
fInfo.height()); | 135 { |
129 SkAutoTUnref<GrTexture> tex(fGenerator->generateTexture(nullptr, kUntiled_Sk
ImageUsageType, | 136 ScopedGenerator generator(this); |
130 &subset)); | 137 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width
(), fInfo.height()); |
| 138 tex.reset(generator->generateTexture(nullptr, kUntiled_SkImageUsageType,
&subset)); |
| 139 } |
131 if (!tex) { | 140 if (!tex) { |
132 bitmap->reset(); | 141 bitmap->reset(); |
133 return false; | 142 return false; |
134 } | 143 } |
135 | 144 |
136 if (!bitmap->tryAllocPixels(fInfo)) { | 145 if (!bitmap->tryAllocPixels(fInfo)) { |
137 bitmap->reset(); | 146 bitmap->reset(); |
138 return false; | 147 return false; |
139 } | 148 } |
140 | 149 |
(...skipping 17 matching lines...) Expand all Loading... |
158 GrTexture* SkImageCacherator::tryLockAsTexture(GrContext* ctx, SkImageUsageType
usage) { | 167 GrTexture* SkImageCacherator::tryLockAsTexture(GrContext* ctx, SkImageUsageType
usage) { |
159 #if SK_SUPPORT_GPU | 168 #if SK_SUPPORT_GPU |
160 GrUniqueKey key; | 169 GrUniqueKey key; |
161 GrMakeKeyFromImageID(&key, fUniqueID, fInfo.width(), fInfo.height(), SkIPoin
t::Make(0, 0), | 170 GrMakeKeyFromImageID(&key, fUniqueID, fInfo.width(), fInfo.height(), SkIPoin
t::Make(0, 0), |
162 *ctx->caps(), usage); | 171 *ctx->caps(), usage); |
163 GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(key); | 172 GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(key); |
164 if (tex) { | 173 if (tex) { |
165 return tex; // we got a cache hit! | 174 return tex; // we got a cache hit! |
166 } | 175 } |
167 | 176 |
168 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width(),
fInfo.height()); | 177 { |
169 tex = fGenerator->generateTexture(ctx, usage, &subset); | 178 ScopedGenerator generator(this); |
| 179 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width
(), fInfo.height()); |
| 180 tex = generator->generateTexture(ctx, usage, &subset); |
| 181 } |
170 if (tex) { | 182 if (tex) { |
171 tex->resourcePriv().setUniqueKey(key); | 183 tex->resourcePriv().setUniqueKey(key); |
172 } | 184 } |
173 return tex; | 185 return tex; |
174 #else | 186 #else |
175 return nullptr; | 187 return nullptr; |
176 #endif | 188 #endif |
177 } | 189 } |
178 | 190 |
179 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, SkImageUsageType usa
ge) { | 191 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, SkImageUsageType usa
ge) { |
180 #if SK_SUPPORT_GPU | 192 #if SK_SUPPORT_GPU |
181 if (!ctx) { | 193 if (!ctx) { |
182 return nullptr; | 194 return nullptr; |
183 } | 195 } |
184 if (GrTexture* tex = this->tryLockAsTexture(ctx, usage)) { | 196 if (GrTexture* tex = this->tryLockAsTexture(ctx, usage)) { |
185 return tex; | 197 return tex; |
186 } | 198 } |
187 | 199 |
188 // Try to get a bitmap and then upload/cache it as a texture | 200 // Try to get a bitmap and then upload/cache it as a texture |
189 | 201 |
190 SkBitmap bitmap; | 202 SkBitmap bitmap; |
191 if (!generate_bitmap(&bitmap, fInfo, fOrigin, fGenerator)) { | 203 if (!this->generateBitmap(&bitmap)) { |
192 return nullptr; | 204 return nullptr; |
193 } | 205 } |
194 return GrRefCachedBitmapTexture(ctx, bitmap, usage); | 206 return GrRefCachedBitmapTexture(ctx, bitmap, usage); |
195 #else | 207 #else |
196 return nullptr; | 208 return nullptr; |
197 #endif | 209 #endif |
198 } | 210 } |
199 | 211 |
OLD | NEW |