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

Side by Side Diff: src/image/SkImage_Gpu.cpp

Issue 2242883004: Store mipmap levels in deferred texture image (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Adding no strict aliasing and some asserts. Created 4 years, 4 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
OLDNEW
1 /* 1 /*
2 * Copyright 2012 Google Inc. 2 * Copyright 2012 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 "SkAutoPixmapStorage.h" 8 #include "SkAutoPixmapStorage.h"
9 #include "GrCaps.h" 9 #include "GrCaps.h"
10 #include "GrContext.h" 10 #include "GrContext.h"
11 #include "GrDrawContext.h" 11 #include "GrDrawContext.h"
12 #include "GrImageIDTextureAdjuster.h" 12 #include "GrImageIDTextureAdjuster.h"
13 #include "GrTexturePriv.h"
13 #include "effects/GrYUVEffect.h" 14 #include "effects/GrYUVEffect.h"
14 #include "SkCanvas.h" 15 #include "SkCanvas.h"
15 #include "SkBitmapCache.h" 16 #include "SkBitmapCache.h"
16 #include "SkGrPriv.h" 17 #include "SkGrPriv.h"
17 #include "SkImage_Gpu.h" 18 #include "SkImage_Gpu.h"
18 #include "SkMipMap.h" 19 #include "SkMipMap.h"
19 #include "SkPixelRef.h" 20 #include "SkPixelRef.h"
20 21
21 SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrText ure* tex, 22 SkImage_Gpu::SkImage_Gpu(int w, int h, uint32_t uniqueID, SkAlphaType at, GrText ure* tex,
22 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted) 23 sk_sp<SkColorSpace> colorSpace, SkBudgeted budgeted)
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 358
358 //////////////////////////////////////////////////////////////////////////////// /////////////////// 359 //////////////////////////////////////////////////////////////////////////////// ///////////////////
359 360
360 namespace { 361 namespace {
361 struct MipMapLevelData { 362 struct MipMapLevelData {
362 void* fPixelData; 363 void* fPixelData;
363 size_t fRowBytes; 364 size_t fRowBytes;
364 }; 365 };
365 366
366 struct DeferredTextureImage { 367 struct DeferredTextureImage {
367 uint32_t fContextUniqueID; 368 uint32_t fContextUniqueID;
369 // Right now, the gamma treatment is only considered when generating mipmaps
370 SkSourceGammaTreatment fGammaTreatment;
368 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpa ce. 371 // We don't store a SkImageInfo because it contains a ref-counted SkColorSpa ce.
369 int fWidth; 372 int fWidth;
370 int fHeight; 373 int fHeight;
371 SkColorType fColorType; 374 SkColorType fColorType;
372 SkAlphaType fAlphaType; 375 SkAlphaType fAlphaType;
373 void* fColorSpace; 376 void* fColorSpace;
374 size_t fColorSpaceSize; 377 size_t fColorSpaceSize;
375 int fColorTableCnt; 378 int fColorTableCnt;
376 uint32_t* fColorTableData; 379 uint32_t* fColorTableData;
377 int fMipMapLevelCount; 380 int fMipMapLevelCount;
378 // The fMipMapLevelData array may contain more than 1 element. 381 // The fMipMapLevelData array may contain more than 1 element.
379 // It contains fMipMapLevelCount elements. 382 // It contains fMipMapLevelCount elements.
380 // That means this struct's size is not known at compile-time. 383 // That means this struct's size is not known at compile-time.
381 MipMapLevelData fMipMapLevelData[1]; 384 MipMapLevelData fMipMapLevelData[1];
382 }; 385 };
383 } // anonymous namespace 386 } // anonymous namespace
384 387
388 static bool should_use_mip_maps(const SkImage::DeferredTextureImageUsageParams & param) {
389 bool shouldUseMipMaps = false;
390
391 // Use mipmaps if either
392 // 1.) it is a perspective matrix, or
393 // 2.) the quality is med/high and the scale is < 1
394 if (param.fMatrix.hasPerspective()) {
395 shouldUseMipMaps = true;
396 }
397 if (param.fQuality == kMedium_SkFilterQuality ||
398 param.fQuality == kHigh_SkFilterQuality) {
399 SkScalar minAxisScale = param.fMatrix.getMinScale();
400 if (minAxisScale != -1.f && minAxisScale < 1.f) {
401 shouldUseMipMaps = true;
402 }
403 }
404
405 return shouldUseMipMaps;
406 }
407
385 size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& prox y, 408 size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& prox y,
386 const DeferredTextureImageUsageParam s params[], 409 const DeferredTextureImageUsageParam s params[],
387 int paramCnt, void* buffer) const { 410 int paramCnt, void* buffer,
411 SkSourceGammaTreatment gammaTreatmen t) const {
388 // Extract relevant min/max values from the params array. 412 // Extract relevant min/max values from the params array.
389 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel; 413 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel;
390 SkFilterQuality highestFilterQuality = params[0].fQuality; 414 SkFilterQuality highestFilterQuality = params[0].fQuality;
415 bool useMipMaps = should_use_mip_maps(params[0]);
391 for (int i = 1; i < paramCnt; ++i) { 416 for (int i = 1; i < paramCnt; ++i) {
392 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel) 417 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel)
393 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel; 418 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel;
394 if (highestFilterQuality < params[i].fQuality) 419 if (highestFilterQuality < params[i].fQuality)
395 highestFilterQuality = params[i].fQuality; 420 highestFilterQuality = params[i].fQuality;
421 useMipMaps |= should_use_mip_maps(params[i]);
396 } 422 }
397 423
398 const bool fillMode = SkToBool(buffer); 424 const bool fillMode = SkToBool(buffer);
399 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) { 425 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) {
400 return 0; 426 return 0;
401 } 427 }
402 428
403 // Calculate scaling parameters. 429 // Calculate scaling parameters.
404 bool isScaled = lowestPreScaleMipLevel != 0; 430 bool isScaled = lowestPreScaleMipLevel != 0;
405 431
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 return 0; 481 return 0;
456 } 482 }
457 } else { 483 } else {
458 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHi nt)) { 484 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHi nt)) {
459 return 0; 485 return 0;
460 } 486 }
461 } 487 }
462 SkASSERT(!pixmap.ctable()); 488 SkASSERT(!pixmap.ctable());
463 } 489 }
464 } 490 }
491 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaTyp e;
465 int mipMapLevelCount = 1; 492 int mipMapLevelCount = 1;
493 if (useMipMaps) {
494 // SkMipMap only deals with the mipmap levels it generates, which does
495 // not include the base level.
496 // That means it generates and holds levels 1-x instead of 0-x.
497 // So the total mipmap level count is 1 more than what
498 // SkMipMap::ComputeLevelCount returns.
499 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scale dSize.height()) + 1;
500
501 // We already initialized pixelSize to the size of the base level.
502 // SkMipMap will generate the extra mipmap levels. Their sizes need to
503 // be added to the total.
504 // Index 0 here does not refer to the base mipmap level -- it is
505 // SkMipMap's first generated mipmap level (level 1).
506 for (int currentMipMapLevelIndex = mipMapLevelCount - 1; currentMipMapLe velIndex >= 0;
507 currentMipMapLevelIndex--) {
508 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), sca ledSize.height(),
509 currentMipMapLevelIndex );
510 SkImageInfo mipInfo = SkImageInfo::MakeN32(mipSize.fWidth, mipSize.f Height, at);
511 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullpt r));
512 }
513 }
466 size_t size = 0; 514 size_t size = 0;
467 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage)); 515 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage));
468 size += dtiSize; 516 size += dtiSize;
469 size += mipMapLevelCount * sizeof(MipMapLevelData); 517 size += mipMapLevelCount * sizeof(MipMapLevelData[1]);
470 size_t pixelOffset = size; 518 size_t pixelOffset = size;
471 size += pixelSize; 519 size += pixelSize;
472 size_t ctOffset = size; 520 size_t ctOffset = size;
473 size += ctSize; 521 size += ctSize;
474 size_t colorSpaceOffset = 0; 522 size_t colorSpaceOffset = 0;
475 size_t colorSpaceSize = 0; 523 size_t colorSpaceSize = 0;
476 if (info.colorSpace()) { 524 if (info.colorSpace()) {
477 colorSpaceOffset = size; 525 colorSpaceOffset = size;
478 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr); 526 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr);
479 size += colorSpaceSize; 527 size += colorSpaceSize;
480 } 528 }
481 if (!fillMode) { 529 if (!fillMode) {
482 return size; 530 return size;
483 } 531 }
484 intptr_t bufferAsInt = reinterpret_cast<intptr_t>(buffer); 532 intptr_t bufferAsInt = reinterpret_cast<intptr_t>(buffer);
cblume 2016/08/12 22:25:35 The problem is we are remapping buffer to several
cblume 2016/08/15 23:39:47 Sorry, I said this wrong. This is not an instance
485 void* pixels = reinterpret_cast<void*>(bufferAsInt + pixelOffset); 533 void* pixels = reinterpret_cast<void*>(bufferAsInt + pixelOffset);
486 SkPMColor* ct = nullptr; 534 SkPMColor* ct = nullptr;
487 if (ctSize) { 535 if (ctSize) {
488 ct = reinterpret_cast<SkPMColor*>(bufferAsInt + ctOffset); 536 ct = reinterpret_cast<SkPMColor*>(bufferAsInt + ctOffset);
cblume 2016/08/12 22:25:35 Here is another.
489 } 537 }
490 538
491 memcpy(pixels, pixmap.addr(), pixmap.getSafeSize()); 539 memcpy(pixels, pixmap.addr(), pixmap.getSafeSize());
492 if (ctSize) { 540 if (ctSize) {
493 memcpy(ct, pixmap.ctable()->readColors(), ctSize); 541 memcpy(ct, pixmap.ctable()->readColors(), ctSize);
494 } 542 }
495 543
496 SkASSERT(info == pixmap.info()); 544 SkASSERT(info == pixmap.info());
497 size_t rowBytes = pixmap.rowBytes(); 545 size_t rowBytes = pixmap.rowBytes();
498 DeferredTextureImage* dti = new (buffer) DeferredTextureImage(); 546 DeferredTextureImage* dti = new (buffer) DeferredTextureImage();
cblume 2016/08/12 22:25:35 Here is a third.
547 dti->fGammaTreatment = gammaTreatment;
499 dti->fContextUniqueID = proxy.fContextUniqueID; 548 dti->fContextUniqueID = proxy.fContextUniqueID;
500 dti->fWidth = info.width(); 549 dti->fWidth = info.width();
501 dti->fHeight = info.height(); 550 dti->fHeight = info.height();
502 dti->fColorType = info.colorType(); 551 dti->fColorType = info.colorType();
503 dti->fAlphaType = info.alphaType(); 552 dti->fAlphaType = info.alphaType();
504 dti->fColorTableCnt = ctCount; 553 dti->fColorTableCnt = ctCount;
505 dti->fColorTableData = ct; 554 dti->fColorTableData = ct;
cblume 2016/08/12 22:25:35 At this point, 'ct' may not have been written beca
cblume 2016/08/15 23:39:47 I think this might be the bigger issue. At this po
506 dti->fMipMapLevelCount = mipMapLevelCount; 555 dti->fMipMapLevelCount = mipMapLevelCount;
507 dti->fMipMapLevelData[0].fPixelData = pixels; 556 dti->fMipMapLevelData[0].fPixelData = pixels;
508 dti->fMipMapLevelData[0].fRowBytes = rowBytes; 557 dti->fMipMapLevelData[0].fRowBytes = rowBytes;
509 if (colorSpaceSize) { 558 if (colorSpaceSize) {
510 dti->fColorSpace = reinterpret_cast<void*>(bufferAsInt + colorSpaceOffse t); 559 dti->fColorSpace = reinterpret_cast<void*>(bufferAsInt + colorSpaceOffse t);
cblume 2016/08/12 22:25:35 dti and bufferAsInt may be rearranged here. I'm no
511 dti->fColorSpaceSize = colorSpaceSize; 560 dti->fColorSpaceSize = colorSpaceSize;
512 info.colorSpace()->writeToMemory(dti->fColorSpace); 561 info.colorSpace()->writeToMemory(dti->fColorSpace);
513 } else { 562 } else {
514 dti->fColorSpace = nullptr; 563 dti->fColorSpace = nullptr;
515 dti->fColorSpaceSize = 0; 564 dti->fColorSpaceSize = 0;
516 } 565 }
566
567 // Fill in the mipmap levels if they exist
568 intptr_t mipLevelPtr = bufferAsInt + pixelOffset + SkAlign8(SkAutoPixmapStor age::AllocSize(
cblume 2016/08/12 22:25:35 This feeds off bufferAsInt and isn't introducing a
569 info, nullptr));
570 if (useMipMaps) {
571 SkAutoTDelete<SkMipMap> mipmaps(SkMipMap::Build(pixmap, gammaTreatment, nullptr));
572 // SkMipMap holds only the mipmap levels it generates.
573 // A programmer can use the data they provided to SkMipMap::Build as lev el 0.
574 // So the SkMipMap provides levels 1-x but it stores them in its own
575 // range 0-(x-1).
576 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLeve lCount - 1;
577 generatedMipLevelIndex++) {
578 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), sca ledSize.height(),
579 generatedMipLevelIndex) ;
580 SkImageInfo mipInfo = SkImageInfo::MakeN32(mipSize.fWidth, mipSize.f Height, at);
581 SkMipMap::Level mipLevel;
582 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel);
583 SkASSERT(mipLevelPtr > bufferAsInt && (size_t)mipLevelPtr < bufferAs Int + pixelOffset + pixelSize);
584 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <= bufferAsIn t + pixelOffset + pixelSize);
cblume 2016/08/12 22:25:35 (I added some asserts to show the memcpy is sane.)
585 //SkASSERT(false);
586 memcpy(reinterpret_cast<void*>(mipLevelPtr), mipLevel.fPixmap.addr() ,
cblume 2016/08/12 22:25:35 Here, we cast mipLevelPtr back to void*. The compi
587 mipLevel.fPixmap.getSafeSize());
588 dti->fMipMapLevelData[generatedMipLevelIndex + 1].fPixelData =
589 reinterpret_cast<void*>(mipLevelPtr);
cblume 2016/08/12 22:25:35 dti's and mipLevelPtr's reads and writes can be re
590 dti->fMipMapLevelData[generatedMipLevelIndex + 1].fRowBytes =
591 mipLevel.fPixmap.rowBytes();
592 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize());
cblume 2016/08/12 22:25:35 Or this line could be reordered above the dti->bla
593 }
594 }
517 return size; 595 return size;
518 } 596 }
519 597
520 sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, con st void* data, 598 sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, con st void* data,
521 SkBudgeted budgeted) { 599 SkBudgeted budgeted) {
522 if (!data) { 600 if (!data) {
523 return nullptr; 601 return nullptr;
524 } 602 }
525 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImag e*>(data); 603 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImag e*>(data);
526 604
527 if (!context || context->uniqueID() != dti->fContextUniqueID) { 605 if (!context || context->uniqueID() != dti->fContextUniqueID) {
528 return nullptr; 606 return nullptr;
529 } 607 }
530 SkAutoTUnref<SkColorTable> colorTable; 608 SkAutoTUnref<SkColorTable> colorTable;
531 if (dti->fColorTableCnt) { 609 if (dti->fColorTableCnt) {
532 SkASSERT(dti->fColorTableData); 610 SkASSERT(dti->fColorTableData);
533 colorTable.reset(new SkColorTable(dti->fColorTableData, dti->fColorTable Cnt)); 611 colorTable.reset(new SkColorTable(dti->fColorTableData, dti->fColorTable Cnt));
534 } 612 }
535 SkASSERT(dti->fMipMapLevelCount == 1); 613 int mipLevelCount = dti->fMipMapLevelCount;
614 SkASSERT(mipLevelCount >= 1);
536 sk_sp<SkColorSpace> colorSpace; 615 sk_sp<SkColorSpace> colorSpace;
537 if (dti->fColorSpaceSize) { 616 if (dti->fColorSpaceSize) {
538 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpac eSize); 617 colorSpace = SkColorSpace::Deserialize(dti->fColorSpace, dti->fColorSpac eSize);
539 } 618 }
540 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight, 619 SkImageInfo info = SkImageInfo::Make(dti->fWidth, dti->fHeight,
541 dti->fColorType, dti->fAlphaType, color Space); 620 dti->fColorType, dti->fAlphaType, color Space);
542 SkPixmap pixmap; 621 if (mipLevelCount == 1) {
543 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData, 622 SkPixmap pixmap;
544 dti->fMipMapLevelData[0].fRowBytes, colorTable.get()); 623 pixmap.reset(info, dti->fMipMapLevelData[0].fPixelData,
545 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted); 624 dti->fMipMapLevelData[0].fRowBytes, colorTable.get());
625 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted);
626 } else {
627 SkAutoTDeleteArray<GrMipLevel> texels(new GrMipLevel[mipLevelCount]);
628 for (int i = 0; i < mipLevelCount; i++) {
629 texels[i].fPixels = dti->fMipMapLevelData[i].fPixelData;
630 texels[i].fRowBytes = dti->fMipMapLevelData[i].fRowBytes;
631 }
632
633 return SkImage::MakeTextureFromMipMap(context, info, texels.get(),
634 mipLevelCount, SkBudgeted::kYes,
635 dti->fGammaTreatment);
636 }
546 } 637 }
547 638
548 //////////////////////////////////////////////////////////////////////////////// /////////////////// 639 //////////////////////////////////////////////////////////////////////////////// ///////////////////
549 640
550 GrTexture* GrDeepCopyTexture(GrTexture* src, SkBudgeted budgeted) { 641 GrTexture* GrDeepCopyTexture(GrTexture* src, SkBudgeted budgeted) {
551 GrContext* ctx = src->getContext(); 642 GrContext* ctx = src->getContext();
552 643
553 GrSurfaceDesc desc = src->desc(); 644 GrSurfaceDesc desc = src->desc();
554 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullp tr, 0); 645 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullp tr, 0);
555 if (!dst) { 646 if (!dst) {
556 return nullptr; 647 return nullptr;
557 } 648 }
558 649
559 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); 650 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight);
560 const SkIPoint dstP = SkIPoint::Make(0, 0); 651 const SkIPoint dstP = SkIPoint::Make(0, 0);
561 ctx->copySurface(dst, src, srcR, dstP); 652 ctx->copySurface(dst, src, srcR, dstP);
562 ctx->flushSurfaceWrites(dst); 653 ctx->flushSurfaceWrites(dst);
563 return dst; 654 return dst;
564 } 655 }
565 656
566 sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info, 657 sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo& info,
567 const GrMipLevel* texels, int mipL evelCount, 658 const GrMipLevel* texels, int mipL evelCount,
568 SkBudgeted budgeted) { 659 SkBudgeted budgeted,
660 SkSourceGammaTreatment gammaTreatm ent) {
569 if (!ctx) { 661 if (!ctx) {
570 return nullptr; 662 return nullptr;
571 } 663 }
572 SkAutoTUnref<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, m ipLevelCount)); 664 SkAutoTUnref<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, m ipLevelCount));
573 if (!texture) { 665 if (!texture) {
574 return nullptr; 666 return nullptr;
575 } 667 }
668 texture->texturePriv().setGammaTreatment(gammaTreatment);
576 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNew ImageUniqueID, 669 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNew ImageUniqueID,
577 info.alphaType(), texture, sk_ref_sp(info.col orSpace()), 670 info.alphaType(), texture, sk_ref_sp(info.col orSpace()),
578 budgeted); 671 budgeted);
579 } 672 }
OLDNEW
« BUILD.gn ('K') | « src/image/SkImage.cpp ('k') | tests/ImageTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698