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 "SkImage_Base.h" | 10 #include "SkImage_Base.h" |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 return check_output_bitmap(*bitmap, fUniqueID); | 133 return check_output_bitmap(*bitmap, fUniqueID); |
134 } | 134 } |
135 | 135 |
136 #if SK_SUPPORT_GPU | 136 #if SK_SUPPORT_GPU |
137 // Try to get a texture and read it back to raster (and then cache that with
our ID) | 137 // Try to get a texture and read it back to raster (and then cache that with
our ID) |
138 SkAutoTUnref<GrTexture> tex; | 138 SkAutoTUnref<GrTexture> tex; |
139 | 139 |
140 { | 140 { |
141 ScopedGenerator generator(this); | 141 ScopedGenerator generator(this); |
142 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width
(), fInfo.height()); | 142 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width
(), fInfo.height()); |
143 tex.reset(generator->generateTexture(nullptr, GrTextureParams::ClampNoFi
lter(), &subset)); | 143 tex.reset(generator->generateTexture(nullptr, &subset)); |
144 } | 144 } |
145 if (!tex) { | 145 if (!tex) { |
146 bitmap->reset(); | 146 bitmap->reset(); |
147 return false; | 147 return false; |
148 } | 148 } |
149 | 149 |
150 if (!bitmap->tryAllocPixels(fInfo)) { | 150 if (!bitmap->tryAllocPixels(fInfo)) { |
151 bitmap->reset(); | 151 bitmap->reset(); |
152 return false; | 152 return false; |
153 } | 153 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 | 210 |
211 /* | 211 /* |
212 * We have a 5 ways to try to return a texture (in sorted order) | 212 * We have a 5 ways to try to return a texture (in sorted order) |
213 * | 213 * |
214 * 1. Check the cache for a pre-existing one | 214 * 1. Check the cache for a pre-existing one |
215 * 2. Ask the generator to natively create one | 215 * 2. Ask the generator to natively create one |
216 * 3. Ask the generator to return a compressed form that the GPU might support | 216 * 3. Ask the generator to return a compressed form that the GPU might support |
217 * 4. Ask the generator to return YUV planes, which the GPU can convert | 217 * 4. Ask the generator to return YUV planes, which the GPU can convert |
218 * 5. Ask the generator to return RGB(A) data, which the GPU can convert | 218 * 5. Ask the generator to return RGB(A) data, which the GPU can convert |
219 */ | 219 */ |
220 GrTexture* SkImageCacherator::lockUnstretchedTexture(GrContext* ctx, const GrTex
tureParams& params, | 220 GrTexture* SkImageCacherator::lockUnstretchedTexture(GrContext* ctx, const SkIma
ge* client) { |
221 const SkImage* client) { | |
222 // textures (at least the texture-key) only support 16bit dimensions, so abo
rt early | 221 // textures (at least the texture-key) only support 16bit dimensions, so abo
rt early |
223 // if we're too big. | 222 // if we're too big. |
224 if (fInfo.width() > 0xFFFF || fInfo.height() > 0xFFFF) { | 223 if (fInfo.width() > 0xFFFF || fInfo.height() > 0xFFFF) { |
225 return nullptr; | 224 return nullptr; |
226 } | 225 } |
227 | 226 |
228 GrUniqueKey key; | 227 GrUniqueKey key; |
| 228 const GrTextureParams& noStretchParams = GrTextureParams::ClampNoFilter(); |
229 GrMakeKeyFromImageID(&key, fUniqueID, SkIRect::MakeWH(fInfo.width(), fInfo.h
eight()), | 229 GrMakeKeyFromImageID(&key, fUniqueID, SkIRect::MakeWH(fInfo.width(), fInfo.h
eight()), |
230 *ctx->caps(), params); | 230 *ctx->caps(), noStretchParams); |
231 | 231 |
232 // 1. Check the cache for a pre-existing one | 232 // 1. Check the cache for a pre-existing one |
233 if (GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(ke
y)) { | 233 if (GrTexture* tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(ke
y)) { |
234 return tex; | 234 return tex; |
235 } | 235 } |
236 | 236 |
237 // 2. Ask the genreator to natively create one | 237 // 2. Ask the genreator to natively create one |
238 { | 238 { |
239 ScopedGenerator generator(this); | 239 ScopedGenerator generator(this); |
240 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width
(), fInfo.height()); | 240 SkIRect subset = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), fInfo.width
(), fInfo.height()); |
241 if (GrTexture* tex = generator->generateTexture(ctx, params, &subset)) { | 241 if (GrTexture* tex = generator->generateTexture(ctx, &subset)) { |
242 return set_key_and_return(tex, key); | 242 return set_key_and_return(tex, key); |
243 } | 243 } |
244 } | 244 } |
245 | 245 |
246 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(fInfo); | 246 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(fInfo); |
247 | 247 |
248 // 3. Ask the generator to return a compressed form that the GPU might suppo
rt | 248 // 3. Ask the generator to return a compressed form that the GPU might suppo
rt |
249 SkAutoTUnref<SkData> data(this->refEncoded()); | 249 SkAutoTUnref<SkData> data(this->refEncoded()); |
250 if (data) { | 250 if (data) { |
251 GrTexture* tex = load_compressed_into_texture(ctx, data, desc); | 251 GrTexture* tex = load_compressed_into_texture(ctx, data, desc); |
252 if (tex) { | 252 if (tex) { |
253 return set_key_and_return(tex, key); | 253 return set_key_and_return(tex, key); |
254 } | 254 } |
255 } | 255 } |
256 | 256 |
257 // 4. Ask the generator to return YUV planes, which the GPU can convert | 257 // 4. Ask the generator to return YUV planes, which the GPU can convert |
258 { | 258 { |
259 ScopedGenerator generator(this); | 259 ScopedGenerator generator(this); |
260 Generator_GrYUVProvider provider(generator); | 260 Generator_GrYUVProvider provider(generator); |
261 GrTexture* tex = provider.refAsTexture(ctx, desc, true); | 261 GrTexture* tex = provider.refAsTexture(ctx, desc, true); |
262 if (tex) { | 262 if (tex) { |
263 return set_key_and_return(tex, key); | 263 return set_key_and_return(tex, key); |
264 } | 264 } |
265 } | 265 } |
266 | 266 |
267 // 5. Ask the generator to return RGB(A) data, which the GPU can convert | 267 // 5. Ask the generator to return RGB(A) data, which the GPU can convert |
268 SkBitmap bitmap; | 268 SkBitmap bitmap; |
269 if (this->tryLockAsBitmap(&bitmap, client)) { | 269 if (this->tryLockAsBitmap(&bitmap, client)) { |
270 return GrRefCachedBitmapTexture(ctx, bitmap, params); | 270 return GrRefCachedBitmapTexture(ctx, bitmap, noStretchParams); |
271 } | 271 } |
272 return nullptr; | 272 return nullptr; |
273 } | 273 } |
274 | 274 |
275 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 275 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
276 | 276 |
277 #include "GrTextureMaker.h" | 277 #include "GrTextureMaker.h" |
278 | 278 |
279 class Cacherator_GrTextureMaker : public GrTextureMaker { | 279 class Cacherator_GrTextureMaker : public GrTextureMaker { |
280 public: | 280 public: |
281 Cacherator_GrTextureMaker(SkImageCacherator* cacher, const GrTextureParams&
params, | 281 Cacherator_GrTextureMaker(SkImageCacherator* cacher, const SkImage* client, |
282 const SkImage* client, const GrUniqueKey& unstretc
hedKey) | 282 const GrUniqueKey& unstretchedKey) |
283 : INHERITED(cacher->info().width(), cacher->info().height()) | 283 : INHERITED(cacher->info().width(), cacher->info().height()) |
284 , fCacher(cacher) | 284 , fCacher(cacher) |
285 , fParams(params) | |
286 , fClient(client) | 285 , fClient(client) |
287 , fUnstretchedKey(unstretchedKey) | 286 , fUnstretchedKey(unstretchedKey) |
288 {} | 287 {} |
289 | 288 |
290 protected: | 289 protected: |
291 // TODO: consider overriding this, for the case where the underlying generat
or might be | 290 // TODO: consider overriding this, for the case where the underlying generat
or might be |
292 // able to efficiently produce a "stretched" texture natively (e.g. pi
cture-backed) | 291 // able to efficiently produce a "stretched" texture natively (e.g. pi
cture-backed) |
293 // GrTexture* onGenerateStretchedTexture(GrContext*, const SkGrStretch&) over
ride; | 292 // GrTexture* onGenerateStretchedTexture(GrContext*, const SkGrStretch&) over
ride; |
294 | 293 |
295 GrTexture* onRefUnstretchedTexture(GrContext* ctx) override { | 294 GrTexture* onRefUnstretchedTexture(GrContext* ctx) override { |
296 return fCacher->lockUnstretchedTexture(ctx, fParams, fClient); | 295 return fCacher->lockUnstretchedTexture(ctx, fClient); |
297 } | 296 } |
298 | 297 |
299 bool onMakeStretchedKey(const SkGrStretch& stretch, GrUniqueKey* stretchedKe
y) override { | 298 bool onMakeStretchedKey(const SkGrStretch& stretch, GrUniqueKey* stretchedKe
y) override { |
300 return GrMakeStretchedKey(fUnstretchedKey, stretch, stretchedKey); | 299 return GrMakeStretchedKey(fUnstretchedKey, stretch, stretchedKey); |
301 } | 300 } |
302 | 301 |
303 void onNotifyStretchCached(const GrUniqueKey& stretchedKey) override { | 302 void onNotifyStretchCached(const GrUniqueKey& stretchedKey) override { |
304 if (fClient) { | 303 if (fClient) { |
305 as_IB(fClient)->notifyAddedToCache(); | 304 as_IB(fClient)->notifyAddedToCache(); |
306 } | 305 } |
307 } | 306 } |
308 | 307 |
309 bool onGetROBitmap(SkBitmap* bitmap) override { | 308 bool onGetROBitmap(SkBitmap* bitmap) override { |
310 return fCacher->lockAsBitmap(bitmap, fClient); | 309 return fCacher->lockAsBitmap(bitmap, fClient); |
311 } | 310 } |
312 | 311 |
313 private: | 312 private: |
314 SkImageCacherator* fCacher; | 313 SkImageCacherator* fCacher; |
315 const GrTextureParams& fParams; | |
316 const SkImage* fClient; | 314 const SkImage* fClient; |
317 const GrUniqueKey fUnstretchedKey; | 315 const GrUniqueKey fUnstretchedKey; |
318 | 316 |
319 typedef GrTextureMaker INHERITED; | 317 typedef GrTextureMaker INHERITED; |
320 }; | 318 }; |
321 | 319 |
322 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s& params, | 320 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s& params, |
323 const SkImage* client) { | 321 const SkImage* client) { |
324 if (!ctx) { | 322 if (!ctx) { |
325 return nullptr; | 323 return nullptr; |
326 } | 324 } |
327 | 325 |
328 GrUniqueKey key; | 326 GrUniqueKey key; |
329 GrMakeKeyFromImageID(&key, this->uniqueID(), | 327 GrMakeKeyFromImageID(&key, this->uniqueID(), |
330 SkIRect::MakeWH(this->info().width(), this->info().heig
ht()), | 328 SkIRect::MakeWH(this->info().width(), this->info().heig
ht()), |
331 *ctx->caps(), params); | 329 *ctx->caps(), GrTextureParams::ClampNoFilter()); |
332 | 330 |
333 return Cacherator_GrTextureMaker(this, params, client, key).refCachedTexture
(ctx, params); | 331 return Cacherator_GrTextureMaker(this, client, key).refCachedTexture(ctx, pa
rams); |
334 } | 332 } |
335 | 333 |
336 #else | 334 #else |
337 | 335 |
338 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s&, | 336 GrTexture* SkImageCacherator::lockAsTexture(GrContext* ctx, const GrTextureParam
s&, |
339 const SkImage* client) { | 337 const SkImage* client) { |
340 return nullptr; | 338 return nullptr; |
341 } | 339 } |
342 | 340 |
343 #endif | 341 #endif |
OLD | NEW |