1 /*
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 #include "src/image/SkImage_Raster.h"
8
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkPixelRef.h"
15 #include "include/core/SkPixmap.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkSize.h"
20 #include "include/core/SkTypes.h"
21 #include "src/base/SkRectMemcpy.h"
22 #include "src/core/SkImageInfoPriv.h"
23 #include "src/core/SkImagePriv.h"
24 #include "src/image/SkImage_Base.h"
25
26 #include <cstddef>
27 #include <cstdint>
28 #include <utility>
29
30 class GrDirectContext;
31
32 // fixes https://bug.skia.org/5096
is_not_subset(const SkBitmap & bm)33 static bool is_not_subset(const SkBitmap& bm) {
34 SkASSERT(bm.pixelRef());
35 SkISize dim = SkISize::Make(bm.pixelRef()->width(), bm.pixelRef()->height());
36 SkASSERT(dim != bm.dimensions() || bm.pixelRefOrigin().isZero());
37 return dim == bm.dimensions();
38 }
39
release_data(void * addr,void * context)40 static void release_data(void* addr, void* context) {
41 SkData* data = static_cast<SkData*>(context);
42 data->unref();
43 }
44
SkImage_Raster(const SkImageInfo & info,sk_sp<SkData> data,size_t rowBytes,uint32_t id)45 SkImage_Raster::SkImage_Raster(const SkImageInfo& info, sk_sp<SkData> data, size_t rowBytes,
46 uint32_t id)
47 : SkImage_Base(info, id) {
48 void* addr = const_cast<void*>(data->data());
49
50 fBitmap.installPixels(info, addr, rowBytes, release_data, data.release());
51 fBitmap.setImmutable();
52 }
53
SkImage_Raster(const SkBitmap & bm,bool bitmapMayBeMutable)54 SkImage_Raster::SkImage_Raster(const SkBitmap& bm, bool bitmapMayBeMutable)
55 : SkImage_Base(bm.info(),
56 is_not_subset(bm) ? bm.getGenerationID() : (uint32_t)kNeedNewImageUniqueID)
57 , fBitmap(bm) {
58 SkASSERT(bitmapMayBeMutable || fBitmap.isImmutable());
59 }
60
~SkImage_Raster()61 SkImage_Raster::~SkImage_Raster() {}
62
onReadPixels(GrDirectContext *,const SkImageInfo & dstInfo,void * dstPixels,size_t dstRowBytes,int srcX,int srcY,CachingHint) const63 bool SkImage_Raster::onReadPixels(GrDirectContext*,
64 const SkImageInfo& dstInfo,
65 void* dstPixels,
66 size_t dstRowBytes,
67 int srcX,
68 int srcY,
69 CachingHint) const {
70 SkBitmap shallowCopy(fBitmap);
71 return shallowCopy.readPixels(dstInfo, dstPixels, dstRowBytes, srcX, srcY);
72 }
73
onPeekPixels(SkPixmap * pm) const74 bool SkImage_Raster::onPeekPixels(SkPixmap* pm) const {
75 return fBitmap.peekPixels(pm);
76 }
77
getROPixels(GrDirectContext *,SkBitmap * dst,CachingHint) const78 bool SkImage_Raster::getROPixels(GrDirectContext*, SkBitmap* dst, CachingHint) const {
79 *dst = fBitmap;
80 return true;
81 }
82
copy_bitmap_subset(const SkBitmap & orig,const SkIRect & subset)83 static SkBitmap copy_bitmap_subset(const SkBitmap& orig, const SkIRect& subset) {
84 SkImageInfo info = orig.info().makeDimensions(subset.size());
85 SkBitmap bitmap;
86 if (!bitmap.tryAllocPixels(info)) {
87 return {};
88 }
89
90 void* dst = bitmap.getPixels();
91 void* src = orig.getAddr(subset.x(), subset.y());
92 if (!dst || !src) {
93 SkDEBUGFAIL("SkImage_Raster::onMakeSubset with nullptr src or dst");
94 return {};
95 }
96
97 SkRectMemcpy(dst, bitmap.rowBytes(), src, orig.rowBytes(), bitmap.rowBytes(),
98 subset.height());
99
100 bitmap.setImmutable();
101 return bitmap;
102 }
103
onMakeSubset(GrDirectContext *,const SkIRect & subset) const104 sk_sp<SkImage> SkImage_Raster::onMakeSubset(GrDirectContext*, const SkIRect& subset) const {
105 SkBitmap copy = copy_bitmap_subset(fBitmap, subset);
106 if (copy.isNull()) {
107 return nullptr;
108 } else {
109 return copy.asImage();
110 }
111 }
112
copy_mipmaps(const SkBitmap & src,SkMipmap * srcMips)113 static sk_sp<SkMipmap> copy_mipmaps(const SkBitmap& src, SkMipmap* srcMips) {
114 if (!srcMips) {
115 return nullptr;
116 }
117
118 sk_sp<SkMipmap> dst;
119 dst.reset(SkMipmap::Build(src.pixmap(),
120 /* factoryProc= */ nullptr,
121 /* computeContents= */ false));
122 if (!dst) {
123 return nullptr;
124 }
125 for (int i = 0; i < dst->countLevels(); ++i) {
126 SkMipmap::Level srcLevel, dstLevel;
127 srcMips->getLevel(i, &srcLevel);
128 dst->getLevel(i, &dstLevel);
129 srcLevel.fPixmap.readPixels(dstLevel.fPixmap);
130 }
131
132 return dst;
133 }
134
onMakeSubset(skgpu::graphite::Recorder *,const SkIRect & subset,RequiredProperties requiredProperties) const135 sk_sp<SkImage> SkImage_Raster::onMakeSubset(skgpu::graphite::Recorder*,
136 const SkIRect& subset,
137 RequiredProperties requiredProperties) const {
138 sk_sp<SkImage> img;
139
140 if (requiredProperties.fMipmapped) {
141 bool fullCopy = subset == SkIRect::MakeSize(fBitmap.dimensions());
142
143 sk_sp<SkMipmap> mips = fullCopy ? copy_mipmaps(fBitmap, fBitmap.fMips.get()) : nullptr;
144
145 // SkImage::withMipmaps will always make a copy for us so we can temporarily share
146 // the pixel ref with fBitmap
147 SkBitmap tmpSubset;
148 if (!fBitmap.extractSubset(&tmpSubset, subset)) {
149 return nullptr;
150 }
151
152 sk_sp<SkImage> tmp(new SkImage_Raster(tmpSubset, /* bitmapMayBeMutable= */ true));
153
154 // withMipmaps will auto generate the mipmaps if a nullptr is passed in
155 SkASSERT(!mips || mips->validForRootLevel(tmp->imageInfo()));
156 img = tmp->withMipmaps(std::move(mips));
157 } else {
158 SkBitmap copy = copy_bitmap_subset(fBitmap, subset);
159 if (!copy.isNull()) {
160 img = copy.asImage();
161 }
162 }
163
164 return img;
165 }
166
167 ///////////////////////////////////////////////////////////////////////////////
168
SkMakeImageFromRasterBitmapPriv(const SkBitmap & bm,SkCopyPixelsMode cpm,uint32_t idForCopy)169 sk_sp<SkImage> SkMakeImageFromRasterBitmapPriv(const SkBitmap& bm, SkCopyPixelsMode cpm,
170 uint32_t idForCopy) {
171 if (kAlways_SkCopyPixelsMode == cpm || (!bm.isImmutable() && kNever_SkCopyPixelsMode != cpm)) {
172 SkPixmap pmap;
173 if (bm.peekPixels(&pmap)) {
174 return MakeRasterCopyPriv(pmap, idForCopy);
175 } else {
176 return sk_sp<SkImage>();
177 }
178 }
179 return sk_make_sp<SkImage_Raster>(bm, kNever_SkCopyPixelsMode == cpm);
180 }
181
SkMakeImageFromRasterBitmap(const SkBitmap & bm,SkCopyPixelsMode cpm)182 sk_sp<SkImage> SkMakeImageFromRasterBitmap(const SkBitmap& bm, SkCopyPixelsMode cpm) {
183 if (!SkImageInfoIsValid(bm.info()) || bm.rowBytes() < bm.info().minRowBytes()) {
184 return nullptr;
185 }
186
187 return SkMakeImageFromRasterBitmapPriv(bm, cpm, kNeedNewImageUniqueID);
188 }
189
SkBitmapImageGetPixelRef(const SkImage * image)190 const SkPixelRef* SkBitmapImageGetPixelRef(const SkImage* image) {
191 return ((const SkImage_Raster*)image)->getPixelRef();
192 }
193
onAsLegacyBitmap(GrDirectContext *,SkBitmap * bitmap) const194 bool SkImage_Raster::onAsLegacyBitmap(GrDirectContext*, SkBitmap* bitmap) const {
195 // When we're a snapshot from a surface, our bitmap may not be marked immutable
196 // even though logically always we are, but in that case we can't physically share our
197 // pixelref since the caller might call setImmutable() themselves
198 // (thus changing our state).
199 if (fBitmap.isImmutable()) {
200 SkIPoint origin = fBitmap.pixelRefOrigin();
201 bitmap->setInfo(fBitmap.info(), fBitmap.rowBytes());
202 bitmap->setPixelRef(sk_ref_sp(fBitmap.pixelRef()), origin.x(), origin.y());
203 return true;
204 }
205 return this->SkImage_Base::onAsLegacyBitmap(nullptr, bitmap);
206 }
207
208 ///////////////////////////////////////////////////////////////////////////////
209
onMakeColorTypeAndColorSpace(SkColorType targetCT,sk_sp<SkColorSpace> targetCS,GrDirectContext *) const210 sk_sp<SkImage> SkImage_Raster::onMakeColorTypeAndColorSpace(SkColorType targetCT,
211 sk_sp<SkColorSpace> targetCS,
212 GrDirectContext*) const {
213 SkPixmap src;
214 SkAssertResult(fBitmap.peekPixels(&src));
215
216 SkBitmap dst;
217 if (!dst.tryAllocPixels(fBitmap.info().makeColorType(targetCT).makeColorSpace(targetCS))) {
218 return nullptr;
219 }
220
221 SkAssertResult(dst.writePixels(src));
222 dst.setImmutable();
223 return dst.asImage();
224 }
225
onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const226 sk_sp<SkImage> SkImage_Raster::onReinterpretColorSpace(sk_sp<SkColorSpace> newCS) const {
227 // TODO: If our bitmap is immutable, then we could theoretically create another image sharing
228 // our pixelRef. That doesn't work (without more invasive logic), because the image gets its
229 // gen ID from the bitmap, which gets it from the pixelRef.
230 SkPixmap pixmap = fBitmap.pixmap();
231 pixmap.setColorSpace(std::move(newCS));
232 return SkImages::RasterFromPixmapCopy(pixmap);
233 }
234