Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: src/core/SkMallocPixelRef.cpp

Issue 68973005: Expand pixelref to return SkImageInfo and rowbytes (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: add SK_SUPPORT_LEGACY_ONLOCKPIXELS so we can land in stages for Chrome Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1
2 /* 1 /*
3 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
4 * 3 *
5 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 5 * found in the LICENSE file.
7 */ 6 */
7
8 #include "SkMallocPixelRef.h" 8 #include "SkMallocPixelRef.h"
9 #include "SkBitmap.h" 9 #include "SkBitmap.h"
10 #include "SkFlattenableBuffers.h" 10 #include "SkFlattenableBuffers.h"
11 11
12 SkMallocPixelRef::SkMallocPixelRef(void* storage, size_t size, 12 static bool check_info(const SkImageInfo& info, SkColorTable* ctable) {
13 SkColorTable* ctable, bool ownPixels) { 13 if (info.fWidth < 0 ||
14 if (NULL == storage) { 14 info.fHeight < 0 ||
15 SkASSERT(ownPixels); 15 (unsigned)info.fColorType > (unsigned)kLastEnum_SkColorType ||
16 storage = sk_malloc_throw(size); 16 (unsigned)info.fAlphaType > (unsigned)kLastEnum_SkAlphaType)
17 {
18 return false;
17 } 19 }
20
21 // these seem like good checks, but currently we have (at least) tests
22 // that expect the pixelref to succeed even when there is a mismatch
23 // with colortables. fix?
24 #if 0
25 if (kIndex8_SkColorType == info.fColorType && NULL == ctable) {
26 return false;
27 }
28 if (kIndex8_SkColorType != info.fColorType && NULL != ctable) {
29 return false;
30 }
31 #endif
32 return true;
33 }
34
35 SkMallocPixelRef* SkMallocPixelRef::NewDirect(const SkImageInfo& info,
mtklein 2013/12/04 22:46:00 I would assert that this may be better thought of
reed1 2013/12/05 13:32:39 Agreed. May move this kind of factory out to SkPix
36 void* addr,
37 size_t rowBytes,
38 SkColorTable* ctable) {
39 if (!check_info(info, ctable)) {
40 return NULL;
41 }
42 return SkNEW_ARGS(SkMallocPixelRef, (info, addr, rowBytes, ctable, false));
43 }
44
45 SkMallocPixelRef* SkMallocPixelRef::NewAllocate(const SkImageInfo& info,
46 size_t requestedRowBytes,
47 SkColorTable* ctable) {
48 if (!check_info(info, ctable)) {
49 return NULL;
50 }
51
52 int32_t minRB = info.minRowBytes();
53 if (minRB < 0) {
54 return NULL; // allocation will be too large
55 }
56 if (requestedRowBytes > 0 && (int32_t)requestedRowBytes < minRB) {
57 return NULL; // cannot meet requested rowbytes
58 }
59
60 int32_t rowBytes;
61 if (requestedRowBytes) {
62 rowBytes = requestedRowBytes;
63 } else {
64 rowBytes = minRB;
65 }
66
67 Sk64 bigSize;
68 bigSize.setMul(info.fHeight, rowBytes);
69 if (!bigSize.is32()) {
70 return NULL;
71 }
72
73 size_t size = bigSize.get32();
74 void* addr = sk_malloc_flags(size, 0);
75 if (NULL == addr) {
76 return NULL;
77 }
78
79 return SkNEW_ARGS(SkMallocPixelRef, (info, addr, rowBytes, ctable, true));
80 }
81
82 ///////////////////////////////////////////////////////////////////////////////
83
84 SkMallocPixelRef::SkMallocPixelRef(const SkImageInfo& info, void* storage,
85 size_t rowBytes, SkColorTable* ctable,
86 bool ownsPixels)
87 : fOwnPixels(ownsPixels)
88 {
89 SkASSERT(check_info(info, ctable));
90 SkASSERT(rowBytes >= info.minRowBytes());
91
92 if (kIndex8_SkColorType != info.fColorType) {
93 ctable = NULL;
94 }
95
18 fStorage = storage; 96 fStorage = storage;
19 fSize = size;
20 fCTable = ctable; 97 fCTable = ctable;
98 fRB = rowBytes;
99 fInfo = info;
21 SkSafeRef(ctable); 100 SkSafeRef(ctable);
22 fOwnPixels = ownPixels; 101
23 102 this->setPreLocked(fInfo, fStorage, fRB, fCTable);
24 this->setPreLocked(fStorage, fCTable);
25 } 103 }
26 104
27 SkMallocPixelRef::~SkMallocPixelRef() { 105 SkMallocPixelRef::~SkMallocPixelRef() {
28 SkSafeUnref(fCTable); 106 SkSafeUnref(fCTable);
29 if (fOwnPixels) { 107 if (fOwnPixels) {
30 sk_free(fStorage); 108 sk_free(fStorage);
31 } 109 }
32 } 110 }
33 111
34 void* SkMallocPixelRef::onLockPixels(SkColorTable** ct) { 112 bool SkMallocPixelRef::onGetInfo(SkImageInfo* info) {
35 *ct = fCTable; 113 *info = fInfo;
36 return fStorage; 114 return true;
115 }
116
117 bool SkMallocPixelRef::onNewLockPixels(LockRec* rec) {
118 rec->fPixels = fStorage;
119 rec->fRowBytes = fRB;
120 rec->fColorTable = fCTable;
121 return true;
37 } 122 }
38 123
39 void SkMallocPixelRef::onUnlockPixels() { 124 void SkMallocPixelRef::onUnlockPixels() {
40 // nothing to do 125 // nothing to do
41 } 126 }
42 127
43 void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { 128 void SkMallocPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const {
44 this->INHERITED::flatten(buffer); 129 this->INHERITED::flatten(buffer);
45 130
46 buffer.writeByteArray(fStorage, fSize); 131 fInfo.flatten(buffer);
132 buffer.write32(fRB);
133
134 // TODO: replace this bulk write with a chunky one that can trim off any
135 // trailing bytes on each scanline (in case rowbytes > width*size)
136 size_t size = this->computeMinSize();
137 buffer.writeByteArray(fStorage, size);
47 buffer.writeBool(fCTable != NULL); 138 buffer.writeBool(fCTable != NULL);
48 if (fCTable) { 139 if (fCTable) {
49 fCTable->writeToBuffer(buffer); 140 fCTable->writeToBuffer(buffer);
50 } 141 }
51 } 142 }
52 143
53 SkMallocPixelRef::SkMallocPixelRef(SkFlattenableReadBuffer& buffer) 144 SkMallocPixelRef::SkMallocPixelRef(SkFlattenableReadBuffer& buffer)
54 : INHERITED(buffer, NULL) { 145 : INHERITED(buffer, NULL)
55 fSize = buffer.getArrayCount(); 146 , fOwnPixels(true)
56 fStorage = sk_malloc_throw(fSize); 147 {
57 buffer.readByteArray(fStorage, fSize); 148 fInfo.unflatten(buffer);
149 fRB = buffer.read32();
150 size_t size = this->computeMinSize();
151 fStorage = sk_malloc_throw(size);
152 buffer.readByteArray(fStorage, size);
58 if (buffer.readBool()) { 153 if (buffer.readBool()) {
59 fCTable = SkNEW_ARGS(SkColorTable, (buffer)); 154 fCTable = SkNEW_ARGS(SkColorTable, (buffer));
60 } else { 155 } else {
61 fCTable = NULL; 156 fCTable = NULL;
62 } 157 }
63 fOwnPixels = true;
64 158
65 this->setPreLocked(fStorage, fCTable); 159 this->setPreLocked(fInfo, fStorage, fRB, fCTable);
66 } 160 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698