blob: a0b1e6219a9d8ec883323f66f06d7dccceadf6ae [file] [log] [blame]
reed@google.comc9062042012-07-30 18:06:00 +00001/*
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 "SkMallocPixelRef.h"
12
13static const size_t kIgnoreRowBytesValue = (size_t)~0;
14
15class SkSurface_Raster : public SkSurface_Base {
16public:
17 static bool Valid(const SkImage::Info&, SkColorSpace*, size_t rb = kIgnoreRowBytesValue);
18
19 SkSurface_Raster(const SkImage::Info&, SkColorSpace*, void*, size_t rb);
20 SkSurface_Raster(const SkImage::Info&, SkColorSpace*, SkPixelRef*, size_t rb);
21
22 virtual SkCanvas* onNewCanvas() SK_OVERRIDE;
23 virtual SkSurface* onNewSurface(const SkImage::Info&, SkColorSpace*) SK_OVERRIDE;
24 virtual SkImage* onNewImageShapshot() SK_OVERRIDE;
25 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y,
26 const SkPaint*) SK_OVERRIDE;
27
28private:
29 SkBitmap fBitmap;
30 bool fWeOwnThePixels;
31
32 typedef SkSurface_Base INHERITED;
33};
34
35///////////////////////////////////////////////////////////////////////////////
36
37bool SkSurface_Raster::Valid(const SkImage::Info& info, SkColorSpace* cs,
38 size_t rowBytes) {
39 static const size_t kMaxTotalSize = SK_MaxS32;
40
41 bool isOpaque;
42 SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
43
44 int shift = 0;
45 switch (config) {
46 case SkBitmap::kA8_Config:
47 shift = 0;
48 break;
49 case SkBitmap::kRGB_565_Config:
50 shift = 1;
51 break;
52 case SkBitmap::kARGB_8888_Config:
53 shift = 2;
54 break;
55 default:
56 return false;
57 }
58
59 // TODO: examine colorspace
60
61 if (kIgnoreRowBytesValue == rowBytes) {
62 return true;
63 }
64
65 uint64_t minRB = (uint64_t)info.fWidth << shift;
66 if (minRB > rowBytes) {
67 return false;
68 }
69
70 size_t alignedRowBytes = rowBytes >> shift << shift;
71 if (alignedRowBytes != rowBytes) {
72 return false;
73 }
74
75 uint64_t size = (uint64_t)info.fHeight * rowBytes;
76 if (size > kMaxTotalSize) {
77 return false;
78 }
79
80 return true;
81}
82
83SkSurface_Raster::SkSurface_Raster(const SkImage::Info& info, SkColorSpace* cs,
84 void* pixels, size_t rb)
85 : INHERITED(info.fWidth, info.fHeight) {
86 bool isOpaque;
87 SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
88
89 fBitmap.setConfig(config, info.fWidth, info.fHeight, rb);
90 fBitmap.setPixels(pixels);
91 fBitmap.setIsOpaque(isOpaque);
92 fWeOwnThePixels = false;
93}
94
95SkSurface_Raster::SkSurface_Raster(const SkImage::Info& info, SkColorSpace* cs,
96 SkPixelRef* pr, size_t rb)
97 : INHERITED(info.fWidth, info.fHeight) {
98 bool isOpaque;
99 SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
100
101 fBitmap.setConfig(config, info.fWidth, info.fHeight, rb);
102 fBitmap.setPixelRef(pr);
103 fBitmap.setIsOpaque(isOpaque);
104 fWeOwnThePixels = true;
105
106 if (!isOpaque) {
107 fBitmap.eraseColor(0);
108 }
109}
110
111SkCanvas* SkSurface_Raster::onNewCanvas() {
112 return SkNEW_ARGS(SkCanvas, (fBitmap));
113}
114
115SkSurface* SkSurface_Raster::onNewSurface(const SkImage::Info& info,
116 SkColorSpace* cs) {
117 return SkSurface::NewRaster(info, cs);
118}
119
120SkImage* SkSurface_Raster::onNewImageShapshot() {
121 // if we don't own the pixels, we need to make a deep-copy
122 // if we do, we need to perform a copy-on-write the next time
123 // we draw to this bitmap from our canvas...
124 return SkNewImageFromBitmap(fBitmap);
125}
126
127void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
128 const SkPaint* paint) {
129 canvas->drawBitmap(fBitmap, x, y, paint);
130}
131
132///////////////////////////////////////////////////////////////////////////////
133
134SkSurface* SkSurface::NewRasterDirect(const SkImage::Info& info,
135 SkColorSpace* cs,
136 void* pixels, size_t rowBytes) {
137 if (!SkSurface_Raster::Valid(info, cs, rowBytes)) {
138 return NULL;
139 }
140 if (NULL == pixels) {
141 return NULL;
142 }
143
144 return SkNEW_ARGS(SkSurface_Raster, (info, cs, pixels, rowBytes));
145}
146
147SkSurface* SkSurface::NewRaster(const SkImage::Info& info, SkColorSpace* cs) {
148 if (!SkSurface_Raster::Valid(info, cs)) {
149 return NULL;
150 }
151
152 static const size_t kMaxTotalSize = SK_MaxS32;
153 size_t rowBytes = SkImageMinRowBytes(info);
154 uint64_t size64 = (uint64_t)info.fHeight * rowBytes;
155 if (size64 > kMaxTotalSize) {
156 return NULL;
157 }
158
159 size_t size = (size_t)size64;
160 void* pixels = sk_malloc_throw(size);
161 if (NULL == pixels) {
162 return NULL;
163 }
164
165 SkAutoTUnref<SkPixelRef> pr(SkNEW_ARGS(SkMallocPixelRef, (pixels, size, NULL, true)));
166 return SkNEW_ARGS(SkSurface_Raster, (info, cs, pr, rowBytes));
167}
168