blob: 7f2f5c07ef71ed0e14348c72e4c71bb0d46b7fe9 [file] [log] [blame]
reed@google.com58b21ec2012-07-30 18:20:12 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkImage_Base.h"
reed@google.com58b21ec2012-07-30 18:20:12 +00009#include "SkBitmap.h"
10#include "SkCanvas.h"
reed759373a2015-07-03 21:01:10 -070011#include "SkColorTable.h"
reed@google.com58b21ec2012-07-30 18:20:12 +000012#include "SkData.h"
kkinnunen73953e72015-02-23 22:12:12 -080013#include "SkImagePriv.h"
14#include "SkPixelRef.h"
reed4af267b2014-11-21 08:46:37 -080015#include "SkSurface.h"
reed@google.com58b21ec2012-07-30 18:20:12 +000016
reed746f31f2015-09-16 12:53:29 -070017#if SK_SUPPORT_GPU
18#include "GrContext.h"
19#include "SkGr.h"
20#include "SkGrPriv.h"
21#endif
22
reed@google.com58b21ec2012-07-30 18:20:12 +000023class SkImage_Raster : public SkImage_Base {
24public:
halcanaryc56c6ef2015-09-28 11:55:28 -070025 static bool ValidArgs(const Info& info, size_t rowBytes, bool hasColorTable,
reed759373a2015-07-03 21:01:10 -070026 size_t* minSize) {
reed@google.com58b21ec2012-07-30 18:20:12 +000027 const int maxDimension = SK_MaxS32 >> 2;
reed@google.com58b21ec2012-07-30 18:20:12 +000028
reedb2497c22014-12-31 12:31:43 -080029 if (info.width() <= 0 || info.height() <= 0) {
reed@google.com58b21ec2012-07-30 18:20:12 +000030 return false;
31 }
reede5ea5002014-09-03 11:54:58 -070032 if (info.width() > maxDimension || info.height() > maxDimension) {
reed@google.com58b21ec2012-07-30 18:20:12 +000033 return false;
34 }
reede5ea5002014-09-03 11:54:58 -070035 if ((unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType) {
reed@google.com58b21ec2012-07-30 18:20:12 +000036 return false;
37 }
reede5ea5002014-09-03 11:54:58 -070038 if ((unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) {
reed@google.com58b21ec2012-07-30 18:20:12 +000039 return false;
40 }
41
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +000042 if (kUnknown_SkColorType == info.colorType()) {
reed@google.com58b21ec2012-07-30 18:20:12 +000043 return false;
44 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000045
reed759373a2015-07-03 21:01:10 -070046 const bool needsCT = kIndex_8_SkColorType == info.colorType();
halcanaryc56c6ef2015-09-28 11:55:28 -070047 if (needsCT != hasColorTable) {
reed759373a2015-07-03 21:01:10 -070048 return false;
49 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000050
scroggodb7dc092015-12-18 06:56:01 -080051 if (rowBytes < info.minRowBytes()) {
reed@google.com58b21ec2012-07-30 18:20:12 +000052 return false;
53 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000054
reedde499882015-06-18 13:41:40 -070055 size_t size = info.getSafeSize(rowBytes);
56 if (0 == size) {
reed@google.com58b21ec2012-07-30 18:20:12 +000057 return false;
58 }
reedde499882015-06-18 13:41:40 -070059
60 if (minSize) {
61 *minSize = size;
62 }
reed@google.com58b21ec2012-07-30 18:20:12 +000063 return true;
64 }
65
reed7fb4f8b2016-03-11 04:33:52 -080066 SkImage_Raster(const SkImageInfo&, sk_sp<SkData>, size_t rb, SkColorTable*);
reed@google.com58b21ec2012-07-30 18:20:12 +000067 virtual ~SkImage_Raster();
68
reed09553032015-11-23 12:32:16 -080069 bool onReadPixels(const SkImageInfo&, void*, size_t, int srcX, int srcY, CachingHint) const override;
reed6ceeebd2016-03-09 14:26:26 -080070 bool onPeekPixels(SkPixmap*) const override;
reed05dd2512016-01-05 09:16:19 -080071 SkData* onRefEncoded(GrContext*) const override;
reed09553032015-11-23 12:32:16 -080072 bool getROPixels(SkBitmap*, CachingHint) const override;
bsalomonafa95e22015-10-12 10:39:46 -070073 GrTexture* asTextureRef(GrContext*, const GrTextureParams&) const override;
reed7fb4f8b2016-03-11 04:33:52 -080074 sk_sp<SkImage> onMakeSubset(const SkIRect&) const override;
reed@google.com58b21ec2012-07-30 18:20:12 +000075
76 // exposed for SkSurface_Raster via SkNewImageFromPixelRef
reedaf3fbfc2015-10-04 11:28:36 -070077 SkImage_Raster(const SkImageInfo&, SkPixelRef*, const SkIPoint& origin, size_t rowBytes);
reed@google.com58b21ec2012-07-30 18:20:12 +000078
reed@google.com97af1a62012-08-28 12:19:02 +000079 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
80
mtklein36352bf2015-03-25 18:17:31 -070081 bool isOpaque() const override;
reed3c065112015-07-08 12:46:22 -070082 bool onAsLegacyBitmap(SkBitmap*, LegacyBitmapMode) const override;
piotaixrd2a35222014-08-19 14:29:02 -070083
reedaf3fbfc2015-10-04 11:28:36 -070084 SkImage_Raster(const SkBitmap& bm)
85 : INHERITED(bm.width(), bm.height(), bm.getGenerationID())
reed02d91d12015-07-30 20:13:43 -070086 , fBitmap(bm)
87 {
88 if (bm.pixelRef()->isPreLocked()) {
89 // we only preemptively lock if there is no chance of triggering something expensive
90 // like a lazy decode or imagegenerator. PreLocked means it is flat pixels already.
fmalita9a5d1ab2015-07-27 10:27:28 -070091 fBitmap.lockPixels();
92 }
reed26e0e582015-07-29 11:44:52 -070093 SkASSERT(fBitmap.isImmutable());
fmalita9a5d1ab2015-07-27 10:27:28 -070094 }
halcanaryea4673f2014-08-18 08:27:09 -070095
fmalitaddbbdda2015-08-20 08:47:26 -070096 bool onIsLazyGenerated() const override {
97 return fBitmap.pixelRef() && fBitmap.pixelRef()->isLazyGenerated();
98 }
99
reed@google.com58b21ec2012-07-30 18:20:12 +0000100private:
reedaf3fbfc2015-10-04 11:28:36 -0700101 SkBitmap fBitmap;
reed@google.com58b21ec2012-07-30 18:20:12 +0000102
103 typedef SkImage_Base INHERITED;
104};
105
106///////////////////////////////////////////////////////////////////////////////
107
reed@google.com4f7c6152014-02-06 14:11:56 +0000108static void release_data(void* addr, void* context) {
109 SkData* data = static_cast<SkData*>(context);
110 data->unref();
111}
112
reed7fb4f8b2016-03-11 04:33:52 -0800113SkImage_Raster::SkImage_Raster(const Info& info, sk_sp<SkData> data, size_t rowBytes,
reedaf3fbfc2015-10-04 11:28:36 -0700114 SkColorTable* ctable)
115 : INHERITED(info.width(), info.height(), kNeedNewImageUniqueID)
reed@google.com4f7c6152014-02-06 14:11:56 +0000116{
reed@google.com4f7c6152014-02-06 14:11:56 +0000117 void* addr = const_cast<void*>(data->data());
118
reed7fb4f8b2016-03-11 04:33:52 -0800119 fBitmap.installPixels(info, addr, rowBytes, ctable, release_data, data.release());
reed@google.com58b21ec2012-07-30 18:20:12 +0000120 fBitmap.setImmutable();
reed@google.com4f7c6152014-02-06 14:11:56 +0000121 fBitmap.lockPixels();
reed@google.com58b21ec2012-07-30 18:20:12 +0000122}
123
kkinnunen73953e72015-02-23 22:12:12 -0800124SkImage_Raster::SkImage_Raster(const Info& info, SkPixelRef* pr, const SkIPoint& pixelRefOrigin,
reedaf3fbfc2015-10-04 11:28:36 -0700125 size_t rowBytes)
126 : INHERITED(info.width(), info.height(), pr->getGenerationID())
commit-bot@chromium.orgf1901782014-01-08 22:24:55 +0000127{
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +0000128 fBitmap.setInfo(info, rowBytes);
kkinnunen73953e72015-02-23 22:12:12 -0800129 fBitmap.setPixelRef(pr, pixelRefOrigin);
reed@google.com4f7c6152014-02-06 14:11:56 +0000130 fBitmap.lockPixels();
reed26e0e582015-07-29 11:44:52 -0700131 SkASSERT(fBitmap.isImmutable());
reed@google.com58b21ec2012-07-30 18:20:12 +0000132}
133
134SkImage_Raster::~SkImage_Raster() {}
135
reed96472de2014-12-10 09:53:42 -0800136bool SkImage_Raster::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
reed09553032015-11-23 12:32:16 -0800137 int srcX, int srcY, CachingHint) const {
reedb4f629c2014-12-25 13:55:08 -0800138 SkBitmap shallowCopy(fBitmap);
139 return shallowCopy.readPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY);
reed@google.com4f7c6152014-02-06 14:11:56 +0000140}
141
reed6ceeebd2016-03-09 14:26:26 -0800142bool SkImage_Raster::onPeekPixels(SkPixmap* pm) const {
143 return fBitmap.peekPixels(pm);
reed@google.com4f7c6152014-02-06 14:11:56 +0000144}
145
reed05dd2512016-01-05 09:16:19 -0800146SkData* SkImage_Raster::onRefEncoded(GrContext*) const {
reed871872f2015-06-22 12:48:26 -0700147 SkPixelRef* pr = fBitmap.pixelRef();
148 const SkImageInfo prInfo = pr->info();
149 const SkImageInfo bmInfo = fBitmap.info();
150
151 // we only try if we (the image) cover the entire area of the pixelRef
152 if (prInfo.width() == bmInfo.width() && prInfo.height() == bmInfo.height()) {
153 return pr->refEncodedData();
154 }
halcanary96fcdcc2015-08-27 07:41:13 -0700155 return nullptr;
reed871872f2015-06-22 12:48:26 -0700156}
157
reed09553032015-11-23 12:32:16 -0800158bool SkImage_Raster::getROPixels(SkBitmap* dst, CachingHint) const {
reed@google.com4b0757b2013-05-20 16:33:41 +0000159 *dst = fBitmap;
160 return true;
161}
162
bsalomonafa95e22015-10-12 10:39:46 -0700163GrTexture* SkImage_Raster::asTextureRef(GrContext* ctx, const GrTextureParams& params) const {
reed746f31f2015-09-16 12:53:29 -0700164#if SK_SUPPORT_GPU
165 if (!ctx) {
166 return nullptr;
167 }
168
bsalomonafa95e22015-10-12 10:39:46 -0700169 return GrRefCachedBitmapTexture(ctx, fBitmap, params);
reed746f31f2015-09-16 12:53:29 -0700170#endif
171
172 return nullptr;
173}
174
reed7fb4f8b2016-03-11 04:33:52 -0800175sk_sp<SkImage> SkImage_Raster::onMakeSubset(const SkIRect& subset) const {
reed7b6945b2015-09-24 00:50:58 -0700176 // TODO : could consider heurist of sharing pixels, if subset is pretty close to complete
177
178 SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(), fBitmap.alphaType());
179 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info));
180 if (!surface) {
181 return nullptr;
182 }
183 surface->getCanvas()->clear(0);
184 surface->getCanvas()->drawImage(this, SkIntToScalar(-subset.x()), SkIntToScalar(-subset.y()),
185 nullptr);
reed7fb4f8b2016-03-11 04:33:52 -0800186 return sk_sp<SkImage>(surface->newImageSnapshot());
reed7b6945b2015-09-24 00:50:58 -0700187}
188
reed@google.com58b21ec2012-07-30 18:20:12 +0000189///////////////////////////////////////////////////////////////////////////////
190
reed7fb4f8b2016-03-11 04:33:52 -0800191sk_sp<SkImage> SkImage::MakeRasterCopy(const SkPixmap& pmap) {
reedde499882015-06-18 13:41:40 -0700192 size_t size;
reed7fb4f8b2016-03-11 04:33:52 -0800193 if (!SkImage_Raster::ValidArgs(pmap.info(), pmap.rowBytes(),
194 pmap.ctable() != nullptr, &size) || !pmap.addr()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700195 return nullptr;
reed@google.com58b21ec2012-07-30 18:20:12 +0000196 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000197
reed@google.com58b21ec2012-07-30 18:20:12 +0000198 // Here we actually make a copy of the caller's pixel data
reed7fb4f8b2016-03-11 04:33:52 -0800199 sk_sp<SkData> data(SkData::NewWithCopy(pmap.addr(), size));
200 return sk_make_sp<SkImage_Raster>(pmap.info(), std::move(data), pmap.rowBytes(), pmap.ctable());
reed@google.com58b21ec2012-07-30 18:20:12 +0000201}
202
203
reed7fb4f8b2016-03-11 04:33:52 -0800204sk_sp<SkImage> SkImage::MakeRasterData(const SkImageInfo& info, sk_sp<SkData> data,
205 size_t rowBytes) {
reedde499882015-06-18 13:41:40 -0700206 size_t size;
halcanaryc56c6ef2015-09-28 11:55:28 -0700207 if (!SkImage_Raster::ValidArgs(info, rowBytes, false, &size) || !data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700208 return nullptr;
reed@google.com58b21ec2012-07-30 18:20:12 +0000209 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000210
reed@google.com58b21ec2012-07-30 18:20:12 +0000211 // did they give us enough data?
reed@google.com999da9c2014-02-06 13:43:07 +0000212 if (data->size() < size) {
halcanary96fcdcc2015-08-27 07:41:13 -0700213 return nullptr;
reed@google.com58b21ec2012-07-30 18:20:12 +0000214 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000215
halcanary96fcdcc2015-08-27 07:41:13 -0700216 SkColorTable* ctable = nullptr;
reed7fb4f8b2016-03-11 04:33:52 -0800217 return sk_make_sp<SkImage_Raster>(info, std::move(data), rowBytes, ctable);
reed@google.com58b21ec2012-07-30 18:20:12 +0000218}
219
reed7fb4f8b2016-03-11 04:33:52 -0800220sk_sp<SkImage> SkImage::MakeFromRaster(const SkPixmap& pmap, RasterReleaseProc proc,
221 ReleaseContext ctx) {
reedde499882015-06-18 13:41:40 -0700222 size_t size;
reed7fb4f8b2016-03-11 04:33:52 -0800223 if (!SkImage_Raster::ValidArgs(pmap.info(), pmap.rowBytes(), false, &size) || !pmap.addr()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700224 return nullptr;
reedde499882015-06-18 13:41:40 -0700225 }
226
reed7fb4f8b2016-03-11 04:33:52 -0800227 sk_sp<SkData> data(SkData::NewWithProc(pmap.addr(), size, proc, ctx));
228 return sk_make_sp<SkImage_Raster>(pmap.info(), std::move(data), pmap.rowBytes(), pmap.ctable());
reedde499882015-06-18 13:41:40 -0700229}
230
reed7fb4f8b2016-03-11 04:33:52 -0800231sk_sp<SkImage> SkMakeImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr,
232 const SkIPoint& pixelRefOrigin, size_t rowBytes) {
halcanaryc56c6ef2015-09-28 11:55:28 -0700233 if (!SkImage_Raster::ValidArgs(info, rowBytes, false, nullptr)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700234 return nullptr;
reed7983bf92015-01-23 04:24:16 -0800235 }
reed7fb4f8b2016-03-11 04:33:52 -0800236 return sk_make_sp<SkImage_Raster>(info, pr, pixelRefOrigin, rowBytes);
kkinnunen73953e72015-02-23 22:12:12 -0800237}
238
reed7fb4f8b2016-03-11 04:33:52 -0800239sk_sp<SkImage> SkMakeImageFromRasterBitmap(const SkBitmap& bm, ForceCopyMode forceCopy) {
halcanary96fcdcc2015-08-27 07:41:13 -0700240 SkASSERT(nullptr == bm.getTexture());
reed56179002015-07-07 06:11:19 -0700241
halcanaryc56c6ef2015-09-28 11:55:28 -0700242 bool hasColorTable = false;
243 if (kIndex_8_SkColorType == bm.colorType()) {
244 SkAutoLockPixels autoLockPixels(bm);
245 hasColorTable = bm.getColorTable() != nullptr;
246 }
247
248 if (!SkImage_Raster::ValidArgs(bm.info(), bm.rowBytes(), hasColorTable, nullptr)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700249 return nullptr;
kkinnunen73953e72015-02-23 22:12:12 -0800250 }
251
reed7fb4f8b2016-03-11 04:33:52 -0800252 sk_sp<SkImage> image;
reed26e0e582015-07-29 11:44:52 -0700253 if (kYes_ForceCopyMode == forceCopy || !bm.isImmutable()) {
reed56179002015-07-07 06:11:19 -0700254 SkBitmap tmp(bm);
255 tmp.lockPixels();
reed7fb4f8b2016-03-11 04:33:52 -0800256 SkPixmap pmap;
257 if (tmp.getPixels() && tmp.peekPixels(&pmap)) {
258 image = SkImage::MakeRasterCopy(pmap);
kkinnunen73953e72015-02-23 22:12:12 -0800259 }
reed26e0e582015-07-29 11:44:52 -0700260 } else {
reed7fb4f8b2016-03-11 04:33:52 -0800261 image = sk_make_sp<SkImage_Raster>(bm);
kkinnunen73953e72015-02-23 22:12:12 -0800262 }
263 return image;
reed@google.com58b21ec2012-07-30 18:20:12 +0000264}
265
piotaixr65151752014-10-16 11:58:39 -0700266const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* image) {
267 return ((const SkImage_Raster*)image)->getPixelRef();
reed@google.com97af1a62012-08-28 12:19:02 +0000268}
piotaixrd2a35222014-08-19 14:29:02 -0700269
270bool SkImage_Raster::isOpaque() const {
271 return fBitmap.isOpaque();
272}
reed96a857e2015-01-25 10:33:58 -0800273
reed3c065112015-07-08 12:46:22 -0700274bool SkImage_Raster::onAsLegacyBitmap(SkBitmap* bitmap, LegacyBitmapMode mode) const {
275 if (kRO_LegacyBitmapMode == mode) {
276 // When we're a snapshot from a surface, our bitmap may not be marked immutable
277 // even though logically always we are, but in that case we can't physically share our
278 // pixelref since the caller might call setImmutable() themselves
279 // (thus changing our state).
280 if (fBitmap.isImmutable()) {
fmalitaea561bf2015-07-09 17:10:31 -0700281 bitmap->setInfo(fBitmap.info(), fBitmap.rowBytes());
reed3c065112015-07-08 12:46:22 -0700282 bitmap->setPixelRef(fBitmap.pixelRef(), fBitmap.pixelRefOrigin());
283 return true;
284 }
285 }
286 return this->INHERITED::onAsLegacyBitmap(bitmap, mode);
287}