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 "effects/GrYUVEffect.h" | 13 #include "effects/GrYUVEffect.h" |
14 #include "SkCanvas.h" | 14 #include "SkCanvas.h" |
15 #include "SkBitmapCache.h" | 15 #include "SkBitmapCache.h" |
16 #include "SkGrPixelRef.h" | 16 #include "SkGrPixelRef.h" |
17 #include "SkGrPriv.h" | 17 #include "SkGrPriv.h" |
18 #include "SkImage_Gpu.h" | 18 #include "SkImage_Gpu.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 SkBudgeted budgeted) | 23 SkBudgeted budgeted) |
23 : INHERITED(w, h, uniqueID) | 24 : INHERITED(w, h, uniqueID) |
24 , fTexture(SkRef(tex)) | 25 , fTexture(SkRef(tex)) |
25 , fAlphaType(at) | 26 , fAlphaType(at) |
26 , fBudgeted(budgeted) | 27 , fBudgeted(budgeted) |
27 , fAddedRasterVersionToCache(false) | 28 , fAddedRasterVersionToCache(false) |
28 { | 29 { |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
341 // It contains fMipMapLevelCount elements. | 342 // It contains fMipMapLevelCount elements. |
342 // That means this struct's size is not known at compile-time. | 343 // That means this struct's size is not known at compile-time. |
343 MipMapLevelData fMipMapLevelData[1]; | 344 MipMapLevelData fMipMapLevelData[1]; |
344 }; | 345 }; |
345 Data fData; | 346 Data fData; |
346 | 347 |
347 friend class SkImage; | 348 friend class SkImage; |
348 }; | 349 }; |
349 | 350 |
350 size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& prox y, | 351 size_t SkImage::getDeferredTextureImageData(const GrContextThreadSafeProxy& prox y, |
351 const DeferredTextureImageUsageParam s[], | 352 const DeferredTextureImageUsageParam s params[], |
352 int paramCnt, void* buffer) const { | 353 int paramCnt, void* buffer) const { |
353 const bool fillMode = SkToBool(buffer); | 354 const bool fillMode = SkToBool(buffer); |
354 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) { | 355 if (fillMode && !SkIsAlign8(reinterpret_cast<intptr_t>(buffer))) { |
355 return 0; | 356 return 0; |
356 } | 357 } |
357 | 358 |
358 const int maxTextureSize = proxy.fCaps->maxTextureSize(); | 359 const int maxTextureSize = proxy.fCaps->maxTextureSize(); |
359 if (width() > maxTextureSize || height() > maxTextureSize) { | 360 if (width() > maxTextureSize || height() > maxTextureSize) { |
360 return 0; | 361 return 0; |
361 } | 362 } |
(...skipping 22 matching lines...) Expand all Loading... | |
384 info = SkImageInfo::MakeN32(this->width(), this->height(), at); | 385 info = SkImageInfo::MakeN32(this->width(), this->height(), at); |
385 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr)); | 386 pixelSize = SkAlign8(SkAutoPixmapStorage::AllocSize(info, nullptr)); |
386 if (fillMode) { | 387 if (fillMode) { |
387 pixmap.alloc(info); | 388 pixmap.alloc(info); |
388 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) { | 389 if (!this->readPixels(pixmap, 0, 0, SkImage::kDisallow_CachingHint)) { |
389 return 0; | 390 return 0; |
390 } | 391 } |
391 SkASSERT(!pixmap.ctable()); | 392 SkASSERT(!pixmap.ctable()); |
392 } | 393 } |
393 } | 394 } |
395 bool shouldUseMipMaps = false; | |
394 int mipMapLevelCount = 1; | 396 int mipMapLevelCount = 1; |
ericrk
2016/06/03 18:05:45
This is a bit misleading - if we are using mips, m
cblume
2016/06/03 18:57:31
Hrmmm perhaps I made a mistake.
The way it *should
| |
397 for (int currentParamIndex = paramCnt; currentParamIndex > 0; currentParamIn dex--) { | |
ericrk
2016/06/03 18:05:45
I'm going to need to extract things from params (a
cblume
2016/06/03 18:57:31
Good idea. I'll put this at the top and see how it
| |
398 const SkMatrix& currentMatrix = params[currentParamIndex].fMatrix; | |
ericrk
2016/06/03 18:05:45
won't this index out of bounds and skip the 0th pa
cblume
2016/06/03 18:57:31
Hrmm it will indeed skip the 0th. It should be for
| |
399 SkScalar minScaleFactor = currentMatrix.getMinScale(); | |
400 if (currentMatrix.hasPerspective() || | |
ericrk
2016/06/03 18:05:45
Can you add a comment explaining the logic here (i
cblume
2016/06/03 18:57:31
I agree 100% on the individual bools. That might a
| |
401 ((params[currentParamIndex].fQuality == kMedium_SkFilterQuality || | |
402 params[currentParamIndex].fQuality == kHigh_SkFilterQuality) && | |
403 minScaleFactor != -1.f && minScaleFactor < 1.f)) { | |
404 shouldUseMipMaps = true; | |
405 mipMapLevelCount = SkMipMap::ComputeLevelCount(width(), height()); | |
406 break; | |
407 } | |
408 } | |
409 SkAlphaType at = this->isOpaque() ? kOpaque_SkAlphaType : kPremul_SkAlphaTyp e; | |
410 if (shouldUseMipMaps) { | |
411 for (int currentMipMapLevelIndex = mipMapLevelCount; currentMipMapLevelI ndex > 0; | |
412 currentMipMapLevelIndex--) { | |
413 SkISize mipSize = SkMipMap::ComputeLevelSize(width(), height(), curr entMipMapLevelIndex); | |
414 SkImageInfo mipInfo = SkImageInfo::MakeN32(mipSize.fWidth, mipSize.f Height, at); | |
415 pixelSize += SkAlign8(SkAutoPixmapStorage::AllocSize(mipInfo, nullpt r)); | |
416 } | |
417 } | |
395 size_t size = 0; | 418 size_t size = 0; |
396 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage)); | 419 size_t dtiSize = SkAlign8(sizeof(DeferredTextureImage)); |
397 size += dtiSize; | 420 size += dtiSize; |
398 size += mipMapLevelCount * sizeof(DeferredTextureImage::MipMapLevelData); | 421 size += mipMapLevelCount * sizeof(DeferredTextureImage::MipMapLevelData); |
399 size_t pixelOffset = size; | 422 size_t pixelOffset = size; |
400 size += pixelSize; | 423 size += pixelSize; |
401 size_t ctOffset = size; | 424 size_t ctOffset = size; |
402 size += ctSize; | 425 size += ctSize; |
403 if (!fillMode) { | 426 if (!fillMode) { |
404 return size; | 427 return size; |
(...skipping 13 matching lines...) Expand all Loading... | |
418 SkASSERT(info == pixmap.info()); | 441 SkASSERT(info == pixmap.info()); |
419 size_t rowBytes = pixmap.rowBytes(); | 442 size_t rowBytes = pixmap.rowBytes(); |
420 DeferredTextureImage* dti = new (buffer) DeferredTextureImage(); | 443 DeferredTextureImage* dti = new (buffer) DeferredTextureImage(); |
421 dti->fContextUniqueID = proxy.fContextUniqueID; | 444 dti->fContextUniqueID = proxy.fContextUniqueID; |
422 dti->fData.fInfo = info; | 445 dti->fData.fInfo = info; |
423 dti->fData.fColorTableCnt = ctCount; | 446 dti->fData.fColorTableCnt = ctCount; |
424 dti->fData.fColorTableData = ct; | 447 dti->fData.fColorTableData = ct; |
425 dti->fData.fMipMapLevelCount = mipMapLevelCount; | 448 dti->fData.fMipMapLevelCount = mipMapLevelCount; |
426 dti->fData.fMipMapLevelData[0].fPixelData = pixels; | 449 dti->fData.fMipMapLevelData[0].fPixelData = pixels; |
427 dti->fData.fMipMapLevelData[0].fRowBytes = rowBytes; | 450 dti->fData.fMipMapLevelData[0].fRowBytes = rowBytes; |
451 | |
452 // Fill in the mipmap levels if they exist | |
453 intptr_t mipLevelPtr = bufferAsInt + pixelOffset; | |
454 if (shouldUseMipMaps) { | |
455 SkAutoTDelete<SkMipMap> mipmaps(SkMipMap::Build(pixmap, nullptr)); | |
456 for (int currentMipMapLevelIndex = 1; currentMipMapLevelIndex < mipMapLe velCount; | |
ericrk
2016/06/03 18:05:45
Wont this skip the last mip level? when computed b
cblume
2016/06/03 18:57:31
Good catch.
| |
457 currentMipMapLevelIndex++) { | |
458 SkISize prevMipSize = SkMipMap::ComputeLevelSize(width(), height(), currentMipMapLevelIndex); | |
459 SkImageInfo prevMipInfo = SkImageInfo::MakeN32(prevMipSize.fWidth, p revMipSize.fHeight, at); | |
460 mipLevelPtr += SkAlign8(SkAutoPixmapStorage::AllocSize(prevMipInfo, nullptr)); | |
461 | |
462 SkMipMap::Level currentMipMapLevel; | |
463 mipmaps->getLevel(currentMipMapLevelIndex, ¤tMipMapLevel); | |
464 memcpy(reinterpret_cast<void*>(mipLevelPtr), currentMipMapLevel.fPix map.addr(), | |
465 currentMipMapLevel.fPixmap.getSafeSize()); | |
466 dti->fData.fMipMapLevelData[currentMipMapLevelIndex].fPixelData = | |
467 reinterpret_cast<void*>(mipLevelPtr); | |
468 dti->fData.fMipMapLevelData[currentMipMapLevelIndex].fRowBytes = | |
469 currentMipMapLevel.fPixmap.rowBytes(); | |
470 } | |
471 } | |
428 return size; | 472 return size; |
429 } | 473 } |
430 | 474 |
431 sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, con st void* data, | 475 sk_sp<SkImage> SkImage::MakeFromDeferredTextureImageData(GrContext* context, con st void* data, |
432 SkBudgeted budgeted) { | 476 SkBudgeted budgeted) { |
433 if (!data) { | 477 if (!data) { |
434 return nullptr; | 478 return nullptr; |
435 } | 479 } |
436 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImag e*>(data); | 480 const DeferredTextureImage* dti = reinterpret_cast<const DeferredTextureImag e*>(data); |
437 | 481 |
438 if (!context || context->uniqueID() != dti->fContextUniqueID) { | 482 if (!context || context->uniqueID() != dti->fContextUniqueID) { |
439 return nullptr; | 483 return nullptr; |
440 } | 484 } |
441 SkAutoTUnref<SkColorTable> colorTable; | 485 SkAutoTUnref<SkColorTable> colorTable; |
442 if (dti->fData.fColorTableCnt) { | 486 if (dti->fData.fColorTableCnt) { |
443 SkASSERT(dti->fData.fColorTableData); | 487 SkASSERT(dti->fData.fColorTableData); |
444 colorTable.reset(new SkColorTable(dti->fData.fColorTableData, dti->fData .fColorTableCnt)); | 488 colorTable.reset(new SkColorTable(dti->fData.fColorTableData, dti->fData .fColorTableCnt)); |
445 } | 489 } |
446 SkASSERT(dti->fData.fMipMapLevelCount == 1); | 490 int mipLevelCount = dti->fData.fMipMapLevelCount; |
447 SkPixmap pixmap; | 491 SkASSERT(mipLevelCount >= 1); |
448 pixmap.reset(dti->fData.fInfo, dti->fData.fMipMapLevelData[0].fPixelData, | 492 if (mipLevelCount == 1) { |
449 dti->fData.fMipMapLevelData[0].fRowBytes, colorTable.get()); | 493 SkPixmap pixmap; |
450 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted); | 494 pixmap.reset(dti->fData.fInfo, dti->fData.fMipMapLevelData[0].fPixelData , |
495 dti->fData.fMipMapLevelData[0].fRowBytes, colorTable.get()) ; | |
496 return SkImage::MakeTextureFromPixmap(context, pixmap, budgeted); | |
497 } else { | |
498 SkAutoTDeleteArray<GrMipLevel> texels(new GrMipLevel[mipLevelCount]); | |
499 for (int i = 0; i < mipLevelCount; i++) { | |
500 texels[i].fPixels = dti->fData.fMipMapLevelData[i].fPixelData; | |
501 texels[i].fRowBytes = dti->fData.fMipMapLevelData[i].fRowBytes; | |
502 } | |
503 | |
504 return SkImage::MakeTextureFromMipMap(context, dti->fData.fInfo, texels. get(), | |
505 mipLevelCount, SkBudgeted::kYes); | |
506 } | |
451 } | 507 } |
452 | 508 |
453 //////////////////////////////////////////////////////////////////////////////// /////////////////// | 509 //////////////////////////////////////////////////////////////////////////////// /////////////////// |
454 | 510 |
455 GrTexture* GrDeepCopyTexture(GrTexture* src, SkBudgeted budgeted) { | 511 GrTexture* GrDeepCopyTexture(GrTexture* src, SkBudgeted budgeted) { |
456 GrContext* ctx = src->getContext(); | 512 GrContext* ctx = src->getContext(); |
457 | 513 |
458 GrSurfaceDesc desc = src->desc(); | 514 GrSurfaceDesc desc = src->desc(); |
459 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullp tr, 0); | 515 GrTexture* dst = ctx->textureProvider()->createTexture(desc, budgeted, nullp tr, 0); |
460 if (!dst) { | 516 if (!dst) { |
(...skipping 13 matching lines...) Expand all Loading... | |
474 if (!ctx) { | 530 if (!ctx) { |
475 return nullptr; | 531 return nullptr; |
476 } | 532 } |
477 SkAutoTUnref<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, m ipLevelCount)); | 533 SkAutoTUnref<GrTexture> texture(GrUploadMipMapToTexture(ctx, info, texels, m ipLevelCount)); |
478 if (!texture) { | 534 if (!texture) { |
479 return nullptr; | 535 return nullptr; |
480 } | 536 } |
481 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNew ImageUniqueID, | 537 return sk_make_sp<SkImage_Gpu>(texture->width(), texture->height(), kNeedNew ImageUniqueID, |
482 info.alphaType(), texture, budgeted); | 538 info.alphaType(), texture, budgeted); |
483 } | 539 } |
OLD | NEW |