| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2010 Google Inc. | 2 * Copyright 2010 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 | 8 |
| 9 | 9 |
| 10 #include "SkGrPixelRef.h" | 10 #include "SkGrPixelRef.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 fBitmap.unlockPixels(); | 43 fBitmap.unlockPixels(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 bool SkROLockPixelsPixelRef::onLockPixelsAreWritable() const { | 46 bool SkROLockPixelsPixelRef::onLockPixelsAreWritable() const { |
| 47 return false; | 47 return false; |
| 48 } | 48 } |
| 49 | 49 |
| 50 /////////////////////////////////////////////////////////////////////////////// | 50 /////////////////////////////////////////////////////////////////////////////// |
| 51 | 51 |
| 52 static SkGrPixelRef* copy_to_new_texture_pixelref(GrTexture* texture, SkColorTyp
e dstCT, | 52 static SkGrPixelRef* copy_to_new_texture_pixelref(GrTexture* texture, SkColorTyp
e dstCT, |
| 53 SkColorSpace* dstCS, const SkI
Rect* subset) { | 53 SkColorProfileType dstPT, cons
t SkIRect* subset) { |
| 54 if (nullptr == texture || kUnknown_SkColorType == dstCT) { | 54 if (nullptr == texture || kUnknown_SkColorType == dstCT) { |
| 55 return nullptr; | 55 return nullptr; |
| 56 } | 56 } |
| 57 GrContext* context = texture->getContext(); | 57 GrContext* context = texture->getContext(); |
| 58 if (nullptr == context) { | 58 if (nullptr == context) { |
| 59 return nullptr; | 59 return nullptr; |
| 60 } | 60 } |
| 61 GrSurfaceDesc desc; | 61 GrSurfaceDesc desc; |
| 62 | 62 |
| 63 SkIRect srcRect; | 63 SkIRect srcRect; |
| 64 | 64 |
| 65 if (!subset) { | 65 if (!subset) { |
| 66 desc.fWidth = texture->width(); | 66 desc.fWidth = texture->width(); |
| 67 desc.fHeight = texture->height(); | 67 desc.fHeight = texture->height(); |
| 68 srcRect = SkIRect::MakeWH(texture->width(), texture->height()); | 68 srcRect = SkIRect::MakeWH(texture->width(), texture->height()); |
| 69 } else { | 69 } else { |
| 70 SkASSERT(SkIRect::MakeWH(texture->width(), texture->height()).contains(*
subset)); | 70 SkASSERT(SkIRect::MakeWH(texture->width(), texture->height()).contains(*
subset)); |
| 71 // Create a new texture that is the size of subset. | 71 // Create a new texture that is the size of subset. |
| 72 desc.fWidth = subset->width(); | 72 desc.fWidth = subset->width(); |
| 73 desc.fHeight = subset->height(); | 73 desc.fHeight = subset->height(); |
| 74 srcRect = *subset; | 74 srcRect = *subset; |
| 75 } | 75 } |
| 76 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 76 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 77 desc.fConfig = SkImageInfo2GrPixelConfig(dstCT, kPremul_SkAlphaType, dstCS,
*context->caps()); | 77 desc.fConfig = SkImageInfo2GrPixelConfig(dstCT, kPremul_SkAlphaType, dstPT,
*context->caps()); |
| 78 desc.fIsMipMapped = false; | 78 desc.fIsMipMapped = false; |
| 79 | 79 |
| 80 GrTexture* dst = context->textureProvider()->createTexture(desc, SkBudgeted:
:kNo, nullptr, 0); | 80 GrTexture* dst = context->textureProvider()->createTexture(desc, SkBudgeted:
:kNo, nullptr, 0); |
| 81 if (nullptr == dst) { | 81 if (nullptr == dst) { |
| 82 return nullptr; | 82 return nullptr; |
| 83 } | 83 } |
| 84 | 84 |
| 85 // Blink is relying on the above copy being sent to GL immediately in the ca
se when the source | 85 // Blink is relying on the above copy being sent to GL immediately in the ca
se when the source |
| 86 // is a WebGL canvas backing store. We could have a TODO to remove this flus
h, but we have | 86 // is a WebGL canvas backing store. We could have a TODO to remove this flus
h, but we have |
| 87 // a larger TODO to remove SkGrPixelRef entirely. | 87 // a larger TODO to remove SkGrPixelRef entirely. |
| 88 context->copySurface(dst, texture, srcRect, SkIPoint::Make(0,0)); | 88 context->copySurface(dst, texture, srcRect, SkIPoint::Make(0,0)); |
| 89 context->flushSurfaceWrites(dst); | 89 context->flushSurfaceWrites(dst); |
| 90 | 90 |
| 91 SkImageInfo info = SkImageInfo::Make(desc.fWidth, desc.fHeight, dstCT, kPrem
ul_SkAlphaType, | 91 SkImageInfo info = SkImageInfo::Make(desc.fWidth, desc.fHeight, dstCT, kPrem
ul_SkAlphaType, |
| 92 sk_ref_sp(dstCS)); | 92 dstPT); |
| 93 SkGrPixelRef* pixelRef = new SkGrPixelRef(info, dst); | 93 SkGrPixelRef* pixelRef = new SkGrPixelRef(info, dst); |
| 94 SkSafeUnref(dst); | 94 SkSafeUnref(dst); |
| 95 return pixelRef; | 95 return pixelRef; |
| 96 } | 96 } |
| 97 | 97 |
| 98 /////////////////////////////////////////////////////////////////////////////// | 98 /////////////////////////////////////////////////////////////////////////////// |
| 99 | 99 |
| 100 SkGrPixelRef::SkGrPixelRef(const SkImageInfo& info, GrSurface* surface) : INHERI
TED(info) { | 100 SkGrPixelRef::SkGrPixelRef(const SkImageInfo& info, GrSurface* surface) : INHERI
TED(info) { |
| 101 // For surfaces that are both textures and render targets, the texture owns
the | 101 // For surfaces that are both textures and render targets, the texture owns
the |
| 102 // render target but not vice versa. So we ref the texture to keep both aliv
e for | 102 // render target but not vice versa. So we ref the texture to keep both aliv
e for |
| (...skipping 20 matching lines...) Expand all Loading... |
| 123 return nullptr; | 123 return nullptr; |
| 124 } | 124 } |
| 125 | 125 |
| 126 void SkGrPixelRef::onNotifyPixelsChanged() { | 126 void SkGrPixelRef::onNotifyPixelsChanged() { |
| 127 GrTexture* texture = this->getTexture(); | 127 GrTexture* texture = this->getTexture(); |
| 128 if (texture) { | 128 if (texture) { |
| 129 texture->texturePriv().dirtyMipMaps(true); | 129 texture->texturePriv().dirtyMipMaps(true); |
| 130 } | 130 } |
| 131 } | 131 } |
| 132 | 132 |
| 133 SkPixelRef* SkGrPixelRef::deepCopy(SkColorType dstCT, SkColorSpace* dstCS, const
SkIRect* subset) { | 133 SkPixelRef* SkGrPixelRef::deepCopy(SkColorType dstCT, SkColorProfileType dstPT, |
| 134 const SkIRect* subset) { |
| 134 if (nullptr == fSurface) { | 135 if (nullptr == fSurface) { |
| 135 return nullptr; | 136 return nullptr; |
| 136 } | 137 } |
| 137 | 138 |
| 138 // Note that when copying a render-target-backed pixel ref, we | 139 // Note that when copying a render-target-backed pixel ref, we |
| 139 // return a texture-backed pixel ref instead. This is because | 140 // return a texture-backed pixel ref instead. This is because |
| 140 // render-target pixel refs are usually created in conjunction with | 141 // render-target pixel refs are usually created in conjunction with |
| 141 // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live | 142 // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live |
| 142 // independently of that texture. Texture-backed pixel refs, on the other | 143 // independently of that texture. Texture-backed pixel refs, on the other |
| 143 // hand, own their GrTextures, and are thus self-contained. | 144 // hand, own their GrTextures, and are thus self-contained. |
| 144 return copy_to_new_texture_pixelref(fSurface->asTexture(), dstCT, dstCS, sub
set); | 145 return copy_to_new_texture_pixelref(fSurface->asTexture(), dstCT, dstPT, sub
set); |
| 145 } | 146 } |
| 146 | 147 |
| 147 static bool tryAllocBitmapPixels(SkBitmap* bitmap) { | 148 static bool tryAllocBitmapPixels(SkBitmap* bitmap) { |
| 148 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); | 149 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); |
| 149 if (nullptr != allocator) { | 150 if (nullptr != allocator) { |
| 150 return allocator->allocPixelRef(bitmap, 0); | 151 return allocator->allocPixelRef(bitmap, 0); |
| 151 } else { | 152 } else { |
| 152 // DiscardableMemory is not available, fallback to default allocator | 153 // DiscardableMemory is not available, fallback to default allocator |
| 153 return bitmap->tryAllocPixels(); | 154 return bitmap->tryAllocPixels(); |
| 154 } | 155 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 175 bounds = SkIRect::MakeWH(this->info().width(), this->info().height()); | 176 bounds = SkIRect::MakeWH(this->info().width(), this->info().height()); |
| 176 } | 177 } |
| 177 | 178 |
| 178 //Check the cache | 179 //Check the cache |
| 179 if(!SkBitmapCache::Find(this->getGenerationID(), bounds, dst)) { | 180 if(!SkBitmapCache::Find(this->getGenerationID(), bounds, dst)) { |
| 180 //Cache miss | 181 //Cache miss |
| 181 | 182 |
| 182 SkBitmap cachedBitmap; | 183 SkBitmap cachedBitmap; |
| 183 cachedBitmap.setInfo(SkImageInfo::Make(bounds.width(), bounds.height(),
colorType, | 184 cachedBitmap.setInfo(SkImageInfo::Make(bounds.width(), bounds.height(),
colorType, |
| 184 this->info().alphaType(), | 185 this->info().alphaType(), |
| 185 sk_ref_sp(this->info().colorSpace
()))); | 186 this->info().profileType())); |
| 186 | 187 |
| 187 // If we can't alloc the pixels, then fail | 188 // If we can't alloc the pixels, then fail |
| 188 if (!tryAllocBitmapPixels(&cachedBitmap)) { | 189 if (!tryAllocBitmapPixels(&cachedBitmap)) { |
| 189 return false; | 190 return false; |
| 190 } | 191 } |
| 191 | 192 |
| 192 // Try to read the pixels from the surface | 193 // Try to read the pixels from the surface |
| 193 void* buffer = cachedBitmap.getPixels(); | 194 void* buffer = cachedBitmap.getPixels(); |
| 194 bool readPixelsOk = fSurface->readPixels(bounds.fLeft, bounds.fTop, | 195 bool readPixelsOk = fSurface->readPixels(bounds.fLeft, bounds.fTop, |
| 195 bounds.width(), bounds.height(), | 196 bounds.width(), bounds.height(), |
| 196 config, buffer, cachedBitmap.rowBytes()); | 197 config, buffer, cachedBitmap.rowBytes()); |
| 197 | 198 |
| 198 if (!readPixelsOk) { | 199 if (!readPixelsOk) { |
| 199 return false; | 200 return false; |
| 200 } | 201 } |
| 201 | 202 |
| 202 // If we are here, pixels were read correctly from the surface. | 203 // If we are here, pixels were read correctly from the surface. |
| 203 cachedBitmap.setImmutable(); | 204 cachedBitmap.setImmutable(); |
| 204 //Add to the cache | 205 //Add to the cache |
| 205 SkBitmapCache::Add(this, bounds, cachedBitmap); | 206 SkBitmapCache::Add(this, bounds, cachedBitmap); |
| 206 | 207 |
| 207 dst->swap(cachedBitmap); | 208 dst->swap(cachedBitmap); |
| 208 } | 209 } |
| 209 | 210 |
| 210 return true; | 211 return true; |
| 211 | 212 |
| 212 } | 213 } |
| OLD | NEW |