| OLD | NEW |
| 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 "GrDrawTargetCaps.h" | 10 #include "GrDrawTargetCaps.h" |
| 11 #include "GrGpu.h" | 11 #include "GrGpu.h" |
| 12 #include "GrXferProcessor.h" | 12 #include "GrXferProcessor.h" |
| 13 #include "SkColorFilter.h" | 13 #include "SkColorFilter.h" |
| 14 #include "SkConfig8888.h" | 14 #include "SkConfig8888.h" |
| 15 #include "SkData.h" | 15 #include "SkData.h" |
| 16 #include "SkMessageBus.h" | 16 #include "SkMessageBus.h" |
| 17 #include "SkPixelRef.h" | 17 #include "SkPixelRef.h" |
| 18 #include "SkResourceCache.h" |
| 18 #include "SkTextureCompressor.h" | 19 #include "SkTextureCompressor.h" |
| 20 #include "SkYUVPlanesCache.h" |
| 19 #include "effects/GrDitherEffect.h" | 21 #include "effects/GrDitherEffect.h" |
| 20 #include "effects/GrPorterDuffXferProcessor.h" | 22 #include "effects/GrPorterDuffXferProcessor.h" |
| 21 #include "effects/GrYUVtoRGBEffect.h" | 23 #include "effects/GrYUVtoRGBEffect.h" |
| 22 | 24 |
| 23 #ifndef SK_IGNORE_ETC1_SUPPORT | 25 #ifndef SK_IGNORE_ETC1_SUPPORT |
| 24 # include "ktx.h" | 26 # include "ktx.h" |
| 25 # include "etc1.h" | 27 # include "etc1.h" |
| 26 #endif | 28 #endif |
| 27 | 29 |
| 28 /* Fill out buffer with the compressed format Ganesh expects from a colortable | 30 /* Fill out buffer with the compressed format Ganesh expects from a colortable |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 return NULL; | 216 return NULL; |
| 215 } | 217 } |
| 216 | 218 |
| 217 return sk_gr_allocate_texture(ctx, cache, params, bm, desc, bytes, 0); | 219 return sk_gr_allocate_texture(ctx, cache, params, bm, desc, bytes, 0); |
| 218 } | 220 } |
| 219 #endif // SK_IGNORE_ETC1_SUPPORT | 221 #endif // SK_IGNORE_ETC1_SUPPORT |
| 220 | 222 |
| 221 static GrTexture *load_yuv_texture(GrContext* ctx, bool cache, const GrTexturePa
rams* params, | 223 static GrTexture *load_yuv_texture(GrContext* ctx, bool cache, const GrTexturePa
rams* params, |
| 222 const SkBitmap& bm, const GrSurfaceDesc& desc
) { | 224 const SkBitmap& bm, const GrSurfaceDesc& desc
) { |
| 223 // Subsets are not supported, the whole pixelRef is loaded when using YUV de
coding | 225 // Subsets are not supported, the whole pixelRef is loaded when using YUV de
coding |
| 224 if ((bm.pixelRef()->info().width() != bm.info().width()) || | 226 SkPixelRef* pixelRef = bm.pixelRef(); |
| 225 (bm.pixelRef()->info().height() != bm.info().height())) { | 227 if ((NULL == pixelRef) || |
| 228 (pixelRef->info().width() != bm.info().width()) || |
| 229 (pixelRef->info().height() != bm.info().height())) { |
| 226 return NULL; | 230 return NULL; |
| 227 } | 231 } |
| 228 | 232 |
| 229 SkPixelRef* pixelRef = bm.pixelRef(); | 233 SkYUVPlanesCache::Info yuvInfo; |
| 230 SkISize yuvSizes[3]; | 234 SkAutoTUnref<SkCachedData> cachedData( |
| 231 if ((NULL == pixelRef) || !pixelRef->getYUV8Planes(yuvSizes, NULL, NULL, NUL
L)) { | 235 SkYUVPlanesCache::FindAndRef(pixelRef->getGenerationID(), &yuvInfo)); |
| 232 return NULL; | |
| 233 } | |
| 234 | 236 |
| 235 // Allocate the memory for YUV | |
| 236 size_t totalSize(0); | |
| 237 size_t sizes[3], rowBytes[3]; | |
| 238 for (int i = 0; i < 3; ++i) { | |
| 239 rowBytes[i] = yuvSizes[i].fWidth; | |
| 240 totalSize += sizes[i] = rowBytes[i] * yuvSizes[i].fHeight; | |
| 241 } | |
| 242 SkAutoMalloc storage(totalSize); | |
| 243 void* planes[3]; | 237 void* planes[3]; |
| 244 planes[0] = storage.get(); | 238 if (cachedData->data()) { |
| 245 planes[1] = (uint8_t*)planes[0] + sizes[0]; | 239 planes[0] = (void*)cachedData->data(); |
| 246 planes[2] = (uint8_t*)planes[1] + sizes[1]; | 240 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0]; |
| 241 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1]; |
| 242 } else { |
| 243 // Fetch yuv plane sizes for memory allocation. Here, width and height c
an be |
| 244 // rounded up to JPEG block size and be larger than the image's width an
d height. |
| 245 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, NULL, NULL, NULL)) { |
| 246 return NULL; |
| 247 } |
| 247 | 248 |
| 248 SkYUVColorSpace colorSpace; | 249 // Allocate the memory for YUV |
| 250 size_t totalSize(0); |
| 251 for (int i = 0; i < 3; ++i) { |
| 252 yuvInfo.fRowBytes[i] = yuvInfo.fSize[i].fWidth; |
| 253 yuvInfo.fSizeInMemory[i] = yuvInfo.fRowBytes[i] * yuvInfo.fSize[i].f
Height; |
| 254 totalSize += yuvInfo.fSizeInMemory[i]; |
| 255 } |
| 256 cachedData.reset(SkResourceCache::NewCachedData(totalSize)); |
| 257 planes[0] = cachedData->writable_data(); |
| 258 planes[1] = (uint8_t*)planes[0] + yuvInfo.fSizeInMemory[0]; |
| 259 planes[2] = (uint8_t*)planes[1] + yuvInfo.fSizeInMemory[1]; |
| 249 | 260 |
| 250 // Get the YUV planes | 261 // Get the YUV planes and update plane sizes to actual image size |
| 251 if (!pixelRef->getYUV8Planes(yuvSizes, planes, rowBytes, &colorSpace)) { | 262 if (!pixelRef->getYUV8Planes(yuvInfo.fSize, planes, yuvInfo.fRowBytes, |
| 252 return NULL; | 263 &yuvInfo.fColorSpace)) { |
| 264 return NULL; |
| 265 } |
| 266 |
| 267 // Decoding is done, cache the resulting YUV planes |
| 268 SkYUVPlanesCache::Add(pixelRef->getGenerationID(), cachedData, &yuvInfo)
; |
| 253 } | 269 } |
| 254 | 270 |
| 255 GrSurfaceDesc yuvDesc; | 271 GrSurfaceDesc yuvDesc; |
| 256 yuvDesc.fConfig = kAlpha_8_GrPixelConfig; | 272 yuvDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 257 SkAutoTUnref<GrTexture> yuvTextures[3]; | 273 SkAutoTUnref<GrTexture> yuvTextures[3]; |
| 258 for (int i = 0; i < 3; ++i) { | 274 for (int i = 0; i < 3; ++i) { |
| 259 yuvDesc.fWidth = yuvSizes[i].fWidth; | 275 yuvDesc.fWidth = yuvInfo.fSize[i].fWidth; |
| 260 yuvDesc.fHeight = yuvSizes[i].fHeight; | 276 yuvDesc.fHeight = yuvInfo.fSize[i].fHeight; |
| 261 yuvTextures[i].reset( | 277 yuvTextures[i].reset( |
| 262 ctx->refScratchTexture(yuvDesc, GrContext::kApprox_ScratchTexMatch))
; | 278 ctx->refScratchTexture(yuvDesc, GrContext::kApprox_ScratchTexMatch))
; |
| 263 if (!yuvTextures[i] || | 279 if (!yuvTextures[i] || |
| 264 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight, | 280 !yuvTextures[i]->writePixels(0, 0, yuvDesc.fWidth, yuvDesc.fHeight, |
| 265 yuvDesc.fConfig, planes[i], rowBytes[i]
)) { | 281 yuvDesc.fConfig, planes[i], yuvInfo.fRo
wBytes[i])) { |
| 266 return NULL; | 282 return NULL; |
| 267 } | 283 } |
| 268 } | 284 } |
| 269 | 285 |
| 270 GrSurfaceDesc rtDesc = desc; | 286 GrSurfaceDesc rtDesc = desc; |
| 271 rtDesc.fFlags = rtDesc.fFlags | | 287 rtDesc.fFlags = rtDesc.fFlags | |
| 272 kRenderTarget_GrSurfaceFlag | | 288 kRenderTarget_GrSurfaceFlag | |
| 273 kNoStencil_GrSurfaceFlag; | 289 kNoStencil_GrSurfaceFlag; |
| 274 | 290 |
| 275 GrTexture* result = sk_gr_allocate_texture(ctx, cache, params, bm, rtDesc, N
ULL, 0); | 291 GrTexture* result = sk_gr_allocate_texture(ctx, cache, params, bm, rtDesc, N
ULL, 0); |
| 276 | 292 |
| 277 GrRenderTarget* renderTarget = result ? result->asRenderTarget() : NULL; | 293 GrRenderTarget* renderTarget = result ? result->asRenderTarget() : NULL; |
| 278 if (renderTarget) { | 294 if (renderTarget) { |
| 279 SkAutoTUnref<GrFragmentProcessor> yuvToRgbProcessor( | 295 SkAutoTUnref<GrFragmentProcessor> yuvToRgbProcessor(GrYUVtoRGBEffect::Cr
eate( |
| 280 GrYUVtoRGBEffect::Create(yuvTextures[0], yuvTextures[1], yuvTextures
[2], colorSpace)); | 296 yuvTextures[0], yuvTextures[1], yuvTextures[2], yuvInfo.fColorSp
ace)); |
| 281 GrPaint paint; | 297 GrPaint paint; |
| 282 paint.addColorProcessor(yuvToRgbProcessor); | 298 paint.addColorProcessor(yuvToRgbProcessor); |
| 283 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvSizes[0].fWidth), | 299 SkRect r = SkRect::MakeWH(SkIntToScalar(yuvInfo.fSize[0].fWidth), |
| 284 SkIntToScalar(yuvSizes[0].fHeight)); | 300 SkIntToScalar(yuvInfo.fSize[0].fHeight)); |
| 285 GrContext::AutoRenderTarget autoRT(ctx, renderTarget); | 301 GrContext::AutoRenderTarget autoRT(ctx, renderTarget); |
| 286 GrContext::AutoClip ac(ctx, GrContext::AutoClip::kWideOpen_InitialClip); | 302 GrContext::AutoClip ac(ctx, GrContext::AutoClip::kWideOpen_InitialClip); |
| 287 ctx->drawRect(paint, SkMatrix::I(), r); | 303 ctx->drawRect(paint, SkMatrix::I(), r); |
| 288 } else { | 304 } else { |
| 289 SkSafeSetNull(result); | 305 SkSafeSetNull(result); |
| 290 } | 306 } |
| 291 | 307 |
| 292 return result; | 308 return result; |
| 293 } | 309 } |
| 294 | 310 |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 if (shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintCol
or, &fp) && fp) { | 597 if (shader->asFragmentProcessor(context, skPaint, viewM, NULL, &paintCol
or, &fp) && fp) { |
| 582 grPaint->addColorProcessor(fp)->unref(); | 598 grPaint->addColorProcessor(fp)->unref(); |
| 583 constantColor = false; | 599 constantColor = false; |
| 584 } | 600 } |
| 585 } | 601 } |
| 586 | 602 |
| 587 // The grcolor is automatically set when calling asFragmentProcessor. | 603 // The grcolor is automatically set when calling asFragmentProcessor. |
| 588 // If the shader can be seen as an effect it returns true and adds its effec
t to the grpaint. | 604 // If the shader can be seen as an effect it returns true and adds its effec
t to the grpaint. |
| 589 SkPaint2GrPaintNoShader(context, skPaint, paintColor, constantColor, grPaint
); | 605 SkPaint2GrPaintNoShader(context, skPaint, paintColor, constantColor, grPaint
); |
| 590 } | 606 } |
| OLD | NEW |