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

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

Issue 577063004: Refactoring in SkImage implementations backed by a SkBitmap (Closed) Base URL: https://skia.googlesource.com/skia.git@small_refactor_skimageCodec
Patch Set: Get rid of SkImage_Codec Created 6 years, 3 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
« src/image/SkImage_Gpu.cpp ('K') | « src/image/SkImage_Gpu.cpp ('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 2012 Google Inc. 2 * Copyright 2012 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 "SkImage_Base.h" 8 #include "SkImage_BitmapBase.h"
9 #include "SkImagePriv.h" 9
10 #include "SkBitmap.h" 10 #include "SkBitmap.h"
11 #include "SkCanvas.h"
12 #include "SkData.h" 11 #include "SkData.h"
13 #include "SkDecodingImageGenerator.h" 12 #include "SkDecodingImageGenerator.h"
14 #include "SkMallocPixelRef.h" 13 #include "SkImagePriv.h"
15 14
16 class SkImage_Raster : public SkImage_Base { 15 class SkImage_Raster : public SkImage_BitmapBase {
17 public: 16 public:
18 static bool ValidArgs(const Info& info, size_t rowBytes) { 17 static bool ValidArgs(const Info& info, size_t rowBytes) {
19 const int maxDimension = SK_MaxS32 >> 2; 18 const int maxDimension = SK_MaxS32 >> 2;
20 const size_t kMaxPixelByteSize = SK_MaxS32; 19 const size_t kMaxPixelByteSize = SK_MaxS32;
21 20
22 if (info.width() < 0 || info.height() < 0) { 21 if (info.width() < 0 || info.height() < 0) {
23 return false; 22 return false;
24 } 23 }
25 if (info.width() > maxDimension || info.height() > maxDimension) { 24 if (info.width() > maxDimension || info.height() > maxDimension) {
26 return false; 25 return false;
(...skipping 20 matching lines...) Expand all
47 return false; 46 return false;
48 } 47 }
49 return true; 48 return true;
50 } 49 }
51 50
52 static SkImage* NewEmpty(); 51 static SkImage* NewEmpty();
53 52
54 SkImage_Raster(const SkImageInfo&, SkData*, size_t rb); 53 SkImage_Raster(const SkImageInfo&, SkData*, size_t rb);
55 virtual ~SkImage_Raster(); 54 virtual ~SkImage_Raster();
56 55
57 virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) const SK_ OVERRIDE;
58 virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&,
59 const SkPaint*) const SK_OVERRIDE;
60 virtual bool onReadPixels(SkBitmap*, const SkIRect&) const SK_OVERRIDE; 56 virtual bool onReadPixels(SkBitmap*, const SkIRect&) const SK_OVERRIDE;
61 virtual const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const S K_OVERRIDE; 57 virtual const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const S K_OVERRIDE;
62 virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE; 58 virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE;
63 59
64 // exposed for SkSurface_Raster via SkNewImageFromPixelRef 60 // exposed for SkSurface_Raster via SkNewImageFromPixelRef
65 SkImage_Raster(const SkImageInfo&, SkPixelRef*, size_t rowBytes); 61 SkImage_Raster(const SkImageInfo&, SkPixelRef*, size_t rowBytes);
66 62
67 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } 63 SkPixelRef* getPixelRef() const { return this->getBitmap().pixelRef(); }
68
69 virtual SkShader* onNewShader(SkShader::TileMode,
70 SkShader::TileMode,
71 const SkMatrix* localMatrix) const SK_OVERRIDE ;
72
73 virtual bool isOpaque() const SK_OVERRIDE;
74 64
75 SkImage_Raster(const SkBitmap& bm) 65 SkImage_Raster(const SkBitmap& bm)
76 : INHERITED(bm.width(), bm.height()) 66 : INHERITED(bm) {}
77 , fBitmap(bm) {}
78 67
79 private: 68 private:
80 SkImage_Raster() : INHERITED(0, 0) {} 69 SkImage_Raster() : INHERITED(0, 0) {}
81 70
82 SkBitmap fBitmap; 71 // SkBitmap fBitmap;
83 72
84 typedef SkImage_Base INHERITED; 73 typedef SkImage_BitmapBase INHERITED;
85 }; 74 };
86 75
87 /////////////////////////////////////////////////////////////////////////////// 76 ///////////////////////////////////////////////////////////////////////////////
88 77
89 SkImage* SkImage_Raster::NewEmpty() { 78 SkImage* SkImage_Raster::NewEmpty() {
90 // Returns lazily created singleton 79 // Returns lazily created singleton
91 static SkImage* gEmpty; 80 static SkImage* gEmpty;
92 if (NULL == gEmpty) { 81 if (NULL == gEmpty) {
93 gEmpty = SkNEW(SkImage_Raster); 82 gEmpty = SkNEW(SkImage_Raster);
94 } 83 }
95 gEmpty->ref(); 84 gEmpty->ref();
96 return gEmpty; 85 return gEmpty;
97 } 86 }
98 87
99 static void release_data(void* addr, void* context) { 88 static void release_data(void* addr, void* context) {
100 SkData* data = static_cast<SkData*>(context); 89 SkData* data = static_cast<SkData*>(context);
101 data->unref(); 90 data->unref();
102 } 91 }
103 92
104 SkImage_Raster::SkImage_Raster(const Info& info, SkData* data, size_t rowBytes) 93 SkImage_Raster::SkImage_Raster(const Info& info, SkData* data, size_t rowBytes)
105 : INHERITED(info.width(), info.height()) 94 : INHERITED(info.width(), info.height())
106 { 95 {
107 data->ref(); 96 data->ref();
108 void* addr = const_cast<void*>(data->data()); 97 void* addr = const_cast<void*>(data->data());
109 SkColorTable* ctable = NULL; 98 SkColorTable* ctable = NULL;
110 99
111 fBitmap.installPixels(info, addr, rowBytes, ctable, release_data, data); 100 // Safe (in constructor) but ugly, maybe we should add a constructor with th e same sig. as
112 fBitmap.setImmutable(); 101 // this one in SkImage_BitmapBase ?
113 fBitmap.lockPixels(); 102 SkBitmap* bitmap = const_cast<SkBitmap*>(&this->getBitmap());
103 bitmap->installPixels(info, addr, rowBytes, ctable, release_data, data);
104 bitmap->setImmutable();
105 bitmap->lockPixels();
114 } 106 }
115 107
116 SkImage_Raster::SkImage_Raster(const Info& info, SkPixelRef* pr, size_t rowBytes ) 108 SkImage_Raster::SkImage_Raster(const Info& info, SkPixelRef* pr, size_t rowBytes )
117 : INHERITED(info.width(), info.height()) 109 : INHERITED(info.width(), info.height())
118 { 110 {
119 fBitmap.setInfo(info, rowBytes); 111 // Safe (in constructor) but ugly, maybe we should add a constructor with th e same sig. as
120 fBitmap.setPixelRef(pr); 112 // this one in SkImage_BitmapBase ?
121 fBitmap.lockPixels(); 113 SkBitmap* bitmap = const_cast<SkBitmap*>(&this->getBitmap());
114 bitmap->setInfo(info, rowBytes);
115 bitmap->setPixelRef(pr);
116 bitmap->lockPixels();
122 } 117 }
123 118
124 SkImage_Raster::~SkImage_Raster() {} 119 SkImage_Raster::~SkImage_Raster() {}
125 120
126 SkShader* SkImage_Raster::onNewShader(SkShader::TileMode tileX, SkShader::TileMo de tileY,
127 const SkMatrix* localMatrix) const {
128 return SkShader::CreateBitmapShader(fBitmap, tileX, tileY, localMatrix);
129 }
130
131 void SkImage_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPa int* paint) const {
132 canvas->drawBitmap(fBitmap, x, y, paint);
133 }
134
135 void SkImage_Raster::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
136 const SkPaint* paint) const {
137 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint);
138 }
139
140 bool SkImage_Raster::onReadPixels(SkBitmap* dst, const SkIRect& subset) const { 121 bool SkImage_Raster::onReadPixels(SkBitmap* dst, const SkIRect& subset) const {
141 if (dst->pixelRef()) { 122 if (dst->pixelRef()) {
142 return this->INHERITED::onReadPixels(dst, subset); 123 return this->INHERITED::onReadPixels(dst, subset);
143 } else { 124 } else {
144 SkBitmap src; 125 SkBitmap src;
145 if (!fBitmap.extractSubset(&src, subset)) { 126 if (!this->getBitmap().extractSubset(&src, subset)) {
146 return false; 127 return false;
147 } 128 }
148 return src.copyTo(dst, src.colorType()); 129 return src.copyTo(dst, src.colorType());
149 } 130 }
150 } 131 }
151 132
152 const void* SkImage_Raster::onPeekPixels(SkImageInfo* infoPtr, size_t* rowBytesP tr) const { 133 const void* SkImage_Raster::onPeekPixels(SkImageInfo* infoPtr, size_t* rowBytesP tr) const {
153 const SkImageInfo info = fBitmap.info(); 134 const SkImageInfo info = this->getBitmap().info();
154 if ((kUnknown_SkColorType == info.colorType()) || !fBitmap.getPixels()) { 135 if ((kUnknown_SkColorType == info.colorType()) || !this->getBitmap().getPixe ls()) {
155 return NULL; 136 return NULL;
156 } 137 }
157 *infoPtr = info; 138 *infoPtr = info;
158 *rowBytesPtr = fBitmap.rowBytes(); 139 *rowBytesPtr = this->getBitmap().rowBytes();
159 return fBitmap.getPixels(); 140 return this->getBitmap().getPixels();
160 } 141 }
161 142
162 bool SkImage_Raster::getROPixels(SkBitmap* dst) const { 143 bool SkImage_Raster::getROPixels(SkBitmap* dst) const {
163 *dst = fBitmap; 144 *dst = this->getBitmap();
164 return true; 145 return true;
165 } 146 }
166 147
167 /////////////////////////////////////////////////////////////////////////////// 148 ///////////////////////////////////////////////////////////////////////////////
168 149
169 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, siz e_t rowBytes) { 150 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, siz e_t rowBytes) {
170 if (!SkImage_Raster::ValidArgs(info, rowBytes)) { 151 if (!SkImage_Raster::ValidArgs(info, rowBytes)) {
171 return NULL; 152 return NULL;
172 } 153 }
173 if (0 == info.width() && 0 == info.height()) { 154 if (0 == info.width() && 0 == info.height()) {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 } 195 }
215 196
216 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr, 197 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr,
217 size_t rowBytes) { 198 size_t rowBytes) {
218 return SkNEW_ARGS(SkImage_Raster, (info, pr, rowBytes)); 199 return SkNEW_ARGS(SkImage_Raster, (info, pr, rowBytes));
219 } 200 }
220 201
221 SkPixelRef* SkBitmapImageGetPixelRef(SkImage* image) { 202 SkPixelRef* SkBitmapImageGetPixelRef(SkImage* image) {
222 return ((SkImage_Raster*)image)->getPixelRef(); 203 return ((SkImage_Raster*)image)->getPixelRef();
223 } 204 }
224
225 bool SkImage_Raster::isOpaque() const {
226 return fBitmap.isOpaque();
227 }
OLDNEW
« src/image/SkImage_Gpu.cpp ('K') | « src/image/SkImage_Gpu.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698