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 #include "SkColorFilter.h" | 9 #include "SkColorFilter.h" |
10 #include "SkConfig8888.h" | 10 #include "SkConfig8888.h" |
11 #include "SkData.h" | 11 #include "SkData.h" |
12 #include "SkMessageBus.h" | 12 #include "SkMessageBus.h" |
13 #include "SkPixelRef.h" | 13 #include "SkPixelRef.h" |
14 #include "GrResourceCache.h" | 14 #include "GrResourceCache.h" |
15 #include "GrGpu.h" | 15 #include "GrGpu.h" |
16 #include "effects/GrDitherEffect.h" | 16 #include "effects/GrDitherEffect.h" |
17 #include "GrDrawTargetCaps.h" | 17 #include "GrDrawTargetCaps.h" |
| 18 #include "effects/GrYUVtoRGBEffect.h" |
18 | 19 |
19 #ifndef SK_IGNORE_ETC1_SUPPORT | 20 #ifndef SK_IGNORE_ETC1_SUPPORT |
20 # include "ktx.h" | 21 # include "ktx.h" |
21 # include "etc1.h" | 22 # include "etc1.h" |
22 #endif | 23 #endif |
23 | 24 |
24 /* Fill out buffer with the compressed format Ganesh expects from a colortable | 25 /* Fill out buffer with the compressed format Ganesh expects from a colortable |
25 based bitmap. [palette (colortable) + indices]. | 26 based bitmap. [palette (colortable) + indices]. |
26 | 27 |
27 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others | 28 At the moment Ganesh only supports 8bit version. If Ganesh allowed we others |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 | 187 |
187 GrResourceKey key; | 188 GrResourceKey key; |
188 GrTexture* result = ctx->createTexture(params, desc, cacheID, bytes, 0, &key
); | 189 GrTexture* result = ctx->createTexture(params, desc, cacheID, bytes, 0, &key
); |
189 if (NULL != result) { | 190 if (NULL != result) { |
190 add_genID_listener(key, bm.pixelRef()); | 191 add_genID_listener(key, bm.pixelRef()); |
191 } | 192 } |
192 return result; | 193 return result; |
193 } | 194 } |
194 #endif // SK_IGNORE_ETC1_SUPPORT | 195 #endif // SK_IGNORE_ETC1_SUPPORT |
195 | 196 |
| 197 static GrTexture *load_yuv_texture(GrContext* ctx, const GrTextureParams* params
, |
| 198 const SkBitmap& bm, const GrTextureDesc& desc
) { |
| 199 GrTexture* result = NULL; |
| 200 |
| 201 SkPixelRef* pixelRef = bm.pixelRef(); |
| 202 SkISize yuvSizes[3]; |
| 203 if ((NULL == pixelRef) || !pixelRef->getYUV8Planes(yuvSizes, NULL, NULL)) { |
| 204 return NULL; |
| 205 } |
| 206 |
| 207 // Allocate the memory for YUV |
| 208 size_t totalSize(0); |
| 209 size_t sizes[3], rowBytes[3]; |
| 210 for (int i = 0; i < 3; ++i) { |
| 211 rowBytes[i] = yuvSizes[i].fWidth; |
| 212 totalSize += sizes[i] = rowBytes[i] * yuvSizes[i].fHeight; |
| 213 } |
| 214 SkAutoMalloc storage(totalSize); |
| 215 void* planes[3]; |
| 216 planes[0] = storage.get(); |
| 217 planes[1] = (uint8_t*)planes[0] + sizes[0]; |
| 218 planes[2] = (uint8_t*)planes[1] + sizes[1]; |
| 219 |
| 220 // Get the YUV planes |
| 221 if (!pixelRef->getYUV8Planes(yuvSizes, planes, rowBytes)) { |
| 222 return NULL; |
| 223 } |
| 224 |
| 225 GrTextureDesc yuvDesc; |
| 226 yuvDesc.fConfig = kAlpha_8_GrPixelConfig; |
| 227 GrAutoScratchTexture yuvTextures[3]; |
| 228 for (int i = 0; i < 3; ++i) { |
| 229 yuvDesc.fWidth = yuvSizes[i].fWidth; |
| 230 yuvDesc.fHeight = yuvSizes[i].fHeight; |
| 231 yuvTextures[i].set(ctx, yuvDesc); |
| 232 if ((NULL == yuvTextures[i].texture()) || |
| 233 !ctx->writeTexturePixels(yuvTextures[i].texture(), |
| 234 0, 0, yuvDesc.fWidth, yuvDesc.fHeight, |
| 235 yuvDesc.fConfig, planes[i], rowBytes[i])) { |
| 236 return NULL; |
| 237 } |
| 238 } |
| 239 |
| 240 GrTextureDesc rtDesc = desc; |
| 241 rtDesc.fFlags = rtDesc.fFlags | |
| 242 kRenderTarget_GrTextureFlagBit | |
| 243 kNoStencil_GrTextureFlagBit; |
| 244 |
| 245 // This texture is likely to be used again so leave it in the cache |
| 246 GrCacheID cacheID; |
| 247 generate_bitmap_cache_id(bm, &cacheID); |
| 248 |
| 249 GrResourceKey key; |
| 250 result = ctx->createTexture(params, rtDesc, cacheID, NULL, 0, &key); |
| 251 GrRenderTarget* renderTarget = result ? result->asRenderTarget() : NULL; |
| 252 if (NULL != renderTarget) { |
| 253 add_genID_listener(key, bm.pixelRef()); |
| 254 SkAutoTUnref<GrEffect> yuvToRgbEffect(GrYUVtoRGBEffect::Create( |
| 255 yuvTextures[0].texture(), yuvTextures[1].texture(), yuvTextures[2].t
exture())); |
| 256 GrPaint paint; |
| 257 paint.addColorEffect(yuvToRgbEffect); |
| 258 SkRect r = SkRect::MakeWH(yuvSizes[0].fWidth, yuvSizes[0].fHeight); |
| 259 GrContext::AutoRenderTarget autoRT(ctx, renderTarget); |
| 260 GrContext::AutoMatrix am; |
| 261 am.setIdentity(ctx); |
| 262 GrContext::AutoClip ac(ctx, GrContext::AutoClip::kWideOpen_InitialClip); |
| 263 ctx->drawRect(paint, r); |
| 264 } else { |
| 265 SkSafeSetNull(result); |
| 266 } |
| 267 |
| 268 return result; |
| 269 } |
| 270 |
196 static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx, | 271 static GrTexture* sk_gr_create_bitmap_texture(GrContext* ctx, |
197 bool cache, | 272 bool cache, |
198 const GrTextureParams* params, | 273 const GrTextureParams* params, |
199 const SkBitmap& origBitmap) { | 274 const SkBitmap& origBitmap) { |
200 SkBitmap tmpBitmap; | 275 SkBitmap tmpBitmap; |
201 | 276 |
202 const SkBitmap* bitmap = &origBitmap; | 277 const SkBitmap* bitmap = &origBitmap; |
203 | 278 |
204 GrTextureDesc desc; | 279 GrTextureDesc desc; |
205 generate_bitmap_texture_desc(*bitmap, &desc); | 280 generate_bitmap_texture_desc(*bitmap, &desc); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 // the bitmap has available pixels, then they might not be what the deco
mpressed | 332 // the bitmap has available pixels, then they might not be what the deco
mpressed |
258 // data is. | 333 // data is. |
259 && !(bitmap->readyToDraw())) { | 334 && !(bitmap->readyToDraw())) { |
260 GrTexture *texture = load_etc1_texture(ctx, params, *bitmap, desc); | 335 GrTexture *texture = load_etc1_texture(ctx, params, *bitmap, desc); |
261 if (NULL != texture) { | 336 if (NULL != texture) { |
262 return texture; | 337 return texture; |
263 } | 338 } |
264 } | 339 } |
265 #endif // SK_IGNORE_ETC1_SUPPORT | 340 #endif // SK_IGNORE_ETC1_SUPPORT |
266 | 341 |
| 342 else { |
| 343 GrTexture *texture = load_yuv_texture(ctx, params, *bitmap, desc); |
| 344 if (NULL != texture) { |
| 345 return texture; |
| 346 } |
| 347 } |
267 SkAutoLockPixels alp(*bitmap); | 348 SkAutoLockPixels alp(*bitmap); |
268 if (!bitmap->readyToDraw()) { | 349 if (!bitmap->readyToDraw()) { |
269 return NULL; | 350 return NULL; |
270 } | 351 } |
271 if (cache) { | 352 if (cache) { |
272 // This texture is likely to be used again so leave it in the cache | 353 // This texture is likely to be used again so leave it in the cache |
273 GrCacheID cacheID; | 354 GrCacheID cacheID; |
274 generate_bitmap_cache_id(origBitmap, &cacheID); | 355 generate_bitmap_cache_id(origBitmap, &cacheID); |
275 | 356 |
276 GrResourceKey key; | 357 GrResourceKey key; |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 if (shader->asNewEffect(context, skPaint, NULL, &paintColor, &effect) &&
NULL != effect) { | 615 if (shader->asNewEffect(context, skPaint, NULL, &paintColor, &effect) &&
NULL != effect) { |
535 grPaint->addColorEffect(effect)->unref(); | 616 grPaint->addColorEffect(effect)->unref(); |
536 constantColor = false; | 617 constantColor = false; |
537 } | 618 } |
538 } | 619 } |
539 | 620 |
540 // The grcolor is automatically set when calling asneweffect. | 621 // The grcolor is automatically set when calling asneweffect. |
541 // If the shader can be seen as an effect it returns true and adds its effec
t to the grpaint. | 622 // If the shader can be seen as an effect it returns true and adds its effec
t to the grpaint. |
542 SkPaint2GrPaintNoShader(context, skPaint, paintColor, constantColor, grPaint
); | 623 SkPaint2GrPaintNoShader(context, skPaint, paintColor, constantColor, grPaint
); |
543 } | 624 } |
OLD | NEW |