OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 Google Inc. | 2 * Copyright 2010 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 | 8 |
9 #include "SkGr.h" | 9 #include "SkGr.h" |
10 | 10 |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 return nullptr; | 264 return nullptr; |
265 } | 265 } |
266 | 266 |
267 return ctx->textureProvider()->createTexture(desc, true, bitmap->getPixels()
, | 267 return ctx->textureProvider()->createTexture(desc, true, bitmap->getPixels()
, |
268 bitmap->rowBytes()); | 268 bitmap->rowBytes()); |
269 } | 269 } |
270 | 270 |
271 | 271 |
272 //////////////////////////////////////////////////////////////////////////////// | 272 //////////////////////////////////////////////////////////////////////////////// |
273 | 273 |
274 class Bitmap_GrTextureParamsAdjuster : public GrTextureParamsAdjuster { | 274 static void install_bmp_key_invalidator(const GrUniqueKey& key, SkPixelRef* pixe
lRef) { |
| 275 class Invalidator : public SkPixelRef::GenIDChangeListener { |
| 276 public: |
| 277 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {} |
| 278 private: |
| 279 GrUniqueKeyInvalidatedMessage fMsg; |
| 280 |
| 281 void onChange() override { SkMessageBus<GrUniqueKeyInvalidatedMessage>::
Post(fMsg); } |
| 282 }; |
| 283 |
| 284 pixelRef->addGenIDChangeListener(new Invalidator(key)); |
| 285 } |
| 286 |
| 287 class RasterBitmap_GrTextureMaker : public GrTextureMaker { |
275 public: | 288 public: |
276 Bitmap_GrTextureParamsAdjuster(const SkBitmap& bitmap) | 289 RasterBitmap_GrTextureMaker(const SkBitmap& bitmap) |
277 : INHERITED(bitmap.width(), bitmap.height()) | 290 : INHERITED(bitmap.width(), bitmap.height()) |
278 , fBitmap(bitmap) | 291 , fBitmap(bitmap) |
279 { | 292 { |
| 293 SkASSERT(!bitmap.getTexture()); |
280 if (!bitmap.isVolatile()) { | 294 if (!bitmap.isVolatile()) { |
281 SkIPoint origin = bitmap.pixelRefOrigin(); | 295 SkIPoint origin = bitmap.pixelRefOrigin(); |
282 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.widt
h(), | 296 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.widt
h(), |
283 bitmap.height()); | 297 bitmap.height()); |
284 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGeneration
ID(), subset); | 298 GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGeneration
ID(), subset); |
285 } | 299 } |
286 } | 300 } |
287 | 301 |
288 protected: | 302 protected: |
289 GrTexture* refOriginalTexture(GrContext* ctx) override { | 303 GrTexture* refOriginalTexture(GrContext* ctx) override { |
290 GrTexture* tex = fBitmap.getTexture(); | 304 GrTexture* tex; |
291 if (tex) { | |
292 return SkRef(tex); | |
293 } | |
294 | 305 |
295 if (fOriginalKey.isValid()) { | 306 if (fOriginalKey.isValid()) { |
296 tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(fOriginal
Key); | 307 tex = ctx->textureProvider()->findAndRefTextureByUniqueKey(fOriginal
Key); |
297 if (tex) { | 308 if (tex) { |
298 return tex; | 309 return tex; |
299 } | 310 } |
300 } | 311 } |
301 | 312 |
302 tex = GrUploadBitmapToTexture(ctx, fBitmap); | 313 tex = GrUploadBitmapToTexture(ctx, fBitmap); |
303 if (tex && fOriginalKey.isValid()) { | 314 if (tex && fOriginalKey.isValid()) { |
304 tex->resourcePriv().setUniqueKey(fOriginalKey); | 315 tex->resourcePriv().setUniqueKey(fOriginalKey); |
305 InstallInvalidator(fOriginalKey, fBitmap.pixelRef()); | 316 install_bmp_key_invalidator(fOriginalKey, fBitmap.pixelRef()); |
306 } | 317 } |
307 return tex; | 318 return tex; |
308 } | 319 } |
309 | 320 |
310 void makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) overrid
e { | 321 void makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) overrid
e { |
311 if (fOriginalKey.isValid()) { | 322 if (fOriginalKey.isValid()) { |
312 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey); | 323 MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey); |
313 } | 324 } |
314 } | 325 } |
315 | 326 |
316 void didCacheCopy(const GrUniqueKey& copyKey) override { | 327 void didCacheCopy(const GrUniqueKey& copyKey) override { |
317 InstallInvalidator(copyKey, fBitmap.pixelRef()); | 328 install_bmp_key_invalidator(copyKey, fBitmap.pixelRef()); |
318 } | 329 } |
319 | 330 |
320 private: | 331 private: |
321 static void InstallInvalidator(const GrUniqueKey& key, SkPixelRef* pixelRef)
{ | |
322 class Invalidator : public SkPixelRef::GenIDChangeListener { | |
323 public: | |
324 explicit Invalidator(const GrUniqueKey& key) : fMsg(key) {} | |
325 private: | |
326 GrUniqueKeyInvalidatedMessage fMsg; | |
327 | |
328 void onChange() override { | |
329 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); | |
330 } | |
331 }; | |
332 Invalidator* listener = new Invalidator(key); | |
333 pixelRef->addGenIDChangeListener(listener); | |
334 } | |
335 | |
336 const SkBitmap fBitmap; | 332 const SkBitmap fBitmap; |
337 GrUniqueKey fOriginalKey; | 333 GrUniqueKey fOriginalKey; |
338 | 334 |
339 typedef GrTextureParamsAdjuster INHERITED; | 335 typedef GrTextureMaker INHERITED; |
| 336 }; |
| 337 |
| 338 class TextureBitmap_GrTextureAdjuster : public GrTextureAdjuster { |
| 339 public: |
| 340 explicit TextureBitmap_GrTextureAdjuster(const SkBitmap* bmp) |
| 341 : INHERITED(bmp->getTexture(), SkIRect::MakeWH(bmp->width(), bmp->height
())) |
| 342 , fBmp(bmp) {} |
| 343 |
| 344 private: |
| 345 void makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) override { |
| 346 if (fBmp->isVolatile()) { |
| 347 return; |
| 348 } |
| 349 // The texture subset must represent the whole bitmap. Texture-backed bi
tmaps don't support |
| 350 // extractSubset(). Therefore, either the bitmap and the teture are the
same size or the |
| 351 // subset's dimensions are the bitmap's dimensions. |
| 352 GrUniqueKey baseKey; |
| 353 GrMakeKeyFromImageID(&baseKey, fBmp->getGenerationID(), |
| 354 SkIRect::MakeWH(fBmp->width(), fBmp->height())); |
| 355 MakeCopyKeyFromOrigKey(baseKey, params, copyKey); |
| 356 } |
| 357 |
| 358 void didCacheCopy(const GrUniqueKey& copyKey) override { |
| 359 install_bmp_key_invalidator(copyKey, fBmp->pixelRef()); |
| 360 } |
| 361 |
| 362 const SkBitmap* fBmp; |
| 363 |
| 364 typedef GrTextureAdjuster INHERITED; |
340 }; | 365 }; |
341 | 366 |
342 GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap, | 367 GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap, |
343 const GrTextureParams& params) { | 368 const GrTextureParams& params) { |
344 return Bitmap_GrTextureParamsAdjuster(bitmap).refTextureForParams(ctx, param
s); | 369 if (bitmap.getTexture()) { |
| 370 return TextureBitmap_GrTextureAdjuster(&bitmap).refTextureSafeForParams(
params, nullptr); |
| 371 } |
| 372 return RasterBitmap_GrTextureMaker(bitmap).refTextureForParams(ctx, params); |
345 } | 373 } |
346 | 374 |
347 /////////////////////////////////////////////////////////////////////////////// | 375 /////////////////////////////////////////////////////////////////////////////// |
348 | 376 |
349 // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass | 377 // alphatype is ignore for now, but if GrPixelConfig is expanded to encompass |
350 // alpha info, that will be considered. | 378 // alpha info, that will be considered. |
351 GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProf
ileType pt) { | 379 GrPixelConfig SkImageInfo2GrPixelConfig(SkColorType ct, SkAlphaType, SkColorProf
ileType pt) { |
352 switch (ct) { | 380 switch (ct) { |
353 case kUnknown_SkColorType: | 381 case kUnknown_SkColorType: |
354 return kUnknown_GrPixelConfig; | 382 return kUnknown_GrPixelConfig; |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 SkErrorInternals::SetError( kInvalidPaint_SkError, | 683 SkErrorInternals::SetError( kInvalidPaint_SkError, |
656 "Sorry, I don't understand the filtering
" | 684 "Sorry, I don't understand the filtering
" |
657 "mode you asked for. Falling back to " | 685 "mode you asked for. Falling back to " |
658 "MIPMaps."); | 686 "MIPMaps."); |
659 textureFilterMode = GrTextureParams::kMipMap_FilterMode; | 687 textureFilterMode = GrTextureParams::kMipMap_FilterMode; |
660 break; | 688 break; |
661 | 689 |
662 } | 690 } |
663 return textureFilterMode; | 691 return textureFilterMode; |
664 } | 692 } |
OLD | NEW |