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