reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #include "SkGrTexturePixelRef.h" |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 19 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | #include "GrTexture.h" |
| 21 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 22 | #include "SkRect.h" |
| 23 | #include "SkBitmap.h" |
| 24 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 25 | SkGrTexturePixelRef::SkGrTexturePixelRef(GrTexture* tex) { |
| 26 | fTexture = tex; |
| 27 | GrSafeRef(tex); |
| 28 | } |
| 29 | |
| 30 | SkGrTexturePixelRef::~SkGrTexturePixelRef() { |
| 31 | GrSafeUnref(fTexture); |
| 32 | } |
| 33 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 34 | bool SkGrTexturePixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 35 | if (NULL != fTexture && fTexture->isValid()) { |
| 36 | int left, top, width, height; |
| 37 | if (NULL != subset) { |
| 38 | left = subset->fLeft; |
| 39 | width = subset->width(); |
| 40 | top = subset->fTop; |
| 41 | height = subset->height(); |
| 42 | } else { |
| 43 | left = 0; |
| 44 | width = fTexture->width(); |
| 45 | top = 0; |
| 46 | height = fTexture->height(); |
| 47 | } |
| 48 | dst->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 49 | dst->allocPixels(); |
| 50 | SkAutoLockPixels al(*dst); |
| 51 | void* buffer = dst->getPixels(); |
| 52 | return fTexture->readPixels(left, top, width, height, |
| 53 | kRGBA_8888_GrPixelConfig, |
| 54 | buffer); |
| 55 | } else { |
| 56 | return false; |
| 57 | } |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 58 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 60 | //////////////////////////////////////////////////////////////////////////////// |
| 61 | |
| 62 | SkGrRenderTargetPixelRef::SkGrRenderTargetPixelRef(GrRenderTarget* rt) { |
| 63 | fRenderTarget = rt; |
| 64 | GrSafeRef(fRenderTarget); |
| 65 | } |
| 66 | |
| 67 | SkGrRenderTargetPixelRef::~SkGrRenderTargetPixelRef() { |
| 68 | GrSafeUnref(fRenderTarget); |
| 69 | } |
| 70 | |
| 71 | SkGpuTexture* SkGrRenderTargetPixelRef::getTexture() { |
| 72 | if (NULL != fRenderTarget) { |
| 73 | return (SkGpuTexture*) fRenderTarget->asTexture(); |
| 74 | } |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | bool SkGrRenderTargetPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
| 79 | if (NULL != fRenderTarget && fRenderTarget->isValid()) { |
| 80 | int left, top, width, height; |
| 81 | if (NULL != subset) { |
| 82 | left = subset->fLeft; |
| 83 | width = subset->width(); |
| 84 | top = subset->fTop; |
| 85 | height = subset->height(); |
| 86 | } else { |
| 87 | left = 0; |
| 88 | width = fRenderTarget->width(); |
| 89 | top = 0; |
| 90 | height = fRenderTarget->height(); |
| 91 | } |
| 92 | dst->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 93 | dst->allocPixels(); |
| 94 | SkAutoLockPixels al(*dst); |
| 95 | void* buffer = dst->getPixels(); |
| 96 | return fRenderTarget->readPixels(left, top, width, height, |
| 97 | kRGBA_8888_GrPixelConfig, |
| 98 | buffer); |
| 99 | } else { |
| 100 | return false; |
| 101 | } |
reed@google.com | 4281d65 | 2011-04-08 18:54:20 +0000 | [diff] [blame^] | 102 | } |