SkSurface_Raster.cpp revision c8be09aaf2bbfa004c574553fc9d194ac7f1ce1a
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 "SkImagePriv.h"
10#include "SkCanvas.h"
11#include "SkDevice.h"
12#include "SkMallocPixelRef.h"
13
14static const size_t kIgnoreRowBytesValue = (size_t)~0;
15
16class SkSurface_Raster : public SkSurface_Base {
17public:
18    static bool Valid(const SkImageInfo&, size_t rb = kIgnoreRowBytesValue);
19
20    SkSurface_Raster(const SkImageInfo&, void*, size_t rb,
21                     void (*releaseProc)(void* pixels, void* context), void* context,
22                     const SkSurfaceProps*);
23    SkSurface_Raster(SkPixelRef*, const SkSurfaceProps*);
24
25    SkCanvas* onNewCanvas() override;
26    SkSurface* onNewSurface(const SkImageInfo&) override;
27    SkImage* onNewImageSnapshot(Budgeted) override;
28    void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*) override;
29    void onCopyOnWrite(ContentChangeMode) override;
30    void onRestoreBackingMutability() override;
31
32private:
33    SkBitmap    fBitmap;
34    bool        fWeOwnThePixels;
35
36    typedef SkSurface_Base INHERITED;
37};
38
39///////////////////////////////////////////////////////////////////////////////
40
41bool SkSurface_Raster::Valid(const SkImageInfo& info, size_t rowBytes) {
42    if (info.isEmpty()) {
43        return false;
44    }
45
46    static const size_t kMaxTotalSize = SK_MaxS32;
47
48    int shift = 0;
49    switch (info.colorType()) {
50        case kAlpha_8_SkColorType:
51            shift = 0;
52            break;
53        case kRGB_565_SkColorType:
54            shift = 1;
55            break;
56        case kN32_SkColorType:
57            shift = 2;
58            break;
59        default:
60            return false;
61    }
62
63    if (kIgnoreRowBytesValue == rowBytes) {
64        return true;
65    }
66
67    uint64_t minRB = (uint64_t)info.width() << shift;
68    if (minRB > rowBytes) {
69        return false;
70    }
71
72    size_t alignedRowBytes = rowBytes >> shift << shift;
73    if (alignedRowBytes != rowBytes) {
74        return false;
75    }
76
77    uint64_t size = sk_64_mul(info.height(), rowBytes);
78    if (size > kMaxTotalSize) {
79        return false;
80    }
81
82    return true;
83}
84
85SkSurface_Raster::SkSurface_Raster(const SkImageInfo& info, void* pixels, size_t rb,
86                                   void (*releaseProc)(void* pixels, void* context), void* context,
87                                   const SkSurfaceProps* props)
88    : INHERITED(info, props)
89{
90    fBitmap.installPixels(info, pixels, rb, nullptr, releaseProc, context);
91    fWeOwnThePixels = false;    // We are "Direct"
92}
93
94SkSurface_Raster::SkSurface_Raster(SkPixelRef* pr, const SkSurfaceProps* props)
95    : INHERITED(pr->info().width(), pr->info().height(), props)
96{
97    const SkImageInfo& info = pr->info();
98
99    fBitmap.setInfo(info, info.minRowBytes());
100    fBitmap.setPixelRef(pr);
101    fWeOwnThePixels = true;
102}
103
104SkCanvas* SkSurface_Raster::onNewCanvas() { return new SkCanvas(fBitmap, this->props()); }
105
106SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) {
107    return SkSurface::NewRaster(info, &this->props());
108}
109
110void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
111                              const SkPaint* paint) {
112    canvas->drawBitmap(fBitmap, x, y, paint);
113}
114
115SkImage* SkSurface_Raster::onNewImageSnapshot(Budgeted) {
116    if (fWeOwnThePixels) {
117        // SkImage_raster requires these pixels are immutable for its full lifetime.
118        // We'll undo this via onRestoreBackingMutability() if we can avoid the COW.
119        if (SkPixelRef* pr = fBitmap.pixelRef()) {
120            pr->setTemporarilyImmutable();
121        }
122    }
123    // Our pixels are in memory, so read access on the snapshot SkImage could be cheap.
124    // Lock the shared pixel ref to ensure peekPixels() is usable.
125    return SkNewImageFromRasterBitmap(fBitmap,
126                                      fWeOwnThePixels ? kNo_ForceCopyMode : kYes_ForceCopyMode);
127}
128
129void SkSurface_Raster::onRestoreBackingMutability() {
130    SkASSERT(!this->hasCachedImage());  // Shouldn't be any snapshots out there.
131    if (SkPixelRef* pr = fBitmap.pixelRef()) {
132        pr->restoreMutability();
133    }
134}
135
136void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) {
137    // are we sharing pixelrefs with the image?
138    SkASSERT(this->getCachedImage(kNo_Budgeted));
139    if (SkBitmapImageGetPixelRef(this->getCachedImage(kNo_Budgeted)) == fBitmap.pixelRef()) {
140        SkASSERT(fWeOwnThePixels);
141        if (kDiscard_ContentChangeMode == mode) {
142            fBitmap.setPixelRef(nullptr);
143            fBitmap.allocPixels();
144        } else {
145            SkBitmap prev(fBitmap);
146            prev.deepCopyTo(&fBitmap);
147        }
148        // Now fBitmap is a deep copy of itself (and therefore different from
149        // what is being used by the image. Next we update the canvas to use
150        // this as its backend, so we can't modify the image's pixels anymore.
151        SkASSERT(this->getCachedCanvas());
152        this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap);
153    }
154}
155
156///////////////////////////////////////////////////////////////////////////////
157
158SkSurface* SkSurface::NewRasterDirectReleaseProc(const SkImageInfo& info, void* pixels, size_t rb,
159                                                 void (*releaseProc)(void* pixels, void* context),
160                                                 void* context, const SkSurfaceProps* props) {
161    if (nullptr == releaseProc) {
162        context = nullptr;
163    }
164    if (!SkSurface_Raster::Valid(info, rb)) {
165        return nullptr;
166    }
167    if (nullptr == pixels) {
168        return nullptr;
169    }
170
171    return new SkSurface_Raster(info, pixels, rb, releaseProc, context, props);
172}
173
174SkSurface* SkSurface::NewRasterDirect(const SkImageInfo& info, void* pixels, size_t rowBytes,
175                                      const SkSurfaceProps* props) {
176    return NewRasterDirectReleaseProc(info, pixels, rowBytes, nullptr, nullptr, props);
177}
178
179SkSurface* SkSurface::NewRaster(const SkImageInfo& info, const SkSurfaceProps* props) {
180    if (!SkSurface_Raster::Valid(info)) {
181        return nullptr;
182    }
183
184    SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewZeroed(info, 0, nullptr));
185    if (nullptr == pr.get()) {
186        return nullptr;
187    }
188    return new SkSurface_Raster(pr, props);
189}
190