SkSurface_Raster.cpp revision 96fcdcc219d2a0d3579719b84b28bede76efba64
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    if (!info.isOpaque()) {
104        fBitmap.eraseColor(SK_ColorTRANSPARENT);
105    }
106}
107
108SkCanvas* SkSurface_Raster::onNewCanvas() { return new SkCanvas(fBitmap, this->props()); }
109
110SkSurface* SkSurface_Raster::onNewSurface(const SkImageInfo& info) {
111    return SkSurface::NewRaster(info, &this->props());
112}
113
114void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
115                              const SkPaint* paint) {
116    canvas->drawBitmap(fBitmap, x, y, paint);
117}
118
119SkImage* SkSurface_Raster::onNewImageSnapshot(Budgeted) {
120    if (fWeOwnThePixels) {
121        // SkImage_raster requires these pixels are immutable for its full lifetime.
122        // We'll undo this via onRestoreBackingMutability() if we can avoid the COW.
123        if (SkPixelRef* pr = fBitmap.pixelRef()) {
124            pr->setTemporarilyImmutable();
125        }
126    }
127    // Our pixels are in memory, so read access on the snapshot SkImage could be cheap.
128    // Lock the shared pixel ref to ensure peekPixels() is usable.
129    return SkNewImageFromRasterBitmap(fBitmap, &this->props(),
130                                      fWeOwnThePixels ? kNo_ForceCopyMode : kYes_ForceCopyMode);
131}
132
133void SkSurface_Raster::onRestoreBackingMutability() {
134    SkASSERT(!this->hasCachedImage());  // Shouldn't be any snapshots out there.
135    if (SkPixelRef* pr = fBitmap.pixelRef()) {
136        pr->restoreMutability();
137    }
138}
139
140void SkSurface_Raster::onCopyOnWrite(ContentChangeMode mode) {
141    // are we sharing pixelrefs with the image?
142    SkASSERT(this->getCachedImage(kNo_Budgeted));
143    if (SkBitmapImageGetPixelRef(this->getCachedImage(kNo_Budgeted)) == fBitmap.pixelRef()) {
144        SkASSERT(fWeOwnThePixels);
145        if (kDiscard_ContentChangeMode == mode) {
146            fBitmap.setPixelRef(nullptr);
147            fBitmap.allocPixels();
148        } else {
149            SkBitmap prev(fBitmap);
150            prev.deepCopyTo(&fBitmap);
151        }
152        // Now fBitmap is a deep copy of itself (and therefore different from
153        // what is being used by the image. Next we update the canvas to use
154        // this as its backend, so we can't modify the image's pixels anymore.
155        SkASSERT(this->getCachedCanvas());
156        this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap);
157    }
158}
159
160///////////////////////////////////////////////////////////////////////////////
161
162SkSurface* SkSurface::NewRasterDirectReleaseProc(const SkImageInfo& info, void* pixels, size_t rb,
163                                                 void (*releaseProc)(void* pixels, void* context),
164                                                 void* context, const SkSurfaceProps* props) {
165    if (nullptr == releaseProc) {
166        context = nullptr;
167    }
168    if (!SkSurface_Raster::Valid(info, rb)) {
169        return nullptr;
170    }
171    if (nullptr == pixels) {
172        return nullptr;
173    }
174
175    return new SkSurface_Raster(info, pixels, rb, releaseProc, context, props);
176}
177
178SkSurface* SkSurface::NewRasterDirect(const SkImageInfo& info, void* pixels, size_t rowBytes,
179                                      const SkSurfaceProps* props) {
180    return NewRasterDirectReleaseProc(info, pixels, rowBytes, nullptr, nullptr, props);
181}
182
183SkSurface* SkSurface::NewRaster(const SkImageInfo& info, const SkSurfaceProps* props) {
184    if (!SkSurface_Raster::Valid(info)) {
185        return nullptr;
186    }
187
188    SkAutoTUnref<SkPixelRef> pr(SkMallocPixelRef::NewAllocate(info, 0, nullptr));
189    if (nullptr == pr.get()) {
190        return nullptr;
191    }
192    return new SkSurface_Raster(pr, props);
193}
194