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 "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" | |
14 #include "effects/GrYUVEffect.h" | 13 #include "effects/GrYUVEffect.h" |
15 #include "SkCanvas.h" | 14 #include "SkCanvas.h" |
16 #include "SkBitmapCache.h" | 15 #include "SkBitmapCache.h" |
17 #include "SkGrPixelRef.h" | 16 #include "SkGrPixelRef.h" |
18 #include "SkGrPriv.h" | 17 #include "SkGrPriv.h" |
19 #include "SkImage_Gpu.h" | 18 #include "SkImage_Gpu.h" |
20 #include "SkMipMap.h" | 19 #include "SkMipMap.h" |
21 #include "SkPixelRef.h" | 20 #include "SkPixelRef.h" |
22 | 21 |
23 namespace { | |
24 bool shouldUseMipMaps(const SkImage::DeferredTextureImageUsageParams & param) { | |
25 bool shouldUseMipMaps = false; | |
26 | |
27 // Use mipmaps if either | |
28 // 1.) it is a perspective matrix, or | |
29 // 2.) the quality is med/high and the scale is < 1 | |
30 if (param.fMatrix.hasPerspective()) { | |
31 shouldUseMipMaps = true; | |
32 } | |
33 if (param.fQuality == kMedium_SkFilterQuality || | |
34 param.fQuality == kHigh_SkFilterQuality) { | |
35 SkScalar minAxisScale = param.fMatrix.getMinScale(); | |
36 if (minAxisScale != -1.f && minAxisScale < 1.f) { | |
37 shouldUseMipMaps = true; | |
38 } | |
39 } | |
40 | |
41 return shouldUseMipMaps; | |
42 } | |
43 } | |
44 | |
45 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, |
46 SkBudgeted budgeted) | 23 SkBudgeted budgeted) |
47 : INHERITED(w, h, uniqueID) | 24 : INHERITED(w, h, uniqueID) |
48 , fTexture(SkRef(tex)) | 25 , fTexture(SkRef(tex)) |
49 , fAlphaType(at) | 26 , fAlphaType(at) |
50 , fBudgeted(budgeted) | 27 , fBudgeted(budgeted) |
51 , fAddedRasterVersionToCache(false) | 28 , fAddedRasterVersionToCache(false) |
52 { | 29 { |
53 SkASSERT(tex->width() == w); | 30 SkASSERT(tex->width() == w); |
54 SkASSERT(tex->height() == h); | 31 SkASSERT(tex->height() == h); |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 } | 346 } |
370 | 347 |
371 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 348 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
372 | 349 |
373 class DeferredTextureImage { | 350 class DeferredTextureImage { |
374 public: | 351 public: |
375 SkImage* newImage(GrContext* context, SkBudgeted) const; | 352 SkImage* newImage(GrContext* context, SkBudgeted) const; |
376 | 353 |
377 private: | 354 private: |
378 uint32_t fContextUniqueID; | 355 uint32_t fContextUniqueID; |
379 // Right now, the gamma treatment is only considered when generating mipmaps | |
380 SkSourceGammaTreatment fGammaTreatment; | |
381 struct MipMapLevelData { | 356 struct MipMapLevelData { |
382 void* fPixelData; | 357 void* fPixelData; |
383 size_t fRowBytes; | 358 size_t fRowBytes; |
384 }; | 359 }; |
385 struct Data { | 360 struct Data { |
386 SkImageInfo fInfo; | 361 SkImageInfo fInfo; |
387 int fColorTableCnt; | 362 int fColorTableCnt; |
388 uint32_t* fColorTableData; | 363 uint32_t* fColorTableData; |
389 int fMipMapLevelCount; | 364 int fMipMapLevelCount; |
390 // The fMipMapLevelData array may contain more than 1 element. | 365 // The fMipMapLevelData array may contain more than 1 element. |
391 // It contains fMipMapLevelCount elements. | 366 // It contains fMipMapLevelCount elements. |
392 // That means this struct's size is not known at compile-time. | 367 // That means this struct's size is not known at compile-time. |
393 MipMapLevelData fMipMapLevelData[1]; | 368 MipMapLevelData fMipMapLevelData[1]; |
394 }; | 369 }; |
395 Data fData; | 370 Data fData; |
396 | 371 |
397 friend class SkImage; | 372 friend class SkImage; |
398 }; | 373 }; |
399 | 374 |
400 size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& prox
y, | 375 size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& prox
y, |
401 const DeferredTextureImageUsageParam
s params[], | 376 const DeferredTextureImageUsageParam
s params[], |
402 int paramCnt, void* buffer, | 377 int paramCnt, void* buffer) const { |
403 SkSourceGammaTreatment gammaTreatmen
t) const { | |
404 // Extract relevant min/max values from the params array. | 378 // Extract relevant min/max values from the params array. |
405 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel; | 379 int lowestPreScaleMipLevel = params[0].fPreScaleMipLevel; |
406 SkFilterQuality highestFilterQuality = params[0].fQuality; | 380 SkFilterQuality highestFilterQuality = params[0].fQuality; |
407 bool useMipMaps = shouldUseMipMaps(params[0]); | |
408 for (int i = 1; i < paramCnt; ++i) { | 381 for (int i = 1; i < paramCnt; ++i) { |
409 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel) | 382 if (lowestPreScaleMipLevel > params[i].fPreScaleMipLevel) |
410 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel; | 383 lowestPreScaleMipLevel = params[i].fPreScaleMipLevel; |
411 if (highestFilterQuality < params[i].fQuality) | 384 if (highestFilterQuality < params[i].fQuality) |
412 highestFilterQuality = params[i].fQuality; | 385 highestFilterQuality = params[i].fQuality; |
413 useMipMaps |= shouldUseMipMaps(params[i]); | |
414 } | 386 } |
415 | 387 |
416 const bool fillMode = SkToBool(buffer); | 388 const bool fillMode = SkToBool(buffer); |
417 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) { | 389 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) { |
418 return 0; | 390 return 0; |
419 } | 391 } |
420 | 392 |
421 // Calculate scaling parameters. | 393 // Calculate scaling parameters. |
422 bool isScaled = lowestPreScaleMipLevel != 0; | 394 bool isScaled = lowestPreScaleMipLevel != 0; |
423 | 395 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 return 0; | 445 return 0; |
474 } | 446 } |
475 } else { | 447 } else { |
476 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHi
nt)) { | 448 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHi
nt)) { |
477 return 0; | 449 return 0; |
478 } | 450 } |
479 } | 451 } |
480 SkASSERT(!pixmap.ctable()); | 452 SkASSERT(!pixmap.ctable()); |
481 } | 453 } |
482 } | 454 } |
483 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaTyp
e; | |
484 int mipMapLevelCount = 1; | 455 int mipMapLevelCount = 1; |
485 if (useMipMaps) { | |
486 // SkMipMap only deals with the mipmap levels it generates, which does | |
487 // not include the base level. | |
488 // That means it generates and holds levels 1-x instead of 0-x. | |
489 // So the total mipmap level count is 1 more than what | |
490 // SkMipMap::ComputeLevelCount returns. | |
491 mipMapLevelCount = SkMipMap::ComputeLevelCount(scaledSize.width(), scale
dSize.height()) + 1; | |
492 | |
493 // We already initialized pixelSize to the size of the base level. | |
494 // SkMipMap will generate the extra mipmap levels. Their sizes need to | |
495 // be added to the total. | |
496 // Index 0 here does not refer to the base mipmap level -- it is | |
497 // SkMipMap's first generated mipmap level (level 1). | |
498 for (int currentMipMapLevelIndex = mipMapLevelCount - 1; currentMipMapLe
velIndex >= 0; | |
499 currentMipMapLevelIndex--) { | |
500 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), sca
ledSize.height(), | |
501 currentMipMapLevelIndex
); | |
502 SkImageInfo mipInfo = SkImageInfo::MakeN32(mipSize.fWidth, mipSize.f
Height, at); | |
503 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullpt
r)); | |
504 } | |
505 } | |
506 size_t size = 0; | 456 size_t size = 0; |
507 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage)); | 457 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage)); |
508 size += dtiSize; | 458 size += dtiSize; |
509 size += mipMapLevelCount * sizeof(DeferredTextureImage::MipMapLevelData); | 459 size += mipMapLevelCount * sizeof(DeferredTextureImage::MipMapLevelData); |
510 size_t pixelOffset = size; | 460 size_t pixelOffset = size; |
511 size += pixelSize; | 461 size += pixelSize; |
512 size_t ctOffset = size; | 462 size_t ctOffset = size; |
513 size += ctSize; | 463 size += ctSize; |
514 if (!fillMode) { | 464 if (!fillMode) { |
515 return size; | 465 return size; |
516 } | 466 } |
517 intptr_t bufferAsInt = reinterpret_cast<intptr_t>(buffer); | 467 intptr_t bufferAsInt = reinterpret_cast<intptr_t>(buffer); |
518 void* pixels = reinterpret_cast<void*>(bufferAsInt + pixelOffset); | 468 void* pixels = reinterpret_cast<void*>(bufferAsInt + pixelOffset); |
519 SkPMColor* ct = nullptr; | 469 SkPMColor* ct = nullptr; |
520 if (ctSize) { | 470 if (ctSize) { |
521 ct = reinterpret_cast<SkPMColor*>(bufferAsInt + ctOffset); | 471 ct = reinterpret_cast<SkPMColor*>(bufferAsInt + ctOffset); |
522 } | 472 } |
523 | 473 |
524 memcpy(pixels, pixmap.addr(), pixmap.getSafeSize()); | 474 memcpy(pixels, pixmap.addr(), pixmap.getSafeSize()); |
525 if (ctSize) { | 475 if (ctSize) { |
526 memcpy(ct, pixmap.ctable()->readColors(), ctSize); | 476 memcpy(ct, pixmap.ctable()->readColors(), ctSize); |
527 } | 477 } |
528 | 478 |
529 SkASSERT(info == pixmap.info()); | 479 SkASSERT(info == pixmap.info()); |
530 size_t rowBytes = pixmap.rowBytes(); | 480 size_t rowBytes = pixmap.rowBytes(); |
531 DeferredTextureImage* dti = new (buffer) DeferredTextureImage(); | 481 DeferredTextureImage* dti = new (buffer) DeferredTextureImage(); |
532 dti->fContextUniqueID = proxy.fContextUniqueID; | 482 dti->fContextUniqueID = proxy.fContextUniqueID; |
533 dti->fGammaTreatment = gammaTreatment; | |
534 dti->fData.fInfo = info; | 483 dti->fData.fInfo = info; |
535 dti->fData.fColorTableCnt = ctCount; | 484 dti->fData.fColorTableCnt = ctCount; |
536 dti->fData.fColorTableData = ct; | 485 dti->fData.fColorTableData = ct; |
537 dti->fData.fMipMapLevelCount = mipMapLevelCount; | 486 dti->fData.fMipMapLevelCount = mipMapLevelCount; |
538 dti->fData.fMipMapLevelData[0].fPixelData = pixels; | 487 dti->fData.fMipMapLevelData[0].fPixelData = pixels; |
539 dti->fData.fMipMapLevelData[0].fRowBytes = rowBytes; | 488 dti->fData.fMipMapLevelData[0].fRowBytes = rowBytes; |
540 | |
541 // Fill in the mipmap levels if they exist | |
542 intptr_t mipLevelPtr = bufferAsInt + pixelOffset + SkAlign8(SkAutoPixmapStor
age::AllocSize( | |
543 info, nullptr)); | |
544 if (useMipMaps) { | |
545 SkAutoTDelete<SkMipMap> mipmaps(SkMipMap::Build(pixmap, gammaTreatment,
nullptr)); | |
546 // SkMipMap holds only the mipmap levels it generates. | |
547 // A programmer can use the data they provided to SkMipMap::Build as lev
el 0. | |
548 // So the SkMipMap provides levels 1-x but it stores them in its own | |
549 // range 0-(x-1). | |
550 for (int generatedMipLevelIndex = 0; generatedMipLevelIndex < mipMapLeve
lCount - 1; | |
551 generatedMipLevelIndex++) { | |
552 SkISize mipSize = SkMipMap::ComputeLevelSize(scaledSize.width(), sca
ledSize.height(), | |
553 generatedMipLevelIndex)
; | |
554 SkImageInfo mipInfo = SkImageInfo::MakeN32(mipSize.fWidth, mipSize.f
Height, at); | |
555 SkMipMap::Level mipLevel; | |
556 mipmaps->getLevel(generatedMipLevelIndex, &mipLevel); | |
557 memcpy(reinterpret_cast<void*>(mipLevelPtr), mipLevel.fPixmap.addr()
, | |
558 mipLevel.fPixmap.getSafeSize()); | |
559 dti->fData.fMipMapLevelData[generatedMipLevelIndex + 1].fPixelData = | |
560 reinterpret_cast<void*>(mipLevelPtr); | |
561 dti->fData.fMipMapLevelData[generatedMipLevelIndex + 1].fRowBytes = | |
562 mipLevel.fPixmap.rowBytes(); | |
563 mipLevelPtr += SkAlign8(mipLevel.fPixmap.getSafeSize()); | |
564 } | |
565 } | |
566 return size; | 489 return size; |
567 } | 490 } |
568 | 491 |
569 sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, con
st void* data, | 492 sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, con
st void* data, |
570 SkBudgeted budgeted) { | 493 SkBudgeted budgeted) { |
571 if (!data) { | 494 if (!data) { |
572 return nullptr; | 495 return nullptr; |
573 } | 496 } |
574 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImag
e*>(data); | 497 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImag
e*>(data); |
575 | 498 |
576 if (!context || context->uniqueID() != dti->fContextUniqueID) { | 499 if (!context || context->uniqueID() != dti->fContextUniqueID) { |
577 return nullptr; | 500 return nullptr; |
578 } | 501 } |
579 SkAutoTUnref<SkColorTable> colorTable; | 502 SkAutoTUnref<SkColorTable> colorTable; |
580 if (dti->fData.fColorTableCnt) { | 503 if (dti->fData.fColorTableCnt) { |
581 SkASSERT(dti->fData.fColorTableData); | 504 SkASSERT(dti->fData.fColorTableData); |
582 colorTable.reset(new SkColorTable(dti->fData.fColorTableData, dti->fData
.fColorTableCnt)); | 505 colorTable.reset(new SkColorTable(dti->fData.fColorTableData, dti->fData
.fColorTableCnt)); |
583 } | 506 } |
584 int mipLevelCount = dti->fData.fMipMapLevelCount; | 507 SkASSERT(dti->fData.fMipMapLevelCount == 1); |
585 SkASSERT(mipLevelCount >= 1); | 508 SkPixmap pixmap; |
586 if (mipLevelCount == 1) { | 509 pixmap.reset(dti->fData.fInfo, dti->fData.fMipMapLevelData[0].fPixelData, |
587 SkPixmap pixmap; | 510 dti->fData.fMipMapLevelData[0].fRowBytes, colorTable.get()); |
588 pixmap.reset(dti->fData.fInfo, dti->fData.fMipMapLevelData[0].fPixelData
, | 511 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted); |
589 dti->fData.fMipMapLevelData[0].fRowBytes, colorTable.get())
; | |
590 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted); | |
591 } else { | |
592 SkAutoTDeleteArray<GrMipLevel> texels(new GrMipLevel[mipLevelCount]); | |
593 for (int i = 0; i < mipLevelCount; i++) { | |
594 texels[i].fPixels = dti->fData.fMipMapLevelData[i].fPixelData; | |
595 texels[i].fRowBytes = dti->fData.fMipMapLevelData[i].fRowBytes; | |
596 } | |
597 | |
598 return SkImage::MakeTextureFromMipMap(context, dti->fData.fInfo, texels.
get(), | |
599 mipLevelCount, SkBudgeted::kYes, | |
600 dti->fGammaTreatment); | |
601 } | |
602 } | 512 } |
603 | 513 |
604 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 514 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
605 | 515 |
606 GrTexture* GrDeepCopyTexture(GrTexture* src, SkBudgeted budgeted) { | 516 GrTexture* GrDeepCopyTexture(GrTexture* src, SkBudgeted budgeted) { |
607 GrContext* ctx = src->getContext(); | 517 GrContext* ctx = src->getContext(); |
608 | 518 |
609 GrSurfaceDesc desc = src->desc(); | 519 GrSurfaceDesc desc = src->desc(); |
610 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullp
tr, 0); | 520 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullp
tr, 0); |
611 if (!dst) { | 521 if (!dst) { |
612 return nullptr; | 522 return nullptr; |
613 } | 523 } |
614 | 524 |
615 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); | 525 const SkIRect srcR = SkIRect::MakeWH(desc.fWidth, desc.fHeight); |
616 const SkIPoint dstP = SkIPoint::Make(0, 0); | 526 const SkIPoint dstP = SkIPoint::Make(0, 0); |
617 ctx->copySurface(dst, src, srcR, dstP); | 527 ctx->copySurface(dst, src, srcR, dstP); |
618 ctx->flushSurfaceWrites(dst); | 528 ctx->flushSurfaceWrites(dst); |
619 return dst; | 529 return dst; |
620 } | 530 } |
621 | 531 |
622 sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo&
info, | 532 sk_sp<SkImage> SkImage::MakeTextureFromMipMap(GrContext* ctx, const SkImageInfo&
info, |
623 const GrMipLevel* texels, int mipL
evelCount, | 533 const GrMipLevel* texels, int mipL
evelCount, |
624 SkBudgeted budgeted, | 534 SkBudgeted budgeted) { |
625 SkSourceGammaTreatment gammaTreatm
ent) { | |
626 if (!ctx) { | 535 if (!ctx) { |
627 return nullptr; | 536 return nullptr; |
628 } | 537 } |
629 SkAutoTUnref<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, m
ipLevelCount)); | 538 SkAutoTUnref<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, m
ipLevelCount)); |
630 if (!texture) { | 539 if (!texture) { |
631 return nullptr; | 540 return nullptr; |
632 } | 541 } |
633 texture->texturePriv().setGammaTreatment(gammaTreatment); | |
634 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNew
ImageUniqueID, | 542 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNew
ImageUniqueID, |
635 info.alphaType(), texture, budgeted); | 543 info.alphaType(), texture, budgeted); |
636 } | 544 } |
OLD | NEW |