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" |
| 19 | #include "GrTexture.h" |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 20 | #include "SkRect.h" |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 21 | |
| 22 | // since we call lockPixels recursively on fBitmap, we need a distinct mutex, |
| 23 | // to avoid deadlock with the default one provided by SkPixelRef. |
| 24 | static SkMutex gROLockPixelsPixelRefMutex; |
| 25 | |
| 26 | SkROLockPixelsPixelRef::SkROLockPixelsPixelRef() : INHERITED(&gROLockPixelsPixelRefMutex) { |
| 27 | } |
| 28 | |
| 29 | SkROLockPixelsPixelRef::~SkROLockPixelsPixelRef() { |
| 30 | } |
| 31 | |
| 32 | void* SkROLockPixelsPixelRef::onLockPixels(SkColorTable** ctable) { |
| 33 | if (ctable) { |
| 34 | *ctable = NULL; |
| 35 | } |
| 36 | fBitmap.reset(); |
| 37 | // SkDebugf("---------- calling readpixels in support of lockpixels\n"); |
| 38 | if (!this->onReadPixels(&fBitmap, NULL)) { |
| 39 | SkDebugf("SkROLockPixelsPixelRef::onLockPixels failed!\n"); |
| 40 | return NULL; |
| 41 | } |
| 42 | fBitmap.lockPixels(); |
| 43 | return fBitmap.getPixels(); |
| 44 | } |
| 45 | |
| 46 | void SkROLockPixelsPixelRef::onUnlockPixels() { |
| 47 | fBitmap.unlockPixels(); |
| 48 | } |
| 49 | |
| 50 | bool SkROLockPixelsPixelRef::onLockPixelsAreWritable() const { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 55 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | SkGrTexturePixelRef::SkGrTexturePixelRef(GrTexture* tex) { |
| 57 | fTexture = tex; |
| 58 | GrSafeRef(tex); |
| 59 | } |
| 60 | |
| 61 | SkGrTexturePixelRef::~SkGrTexturePixelRef() { |
| 62 | GrSafeUnref(fTexture); |
| 63 | } |
| 64 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 65 | SkGpuTexture* SkGrTexturePixelRef::getTexture() { |
| 66 | return (SkGpuTexture*)fTexture; |
| 67 | } |
| 68 | |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 69 | bool SkGrTexturePixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 70 | if (NULL != fTexture && fTexture->isValid()) { |
| 71 | int left, top, width, height; |
| 72 | if (NULL != subset) { |
| 73 | left = subset->fLeft; |
| 74 | width = subset->width(); |
| 75 | top = subset->fTop; |
| 76 | height = subset->height(); |
| 77 | } else { |
| 78 | left = 0; |
| 79 | width = fTexture->width(); |
| 80 | top = 0; |
| 81 | height = fTexture->height(); |
| 82 | } |
| 83 | dst->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 84 | dst->allocPixels(); |
| 85 | SkAutoLockPixels al(*dst); |
| 86 | void* buffer = dst->getPixels(); |
| 87 | return fTexture->readPixels(left, top, width, height, |
| 88 | kRGBA_8888_GrPixelConfig, |
| 89 | buffer); |
| 90 | } else { |
| 91 | return false; |
| 92 | } |
reed@google.com | 50dfa01 | 2011-04-01 19:05:36 +0000 | [diff] [blame] | 93 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 94 | |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 95 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 96 | |
| 97 | SkGrRenderTargetPixelRef::SkGrRenderTargetPixelRef(GrRenderTarget* rt) { |
| 98 | fRenderTarget = rt; |
| 99 | GrSafeRef(fRenderTarget); |
| 100 | } |
| 101 | |
| 102 | SkGrRenderTargetPixelRef::~SkGrRenderTargetPixelRef() { |
| 103 | GrSafeUnref(fRenderTarget); |
| 104 | } |
| 105 | |
| 106 | SkGpuTexture* SkGrRenderTargetPixelRef::getTexture() { |
| 107 | if (NULL != fRenderTarget) { |
| 108 | return (SkGpuTexture*) fRenderTarget->asTexture(); |
| 109 | } |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | bool SkGrRenderTargetPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
| 114 | if (NULL != fRenderTarget && fRenderTarget->isValid()) { |
| 115 | int left, top, width, height; |
| 116 | if (NULL != subset) { |
| 117 | left = subset->fLeft; |
| 118 | width = subset->width(); |
| 119 | top = subset->fTop; |
| 120 | height = subset->height(); |
| 121 | } else { |
| 122 | left = 0; |
| 123 | width = fRenderTarget->width(); |
| 124 | top = 0; |
| 125 | height = fRenderTarget->height(); |
| 126 | } |
| 127 | dst->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 128 | dst->allocPixels(); |
| 129 | SkAutoLockPixels al(*dst); |
| 130 | void* buffer = dst->getPixels(); |
| 131 | return fRenderTarget->readPixels(left, top, width, height, |
| 132 | kRGBA_8888_GrPixelConfig, |
| 133 | buffer); |
| 134 | } else { |
| 135 | return false; |
| 136 | } |
reed@google.com | 4281d65 | 2011-04-08 18:54:20 +0000 | [diff] [blame] | 137 | } |
reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 138 | |