blob: 9a1f312252284973fba3a0ddb78d75dbb1b16924 [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"
reed@google.com97af1a62012-08-28 12:19:02 +000011#include "SkDevice.h"
reed@google.comc9062042012-07-30 18:06:00 +000012#include "SkMallocPixelRef.h"
13
14static const size_t kIgnoreRowBytesValue = (size_t)~0;
15
16class SkSurface_Raster : public SkSurface_Base {
17public:
mike@reedtribe.orgb9476252012-11-15 02:37:45 +000018 static bool Valid(const SkImage::Info&, size_t rb = kIgnoreRowBytesValue);
reed@google.comc9062042012-07-30 18:06:00 +000019
mike@reedtribe.orgb9476252012-11-15 02:37:45 +000020 SkSurface_Raster(const SkImage::Info&, void*, size_t rb);
21 SkSurface_Raster(const SkImage::Info&, SkPixelRef*, size_t rb);
reed@google.comc9062042012-07-30 18:06:00 +000022
23 virtual SkCanvas* onNewCanvas() SK_OVERRIDE;
mike@reedtribe.orgb9476252012-11-15 02:37:45 +000024 virtual SkSurface* onNewSurface(const SkImage::Info&) SK_OVERRIDE;
junov@chromium.org5ee449a2013-04-12 20:20:50 +000025 virtual SkImage* onNewImageSnapshot() SK_OVERRIDE;
reed@google.comc9062042012-07-30 18:06:00 +000026 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y,
27 const SkPaint*) SK_OVERRIDE;
junov@chromium.orgacea3ef2013-04-16 19:41:09 +000028 virtual void onCopyOnWrite() SK_OVERRIDE;
reed@google.comc9062042012-07-30 18:06:00 +000029
30private:
31 SkBitmap fBitmap;
32 bool fWeOwnThePixels;
33
34 typedef SkSurface_Base INHERITED;
35};
36
37///////////////////////////////////////////////////////////////////////////////
38
mike@reedtribe.orgb9476252012-11-15 02:37:45 +000039bool SkSurface_Raster::Valid(const SkImage::Info& info, size_t rowBytes) {
reed@google.comc9062042012-07-30 18:06:00 +000040 static const size_t kMaxTotalSize = SK_MaxS32;
41
42 bool isOpaque;
43 SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
44
45 int shift = 0;
46 switch (config) {
47 case SkBitmap::kA8_Config:
48 shift = 0;
49 break;
50 case SkBitmap::kRGB_565_Config:
51 shift = 1;
52 break;
53 case SkBitmap::kARGB_8888_Config:
54 shift = 2;
55 break;
56 default:
57 return false;
58 }
59
60 // TODO: examine colorspace
61
62 if (kIgnoreRowBytesValue == rowBytes) {
63 return true;
64 }
65
66 uint64_t minRB = (uint64_t)info.fWidth << 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 = (uint64_t)info.fHeight * rowBytes;
77 if (size > kMaxTotalSize) {
78 return false;
79 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000080
reed@google.comc9062042012-07-30 18:06:00 +000081 return true;
82}
83
mike@reedtribe.orgb9476252012-11-15 02:37:45 +000084SkSurface_Raster::SkSurface_Raster(const SkImage::Info& info, void* pixels, size_t rb)
reed@google.comc9062042012-07-30 18:06:00 +000085 : INHERITED(info.fWidth, info.fHeight) {
86 bool isOpaque;
87 SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000088
reed@google.comc9062042012-07-30 18:06:00 +000089 fBitmap.setConfig(config, info.fWidth, info.fHeight, rb);
90 fBitmap.setPixels(pixels);
91 fBitmap.setIsOpaque(isOpaque);
reed@google.com97af1a62012-08-28 12:19:02 +000092 fWeOwnThePixels = false; // We are "Direct"
reed@google.comc9062042012-07-30 18:06:00 +000093}
94
mike@reedtribe.orgb9476252012-11-15 02:37:45 +000095SkSurface_Raster::SkSurface_Raster(const SkImage::Info& info, SkPixelRef* pr, size_t rb)
reed@google.comc9062042012-07-30 18:06:00 +000096 : INHERITED(info.fWidth, info.fHeight) {
97 bool isOpaque;
98 SkBitmap::Config config = SkImageInfoToBitmapConfig(info, &isOpaque);
99
100 fBitmap.setConfig(config, info.fWidth, info.fHeight, rb);
101 fBitmap.setPixelRef(pr);
102 fBitmap.setIsOpaque(isOpaque);
103 fWeOwnThePixels = true;
104
105 if (!isOpaque) {
junov@google.comdbfac8a2012-12-06 21:47:40 +0000106 fBitmap.eraseColor(SK_ColorTRANSPARENT);
reed@google.comc9062042012-07-30 18:06:00 +0000107 }
108}
109
110SkCanvas* SkSurface_Raster::onNewCanvas() {
111 return SkNEW_ARGS(SkCanvas, (fBitmap));
112}
113
mike@reedtribe.orgb9476252012-11-15 02:37:45 +0000114SkSurface* SkSurface_Raster::onNewSurface(const SkImage::Info& info) {
115 return SkSurface::NewRaster(info);
reed@google.comc9062042012-07-30 18:06:00 +0000116}
117
reed@google.comc9062042012-07-30 18:06:00 +0000118void SkSurface_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
119 const SkPaint* paint) {
120 canvas->drawBitmap(fBitmap, x, y, paint);
121}
122
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000123SkImage* SkSurface_Raster::onNewImageSnapshot() {
reed@google.com97af1a62012-08-28 12:19:02 +0000124 return SkNewImageFromBitmap(fBitmap, fWeOwnThePixels);
125}
126
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000127void SkSurface_Raster::onCopyOnWrite() {
reed@google.com97af1a62012-08-28 12:19:02 +0000128 // are we sharing pixelrefs with the image?
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000129 SkASSERT(NULL !=this->getCachedImage());
130 if (SkBitmapImageGetPixelRef(this->getCachedImage()) == fBitmap.pixelRef()) {
reed@google.com97af1a62012-08-28 12:19:02 +0000131 SkASSERT(fWeOwnThePixels);
132 SkBitmap prev(fBitmap);
133 prev.deepCopyTo(&fBitmap, prev.config());
134 // Now fBitmap is a deep copy of itself (and therefore different from
135 // what is being used by the image. Next we update the canvas to use
136 // this as its backend, so we can't modify the image's pixels anymore.
junov@chromium.orgacea3ef2013-04-16 19:41:09 +0000137 SkASSERT(NULL != this->getCachedCanvas());
138 this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap);
reed@google.com97af1a62012-08-28 12:19:02 +0000139 }
140}
141
reed@google.comc9062042012-07-30 18:06:00 +0000142///////////////////////////////////////////////////////////////////////////////
143
mike@reedtribe.orgb9476252012-11-15 02:37:45 +0000144SkSurface* SkSurface::NewRasterDirect(const SkImage::Info& info, void* pixels, size_t rowBytes) {
145 if (!SkSurface_Raster::Valid(info, rowBytes)) {
reed@google.comc9062042012-07-30 18:06:00 +0000146 return NULL;
147 }
148 if (NULL == pixels) {
149 return NULL;
150 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000151
mike@reedtribe.orgb9476252012-11-15 02:37:45 +0000152 return SkNEW_ARGS(SkSurface_Raster, (info, pixels, rowBytes));
reed@google.comc9062042012-07-30 18:06:00 +0000153}
154
mike@reedtribe.orgb9476252012-11-15 02:37:45 +0000155SkSurface* SkSurface::NewRaster(const SkImage::Info& info) {
156 if (!SkSurface_Raster::Valid(info)) {
reed@google.comc9062042012-07-30 18:06:00 +0000157 return NULL;
158 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000159
reed@google.comc9062042012-07-30 18:06:00 +0000160 static const size_t kMaxTotalSize = SK_MaxS32;
161 size_t rowBytes = SkImageMinRowBytes(info);
162 uint64_t size64 = (uint64_t)info.fHeight * rowBytes;
163 if (size64 > kMaxTotalSize) {
164 return NULL;
165 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000166
reed@google.comc9062042012-07-30 18:06:00 +0000167 size_t size = (size_t)size64;
168 void* pixels = sk_malloc_throw(size);
169 if (NULL == pixels) {
170 return NULL;
171 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000172
reed@google.comc9062042012-07-30 18:06:00 +0000173 SkAutoTUnref<SkPixelRef> pr(SkNEW_ARGS(SkMallocPixelRef, (pixels, size, NULL, true)));
mike@reedtribe.orgb9476252012-11-15 02:37:45 +0000174 return SkNEW_ARGS(SkSurface_Raster, (info, pr, rowBytes));
reed@google.comc9062042012-07-30 18:06:00 +0000175}