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

Side by Side Diff: src/core/SkCanvas.cpp

Issue 199413013: add new readPixels with direct memory parameters (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 6 years, 9 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 | Annotate | Revision Log
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2008 The Android Open Source Project 3 * Copyright 2008 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
(...skipping 649 matching lines...) Expand 10 before | Expand all | Expand 10 after
660 } 660 }
661 // now jam our 1st clip to be bounds, and intersect the rest with that 661 // now jam our 1st clip to be bounds, and intersect the rest with that
662 rec->fRasterClip->setRect(bounds); 662 rec->fRasterClip->setRect(bounds);
663 while ((rec = (MCRec*)iter.next()) != NULL) { 663 while ((rec = (MCRec*)iter.next()) != NULL) {
664 (void)rec->fRasterClip->op(bounds, SkRegion::kIntersect_Op); 664 (void)rec->fRasterClip->op(bounds, SkRegion::kIntersect_Op);
665 } 665 }
666 666
667 return device; 667 return device;
668 } 668 }
669 669
670 #ifdef SK_SUPPORT_LEGACY_READPIXELSCONFIG
670 bool SkCanvas::readPixels(SkBitmap* bitmap, 671 bool SkCanvas::readPixels(SkBitmap* bitmap,
671 int x, int y, 672 int x, int y,
672 Config8888 config8888) { 673 Config8888 config8888) {
673 SkBaseDevice* device = this->getDevice(); 674 SkBaseDevice* device = this->getDevice();
674 if (!device) { 675 if (!device) {
675 return false; 676 return false;
676 } 677 }
677 return device->readPixels(bitmap, x, y, config8888); 678 return device->readPixels(bitmap, x, y, config8888);
678 } 679 }
680 #endif
681
682 bool SkCanvas::readPixels(SkBitmap* bitmap, int x, int y) {
683 if (kUnknown_SkColorType == bitmap->colorType() || bitmap->getTexture()) {
684 return false;
685 }
686
687 bool weAllocated = false;
688 if (NULL == bitmap->pixelRef()) {
689 if (!bitmap->allocPixels()) {
690 return false;
691 }
692 weAllocated = true;
693 }
694
695 SkBitmap bm(*bitmap);
696 bm.lockPixels();
697 if (bm.getPixels() && this->readPixels(bm.info(), bm.getPixels(), bm.rowByte s(), x, y)) {
698 return true;
699 }
700
701 if (weAllocated) {
702 bitmap->setPixelRef(NULL);
703 }
704 return false;
705 }
679 706
680 bool SkCanvas::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) { 707 bool SkCanvas::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
708 SkIRect r = srcRect;
709 const SkISize size = this->getBaseLayerSize();
710 if (!r.intersect(0, 0, size.width(), size.height())) {
711 bitmap->reset();
712 return false;
713 }
714
715 if (!bitmap->allocN32Pixels(r.width(), r.height())) {
716 // bitmap will already be reset.
717 return false;
718 }
719 if (!this->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes( ), r.x(), r.y())) {
720 bitmap->reset();
721 return false;
722 }
723 return true;
724 }
725
726 bool SkCanvas::readPixels(const SkImageInfo& origInfo, void* dstP, size_t rowByt es, int x, int y) {
727 switch (origInfo.colorType()) {
728 case kUnknown_SkColorType:
729 case kIndex_8_SkColorType:
730 return false;
731 default:
732 break;
733 }
734 if (NULL == dstP || rowBytes < origInfo.minRowBytes()) {
735 return false;
736 }
737 if (0 == origInfo.width() || 0 == origInfo.height()) {
738 return false;
739 }
740
681 SkBaseDevice* device = this->getDevice(); 741 SkBaseDevice* device = this->getDevice();
682 if (!device) { 742 if (!device) {
683 return false; 743 return false;
684 } 744 }
685 745
686 SkIRect bounds; 746 const SkISize size = this->getBaseLayerSize();
687 bounds.set(0, 0, device->width(), device->height()); 747 SkIRect srcR = SkIRect::MakeXYWH(x, y, origInfo.width(), origInfo.height());
688 if (!bounds.intersect(srcRect)) { 748 if (!srcR.intersect(0, 0, size.width(), size.height())) {
689 return false; 749 return false;
690 } 750 }
691 751
692 SkBitmap tmp; 752 SkImageInfo info = origInfo;
693 tmp.setConfig(SkBitmap::kARGB_8888_Config, bounds.width(), 753 // the intersect may have shrunk info's logical size
694 bounds.height()); 754 info.fWidth = srcR.width();
695 if (this->readPixels(&tmp, bounds.fLeft, bounds.fTop)) { 755 info.fHeight = srcR.height();
696 bitmap->swap(tmp); 756
697 return true; 757 // if x or y are negative, then we have to adjust pixels
698 } else { 758 if (x > 0) {
699 return false; 759 x = 0;
700 } 760 }
761 if (y > 0) {
762 y = 0;
763 }
764 // here x,y are either 0 or negative
765 dstP = ((char*)dstP - y * rowBytes - x * info.bytesPerPixel());
766
767 // The device can assert that the requested area is always contained in its bounds
768 return device->readPixels(info, dstP, rowBytes, srcR.x(), srcR.y());
701 } 769 }
702 770
703 #ifdef SK_SUPPORT_LEGACY_WRITEPIXELSCONFIG 771 #ifdef SK_SUPPORT_LEGACY_WRITEPIXELSCONFIG
704 void SkCanvas::writePixels(const SkBitmap& bitmap, int x, int y, 772 void SkCanvas::writePixels(const SkBitmap& bitmap, int x, int y,
705 Config8888 config8888) { 773 Config8888 config8888) {
706 SkBaseDevice* device = this->getDevice(); 774 SkBaseDevice* device = this->getDevice();
707 if (device) { 775 if (device) {
708 if (SkIRect::Intersects(SkIRect::MakeSize(this->getDeviceSize()), 776 if (SkIRect::Intersects(SkIRect::MakeSize(this->getDeviceSize()),
709 SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.h eight()))) { 777 SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.h eight()))) {
710 device->accessBitmap(true); 778 device->accessBitmap(true);
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after
1070 1138
1071 void* SkCanvas::onAccessTopLayerPixels(SkImageInfo* info, size_t* rowBytes) { 1139 void* SkCanvas::onAccessTopLayerPixels(SkImageInfo* info, size_t* rowBytes) {
1072 SkBaseDevice* dev = this->getTopDevice(); 1140 SkBaseDevice* dev = this->getTopDevice();
1073 return dev ? dev->accessPixels(info, rowBytes) : NULL; 1141 return dev ? dev->accessPixels(info, rowBytes) : NULL;
1074 } 1142 }
1075 1143
1076 SkAutoROCanvasPixels::SkAutoROCanvasPixels(SkCanvas* canvas) { 1144 SkAutoROCanvasPixels::SkAutoROCanvasPixels(SkCanvas* canvas) {
1077 fAddr = canvas->peekPixels(&fInfo, &fRowBytes); 1145 fAddr = canvas->peekPixels(&fInfo, &fRowBytes);
1078 if (NULL == fAddr) { 1146 if (NULL == fAddr) {
1079 fInfo = canvas->imageInfo(); 1147 fInfo = canvas->imageInfo();
1080 if (kUnknown_SkColorType == fInfo.colorType() || 1148 if (kUnknown_SkColorType == fInfo.colorType() || !fBitmap.allocPixels(fI nfo)) {
1081 !fBitmap.allocPixels(fInfo))
1082 {
1083 return; // failure, fAddr is NULL 1149 return; // failure, fAddr is NULL
1084 } 1150 }
1085 fBitmap.lockPixels();
1086 if (!canvas->readPixels(&fBitmap, 0, 0)) { 1151 if (!canvas->readPixels(&fBitmap, 0, 0)) {
1087 return; // failure, fAddr is NULL 1152 return; // failure, fAddr is NULL
1088 } 1153 }
1089 fAddr = fBitmap.getPixels(); 1154 fAddr = fBitmap.getPixels();
1090 fRowBytes = fBitmap.rowBytes(); 1155 fRowBytes = fBitmap.rowBytes();
1091 } 1156 }
1092 SkASSERT(fAddr); // success 1157 SkASSERT(fAddr); // success
1093 } 1158 }
1094 1159
1095 bool SkAutoROCanvasPixels::asROBitmap(SkBitmap* bitmap) const { 1160 bool SkAutoROCanvasPixels::asROBitmap(SkBitmap* bitmap) const {
(...skipping 1478 matching lines...) Expand 10 before | Expand all | Expand 10 after
2574 if (!bitmap.installPixels(info, pixels, rowBytes)) { 2639 if (!bitmap.installPixels(info, pixels, rowBytes)) {
2575 return NULL; 2640 return NULL;
2576 } 2641 }
2577 2642
2578 // should this functionality be moved into allocPixels()? 2643 // should this functionality be moved into allocPixels()?
2579 if (!bitmap.info().isOpaque()) { 2644 if (!bitmap.info().isOpaque()) {
2580 bitmap.eraseColor(0); 2645 bitmap.eraseColor(0);
2581 } 2646 }
2582 return SkNEW_ARGS(SkCanvas, (bitmap)); 2647 return SkNEW_ARGS(SkCanvas, (bitmap));
2583 } 2648 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698