OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2010 Google Inc. | 3 * Copyright 2010 Google Inc. |
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 | 10 |
11 #include "SkGrPixelRef.h" | 11 #include "SkGrPixelRef.h" |
12 | 12 |
13 #include "GrContext.h" | 13 #include "GrContext.h" |
14 #include "GrTexture.h" | 14 #include "GrTexture.h" |
15 #include "GrTexturePriv.h" | 15 #include "GrTexturePriv.h" |
16 #include "SkBitmapCache.h" | 16 #include "SkBitmapCache.h" |
17 #include "SkGr.h" | 17 #include "SkGr.h" |
18 #include "SkRect.h" | 18 #include "SkRect.h" |
19 | 19 |
20 SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info) | 20 SkROLockPixelsPixelRef::SkROLockPixelsPixelRef(const SkImageInfo& info) |
21 : INHERITED(info) {} | 21 : INHERITED(info) {} |
22 | 22 |
23 SkROLockPixelsPixelRef::~SkROLockPixelsPixelRef() {} | 23 SkROLockPixelsPixelRef::~SkROLockPixelsPixelRef() {} |
24 | 24 |
25 bool SkROLockPixelsPixelRef::onNewLockPixels(LockRec* rec) { | 25 bool SkROLockPixelsPixelRef::onNewLockPixels(LockRec* rec) { |
26 fBitmap.reset(); | 26 fBitmap.reset(); |
27 // SkDebugf("---------- calling readpixels in support of lockpixels\n"); | 27 // SkDebugf("---------- calling readpixels in support of lockpixels\n"); |
28 if (!this->onReadPixels(&fBitmap, NULL)) { | 28 if (!this->onReadPixels(&fBitmap, nullptr)) { |
29 SkDebugf("SkROLockPixelsPixelRef::onLockPixels failed!\n"); | 29 SkDebugf("SkROLockPixelsPixelRef::onLockPixels failed!\n"); |
30 return false; | 30 return false; |
31 } | 31 } |
32 fBitmap.lockPixels(); | 32 fBitmap.lockPixels(); |
33 if (NULL == fBitmap.getPixels()) { | 33 if (nullptr == fBitmap.getPixels()) { |
34 return false; | 34 return false; |
35 } | 35 } |
36 | 36 |
37 rec->fPixels = fBitmap.getPixels(); | 37 rec->fPixels = fBitmap.getPixels(); |
38 rec->fColorTable = NULL; | 38 rec->fColorTable = nullptr; |
39 rec->fRowBytes = fBitmap.rowBytes(); | 39 rec->fRowBytes = fBitmap.rowBytes(); |
40 return true; | 40 return true; |
41 } | 41 } |
42 | 42 |
43 void SkROLockPixelsPixelRef::onUnlockPixels() { | 43 void SkROLockPixelsPixelRef::onUnlockPixels() { |
44 fBitmap.unlockPixels(); | 44 fBitmap.unlockPixels(); |
45 } | 45 } |
46 | 46 |
47 bool SkROLockPixelsPixelRef::onLockPixelsAreWritable() const { | 47 bool SkROLockPixelsPixelRef::onLockPixelsAreWritable() const { |
48 return false; | 48 return false; |
49 } | 49 } |
50 | 50 |
51 /////////////////////////////////////////////////////////////////////////////// | 51 /////////////////////////////////////////////////////////////////////////////// |
52 | 52 |
53 static SkGrPixelRef* copy_to_new_texture_pixelref(GrTexture* texture, SkColorTyp
e dstCT, | 53 static SkGrPixelRef* copy_to_new_texture_pixelref(GrTexture* texture, SkColorTyp
e dstCT, |
54 SkColorProfileType dstPT, cons
t SkIRect* subset) { | 54 SkColorProfileType dstPT, cons
t SkIRect* subset) { |
55 if (NULL == texture || kUnknown_SkColorType == dstCT) { | 55 if (nullptr == texture || kUnknown_SkColorType == dstCT) { |
56 return NULL; | 56 return nullptr; |
57 } | 57 } |
58 GrContext* context = texture->getContext(); | 58 GrContext* context = texture->getContext(); |
59 if (NULL == context) { | 59 if (nullptr == context) { |
60 return NULL; | 60 return nullptr; |
61 } | 61 } |
62 GrSurfaceDesc desc; | 62 GrSurfaceDesc desc; |
63 | 63 |
64 SkIRect srcRect; | 64 SkIRect srcRect; |
65 | 65 |
66 if (!subset) { | 66 if (!subset) { |
67 desc.fWidth = texture->width(); | 67 desc.fWidth = texture->width(); |
68 desc.fHeight = texture->height(); | 68 desc.fHeight = texture->height(); |
69 srcRect = SkIRect::MakeWH(texture->width(), texture->height()); | 69 srcRect = SkIRect::MakeWH(texture->width(), texture->height()); |
70 } else { | 70 } else { |
71 SkASSERT(SkIRect::MakeWH(texture->width(), texture->height()).contains(*
subset)); | 71 SkASSERT(SkIRect::MakeWH(texture->width(), texture->height()).contains(*
subset)); |
72 // Create a new texture that is the size of subset. | 72 // Create a new texture that is the size of subset. |
73 desc.fWidth = subset->width(); | 73 desc.fWidth = subset->width(); |
74 desc.fHeight = subset->height(); | 74 desc.fHeight = subset->height(); |
75 srcRect = *subset; | 75 srcRect = *subset; |
76 } | 76 } |
77 desc.fFlags = kRenderTarget_GrSurfaceFlag; | 77 desc.fFlags = kRenderTarget_GrSurfaceFlag; |
78 desc.fConfig = SkImageInfo2GrPixelConfig(dstCT, kPremul_SkAlphaType, dstPT); | 78 desc.fConfig = SkImageInfo2GrPixelConfig(dstCT, kPremul_SkAlphaType, dstPT); |
79 | 79 |
80 GrTexture* dst = context->textureProvider()->createTexture(desc, false, NULL
, 0); | 80 GrTexture* dst = context->textureProvider()->createTexture(desc, false, null
ptr, 0); |
81 if (NULL == dst) { | 81 if (nullptr == dst) { |
82 return NULL; | 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 flag, but we have | 86 // is a WebGL canvas backing store. We could have a TODO to remove this flus
h flag, but we have |
87 // a larger TODO to remove SkGrPixelRef entirely. | 87 // a larger TODO to remove SkGrPixelRef entirely. |
88 context->copySurface(dst->asRenderTarget(), texture, srcRect, SkIPoint::Make
(0,0), | 88 context->copySurface(dst->asRenderTarget(), texture, srcRect, SkIPoint::Make
(0,0), |
89 GrContext::kFlushWrites_PixelOp); | 89 GrContext::kFlushWrites_PixelOp); |
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 dstPT); | 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 |
103 // the lifetime of this pixel ref. | 103 // the lifetime of this pixel ref. |
104 fSurface = SkSafeRef(surface->asTexture()); | 104 fSurface = SkSafeRef(surface->asTexture()); |
105 if (NULL == fSurface) { | 105 if (nullptr == fSurface) { |
106 fSurface = SkSafeRef(surface); | 106 fSurface = SkSafeRef(surface); |
107 } | 107 } |
108 | 108 |
109 if (fSurface) { | 109 if (fSurface) { |
110 SkASSERT(info.width() <= fSurface->width()); | 110 SkASSERT(info.width() <= fSurface->width()); |
111 SkASSERT(info.height() <= fSurface->height()); | 111 SkASSERT(info.height() <= fSurface->height()); |
112 } | 112 } |
113 } | 113 } |
114 | 114 |
115 SkGrPixelRef::~SkGrPixelRef() { | 115 SkGrPixelRef::~SkGrPixelRef() { |
116 SkSafeUnref(fSurface); | 116 SkSafeUnref(fSurface); |
117 } | 117 } |
118 | 118 |
119 GrTexture* SkGrPixelRef::getTexture() { | 119 GrTexture* SkGrPixelRef::getTexture() { |
120 if (fSurface) { | 120 if (fSurface) { |
121 return fSurface->asTexture(); | 121 return fSurface->asTexture(); |
122 } | 122 } |
123 return NULL; | 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, SkColorProfileType dstPT, | 133 SkPixelRef* SkGrPixelRef::deepCopy(SkColorType dstCT, SkColorProfileType dstPT, |
134 const SkIRect* subset) { | 134 const SkIRect* subset) { |
135 if (NULL == fSurface) { | 135 if (nullptr == fSurface) { |
136 return NULL; | 136 return nullptr; |
137 } | 137 } |
138 | 138 |
139 // Note that when copying a render-target-backed pixel ref, we | 139 // Note that when copying a render-target-backed pixel ref, we |
140 // return a texture-backed pixel ref instead. This is because | 140 // return a texture-backed pixel ref instead. This is because |
141 // render-target pixel refs are usually created in conjunction with | 141 // render-target pixel refs are usually created in conjunction with |
142 // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live | 142 // a GrTexture owned elsewhere (e.g., SkGpuDevice), and cannot live |
143 // independently of that texture. Texture-backed pixel refs, on the other | 143 // independently of that texture. Texture-backed pixel refs, on the other |
144 // hand, own their GrTextures, and are thus self-contained. | 144 // hand, own their GrTextures, and are thus self-contained. |
145 return copy_to_new_texture_pixelref(fSurface->asTexture(), dstCT, dstPT, sub
set); | 145 return copy_to_new_texture_pixelref(fSurface->asTexture(), dstCT, dstPT, sub
set); |
146 } | 146 } |
147 | 147 |
148 static bool tryAllocBitmapPixels(SkBitmap* bitmap) { | 148 static bool tryAllocBitmapPixels(SkBitmap* bitmap) { |
149 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); | 149 SkBitmap::Allocator* allocator = SkBitmapCache::GetAllocator(); |
150 if (NULL != allocator) { | 150 if (nullptr != allocator) { |
151 return allocator->allocPixelRef(bitmap, 0); | 151 return allocator->allocPixelRef(bitmap, 0); |
152 } else { | 152 } else { |
153 // DiscardableMemory is not available, fallback to default allocator | 153 // DiscardableMemory is not available, fallback to default allocator |
154 return bitmap->tryAllocPixels(); | 154 return bitmap->tryAllocPixels(); |
155 } | 155 } |
156 } | 156 } |
157 | 157 |
158 bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { | 158 bool SkGrPixelRef::onReadPixels(SkBitmap* dst, const SkIRect* subset) { |
159 if (NULL == fSurface || fSurface->wasDestroyed()) { | 159 if (nullptr == fSurface || fSurface->wasDestroyed()) { |
160 return false; | 160 return false; |
161 } | 161 } |
162 | 162 |
163 SkIRect bounds; | 163 SkIRect bounds; |
164 if (subset) { | 164 if (subset) { |
165 bounds = *subset; | 165 bounds = *subset; |
166 } else { | 166 } else { |
167 bounds = SkIRect::MakeWH(this->info().width(), this->info().height()); | 167 bounds = SkIRect::MakeWH(this->info().width(), this->info().height()); |
168 } | 168 } |
169 | 169 |
(...skipping 24 matching lines...) Expand all Loading... |
194 cachedBitmap.setImmutable(); | 194 cachedBitmap.setImmutable(); |
195 //Add to the cache | 195 //Add to the cache |
196 SkBitmapCache::Add(this, bounds, cachedBitmap); | 196 SkBitmapCache::Add(this, bounds, cachedBitmap); |
197 | 197 |
198 dst->swap(cachedBitmap); | 198 dst->swap(cachedBitmap); |
199 } | 199 } |
200 | 200 |
201 return true; | 201 return true; |
202 | 202 |
203 } | 203 } |
OLD | NEW |