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

Side by Side Diff: src/gpu/SkGr.cpp

Issue 1376603002: try texture-maker to generalize stretching for npot and min-tex-size (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "SkGr.h" 8 #include "SkGr.h"
9 9
10 #include "GrCaps.h" 10 #include "GrCaps.h"
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 break; 496 break;
497 case Stretch::kNone_Type: 497 case Stretch::kNone_Type:
498 SkDEBUGFAIL("Shouldn't get here."); 498 SkDEBUGFAIL("Shouldn't get here.");
499 break; 499 break;
500 } 500 }
501 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar (stretch.fHeight)); 501 SkRect dstRect = SkRect::MakeWH(SkIntToScalar(stretch.fWidth), SkIntToScalar (stretch.fHeight));
502 canvas.drawBitmapRect(bmp, dstRect, &paint); 502 canvas.drawBitmapRect(bmp, dstRect, &paint);
503 return stretched; 503 return stretched;
504 } 504 }
505 505
506 static GrTexture* create_bitmap_texture(GrContext* ctx,
507 const SkBitmap& bmp,
508 const Stretch& stretch,
509 const GrUniqueKey& unstretchedKey,
510 const GrUniqueKey& stretchedKey) {
511 if (Stretch::kNone_Type != stretch.fType) {
512 SkAutoTUnref<GrTexture> unstretched;
513 // Check if we have the unstretched version in the cache, if not create it.
514 if (unstretchedKey.isValid()) {
515 unstretched.reset(ctx->textureProvider()->findAndRefTextureByUniqueK ey(unstretchedKey));
516 }
517 if (!unstretched) {
518 unstretched.reset(create_unstretched_bitmap_texture(ctx, bmp, unstre tchedKey));
519 if (!unstretched) {
520 // We might not have been able to create a unstrecthed texture b ecause it is smaller
521 // than the min texture size. In that case do cpu stretching.
522 SkBitmap stretchedBmp = stretch_on_cpu(bmp, stretch);
523 return create_unstretched_bitmap_texture(ctx, stretchedBmp, stre tchedKey);
524 }
525 }
526 return stretch_texture(unstretched, stretch, bmp.pixelRef(), stretchedKe y);
527 }
528 return create_unstretched_bitmap_texture(ctx, bmp, unstretchedKey);
529 }
530
531 bool GrIsImageInCache(const GrContext* ctx, uint32_t imageID, const SkIRect& sub set, 506 bool GrIsImageInCache(const GrContext* ctx, uint32_t imageID, const SkIRect& sub set,
532 GrTexture* nativeTexture, const GrTextureParams* params) { 507 GrTexture* nativeTexture, const GrTextureParams* params) {
533 Stretch stretch; 508 Stretch stretch;
534 get_stretch(ctx, subset.width(), subset.height(), params, &stretch); 509 get_stretch(ctx, subset.width(), subset.height(), params, &stretch);
535 510
536 // Handle the case where the bitmap/image is explicitly texture backed. 511 // Handle the case where the bitmap/image is explicitly texture backed.
537 if (nativeTexture) { 512 if (nativeTexture) {
538 if (Stretch::kNone_Type == stretch.fType) { 513 if (Stretch::kNone_Type == stretch.fType) {
539 return true; 514 return true;
540 } 515 }
(...skipping 14 matching lines...) Expand all
555 530
556 bool GrIsBitmapInCache(const GrContext* ctx, const SkBitmap& bitmap, 531 bool GrIsBitmapInCache(const GrContext* ctx, const SkBitmap& bitmap,
557 const GrTextureParams* params) { 532 const GrTextureParams* params) {
558 if (bitmap.isVolatile()) { 533 if (bitmap.isVolatile()) {
559 return false; // we don't cache volatile bitmaps. 534 return false; // we don't cache volatile bitmaps.
560 } 535 }
561 return GrIsImageInCache(ctx, bitmap.getGenerationID(), bitmap.getSubset(), b itmap.getTexture(), 536 return GrIsImageInCache(ctx, bitmap.getGenerationID(), bitmap.getSubset(), b itmap.getTexture(),
562 params); 537 params);
563 } 538 }
564 539
565 GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, 540 class GrTextureMaker {
bsalomon 2015/09/29 13:23:24 Is this going to be exposed outside of this cpp? J
reed1 2015/09/29 17:41:17 Not sure, but likely (for generators, etc.) so I w
566 const SkBitmap& bitmap, 541 public:
567 const GrTextureParams* params) { 542 GrTextureMaker(int width, int height) : fWidth(width), fHeight(height) {}
568 543
544 int width() const { return fWidth; }
545 int height() const { return fHeight; }
546
547 GrTexture* refCachedTexture(GrContext*, const GrTextureParams*);
548
549 protected:
550 virtual GrTexture* peekTexture() = 0;
bsalomon 2015/09/29 13:23:24 wonder about all these virtuals... this guy can ge
reed1 2015/09/29 17:41:17 Agree to all concerns. Will start with trying to r
551 virtual void notifyStretchCached(const GrUniqueKey& stretchedKey) = 0;
552 virtual bool isVolatile() = 0;
553 virtual uint32_t imageID() = 0;
554 virtual SkIRect subset() = 0;
555 virtual GrTexture* onRefCachedTexture(GrContext*, const GrUniqueKey& unstret chedKey) = 0;
556 virtual bool getROBitmap(SkBitmap*) = 0;
557
558 private:
559 const int fWidth;
560 const int fHeight;
561
562 GrTexture* stretchTexture(GrTexture* tex, const Stretch& stretch,
563 const GrUniqueKey& stretchedKey) {
564 GrTexture* result = stretch_texture(tex, stretch, nullptr, stretchedKey) ;
565 if (result && stretchedKey.isValid()) {
566 this->notifyStretchCached(stretchedKey);
567 }
568 return result;
569 }
570 };
571
572 GrTexture* GrTextureMaker::refCachedTexture(GrContext* ctx, const GrTextureParam s* params) {
569 Stretch stretch; 573 Stretch stretch;
570 get_stretch(ctx, bitmap.width(), bitmap.height(), params, &stretch); 574 get_stretch(ctx, this->width(), this->height(), params, &stretch);
571 575
572 GrTexture* result = bitmap.getTexture(); 576 GrTexture* result = this->peekTexture();
573 if (result) { 577 if (result) {
574 if (Stretch::kNone_Type == stretch.fType) { 578 if (Stretch::kNone_Type == stretch.fType) {
575 return SkRef(result); 579 return SkRef(result);
576 } 580 }
577 GrUniqueKey stretchedKey; 581 GrUniqueKey stretchedKey;
578 // Don't create a key for the resized version if the bmp is volatile. 582 // Don't create a key for the resized version if the bmp is volatile.
579 if (!bitmap.isVolatile()) { 583 if (!this->isVolatile()) {
580 const GrUniqueKey& key = result->getUniqueKey(); 584 const GrUniqueKey& key = result->getUniqueKey();
581 if (key.isValid()) { 585 if (key.isValid()) {
582 make_stretched_key(key, stretch, &stretchedKey); 586 make_stretched_key(key, stretch, &stretchedKey);
583 GrTexture* stretched = 587 GrTexture* stretched =
584 ctx->textureProvider()->findAndRefTextureByUniqueKey(stretch edKey); 588 ctx->textureProvider()->findAndRefTextureByUniqueKey(stretch edKey);
585 if (stretched) { 589 if (stretched) {
586 return stretched; 590 return stretched;
587 } 591 }
588 } 592 }
589 } 593 }
590 return stretch_texture(result, stretch, bitmap.pixelRef(), stretchedKey) ; 594 return this->stretchTexture(result, stretch, stretchedKey);
591 } 595 }
592 596
593 GrUniqueKey key, resizedKey; 597 GrUniqueKey key, stretchedKey;
594 598
595 if (!bitmap.isVolatile()) { 599 if (!this->isVolatile()) {
596 // If the bitmap isn't changing try to find a cached copy first. 600 // If the bitmap isn't changing try to find a cached copy first.
597 make_image_keys(bitmap.getGenerationID(), bitmap.getSubset(), stretch, & key, &resizedKey); 601 make_image_keys(this->imageID(), this->subset(), stretch, &key, &stretch edKey);
598 602
599 result = ctx->textureProvider()->findAndRefTextureByUniqueKey( 603 result = ctx->textureProvider()->findAndRefTextureByUniqueKey(
600 resizedKey.isValid() ? resizedKey : key); 604 stretchedKey.isValid() ? stretchedKey : key);
601 if (result) { 605 if (result) {
602 return result; 606 return result;
603 } 607 }
604 } 608 }
605 609
606 result = create_bitmap_texture(ctx, bitmap, stretch, key, resizedKey); 610 if (Stretch::kNone_Type != stretch.fType) {
607 if (result) { 611 SkAutoTUnref<GrTexture> unstretched;
608 return result; 612 // Check if we have the unstretched version in the cache, if not create it.
613 if (key.isValid()) {
614 unstretched.reset(ctx->textureProvider()->findAndRefTextureByUniqueK ey(key));
615 }
616 if (!unstretched) {
617 if (this->width() < ctx->caps()->minTextureSize() ||
618 this->height() < ctx->caps()->minTextureSize()) {
619 SkBitmap bitmap;
620 if (!this->getROBitmap(&bitmap)) {
621 return nullptr;
622 }
623 SkBitmap stretchedBmp = stretch_on_cpu(bitmap, stretch);
624 return create_unstretched_bitmap_texture(ctx, stretchedBmp, stre tchedKey);
625 } else {
626 unstretched.reset(this->onRefCachedTexture(ctx, key));
627 if (!unstretched) {
628 return nullptr;
629 }
630 }
631 }
632 return this->stretchTexture(unstretched, stretch, stretchedKey);
633 } else {
634 return this->onRefCachedTexture(ctx, key);
609 } 635 }
636 }
610 637
611 SkErrorInternals::SetError( kInternalError_SkError, 638 class Bitmap_GrTextureMaker : public GrTextureMaker {
612 "---- failed to create texture for cache [%d %d] \n", 639 public:
613 bitmap.width(), bitmap.height()); 640 Bitmap_GrTextureMaker(const SkBitmap& bitmap)
641 : INHERITED(bitmap.width(), bitmap.height())
642 , fBitmap(bitmap)
643 {}
614 644
615 return nullptr; 645 protected:
646 bool isVolatile() override { return fBitmap.isVolatile(); }
647 uint32_t imageID() override { return fBitmap.getGenerationID(); }
648 SkIRect subset() override { return fBitmap.getSubset(); }
649 GrTexture* peekTexture() override {
650 return fBitmap.getTexture();
651 }
652 void notifyStretchCached(const GrUniqueKey& stretchedKey) override {
653 fBitmap.pixelRef()->addGenIDChangeListener(new BitmapInvalidator(stretch edKey));
654 }
655 GrTexture* onRefCachedTexture(GrContext* ctx, const GrUniqueKey& unstretched Key) override {
656 return create_unstretched_bitmap_texture(ctx, fBitmap, unstretchedKey);
657 }
658 bool getROBitmap(SkBitmap* bitmap) override {
659 *bitmap = fBitmap;
660 return true;
661 }
662 private:
663 const SkBitmap fBitmap;
664
665 typedef GrTextureMaker INHERITED;
666 };
667
668 GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, const SkBitmap& bitmap,
669 const GrTextureParams* params) {
670 return Bitmap_GrTextureMaker(bitmap).refCachedTexture(ctx, params);
616 } 671 }
617 672
618 // TODO: make this be the canonical signature, and turn the version that takes G rTextureParams* 673 // TODO: make this be the canonical signature, and turn the version that takes G rTextureParams*
619 // into a wrapper that contains the inverse of these tables. 674 // into a wrapper that contains the inverse of these tables.
620 GrTexture* GrRefCachedBitmapTexture(GrContext* ctx, 675 GrTexture* GrRefCachedBitmapTexture(GrContext* ctx,
621 const SkBitmap& bitmap, 676 const SkBitmap& bitmap,
622 SkImageUsageType usage) { 677 SkImageUsageType usage) {
623 // Just need a params that will trigger the correct cache key / etc, since t he usage doesn't 678 // Just need a params that will trigger the correct cache key / etc, since t he usage doesn't
624 // tell us the specifics about filter level or specific tiling. 679 // tell us the specifics about filter level or specific tiling.
625 680
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
941 SkErrorInternals::SetError( kInvalidPaint_SkError, 996 SkErrorInternals::SetError( kInvalidPaint_SkError,
942 "Sorry, I don't understand the filtering " 997 "Sorry, I don't understand the filtering "
943 "mode you asked for. Falling back to " 998 "mode you asked for. Falling back to "
944 "MIPMaps."); 999 "MIPMaps.");
945 textureFilterMode = GrTextureParams::kMipMap_FilterMode; 1000 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
946 break; 1001 break;
947 1002
948 } 1003 }
949 return textureFilterMode; 1004 return textureFilterMode;
950 } 1005 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698