| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
| robertphillips@google.com | 5088eb4 | 2012-06-28 20:59:13 +0000 | [diff] [blame] | 11 | #include "SkGrPixelRef.h" |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 12 | #include "GrContext.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | #include "GrTexture.h" |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 14 | #include "SkGr.h" |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 15 | #include "SkRect.h" |
| reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 16 | |
| 17 | // since we call lockPixels recursively on fBitmap, we need a distinct mutex, |
| 18 | // to avoid deadlock with the default one provided by SkPixelRef. |
| digit@google.com | 1771cbf | 2012-01-26 21:26:40 +0000 | [diff] [blame] | 19 | SK_DECLARE_STATIC_MUTEX(gROLockPixelsPixelRefMutex); |
| reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 20 | |
| reed@google.com | 93b4059 | 2013-12-13 14:18:14 +0000 | [diff] [blame^] | 21 | SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info) |
| 22 | : INHERITED(info, &gROLockPixelsPixelRefMutex) { |
| reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | SkROLockPixelsPixelRef::~SkROLockPixelsPixelRef() { |
| 26 | } |
| 27 | |
| reed@google.com | 93b4059 | 2013-12-13 14:18:14 +0000 | [diff] [blame^] | 28 | bool SkROLockPixelsPixelRef::onNewLockPixels(LockRec* rec) { |
| reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 29 | fBitmap.reset(); |
| 30 | // SkDebugf("---------- calling readpixels in support of lockpixels\n"); |
| 31 | if (!this->onReadPixels(&fBitmap, NULL)) { |
| 32 | SkDebugf("SkROLockPixelsPixelRef::onLockPixels failed!\n"); |
| reed@google.com | 93b4059 | 2013-12-13 14:18:14 +0000 | [diff] [blame^] | 33 | return false; |
| reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 34 | } |
| 35 | fBitmap.lockPixels(); |
| reed@google.com | 93b4059 | 2013-12-13 14:18:14 +0000 | [diff] [blame^] | 36 | if (NULL == fBitmap.getPixels()) { |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | rec->fPixels = fBitmap.getPixels(); |
| 41 | rec->fColorTable = NULL; |
| 42 | rec->fRowBytes = fBitmap.rowBytes(); |
| 43 | return true; |
| reed@google.com | 9c49bc3 | 2011-07-07 13:42:37 +0000 | [diff] [blame] | 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 | |
| scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 56 | static SkGrPixelRef* copyToTexturePixelRef(GrTexture* texture, SkBitmap::Config dstConfig, |
| 57 | const SkIRect* subset) { |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 58 | if (NULL == texture) { |
| 59 | return NULL; |
| 60 | } |
| 61 | GrContext* context = texture->getContext(); |
| 62 | if (NULL == context) { |
| 63 | return NULL; |
| 64 | } |
| 65 | GrTextureDesc desc; |
| 66 | |
| scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 67 | SkIPoint pointStorage; |
| 68 | SkIPoint* topLeft; |
| 69 | if (subset != NULL) { |
| 70 | SkASSERT(SkIRect::MakeWH(texture->width(), texture->height()).contains(*subset)); |
| 71 | // Create a new texture that is the size of subset. |
| 72 | desc.fWidth = subset->width(); |
| 73 | desc.fHeight = subset->height(); |
| 74 | pointStorage.set(subset->x(), subset->y()); |
| 75 | topLeft = &pointStorage; |
| 76 | } else { |
| 77 | desc.fWidth = texture->width(); |
| 78 | desc.fHeight = texture->height(); |
| 79 | topLeft = NULL; |
| 80 | } |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 81 | desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
| rileya@google.com | 24f3ad1 | 2012-07-18 21:47:40 +0000 | [diff] [blame] | 82 | desc.fConfig = SkBitmapConfig2GrPixelConfig(dstConfig); |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 83 | |
| reed@google.com | 93b4059 | 2013-12-13 14:18:14 +0000 | [diff] [blame^] | 84 | SkImageInfo info; |
| 85 | if (!GrPixelConfig2ColorType(desc.fConfig, &info.fColorType)) { |
| 86 | return NULL; |
| 87 | } |
| 88 | info.fWidth = desc.fWidth; |
| 89 | info.fHeight = desc.fHeight; |
| 90 | info.fAlphaType = kPremul_SkAlphaType; |
| 91 | |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 92 | GrTexture* dst = context->createUncachedTexture(desc, NULL, 0); |
| 93 | if (NULL == dst) { |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 97 | context->copyTexture(texture, dst->asRenderTarget(), topLeft); |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 98 | |
| robertphillips@google.com | 41efe04 | 2012-06-29 20:55:14 +0000 | [diff] [blame] | 99 | // TODO: figure out if this is responsible for Chrome canvas errors |
| 100 | #if 0 |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 101 | // The render texture we have created (to perform the copy) isn't fully |
| 102 | // functional (since it doesn't have a stencil buffer). Release it here |
| 103 | // so the caller doesn't try to render to it. |
| 104 | // TODO: we can undo this release when dynamic stencil buffer attach/ |
| 105 | // detach has been implemented |
| 106 | dst->releaseRenderTarget(); |
| robertphillips@google.com | 41efe04 | 2012-06-29 20:55:14 +0000 | [diff] [blame] | 107 | #endif |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 108 | |
| reed@google.com | 93b4059 | 2013-12-13 14:18:14 +0000 | [diff] [blame^] | 109 | SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (info, dst)); |
| commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 110 | SkSafeUnref(dst); |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 111 | return pixelRef; |
| 112 | } |
| 113 | |
| 114 | /////////////////////////////////////////////////////////////////////////////// |
| 115 | |
| reed@google.com | 93b4059 | 2013-12-13 14:18:14 +0000 | [diff] [blame^] | 116 | SkGrPixelRef::SkGrPixelRef(const SkImageInfo& info, GrSurface* surface, |
| 117 | bool transferCacheLock) |
| 118 | : INHERITED(info) |
| 119 | { |
| reed@google.com | 6186787 | 2013-12-09 13:41:06 +0000 | [diff] [blame] | 120 | // TODO: figure out if this is responsible for Chrome canvas errors |
| 121 | #if 0 |
| 122 | // The GrTexture has a ref to the GrRenderTarget but not vice versa. |
| 123 | // If the GrTexture exists take a ref to that (rather than the render |
| 124 | // target) |
| 125 | fSurface = surface->asTexture(); |
| 126 | #else |
| 127 | fSurface = NULL; |
| 128 | #endif |
| 129 | if (NULL == fSurface) { |
| 130 | fSurface = surface; |
| 131 | } |
| 132 | fUnlock = transferCacheLock; |
| 133 | SkSafeRef(surface); |
| 134 | } |
| 135 | |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 136 | SkGrPixelRef::~SkGrPixelRef() { |
| bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 137 | if (fUnlock) { |
| 138 | GrContext* context = fSurface->getContext(); |
| robertphillips@google.com | d07cb0c | 2012-08-30 19:22:29 +0000 | [diff] [blame] | 139 | GrTexture* texture = fSurface->asTexture(); |
| bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 140 | if (NULL != context && NULL != texture) { |
| robertphillips@google.com | 9fbcad0 | 2012-09-09 14:44:15 +0000 | [diff] [blame] | 141 | context->unlockScratchTexture(texture); |
| bsalomon@google.com | 8090e65 | 2012-08-28 15:07:11 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 144 | SkSafeUnref(fSurface); |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 147 | GrTexture* SkGrPixelRef::getTexture() { |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 148 | if (NULL != fSurface) { |
| commit-bot@chromium.org | b8d00db | 2013-06-26 19:18:23 +0000 | [diff] [blame] | 149 | return fSurface->asTexture(); |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 150 | } |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 154 | SkPixelRef* SkGrPixelRef::deepCopy(SkBitmap::Config dstConfig, const SkIRect* subset) { |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 155 | if (NULL == fSurface) { |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 156 | return NULL; |
| 157 | } |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 158 | |
| 159 | // Note that when copying a render-target-backed pixel ref, we |
| 160 | // return a texture-backed pixel ref instead. This is because |
| 161 | // render-target pixel refs are usually created in conjunction with |
| 162 | // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live |
| 163 | // independently of that texture. Texture-backed pixel refs, on the other |
| 164 | // hand, own their GrTextures, and are thus self-contained. |
| scroggo@google.com | a2a3192 | 2012-12-07 19:14:45 +0000 | [diff] [blame] | 165 | return copyToTexturePixelRef(fSurface->asTexture(), dstConfig, subset); |
| senorblanco@chromium.org | ef843cd | 2011-12-02 19:11:17 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 168 | bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
| 169 | if (NULL == fSurface || !fSurface->isValid()) { |
| bsalomon@google.com | 669fdc4 | 2011-04-05 17:08:27 +0000 | [diff] [blame] | 170 | return false; |
| 171 | } |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 172 | |
| 173 | int left, top, width, height; |
| 174 | if (NULL != subset) { |
| 175 | left = subset->fLeft; |
| 176 | width = subset->width(); |
| 177 | top = subset->fTop; |
| 178 | height = subset->height(); |
| 179 | } else { |
| 180 | left = 0; |
| 181 | width = fSurface->width(); |
| 182 | top = 0; |
| 183 | height = fSurface->height(); |
| 184 | } |
| 185 | dst->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| bsalomon@google.com | 009bcca | 2012-12-18 21:40:08 +0000 | [diff] [blame] | 186 | if (!dst->allocPixels()) { |
| 187 | SkDebugf("SkGrPixelRef::onReadPixels failed to alloc bitmap for result!\n"); |
| 188 | return false; |
| 189 | } |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 190 | SkAutoLockPixels al(*dst); |
| 191 | void* buffer = dst->getPixels(); |
| 192 | return fSurface->readPixels(left, top, width, height, |
| bsalomon@google.com | fec0bc3 | 2013-02-07 14:43:04 +0000 | [diff] [blame] | 193 | kSkia8888_GrPixelConfig, |
| robertphillips@google.com | d881bc1 | 2012-06-28 20:02:39 +0000 | [diff] [blame] | 194 | buffer, dst->rowBytes()); |
| reed@google.com | 4281d65 | 2011-04-08 18:54:20 +0000 | [diff] [blame] | 195 | } |