OLD | NEW |
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 <cstddef> | 8 #include <cstddef> |
9 #include <cstring> | 9 #include <cstring> |
10 #include <type_traits> | 10 #include <type_traits> |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 | 402 |
403 | 403 |
404 return shouldUseMipMaps; | 404 return shouldUseMipMaps; |
405 } | 405 } |
406 | 406 |
407 namespace { | 407 namespace { |
408 | 408 |
409 class DTIBufferFiller | 409 class DTIBufferFiller |
410 { | 410 { |
411 public: | 411 public: |
412 explicit DTIBufferFiller(uintptr_t bufferAsInt) | 412 explicit DTIBufferFiller(char* bufferAsCharPtr) |
413 : bufferAsInt_(bufferAsInt) {} | 413 : bufferAsCharPtr_(bufferAsCharPtr) {} |
414 | 414 |
415 void fillMember(const void* source, size_t memberOffset, size_t size) { | 415 void fillMember(const void* source, size_t memberOffset, size_t size) { |
416 memcpy(reinterpret_cast<void*>(bufferAsInt_ + memberOffset), source, siz
e); | 416 memcpy(bufferAsCharPtr_ + memberOffset, source, size); |
417 } | 417 } |
418 | 418 |
419 private: | 419 private: |
420 | 420 |
421 uintptr_t bufferAsInt_; | 421 char* bufferAsCharPtr_; |
422 }; | 422 }; |
423 } | 423 } |
424 | 424 |
425 #define FILL_MEMBER(bufferFiller, member, source) \ | 425 #define FILL_MEMBER(bufferFiller, member, source) \ |
426 bufferFiller.fillMember(source, \ | 426 bufferFiller.fillMember(source, \ |
427 offsetof(DeferredTextureImage, member), \ | 427 offsetof(DeferredTextureImage, member), \ |
428 sizeof(DeferredTextureImage::member)); | 428 sizeof(DeferredTextureImage::member)); |
429 | 429 |
430 size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& prox
y, | 430 size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& prox
y, |
431 const DeferredTextureImageUsageParam
s params[], | 431 const DeferredTextureImageUsageParam
s params[], |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 size_t colorSpaceOffset = 0; | 545 size_t colorSpaceOffset = 0; |
546 size_t colorSpaceSize = 0; | 546 size_t colorSpaceSize = 0; |
547 if (info.colorSpace()) { | 547 if (info.colorSpace()) { |
548 colorSpaceOffset = size; | 548 colorSpaceOffset = size; |
549 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr); | 549 colorSpaceSize = info.colorSpace()->writeToMemory(nullptr); |
550 size += colorSpaceSize; | 550 size += colorSpaceSize; |
551 } | 551 } |
552 if (!fillMode) { | 552 if (!fillMode) { |
553 return size; | 553 return size; |
554 } | 554 } |
555 uintptr_t bufferAsInt = reinterpret_cast<uintptr_t>(buffer); | 555 char* bufferAsCharPtr = reinterpret_cast<char*>(buffer); |
556 uintptr_t pixelsAsInt = bufferAsInt + pixelOffset; | 556 char* pixelsAsCharPtr = bufferAsCharPtr + pixelOffset; |
557 void* pixels = reinterpret_cast<void*>(pixelsAsInt); | 557 void* pixels = pixelsAsCharPtr; |
558 void* ct = nullptr; | 558 void* ct = nullptr; |
559 if (ctSize) { | 559 if (ctSize) { |
560 ct = reinterpret_cast<void*>(bufferAsInt + ctOffset); | 560 ct = bufferAsCharPtr + ctOffset; |
561 } | 561 } |
562 | 562 |
563 memcpy(reinterpret_cast<void*>(SkAlign8(pixelsAsInt)), pixmap.addr(), pixmap
.getSafeSize()); | 563 memcpy(reinterpret_cast<void*>(SkAlign8(reinterpret_cast<uintptr_t>(pixelsAs
CharPtr))), |
| 564 pixmap.addr(), pixmap.getSafeSize()); |
564 if (ctSize) { | 565 if (ctSize) { |
565 memcpy(ct, pixmap.ctable()->readColors(), ctSize); | 566 memcpy(ct, pixmap.ctable()->readColors(), ctSize); |
566 } | 567 } |
567 | 568 |
568 SkASSERT(info == pixmap.info()); | 569 SkASSERT(info == pixmap.info()); |
569 size_t rowBytes = pixmap.rowBytes(); | 570 size_t rowBytes = pixmap.rowBytes(); |
570 static_assert(std::is_standard_layout<DeferredTextureImage>::value, | 571 static_assert(std::is_standard_layout<DeferredTextureImage>::value, |
571 "offsetof, which we use below, requires the type have standard
layout"); | 572 "offsetof, which we use below, requires the type have standard
layout"); |
572 auto dtiBufferFiller = DTIBufferFiller{bufferAsInt}; | 573 auto dtiBufferFiller = DTIBufferFiller{bufferAsCharPtr}; |
573 FILL_MEMBER(dtiBufferFiller, fGammaTreatment, &gammaTreatment); | 574 FILL_MEMBER(dtiBufferFiller, fGammaTreatment, &gammaTreatment); |
574 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID); | 575 FILL_MEMBER(dtiBufferFiller, fContextUniqueID, &proxy.fContextUniqueID); |
575 int width = info.width(); | 576 int width = info.width(); |
576 FILL_MEMBER(dtiBufferFiller, fWidth, &width); | 577 FILL_MEMBER(dtiBufferFiller, fWidth, &width); |
577 int height = info.height(); | 578 int height = info.height(); |
578 FILL_MEMBER(dtiBufferFiller, fHeight, &height); | 579 FILL_MEMBER(dtiBufferFiller, fHeight, &height); |
579 SkColorType colorType = info.colorType(); | 580 SkColorType colorType = info.colorType(); |
580 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType); | 581 FILL_MEMBER(dtiBufferFiller, fColorType, &colorType); |
581 SkAlphaType alphaType = info.alphaType(); | 582 SkAlphaType alphaType = info.alphaType(); |
582 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType); | 583 FILL_MEMBER(dtiBufferFiller, fAlphaType, &alphaType); |
583 FILL_MEMBER(dtiBufferFiller, fColorTableCnt, &ctCount); | 584 FILL_MEMBER(dtiBufferFiller, fColorTableCnt, &ctCount); |
584 FILL_MEMBER(dtiBufferFiller, fColorTableData, &ct); | 585 FILL_MEMBER(dtiBufferFiller, fColorTableData, &ct); |
585 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount); | 586 FILL_MEMBER(dtiBufferFiller, fMipMapLevelCount, &mipMapLevelCount); |
586 memcpy(reinterpret_cast<void*>(bufferAsInt + | 587 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].
fPixelData), |
587 offsetof(DeferredTextureImage, fMipMapLevelDat
a[0].fPixelData)), | |
588 &pixels, sizeof(pixels)); | 588 &pixels, sizeof(pixels)); |
589 memcpy(reinterpret_cast<void*>(bufferAsInt + | 589 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevelData[0].
fRowBytes), |
590 offsetof(DeferredTextureImage, fMipMapLevelDat
a[0].fRowBytes)), | |
591 &rowBytes, sizeof(rowBytes)); | 590 &rowBytes, sizeof(rowBytes)); |
592 if (colorSpaceSize) { | 591 if (colorSpaceSize) { |
593 void* colorSpace = reinterpret_cast<void*>(bufferAsInt + colorSpaceOffse
t); | 592 void* colorSpace = bufferAsCharPtr + colorSpaceOffset; |
594 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace); | 593 FILL_MEMBER(dtiBufferFiller, fColorSpace, &colorSpace); |
595 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize); | 594 FILL_MEMBER(dtiBufferFiller, fColorSpaceSize, &colorSpaceSize); |
596 info.colorSpace()->writeToMemory(reinterpret_cast<void*>(bufferAsInt + c
olorSpaceOffset)); | 595 info.colorSpace()->writeToMemory(bufferAsCharPtr + colorSpaceOffset); |
597 } else { | 596 } else { |
598 memset(reinterpret_cast<void*>(bufferAsInt + | 597 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpace), |
599 offsetof(DeferredTextureImage, fColorSpac
e)), | |
600 0, sizeof(DeferredTextureImage::fColorSpace)); | 598 0, sizeof(DeferredTextureImage::fColorSpace)); |
601 memset(reinterpret_cast<void*>(bufferAsInt + | 599 memset(bufferAsCharPtr + offsetof(DeferredTextureImage, fColorSpaceSize)
, |
602 offsetof(DeferredTextureImage, fColorSpac
eSize)), | |
603 0, sizeof(DeferredTextureImage::fColorSpaceSize)); | 600 0, sizeof(DeferredTextureImage::fColorSpaceSize)); |
604 } | 601 } |
605 | 602 |
606 // Fill in the mipmap levels if they exist | 603 // Fill in the mipmap levels if they exist |
607 uintptr_t mipLevelPtr = pixelsAsInt + SkAlign8(pixmap.getSafeSize()); | 604 char* mipLevelPtr = pixelsAsCharPtr + SkAlign8(pixmap.getSafeSize()); |
608 | 605 |
609 if (useMipMaps) { | 606 if (useMipMaps) { |
610 static_assert(std::is_standard_layout<MipMapLevelData>::value, | 607 static_assert(std::is_standard_layout<MipMapLevelData>::value, |
611 "offsetof, which we use below, requires the type have a st
andard layout"); | 608 "offsetof, which we use below, requires the type have a st
andard layout"); |
612 | 609 |
613 SkAutoTDelete<SkMipMap> mipmaps(SkMipMap::Build(pixmap, gammaTreatment,
nullptr)); | 610 SkAutoTDelete<SkMipMap> mipmaps(SkMipMap::Build(pixmap, gammaTreatment,
nullptr)); |
614 // SkMipMap holds only the mipmap levels it generates. | 611 // SkMipMap holds only the mipmap levels it generates. |
615 // A programmer can use the data they provided to SkMipMap::Build as lev
el 0. | 612 // A programmer can use the data they provided to SkMipMap::Build as lev
el 0. |
616 // So the SkMipMap provides levels 1-x but it stores them in its own | 613 // So the SkMipMap provides levels 1-x but it stores them in its own |
617 // range 0-(x-1). | 614 // range 0-(x-1). |
618 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLeve
lCount - 1; | 615 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLeve
lCount - 1; |
619 generatedMipLevelIndex++) { | 616 generatedMipLevelIndex++) { |
620 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), sca
ledSize.height(), | 617 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), sca
ledSize.height(), |
621 generatedMipLevelIndex)
; | 618 generatedMipLevelIndex)
; |
622 | 619 |
623 SkImageInfo mipInfo = SkImageInfo::MakeN32(mipSize.fWidth, mipSize.f
Height, at); | 620 SkImageInfo mipInfo = SkImageInfo::MakeN32(mipSize.fWidth, mipSize.f
Height, at); |
624 SkMipMap::Level mipLevel; | 621 SkMipMap::Level mipLevel; |
625 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel); | 622 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel); |
626 | 623 |
627 // Make sure the mipmap data is after the start of the buffer | 624 // Make sure the mipmap data is after the start of the buffer |
628 SkASSERT(mipLevelPtr > bufferAsInt); | 625 SkASSERT(mipLevelPtr > bufferAsCharPtr); |
629 // Make sure the mipmap data starts before the end of the buffer | 626 // Make sure the mipmap data starts before the end of the buffer |
630 SkASSERT(static_cast<size_t>(mipLevelPtr) < bufferAsInt + pixelOffse
t + pixelSize); | 627 SkASSERT(mipLevelPtr < bufferAsCharPtr + pixelOffset + pixelSize); |
631 // Make sure the mipmap data ends before the end of the buffer | 628 // Make sure the mipmap data ends before the end of the buffer |
632 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <= | 629 SkASSERT(mipLevelPtr + mipLevel.fPixmap.getSafeSize() <= |
633 bufferAsInt + pixelOffset + pixelSize); | 630 bufferAsCharPtr + pixelOffset + pixelSize); |
634 | 631 |
635 // getSafeSize includes rowbyte padding except for the last row, | 632 // getSafeSize includes rowbyte padding except for the last row, |
636 // right? | 633 // right? |
637 | 634 |
638 memcpy(reinterpret_cast<void*>(mipLevelPtr), mipLevel.fPixmap.addr()
, | 635 memcpy(mipLevelPtr, mipLevel.fPixmap.addr(), mipLevel.fPixmap.getSaf
eSize()); |
639 mipLevel.fPixmap.getSafeSize()); | |
640 | 636 |
641 memcpy(reinterpret_cast<void*>(bufferAsInt + | 637 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevel
Data) + |
642 offsetof(DeferredTextureImage, fMipMapLevelData) + | 638 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) + |
643 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) + | 639 offsetof(MipMapLevelData, fPixelData), &mipLevelPtr, sizeof(v
oid*)); |
644 offsetof(MipMapLevelData, fPixelData)), | |
645 &mipLevelPtr, sizeof(void*)); | |
646 size_t rowBytes = mipLevel.fPixmap.rowBytes(); | 640 size_t rowBytes = mipLevel.fPixmap.rowBytes(); |
647 memcpy(reinterpret_cast<void*>(bufferAsInt + | 641 memcpy(bufferAsCharPtr + offsetof(DeferredTextureImage, fMipMapLevel
Data) + |
648 offsetof(DeferredTextureImage, fMipMapLevelData) + | 642 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) + |
649 sizeof(MipMapLevelData) * (generatedMipLevelIndex + 1) + | 643 offsetof(MipMapLevelData, fRowBytes), &rowBytes, sizeof(rowBy
tes)); |
650 offsetof(MipMapLevelData, fRowBytes)), | |
651 &rowBytes, sizeof(rowBytes)); | |
652 | 644 |
653 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize()); | 645 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize()); |
654 } | 646 } |
655 } | 647 } |
656 return size; | 648 return size; |
657 } | 649 } |
658 | 650 |
659 sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, con
st void* data, | 651 sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, con
st void* data, |
660 SkBudgeted budgeted) { | 652 SkBudgeted budgeted) { |
661 if (!data) { | 653 if (!data) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
708 } | 700 } |
709 SkAutoTUnref<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, m
ipLevelCount)); | 701 SkAutoTUnref<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, m
ipLevelCount)); |
710 if (!texture) { | 702 if (!texture) { |
711 return nullptr; | 703 return nullptr; |
712 } | 704 } |
713 texture->texturePriv().setGammaTreatment(gammaTreatment); | 705 texture->texturePriv().setGammaTreatment(gammaTreatment); |
714 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNew
ImageUniqueID, | 706 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNew
ImageUniqueID, |
715 info.alphaType(), texture, sk_ref_sp(info.col
orSpace()), | 707 info.alphaType(), texture, sk_ref_sp(info.col
orSpace()), |
716 budgeted); | 708 budgeted); |
717 } | 709 } |
OLD | NEW |