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
8#include "SkSurface_Base.h"
9#include "SkImageInfoPriv.h"
10#include "SkImagePriv.h"
11#include "SkCanvas.h"
12#include "SkDevice.h"
13#include "SkMallocPixelRef.h"
14
15class SkSurface_Raster : public SkSurface_Base {
16public:
17    SkSurface_Raster(const SkImageInfo&, void*, size_t rb,
18                     void (*releaseProc)(void* pixels, void* context), void* context,
19                     const SkSurfaceProps*);
20    SkSurface_Raster(const SkImageInfo& info, sk_sp<SkPixelRef>, const SkSurfaceProps*);
21
22    SkCanvas* onNewCanvas() override;
23    sk_sp<SkSurface> onNewSurface(const SkImageInfo&) override;
24    sk_sp<SkImage> onNewImageSnapshot() override;
25    void onWritePixels(const SkPixmap&, int x, int y) override;
26    void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override;
27    void onCopyOnWrite(ContentChangeMode) override;
28    void onRestoreBackingMutability() override;
29
30private:
31    SkBitmap    fBitmap;
32    size_t      fRowBytes;
33    bool        fWeOwnThePixels;
34
35    typedef SkSurface_Base INHERITED;
36};
37
38///////////////////////////////////////////////////////////////////////////////
39
40bool SkSurfaceValidateRasterInfo(const SkImageInfo& info, size_t rowBytes) {
41    if (!SkImageInfoIsValidCommon(info)) {
42        return false;
43    }
44    if (info.isEmpty()) {
45        return false;
46    }
47
48    static const size_t kMaxTotalSize = SK_MaxS32;
49
50    // TODO(mtklein,brianosman): revisit all these color space decisions
51    switch (info.colorType()) {
52        case kAlpha_8_SkColorType:
53        case kGray_8_SkColorType:
54        case kRGB_565_SkColorType:
55        case kARGB_4444_SkColorType:
56        case kRGB_888x_SkColorType:
57        case kRGBA_1010102_SkColorType:
58        case kRGB_101010x_SkColorType:
59            if (info.colorSpace()) {
60                return false;
61            }
62            break;
63
64        case kRGBA_8888_SkColorType:
65        case kBGRA_8888_SkColorType:
66            if (info.colorSpace() && !info.colorSpace()->gammaCloseToSRGB()) {
67                return false;
68            }
69            break;
70        case kRGBA_F16_SkColorType:
71            if (info.colorSpace() && (!info.colorSpace()->gammaIsLinear())) {
72                return false;
73            }
74            break;
75        default:
76            return false;
77    }
78
79    if (kIgnoreRowBytesValue == rowBytes) {
80        return true;
81    }
82
83    int shift = info.shiftPerPixel();
84
85    uint64_t minRB = (uint64_t)info.width() << shift;
86    if (minRB > rowBytes) {
87        return false;
88    }
89
90    size_t alignedRowBytes = rowBytes >> shift << shift;
91    if (alignedRowBytes != rowBytes) {
92        return false;
93    }
94
95    uint64_t size = sk_64_mul(info.height(), rowBytes);
96    if (size > kMaxTotalSize) {
97        return false;
98    }
99
100    return true;
101}
102
103SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t rb,
104                                   void (*releaseProc)(void* pixels, void* context), void* context,
105                                   const SkSurfaceProps* props)
106    : INHERITED(info, props)
107{
108    fBitmap.installPixels(info, pixels, rb, releaseProc, context);
109    fRowBytes = 0;              // don't need to track the rowbytes
110    fWeOwnThePixels = false;    // We are "Direct"
111}
112
113SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, sk_sp<SkPixelRef> pr,
114                                   const SkSurfaceProps* props)
115    : INHERITED(pr->width(), pr->height(), props)
116{
117    fBitmap.setInfo(info, pr->rowBytes());
118    fRowBytes = pr->rowBytes(); // we track this, so that subsequent re-allocs will match
119    fBitmap.setPixelRef(std::move(pr), 0, 0);
120    fWeOwnThePixels = true;
121}
122
123SkCanvas* SkSurface_Raster::onNewCanvas() { return new SkCanvas(fBitmap, this->props()); }
124
125sk_sp<SkSurface> SkSurface_Raster::onNewSurface(const SkImageInfo& info) {
126    return SkSurface::MakeRaster(info, &this->props());
127}
128
129void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
130                              const SkPaint* paint) {
131    canvas->drawBitmap(fBitmap, x, y, paint);
132}
133
134sk_sp<SkImage> SkSurface_Raster::onNewImageSnapshot() {
135    SkCopyPixelsMode cpm = kIfMutable_SkCopyPixelsMode;
136    if (fWeOwnThePixels) {
137        // SkImage_raster requires these pixels are immutable for its full lifetime.
138        // We'll undo this via onRestoreBackingMutability() if we can avoid the COW.
139        if (SkPixelRef* pr = fBitmap.pixelRef()) {
140            pr->setTemporarilyImmutable();
141        }
142    } else {
143        cpm = kAlways_SkCopyPixelsMode;
144    }
145
146    // Our pixels are in memory, so read access on the snapshot SkImage could be cheap.
147    // Lock the shared pixel ref to ensure peekPixels() is usable.
148    return SkMakeImageFromRasterBitmap(fBitmap, cpm);
149}
150
151void SkSurface_Raster::onWritePixels(const SkPixmap& src, int x, int y) {
152    fBitmap.writePixels(src, x, y);
153}
154
155void SkSurface_Raster::onRestoreBackingMutability() {
156    SkASSERT(!this->hasCachedImage());  // Shouldn't be any snapshots out there.
157    if (SkPixelRef* pr = fBitmap.pixelRef()) {
158        pr->restoreMutability();
159    }
160}
161
162void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) {
163    // are we sharing pixelrefs with the image?
164    sk_sp<SkImage> cached(this->refCachedImage());
165    SkASSERT(cached);
166    if (SkBitmapImageGetPixelRef(cached.get()) == fBitmap.pixelRef()) {
167        SkASSERT(fWeOwnThePixels);
168        if (kDiscard_ContentChangeMode == mode) {
169            fBitmap.allocPixels();
170        } else {
171            SkBitmap prev(fBitmap);
172            fBitmap.allocPixels();
173            SkASSERT(prev.info() == fBitmap.info());
174            SkASSERT(prev.rowBytes() == fBitmap.rowBytes());
175            memcpy(fBitmap.getPixels(), prev.getPixels(), fBitmap.computeByteSize());
176        }
177        SkASSERT(fBitmap.rowBytes() == fRowBytes);  // be sure we always use the same value
178
179        // Now fBitmap is a deep copy of itself (and therefore different from
180        // what is being used by the image. Next we update the canvas to use
181        // this as its backend, so we can't modify the image's pixels anymore.
182        SkASSERT(this->getCachedCanvas());
183        this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap);
184    }
185}
186
187///////////////////////////////////////////////////////////////////////////////
188
189sk_sp<SkSurface> SkSurface::MakeRasterDirectReleaseProc(const SkImageInfo& info, void* pixels,
190        size_t rb, void (*releaseProc)(void* pixels, void* context), void* context,
191        const SkSurfaceProps* props) {
192    if (nullptr == releaseProc) {
193        context = nullptr;
194    }
195    if (!SkSurfaceValidateRasterInfo(info, rb)) {
196        return nullptr;
197    }
198    if (nullptr == pixels) {
199        return nullptr;
200    }
201
202    return sk_make_sp<SkSurface_Raster>(info, pixels, rb, releaseProc, context, props);
203}
204
205sk_sp<SkSurface> SkSurface::MakeRasterDirect(const SkImageInfo& info, void* pixels, size_t rowBytes,
206                                             const SkSurfaceProps* props) {
207    return MakeRasterDirectReleaseProc(info, pixels, rowBytes, nullptr, nullptr, props);
208}
209
210sk_sp<SkSurface> SkSurface::MakeRaster(const SkImageInfo& info, size_t rowBytes,
211                                       const SkSurfaceProps* props) {
212    if (!SkSurfaceValidateRasterInfo(info)) {
213        return nullptr;
214    }
215
216    sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeZeroed(info, rowBytes);
217    if (!pr) {
218        return nullptr;
219    }
220    if (rowBytes) {
221        SkASSERT(pr->rowBytes() == rowBytes);
222    }
223    return sk_make_sp<SkSurface_Raster>(info, std::move(pr), props);
224}
225