Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2224)

Side by Side Diff: src/utils/SkTextureCompressor.cpp

Issue 373243002: Optimized R11 EAC compressor (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 Google Inc. 2 * Copyright 2014 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 "SkTextureCompressor.h" 8 #include "SkTextureCompressor.h"
9 9
10 #include "SkBitmap.h" 10 #include "SkBitmap.h"
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 // This is really just for correctness, in all of my tests we 273 // This is really just for correctness, in all of my tests we
274 // never take this step. We don't lose too much perf here because 274 // never take this step. We don't lose too much perf here because
275 // most of the processing in this function is worth it for the 275 // most of the processing in this function is worth it for the
276 // 1 == nUniquePixels optimization. 276 // 1 == nUniquePixels optimization.
277 return compress_latc_block_bb(pixels); 277 return compress_latc_block_bb(pixels);
278 } else { 278 } else {
279 return compress_latc_block_bb_ignore_extremal(pixels); 279 return compress_latc_block_bb_ignore_extremal(pixels);
280 } 280 }
281 } 281 }
282 282
283 static bool compress_a8_to_latc(uint8_t* dst, const uint8_t* src, 283 static inline bool compress_a8_to_latc(uint8_t* dst, const uint8_t* src,
284 int width, int height, int rowBytes) { 284 int width, int height, int rowBytes) {
285 return compress_4x4_a8_to_64bit(dst, src, width, height, rowBytes, compress_ latc_block); 285 return compress_4x4_a8_to_64bit(dst, src, width, height, rowBytes, compress_ latc_block);
286 } 286 }
287 287
288 //////////////////////////////////////////////////////////////////////////////// 288 ////////////////////////////////////////////////////////////////////////////////
289 // 289 //
290 // R11 EAC Compressor 290 // R11 EAC Compressor
291 // 291 //
292 //////////////////////////////////////////////////////////////////////////////// 292 ////////////////////////////////////////////////////////////////////////////////
293 293
294 // #define COMPRESS_R11_EAC_SLOW 1
295 // #define COMPRESS_R11_EAC_FAST 1
296 #define COMPRESS_R11_EAC_FASTEST 1
297
294 // Blocks compressed into R11 EAC are represented as follows: 298 // Blocks compressed into R11 EAC are represented as follows:
295 // 0000000000000000000000000000000000000000000000000000000000000000 299 // 0000000000000000000000000000000000000000000000000000000000000000
296 // |base_cw|mod|mul| ----------------- indices ------------------- 300 // |base_cw|mod|mul| ----------------- indices -------------------
297 // 301 //
298 // To reconstruct the value of a given pixel, we use the formula: 302 // To reconstruct the value of a given pixel, we use the formula:
299 // clamp[0, 2047](base_cw * 8 + 4 + mod_val*mul*8) 303 // clamp[0, 2047](base_cw * 8 + 4 + mod_val*mul*8)
300 // 304 //
301 // mod_val is chosen from a palette of values based on the index of the 305 // mod_val is chosen from a palette of values based on the index of the
302 // given pixel. The palette is chosen by the value stored in mod. 306 // given pixel. The palette is chosen by the value stored in mod.
303 // This formula returns a value between 0 and 2047, which is converted 307 // This formula returns a value between 0 and 2047, which is converted
(...skipping 16 matching lines...) Expand all
320 {-2, -6, -8, -10, 1, 5, 7, 9}, 324 {-2, -6, -8, -10, 1, 5, 7, 9},
321 {-2, -5, -8, -10, 1, 4, 7, 9}, 325 {-2, -5, -8, -10, 1, 4, 7, 9},
322 {-2, -4, -8, -10, 1, 3, 7, 9}, 326 {-2, -4, -8, -10, 1, 3, 7, 9},
323 {-2, -5, -7, -10, 1, 4, 6, 9}, 327 {-2, -5, -7, -10, 1, 4, 6, 9},
324 {-3, -4, -7, -10, 2, 3, 6, 9}, 328 {-3, -4, -7, -10, 2, 3, 6, 9},
325 {-1, -2, -3, -10, 0, 1, 2, 9}, 329 {-1, -2, -3, -10, 0, 1, 2, 9},
326 {-4, -6, -8, -9, 3, 5, 7, 8}, 330 {-4, -6, -8, -9, 3, 5, 7, 8},
327 {-3, -5, -7, -9, 2, 4, 6, 8} 331 {-3, -5, -7, -9, 2, 4, 6, 8}
328 }; 332 };
329 333
334 #if COMPRESS_R11_EAC_SLOW
330 // Pack the base codeword, palette, and multiplier into the 64 bits necessary 335 // Pack the base codeword, palette, and multiplier into the 64 bits necessary
331 // to decode it. 336 // to decode it.
332 static uint64_t pack_r11eac_block(uint16_t base_cw, uint16_t palette, uint16_t m ultiplier, 337 static uint64_t pack_r11eac_block(uint16_t base_cw, uint16_t palette, uint16_t m ultiplier,
333 uint64_t indices) { 338 uint64_t indices) {
334 SkASSERT(palette < 16); 339 SkASSERT(palette < 16);
335 SkASSERT(multiplier < 16); 340 SkASSERT(multiplier < 16);
336 SkASSERT(indices < (static_cast<uint64_t>(1) << 48)); 341 SkASSERT(indices < (static_cast<uint64_t>(1) << 48));
337 342
338 const uint64_t b = static_cast<uint64_t>(base_cw) << 56; 343 const uint64_t b = static_cast<uint64_t>(base_cw) << 56;
339 const uint64_t m = static_cast<uint64_t>(multiplier) << 52; 344 const uint64_t m = static_cast<uint64_t>(multiplier) << 52;
340 const uint64_t p = static_cast<uint64_t>(palette) << 48; 345 const uint64_t p = static_cast<uint64_t>(palette) << 48;
341 return SkEndian_SwapBE64(b | m | p | indices); 346 return SkEndian_SwapBE64(b | m | p | indices);
342 } 347 }
343 348
344 // Given a base codeword, a modifier, and a multiplier, compute the proper 349 // Given a base codeword, a modifier, and a multiplier, compute the proper
345 // pixel value in the range [0, 2047]. 350 // pixel value in the range [0, 2047].
346 static uint16_t compute_r11eac_pixel(int base_cw, int modifier, int multiplier) { 351 static uint16_t compute_r11eac_pixel(int base_cw, int modifier, int multiplier) {
347 int ret = (base_cw * 8 + 4) + (modifier * multiplier * 8); 352 int ret = (base_cw * 8 + 4) + (modifier * multiplier * 8);
348 return (ret > 2047)? 2047 : ((ret < 0)? 0 : ret); 353 return (ret > 2047)? 2047 : ((ret < 0)? 0 : ret);
349 } 354 }
350 355
351 // Compress a block into R11 EAC format. 356 // Compress a block into R11 EAC format.
352 // The compression works as follows: 357 // The compression works as follows:
353 // 1. Find the center of the span of the block's values. Use this as the base co deword. 358 // 1. Find the center of the span of the block's values. Use this as the base co deword.
354 // 2. Choose a multiplier based roughly on the size of the span of block values 359 // 2. Choose a multiplier based roughly on the size of the span of block values
355 // 3. Iterate through each palette and choose the one with the most accurate 360 // 3. Iterate through each palette and choose the one with the most accurate
356 // modifiers. 361 // modifiers.
357 static uint64_t compress_heterogeneous_r11eac_block(const uint8_t block[16]) { 362 static inline uint64_t compress_heterogeneous_r11eac_block(const uint8_t block[1 6]) {
358 // Find the center of the data... 363 // Find the center of the data...
359 uint16_t bmin = block[0]; 364 uint16_t bmin = block[0];
360 uint16_t bmax = block[0]; 365 uint16_t bmax = block[0];
361 for (int i = 1; i < 16; ++i) { 366 for (int i = 1; i < 16; ++i) {
362 bmin = SkTMin<uint16_t>(bmin, block[i]); 367 bmin = SkTMin<uint16_t>(bmin, block[i]);
363 bmax = SkTMax<uint16_t>(bmax, block[i]); 368 bmax = SkTMax<uint16_t>(bmax, block[i]);
364 } 369 }
365 370
366 uint16_t center = (bmax + bmin) >> 1; 371 uint16_t center = (bmax + bmin) >> 1;
367 SkASSERT(center <= 255); 372 SkASSERT(center <= 255);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 if (error < bestError) { 430 if (error < bestError) {
426 bestPalette = paletteIdx; 431 bestPalette = paletteIdx;
427 bestIndices = indices; 432 bestIndices = indices;
428 bestError = error; 433 bestError = error;
429 } 434 }
430 } 435 }
431 436
432 // Finally, pack everything together... 437 // Finally, pack everything together...
433 return pack_r11eac_block(center, bestPalette, multiplier, bestIndices); 438 return pack_r11eac_block(center, bestPalette, multiplier, bestIndices);
434 } 439 }
440 #endif // COMPRESS_R11_EAC_SLOW
435 441
442 #if COMPRESS_R11_EAC_FAST
robertphillips 2014/07/09 12:24:25 // This works by ... ?
krajcevski 2014/07/09 15:18:08 Done.
443 static inline uint64_t compress_heterogeneous_r11eac_block(const uint8_t block[1 6]) {
444 uint64_t retVal = static_cast<uint64_t>(0x8490) << 48;
445 for(int i = 0; i < 4; ++i) {
446 for(int j = 0; j < 4; ++j) {
447 const int shift = 45-3*(j*4+i);
448 SkASSERT(shift <= 45);
449 const uint64_t idx = block[i*4+j] >> 5;
450 SkASSERT(idx < 8);
451 switch(idx) {
robertphillips 2014/07/09 12:24:26 each of these guys on their own line ?
krajcevski 2014/07/09 15:18:07 Done.
452 case 0: case 1: case 2: case 3:
453 retVal |= (3-idx) << shift;
454 break;
455 default:
456 retVal |= idx << shift;
457 break;
458 }
459 }
460 }
461
462 return SkEndian_SwapBE64(retVal);
463 }
464 #endif // COMPRESS_R11_EAC_FAST
465
466 #if (COMPRESS_R11_EAC_SLOW) || (COMPRESS_R11_EAC_FAST)
436 static uint64_t compress_r11eac_block(const uint8_t block[16]) { 467 static uint64_t compress_r11eac_block(const uint8_t block[16]) {
437 // Are all blocks a solid color? 468 // Are all blocks a solid color?
438 bool solid = true; 469 bool solid = true;
439 for (int i = 1; i < 16; ++i) { 470 for (int i = 1; i < 16; ++i) {
440 if (block[i] != block[0]) { 471 if (block[i] != block[0]) {
441 solid = false; 472 solid = false;
442 break; 473 break;
443 } 474 }
444 } 475 }
445 476
446 // Fully transparent? We know the encoding... 477 if (solid) {
447 if (solid && 0 == block[0]) { 478 switch(block[0]) {
448 // (0x0060 << 48) produces the following: 479 // Fully transparent? We know the encoding...
449 // basw_cw: 0 480 case 0:
450 // mod: 6, palette: {-4, -7, -8, -11, 3, 6, 7, 10} 481 // (0x0020 << 48) produces the following:
451 // mod_val: -3 482 // basw_cw: 0
452 // 483 // mod: 0, palette: {-3, -6, -9, -15, 2, 5, 8, 14}
453 // this gives the following formula: 484 // multiplier: 2
454 // clamp[0, 2047](0*8+4+(-4)) = 0 485 // mod_val: -3
455 return SkEndian_SwapBE64(static_cast<uint64_t>(0x0060) << 48); 486 //
456 487 // this gives the following formula:
457 // Fully opaque? We know this encoding too... 488 // clamp[0, 2047](0*8+4+(-3)*2*8) = 0
458 } else if (solid && 255 == block[0]) { 489 //
459 // -1 produces the following: 490 // Furthermore, it is impervious to endianness:
460 // basw_cw: 255 491 // 0x0020000000002000ULL
461 // mod: 15, palette: {-3, -5, -7, -9, 2, 4, 6, 8} 492 // Will produce one pixel with index 2, which gives:
462 // mod_val: 8 493 // clamp[0, 2047](0*8+4+(-9)*2*8) = 0
463 // 494 return 0x0020000000002000ULL;
464 // this gives the following formula: 495
465 // clamp[0, 2047](255*8+4+8*8*8) = clamp[0, 2047](2556) = 2047 496 // Fully opaque? We know this encoding too...
466 return static_cast<uint64_t>(-1); 497 case 255:
467 } 498
468 499 // -1 produces the following:
469 #if 0 500 // basw_cw: 255
470 else if (solid) { 501 // mod: 15, palette: {-3, -5, -7, -9, 2, 4, 6, 8}
471 // !TODO! krajcevski: 502 // mod_val: 8
472 // This will probably never happen, since we're using this format 503 //
473 // primarily for compressing alpha maps. Usually the only 504 // this gives the following formula:
474 // non-fullly opaque or fully transparent blocks are not a solid 505 // clamp[0, 2047](255*8+4+8*8*8) = clamp[0, 2047](2556) = 2047
475 // intermediate color. If we notice that they are, then we can 506 return -1ULL;
476 // add another optimization... 507
477 } 508 default:
509 // !TODO! krajcevski:
510 // This will probably never happen, since we're using this forma t
511 // primarily for compressing alpha maps. Usually the only
512 // non-fullly opaque or fully transparent blocks are not a solid
513 // intermediate color. If we notice that they are, then we can
514 // add another optimization...
515 break;
516 }
517 }
518
519 return compress_heterogeneous_r11eac_block(block);
520 }
521 #endif // (COMPRESS_R11_EAC_SLOW) || (COMPRESS_R11_EAC_FAST)
522
523 #if COMPRESS_R11_EAC_FASTEST
524 static inline uint64_t interleave6(uint64_t a, uint64_t b) {
525 // If our block indices are laid out as:
526 // a b c d
527 // e f g h
528 // i j k l
529 // m n o p
530 //
robertphillips 2014/07/09 12:24:25 May want to pick different input names. 'a' and 'b
krajcevski 2014/07/09 15:18:08 Done.
531 // This function expects a and b to contain the first two rows interleaved
532 // in the least significant bits of a and b. In other words...
533 //
534 // If the architecture is big endian, then a and b will contain the followin g:
535 // Bits 0-31:
536 // a: 00 a e 00 b f 00 c g 00 d h
537 // b: 00 i m 00 j n 00 k o 00 l p
538 //
539 // If the architecture is little endian, then a and b will contain the follo wing:
540 // Bits 0-31:
541 // a: 00 d h 00 c g 00 b f 00 a e
542 // b: 00 l p 00 k o 00 j n 00 i m
543 //
544 // This function returns a packing of the form:
545 // a e i m b f j n c g k o d h l p
546 //
547 // !SPEED! this function might be even faster if certain SIMD intrinsics are
548 // used..
549
550 // For both architectures, we can figure out a packing of the bits by
551 // using a shuffle and a few shift-rotates...
552 uint64_t x = (static_cast<uint64_t>(a) << 32) | static_cast<uint64_t>(b);
553
554 // x: 00 a e 00 b f 00 c g 00 d h 00 i m 00 j n 00 k o 00 l p
555
556 uint64_t t = (x ^ (x >> 10)) & 0x3FC0003FC00000ULL;
557 x = x ^ t ^ (t << 10);
558
559 // x: b f 00 00 00 a e c g i m 00 00 00 d h j n 00 k o 00 l p
560
561 x |= ((x << 52) & (0x3FULL << 52));
562 x = (x | ((x << 20) & (0x3FULL << 28))) >> 16;
563
564 #if defined (SK_CPU_BENDIAN)
565 // x: 00 00 00 00 00 00 00 00 b f l p a e c g i m k o d h j n
566
567 t = (x ^ (x >> 6)) & 0xFC0000ULL;
568 x = x ^ t ^ (t << 6);
569
570 // x: 00 00 00 00 00 00 00 00 b f l p a e i m c g k o d h j n
571
572 t = (x ^ (x >> 36)) & 0x3FULL;
573 x = x ^ t ^ (t << 36);
574
575 // x: 00 00 00 00 00 00 00 00 b f j n a e i m c g k o d h l p
576
577 t = (x ^ (x >> 12)) & 0xFFF000000ULL;
578 x = x ^ t ^ (t << 12);
579
580 // x: 00 00 00 00 00 00 00 00 a e i m b f j n c g k o d h l p
581 return x;
582 #else
583 // If our CPU is little endian, then the above logic will
584 // produce the following indices:
585 // x: 00 00 00 00 00 00 00 00 c g i m d h b f l p j n a e k o
586
587 t = (x ^ (x >> 6)) & 0xFC0000ULL;
588 x = x ^ t ^ (t << 6);
589
590 // x: 00 00 00 00 00 00 00 00 c g i m d h l p b f j n a e k o
591
592 t = (x ^ (x >> 36)) & 0xFC0ULL;
593 x = x ^ t ^ (t << 36);
594
595 // x: 00 00 00 00 00 00 00 00 a e i m d h l p b f j n c g k o
596
597 x = (x & (0xFFFULL << 36)) | ((x & 0xFFFFFFULL) << 12) | ((x >> 24) & 0xFFFU LL);
598
599 // x: 00 00 00 00 00 00 00 00 a e i m b f j n c g k o d h l p
600
601 return x;
478 #endif 602 #endif
479 603 }
480 return compress_heterogeneous_r11eac_block(block); 604
481 } 605 // This function converts an integer containing four bytes of alpha
482 606 // values into an integer containing four bytes of indices into R11 EAC.
483 static bool compress_a8_to_r11eac(uint8_t* dst, const uint8_t* src, 607 // Note, there needs to be a mapping of indices:
484 int width, int height, int rowBytes) { 608 // 0 1 2 3 4 5 6 7
609 // 3 2 1 0 4 5 6 7
610 //
611 // To compute this, we first negate each byte, and then add three, which
612 // gives the mapping
613 // 3 2 1 0 -1 -2 -3 -4
614 //
615 // Then we mask out the negative values, take their absolute value, and
616 // add three.
617 //
618 // Most of the voodoo in this function comes from Hacker's Delight, section 2-18
robertphillips 2014/07/09 12:24:26 inline ?
krajcevski 2014/07/09 15:18:07 Done.
619 static uint32_t convert_indices(uint32_t x) {
620 // Take the top three bits...
621 x = (x & 0xE0E0E0E0) >> 5;
622
623 // Negate...
624 x = ~((0x80808080 - x) ^ 0x7F7F7F7F);
625
626 // Add three
627 uint32_t s = (x & 0x7F7F7F7F) + 0x03030303;
628 x = ((x ^ 0x03030303) & 0x80808080) ^ s;
629
630 // Absolute value
631 uint32_t a = x & 0x80808080;
632
633 // Aside: mask negatives (m is three if the byte was negative)
634 uint32_t m = a >> 6;
635 m |= m >> 1;
636
637 // .. continue absolute value
638 uint32_t b = a >> 7;
639 x = (x ^ ((a - b) | a)) + b;
640
641 // Add three
642 return x + m;
643 }
644
645 static uint64_t compress_r11eac_block_fast(const uint8_t* src, int rowBytes) {
robertphillips 2014/07/09 12:24:26 ri* -> inputRow* ? ri* -> alphaRow* ?
krajcevski 2014/07/09 15:18:08 Done.
646 const uint32_t ri1 = *(reinterpret_cast<const uint32_t*>(src));
647 const uint32_t ri2 = *(reinterpret_cast<const uint32_t*>(src + rowBytes));
648 const uint32_t ri3 = *(reinterpret_cast<const uint32_t*>(src + 2*rowBytes));
649 const uint32_t ri4 = *(reinterpret_cast<const uint32_t*>(src + 3*rowBytes));
650
651 if (ri1 == ri2 && ri1 == ri3 && ri1 == ri4) {
652 if (0 == ri1) {
robertphillips 2014/07/09 12:24:26 // Fully transparent block ?
krajcevski 2014/07/09 15:18:07 Done.
653 return 0x0020000000002000ULL;
654 } else if (0xFFFFFFFF == ri1) {
robertphillips 2014/07/09 12:24:25 // Fully opaque block ?
krajcevski 2014/07/09 15:18:08 Done.
655 return -1ULL;
656 }
657 }
658
robertphillips 2014/07/09 12:24:26 r* -> indexRow* ?
krajcevski 2014/07/09 15:18:08 Done.
659 const uint32_t r1 = convert_indices(ri1);
660 const uint32_t r2 = convert_indices(ri2);
661 const uint32_t r3 = convert_indices(ri3);
662 const uint32_t r4 = convert_indices(ri4);
robertphillips 2014/07/09 12:24:26 // comment ?
krajcevski 2014/07/09 15:18:07 Done.
663 const uint32_t r1r2 = (r1 << 3) | r2;
664 const uint32_t r3r4 = (r3 << 3) | r4;
665 const uint64_t indices = interleave6(r1r2, r3r4);
robertphillips 2014/07/09 12:24:25 // comment ?
krajcevski 2014/07/09 15:18:08 Done.
666 return SkEndian_SwapBE64(0x8490000000000000ULL | indices);
667 }
668
669 static bool compress_a8_to_r11eac_fast(uint8_t* dst, const uint8_t* src,
670 int width, int height, int rowBytes) {
671 // Make sure that our data is well-formed enough to be considered for compre ssion
672 if (0 == width || 0 == height || (width % 4) != 0 || (height % 4) != 0) {
673 return false;
674 }
675
robertphillips 2014/07/09 12:24:25 const ?
krajcevski 2014/07/09 15:18:07 Done.
676 int blocksX = width >> 2;
677 int blocksY = height >> 2;
678
679 uint64_t* encPtr = reinterpret_cast<uint64_t*>(dst);
680 for (int y = 0; y < blocksY; ++y) {
681 for (int x = 0; x < blocksX; ++x) {
682 // Compress it
683 *encPtr = compress_r11eac_block_fast(src + 4*x, rowBytes);
684 ++encPtr;
685 }
686 src += 4 * rowBytes;
687 }
688 return true;
689 }
690 #endif // COMPRESS_R11_EAC_FASTEST
691
692 static inline bool compress_a8_to_r11eac(uint8_t* dst, const uint8_t* src,
693 int width, int height, int rowBytes) {
694 #if (COMPRESS_R11_EAC_SLOW) || (COMPRESS_R11_EAC_FAST)
485 return compress_4x4_a8_to_64bit(dst, src, width, height, rowBytes, compress_ r11eac_block); 695 return compress_4x4_a8_to_64bit(dst, src, width, height, rowBytes, compress_ r11eac_block);
696 #elif COMPRESS_R11_EAC_FASTEST
697 return compress_a8_to_r11eac_fast(dst, src, width, height, rowBytes);
698 #else
699 #error "Must choose R11 EAC algorithm"
700 #endif
486 } 701 }
487 702
488 //////////////////////////////////////////////////////////////////////////////// 703 ////////////////////////////////////////////////////////////////////////////////
489 704
490 namespace SkTextureCompressor { 705 namespace SkTextureCompressor {
491 706
492 static size_t get_compressed_data_size(Format fmt, int width, int height) { 707 static inline size_t get_compressed_data_size(Format fmt, int width, int height) {
493 switch (fmt) { 708 switch (fmt) {
709 // These formats are 64 bits per 4x4 block.
494 case kR11_EAC_Format: 710 case kR11_EAC_Format:
495 case kLATC_Format: 711 case kLATC_Format:
496 { 712 {
497 // The LATC format is 64 bits per 4x4 block.
498 static const int kLATCEncodedBlockSize = 8; 713 static const int kLATCEncodedBlockSize = 8;
499 714
500 int blocksX = width / kLATCBlockSize; 715 const int blocksX = width / kLATCBlockSize;
501 int blocksY = height / kLATCBlockSize; 716 const int blocksY = height / kLATCBlockSize;
502 717
503 return blocksX * blocksY * kLATCEncodedBlockSize; 718 return blocksX * blocksY * kLATCEncodedBlockSize;
504 } 719 }
505 720
506 default: 721 default:
507 SkFAIL("Unknown compressed format!"); 722 SkFAIL("Unknown compressed format!");
508 return 0; 723 return 0;
509 } 724 }
510 } 725 }
511 726
512 typedef bool (*CompressBitmapProc)(uint8_t* dst, const uint8_t* src, 727 typedef bool (*CompressBitmapProc)(uint8_t* dst, const uint8_t* src,
513 int width, int height, int rowBytes); 728 int width, int height, int rowBytes);
514 729
515 bool CompressBufferToFormat(uint8_t* dst, const uint8_t* src, SkColorType srcCol orType, 730 bool CompressBufferToFormat(uint8_t* dst, const uint8_t* src, SkColorType srcCol orType,
516 int width, int height, int rowBytes, Format format) { 731 int width, int height, int rowBytes, Format format) {
517 732
518 CompressBitmapProc kProcMap[kFormatCnt][kLastEnum_SkColorType + 1]; 733 CompressBitmapProc kProcMap[kFormatCnt][kLastEnum_SkColorType + 1];
519 memset(kProcMap, 0, sizeof(kProcMap)); 734 memset(kProcMap, 0, sizeof(kProcMap));
520 735
521 kProcMap[kLATC_Format][kAlpha_8_SkColorType] = compress_a8_to_latc; 736 kProcMap[kLATC_Format][kAlpha_8_SkColorType] = compress_a8_to_latc;
522 kProcMap[kR11_EAC_Format][kAlpha_8_SkColorType] = compress_a8_to_r11eac; 737 kProcMap[kR11_EAC_Format][kAlpha_8_SkColorType] = compress_a8_to_r11eac;
523 738
524 CompressBitmapProc proc = kProcMap[format][srcColorType]; 739 CompressBitmapProc proc = kProcMap[format][srcColorType];
525 if (NULL != proc) { 740 if (NULL != proc) {
526 return proc(dst, src, width, height, rowBytes); 741 return proc(dst, src, width, height, rowBytes);
527 } 742 }
528 743
529 return false; 744 return false;
530 } 745 }
531 746
532 SkData *CompressBitmapToFormat(const SkBitmap &bitmap, Format format) { 747 SkData *CompressBitmapToFormat(const SkBitmap &bitmap, Format format) {
533 SkAutoLockPixels alp(bitmap); 748 SkAutoLockPixels alp(bitmap);
534 749
535 int compressedDataSize = get_compressed_data_size(format, bitmap.width(), bi tmap.height()); 750 int compressedDataSize = get_compressed_data_size(format, bitmap.width(), bi tmap.height());
536 const uint8_t* src = reinterpret_cast<const uint8_t*>(bitmap.getPixels()); 751 const uint8_t* src = reinterpret_cast<const uint8_t*>(bitmap.getPixels());
537 uint8_t* dst = reinterpret_cast<uint8_t*>(sk_malloc_throw(compressedDataSize )); 752 uint8_t* dst = reinterpret_cast<uint8_t*>(sk_malloc_throw(compressedDataSize ));
538 if (CompressBufferToFormat(dst, src, bitmap.colorType(), bitmap.width(), bit map.height(), 753 if (CompressBufferToFormat(dst, src, bitmap.colorType(), bitmap.width(), bit map.height(),
539 bitmap.rowBytes(), format)) { 754 bitmap.rowBytes(), format)) {
540 return SkData::NewFromMalloc(dst, compressedDataSize); 755 return SkData::NewFromMalloc(dst, compressedDataSize);
541 } 756 }
542 757
543 sk_free(dst); 758 sk_free(dst);
544 return NULL; 759 return NULL;
545 } 760 }
546 761
547 } // namespace SkTextureCompressor 762 } // namespace SkTextureCompressor
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698