Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(253)

Side by Side Diff: src/image/SkImageShader.cpp

Issue 2239723002: fix memory leak, remake Imageshader to use sk_sp (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/image/SkImageShader.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 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 #include "SkBitmapProcShader.h" 8 #include "SkBitmapProcShader.h"
9 #include "SkBitmapProvider.h" 9 #include "SkBitmapProvider.h"
10 #include "SkColorShader.h" 10 #include "SkColorShader.h"
11 #include "SkColorTable.h" 11 #include "SkColorTable.h"
12 #include "SkEmptyShader.h" 12 #include "SkEmptyShader.h"
13 #include "SkImage_Base.h" 13 #include "SkImage_Base.h"
14 #include "SkImageShader.h" 14 #include "SkImageShader.h"
15 #include "SkReadBuffer.h" 15 #include "SkReadBuffer.h"
16 #include "SkWriteBuffer.h" 16 #include "SkWriteBuffer.h"
17 17
18 SkImageShader::SkImageShader(const SkImage* img, TileMode tmx, TileMode tmy, con st SkMatrix* matrix) 18 SkImageShader::SkImageShader(sk_sp<SkImage> img, TileMode tmx, TileMode tmy, con st SkMatrix* matrix)
19 : INHERITED(matrix) 19 : INHERITED(matrix)
20 , fImage(SkRef(img)) 20 , fImage(std::move(img))
21 , fTileModeX(tmx) 21 , fTileModeX(tmx)
22 , fTileModeY(tmy) 22 , fTileModeY(tmy)
23 {} 23 {}
24 24
25 sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) { 25 sk_sp<SkFlattenable> SkImageShader::CreateProc(SkReadBuffer& buffer) {
26 const TileMode tx = (TileMode)buffer.readUInt(); 26 const TileMode tx = (TileMode)buffer.readUInt();
27 const TileMode ty = (TileMode)buffer.readUInt(); 27 const TileMode ty = (TileMode)buffer.readUInt();
28 SkMatrix matrix; 28 SkMatrix matrix;
29 buffer.readMatrix(&matrix); 29 buffer.readMatrix(&matrix);
30 sk_sp<SkImage> img = buffer.readImage(); 30 sk_sp<SkImage> img = buffer.readImage();
31 if (!img) { 31 if (!img) {
32 return nullptr; 32 return nullptr;
33 } 33 }
34 return SkImageShader::Make(img.release(), tx, ty, &matrix); 34 return SkImageShader::Make(std::move(img), tx, ty, &matrix);
35 } 35 }
36 36
37 void SkImageShader::flatten(SkWriteBuffer& buffer) const { 37 void SkImageShader::flatten(SkWriteBuffer& buffer) const {
38 buffer.writeUInt(fTileModeX); 38 buffer.writeUInt(fTileModeX);
39 buffer.writeUInt(fTileModeY); 39 buffer.writeUInt(fTileModeY);
40 buffer.writeMatrix(this->getLocalMatrix()); 40 buffer.writeMatrix(this->getLocalMatrix());
41 buffer.writeImage(fImage); 41 buffer.writeImage(fImage.get());
42 } 42 }
43 43
44 bool SkImageShader::isOpaque() const { 44 bool SkImageShader::isOpaque() const {
45 return fImage->isOpaque(); 45 return fImage->isOpaque();
46 } 46 }
47 47
48 size_t SkImageShader::onContextSize(const ContextRec& rec) const { 48 size_t SkImageShader::onContextSize(const ContextRec& rec) const {
49 return SkBitmapProcLegacyShader::ContextSize(rec, SkBitmapProvider(fImage).i nfo()); 49 return SkBitmapProcLegacyShader::ContextSize(rec, SkBitmapProvider(fImage.ge t()).info());
50 } 50 }
51 51
52 SkShader::Context* SkImageShader::onCreateContext(const ContextRec& rec, void* s torage) const { 52 SkShader::Context* SkImageShader::onCreateContext(const ContextRec& rec, void* s torage) const {
53 return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY, 53 return SkBitmapProcLegacyShader::MakeContext(*this, fTileModeX, fTileModeY,
54 SkBitmapProvider(fImage), rec, storag e); 54 SkBitmapProvider(fImage.get()), rec, storage);
55 } 55 }
56 56
57 SkImage* SkImageShader::onIsAImage(SkMatrix* texM, TileMode xy[]) const { 57 SkImage* SkImageShader::onIsAImage(SkMatrix* texM, TileMode xy[]) const {
58 if (texM) { 58 if (texM) {
59 *texM = this->getLocalMatrix(); 59 *texM = this->getLocalMatrix();
60 } 60 }
61 if (xy) { 61 if (xy) {
62 xy[0] = (TileMode)fTileModeX; 62 xy[0] = (TileMode)fTileModeX;
63 xy[1] = (TileMode)fTileModeY; 63 xy[1] = (TileMode)fTileModeY;
64 } 64 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 const SkColorTable& ctable = *pmap.ctable(); 122 const SkColorTable& ctable = *pmap.ctable();
123 *color = SkUnPreMultiply::PMColorToColor(ctable[*pmap.addr8(0, 0)]); 123 *color = SkUnPreMultiply::PMColorToColor(ctable[*pmap.addr8(0, 0)]);
124 return true; 124 return true;
125 } 125 }
126 default: // just skip the other configs for now 126 default: // just skip the other configs for now
127 break; 127 break;
128 } 128 }
129 return false; 129 return false;
130 } 130 }
131 131
132 sk_sp<SkShader> SkImageShader::Make(const SkImage* image, TileMode tx, TileMode ty, 132 sk_sp<SkShader> SkImageShader::Make(sk_sp<SkImage> image, TileMode tx, TileMode ty,
133 const SkMatrix* localMatrix, 133 const SkMatrix* localMatrix,
134 SkTBlitterAllocator* allocator) { 134 SkTBlitterAllocator* allocator) {
135 SkShader* shader; 135 SkShader* shader;
136 SkColor color; 136 SkColor color;
137 if (!image || bitmap_is_too_big(image->width(), image->height())) { 137 if (!image || bitmap_is_too_big(image->width(), image->height())) {
138 if (nullptr == allocator) { 138 if (nullptr == allocator) {
139 shader = new SkEmptyShader; 139 shader = new SkEmptyShader;
140 } else { 140 } else {
141 shader = allocator->createT<SkEmptyShader>(); 141 shader = allocator->createT<SkEmptyShader>();
142 } 142 }
143 } else if (can_use_color_shader(image, &color)) { 143 } else if (can_use_color_shader(image.get(), &color)) {
144 if (nullptr == allocator) { 144 if (nullptr == allocator) {
145 shader = new SkColorShader(color); 145 shader = new SkColorShader(color);
146 } else { 146 } else {
147 shader = allocator->createT<SkColorShader>(color); 147 shader = allocator->createT<SkColorShader>(color);
148 } 148 }
149 } else { 149 } else {
150 if (nullptr == allocator) { 150 if (nullptr == allocator) {
151 shader = new SkImageShader(image, tx, ty, localMatrix); 151 shader = new SkImageShader(image, tx, ty, localMatrix);
152 } else { 152 } else {
153 shader = allocator->createT<SkImageShader>(image, tx, ty, localMatri x); 153 shader = allocator->createT<SkImageShader>(image, tx, ty, localMatri x);
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 234
235 sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkShader::TileMode tmx, 235 sk_sp<SkShader> SkMakeBitmapShader(const SkBitmap& src, SkShader::TileMode tmx,
236 SkShader::TileMode tmy, const SkMatrix* local Matrix, 236 SkShader::TileMode tmy, const SkMatrix* local Matrix,
237 SkCopyPixelsMode cpm, SkTBlitterAllocator* al locator) { 237 SkCopyPixelsMode cpm, SkTBlitterAllocator* al locator) {
238 // Until we learn otherwise, it seems that any caller that is passing an all ocator must be 238 // Until we learn otherwise, it seems that any caller that is passing an all ocator must be
239 // assuming that the returned shader will have a stack-frame lifetime, so we assert that 239 // assuming that the returned shader will have a stack-frame lifetime, so we assert that
240 // they are also asking for kNever_SkCopyPixelsMode. If that proves otherwis e, we can remove 240 // they are also asking for kNever_SkCopyPixelsMode. If that proves otherwis e, we can remove
241 // or modify this assert. 241 // or modify this assert.
242 SkASSERT(!allocator || (kNever_SkCopyPixelsMode == cpm)); 242 SkASSERT(!allocator || (kNever_SkCopyPixelsMode == cpm));
243 243
244 return SkImageShader::Make(SkMakeImageFromRasterBitmap(src, cpm, allocator). get(), 244 return SkImageShader::Make(SkMakeImageFromRasterBitmap(src, cpm, allocator),
245 tmx, tmy, localMatrix, allocator); 245 tmx, tmy, localMatrix, allocator);
246 } 246 }
247 247
248 static sk_sp<SkFlattenable> SkBitmapProcShader_CreateProc(SkReadBuffer& buffer) { 248 static sk_sp<SkFlattenable> SkBitmapProcShader_CreateProc(SkReadBuffer& buffer) {
249 SkMatrix lm; 249 SkMatrix lm;
250 buffer.readMatrix(&lm); 250 buffer.readMatrix(&lm);
251 sk_sp<SkImage> image = buffer.readBitmapAsImage(); 251 sk_sp<SkImage> image = buffer.readBitmapAsImage();
252 SkShader::TileMode mx = (SkShader::TileMode)buffer.readUInt(); 252 SkShader::TileMode mx = (SkShader::TileMode)buffer.readUInt();
253 SkShader::TileMode my = (SkShader::TileMode)buffer.readUInt(); 253 SkShader::TileMode my = (SkShader::TileMode)buffer.readUInt();
254 return image ? image->makeShader(mx, my, &lm) : nullptr; 254 return image ? image->makeShader(mx, my, &lm) : nullptr;
255 } 255 }
256 256
257 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShader) 257 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShader)
258 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkImageShader) 258 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkImageShader)
259 SkFlattenable::Register("SkBitmapProcShader", SkBitmapProcShader_CreateProc, kSk Shader_Type); 259 SkFlattenable::Register("SkBitmapProcShader", SkBitmapProcShader_CreateProc, kSk Shader_Type);
260 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 260 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
261 261
OLDNEW
« no previous file with comments | « src/image/SkImageShader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698