blob: 7828baf82e1ae6c231156c3d9d1c747dfd0737c3 [file] [log] [blame]
Jason Samseb4fe182011-05-26 16:33:01 -07001/*
Jason Samsbc0ca6b2013-02-15 18:13:43 -08002 * Copyright (C) 2013 The Android Open Source Project
Jason Samseb4fe182011-05-26 16:33:01 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18#include "rsdCore.h"
Jason Samseb4fe182011-05-26 16:33:01 -070019#include "rsdAllocation.h"
20
21#include "rsAllocation.h"
22
Jason Sams7ac2a4d2012-02-15 12:04:24 -080023#include "system/window.h"
Jason Sams7ac2a4d2012-02-15 12:04:24 -080024#include "ui/Rect.h"
25#include "ui/GraphicBufferMapper.h"
Jason Sams93eacc72012-12-18 14:26:57 -080026
27#ifndef RS_COMPATIBILITY_LIB
28#include "rsdFrameBufferObj.h"
Andy McFadden58fd6a52012-12-18 09:50:03 -080029#include "gui/GLConsumer.h"
Jason Sams93eacc72012-12-18 14:26:57 -080030#include "hardware/gralloc.h"
Jason Sams7ac2a4d2012-02-15 12:04:24 -080031
Jason Samseb4fe182011-05-26 16:33:01 -070032#include <GLES/gl.h>
33#include <GLES2/gl2.h>
34#include <GLES/glext.h>
Jason Sams93eacc72012-12-18 14:26:57 -080035#endif
Jason Samseb4fe182011-05-26 16:33:01 -070036
37using namespace android;
38using namespace android::renderscript;
39
40
Jason Sams93eacc72012-12-18 14:26:57 -080041#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -070042const static GLenum gFaceOrder[] = {
43 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
44 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
45 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
46 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
47 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
48 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
49};
50
Jason Samsa614ae12011-05-26 17:05:51 -070051GLenum rsdTypeToGLType(RsDataType t) {
52 switch (t) {
53 case RS_TYPE_UNSIGNED_5_6_5: return GL_UNSIGNED_SHORT_5_6_5;
54 case RS_TYPE_UNSIGNED_5_5_5_1: return GL_UNSIGNED_SHORT_5_5_5_1;
55 case RS_TYPE_UNSIGNED_4_4_4_4: return GL_UNSIGNED_SHORT_4_4_4_4;
56
57 //case RS_TYPE_FLOAT_16: return GL_HALF_FLOAT;
58 case RS_TYPE_FLOAT_32: return GL_FLOAT;
59 case RS_TYPE_UNSIGNED_8: return GL_UNSIGNED_BYTE;
60 case RS_TYPE_UNSIGNED_16: return GL_UNSIGNED_SHORT;
61 case RS_TYPE_SIGNED_8: return GL_BYTE;
62 case RS_TYPE_SIGNED_16: return GL_SHORT;
63 default: break;
64 }
65 return 0;
66}
67
68GLenum rsdKindToGLFormat(RsDataKind k) {
69 switch (k) {
70 case RS_KIND_PIXEL_L: return GL_LUMINANCE;
71 case RS_KIND_PIXEL_A: return GL_ALPHA;
72 case RS_KIND_PIXEL_LA: return GL_LUMINANCE_ALPHA;
73 case RS_KIND_PIXEL_RGB: return GL_RGB;
74 case RS_KIND_PIXEL_RGBA: return GL_RGBA;
75 case RS_KIND_PIXEL_DEPTH: return GL_DEPTH_COMPONENT16;
76 default: break;
77 }
78 return 0;
79}
Jason Sams93eacc72012-12-18 14:26:57 -080080#endif
Jason Samsa614ae12011-05-26 17:05:51 -070081
Jason Sams807fdc42012-07-25 17:55:39 -070082uint8_t *GetOffsetPtr(const android::renderscript::Allocation *alloc,
83 uint32_t xoff, uint32_t yoff, uint32_t lod,
84 RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -080085 uint8_t *ptr = (uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr;
86 ptr += face * alloc->mHal.drvState.faceOffset;
87 ptr += yoff * alloc->mHal.drvState.lod[lod].stride;
Jason Sams807fdc42012-07-25 17:55:39 -070088 ptr += xoff * alloc->mHal.state.elementSizeBytes;
89 return ptr;
90}
91
Jason Samsa614ae12011-05-26 17:05:51 -070092
Jason Sams2382aba2011-09-13 15:41:01 -070093static void Update2DTexture(const Context *rsc, const Allocation *alloc, const void *ptr,
94 uint32_t xoff, uint32_t yoff, uint32_t lod,
95 RsAllocationCubemapFace face, uint32_t w, uint32_t h) {
Jason Sams93eacc72012-12-18 14:26:57 -080096#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -070097 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
98
Jason Samseb4fe182011-05-26 16:33:01 -070099 rsAssert(drv->textureID);
Jason Sams2382aba2011-09-13 15:41:01 -0700100 RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
101 RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
Jason Samseb4fe182011-05-26 16:33:01 -0700102 GLenum t = GL_TEXTURE_2D;
103 if (alloc->mHal.state.hasFaces) {
104 t = gFaceOrder[face];
105 }
Jason Sams2382aba2011-09-13 15:41:01 -0700106 RSD_CALL_GL(glTexSubImage2D, t, lod, xoff, yoff, w, h, drv->glFormat, drv->glType, ptr);
Jason Sams93eacc72012-12-18 14:26:57 -0800107#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700108}
109
110
Jason Sams93eacc72012-12-18 14:26:57 -0800111#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700112static void Upload2DTexture(const Context *rsc, const Allocation *alloc, bool isFirstUpload) {
113 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
114
Jason Sams2382aba2011-09-13 15:41:01 -0700115 RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
116 RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
Jason Samseb4fe182011-05-26 16:33:01 -0700117
118 uint32_t faceCount = 1;
119 if (alloc->mHal.state.hasFaces) {
120 faceCount = 6;
121 }
122
123 rsdGLCheckError(rsc, "Upload2DTexture 1 ");
124 for (uint32_t face = 0; face < faceCount; face ++) {
125 for (uint32_t lod = 0; lod < alloc->mHal.state.type->getLODCount(); lod++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700126 const uint8_t *p = GetOffsetPtr(alloc, 0, 0, lod, (RsAllocationCubemapFace)face);
Jason Samseb4fe182011-05-26 16:33:01 -0700127
128 GLenum t = GL_TEXTURE_2D;
129 if (alloc->mHal.state.hasFaces) {
130 t = gFaceOrder[face];
131 }
132
133 if (isFirstUpload) {
Jason Sams2382aba2011-09-13 15:41:01 -0700134 RSD_CALL_GL(glTexImage2D, t, lod, drv->glFormat,
Jason Samseb4fe182011-05-26 16:33:01 -0700135 alloc->mHal.state.type->getLODDimX(lod),
136 alloc->mHal.state.type->getLODDimY(lod),
Jason Samsa614ae12011-05-26 17:05:51 -0700137 0, drv->glFormat, drv->glType, p);
Jason Samseb4fe182011-05-26 16:33:01 -0700138 } else {
Jason Sams2382aba2011-09-13 15:41:01 -0700139 RSD_CALL_GL(glTexSubImage2D, t, lod, 0, 0,
Jason Samseb4fe182011-05-26 16:33:01 -0700140 alloc->mHal.state.type->getLODDimX(lod),
141 alloc->mHal.state.type->getLODDimY(lod),
Jason Samsa614ae12011-05-26 17:05:51 -0700142 drv->glFormat, drv->glType, p);
Jason Samseb4fe182011-05-26 16:33:01 -0700143 }
144 }
145 }
146
147 if (alloc->mHal.state.mipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
Jason Sams2382aba2011-09-13 15:41:01 -0700148 RSD_CALL_GL(glGenerateMipmap, drv->glTarget);
Jason Samseb4fe182011-05-26 16:33:01 -0700149 }
150 rsdGLCheckError(rsc, "Upload2DTexture");
151}
Jason Sams93eacc72012-12-18 14:26:57 -0800152#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700153
154static void UploadToTexture(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800155#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700156 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
157
Jason Sams3522f402012-03-23 11:47:26 -0700158 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
Jason Sams41e373d2012-01-13 14:01:20 -0800159 if (!drv->textureID) {
160 RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
161 }
162 return;
163 }
164
Jason Samsa614ae12011-05-26 17:05:51 -0700165 if (!drv->glType || !drv->glFormat) {
Jason Samseb4fe182011-05-26 16:33:01 -0700166 return;
167 }
168
Jason Sams709a0972012-11-15 18:18:04 -0800169 if (!alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Samseb4fe182011-05-26 16:33:01 -0700170 return;
171 }
172
173 bool isFirstUpload = false;
174
175 if (!drv->textureID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700176 RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
Jason Samseb4fe182011-05-26 16:33:01 -0700177 isFirstUpload = true;
178 }
179
180 Upload2DTexture(rsc, alloc, isFirstUpload);
181
182 if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
Jason Sams709a0972012-11-15 18:18:04 -0800183 if (alloc->mHal.drvState.lod[0].mallocPtr) {
184 free(alloc->mHal.drvState.lod[0].mallocPtr);
185 alloc->mHal.drvState.lod[0].mallocPtr = NULL;
Jason Samseb4fe182011-05-26 16:33:01 -0700186 }
187 }
188 rsdGLCheckError(rsc, "UploadToTexture");
Jason Sams93eacc72012-12-18 14:26:57 -0800189#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700190}
191
192static void AllocateRenderTarget(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800193#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700194 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
195
Jason Samsa614ae12011-05-26 17:05:51 -0700196 if (!drv->glFormat) {
Jason Samseb4fe182011-05-26 16:33:01 -0700197 return;
198 }
199
200 if (!drv->renderTargetID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700201 RSD_CALL_GL(glGenRenderbuffers, 1, &drv->renderTargetID);
Jason Samseb4fe182011-05-26 16:33:01 -0700202
203 if (!drv->renderTargetID) {
204 // This should generally not happen
Steve Blockaf12ac62012-01-06 19:20:56 +0000205 ALOGE("allocateRenderTarget failed to gen mRenderTargetID");
Jason Samseb4fe182011-05-26 16:33:01 -0700206 rsc->dumpDebug();
207 return;
208 }
Jason Sams2382aba2011-09-13 15:41:01 -0700209 RSD_CALL_GL(glBindRenderbuffer, GL_RENDERBUFFER, drv->renderTargetID);
210 RSD_CALL_GL(glRenderbufferStorage, GL_RENDERBUFFER, drv->glFormat,
Jason Samsa572aca2013-01-09 11:52:26 -0800211 alloc->mHal.drvState.lod[0].dimX, alloc->mHal.drvState.lod[0].dimY);
Jason Samseb4fe182011-05-26 16:33:01 -0700212 }
213 rsdGLCheckError(rsc, "AllocateRenderTarget");
Jason Sams93eacc72012-12-18 14:26:57 -0800214#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700215}
216
217static void UploadToBufferObject(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800218#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700219 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
220
221 rsAssert(!alloc->mHal.state.type->getDimY());
222 rsAssert(!alloc->mHal.state.type->getDimZ());
223
224 //alloc->mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
225
226 if (!drv->bufferID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700227 RSD_CALL_GL(glGenBuffers, 1, &drv->bufferID);
Jason Samseb4fe182011-05-26 16:33:01 -0700228 }
229 if (!drv->bufferID) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000230 ALOGE("Upload to buffer object failed");
Jason Samseb4fe182011-05-26 16:33:01 -0700231 drv->uploadDeferred = true;
232 return;
233 }
Jason Sams2382aba2011-09-13 15:41:01 -0700234 RSD_CALL_GL(glBindBuffer, drv->glTarget, drv->bufferID);
235 RSD_CALL_GL(glBufferData, drv->glTarget, alloc->mHal.state.type->getSizeBytes(),
Jason Sams709a0972012-11-15 18:18:04 -0800236 alloc->mHal.drvState.lod[0].mallocPtr, GL_DYNAMIC_DRAW);
Jason Sams2382aba2011-09-13 15:41:01 -0700237 RSD_CALL_GL(glBindBuffer, drv->glTarget, 0);
Jason Samseb4fe182011-05-26 16:33:01 -0700238 rsdGLCheckError(rsc, "UploadToBufferObject");
Jason Sams93eacc72012-12-18 14:26:57 -0800239#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700240}
241
Jason Sams463bfce2012-08-21 13:54:42 -0700242static size_t AllocationBuildPointerTable(const Context *rsc, const Allocation *alloc,
243 const Type *type, uint8_t *ptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800244 alloc->mHal.drvState.lod[0].dimX = type->getDimX();
245 alloc->mHal.drvState.lod[0].dimY = type->getDimY();
Jason Samsf2b611e2012-12-27 19:05:22 -0800246 alloc->mHal.drvState.lod[0].dimZ = type->getDimZ();
Jason Sams709a0972012-11-15 18:18:04 -0800247 alloc->mHal.drvState.lod[0].mallocPtr = 0;
Stephen Hines94999c32013-02-05 16:58:22 -0800248 // Stride needs to be 16-byte aligned too!
249 size_t stride = alloc->mHal.drvState.lod[0].dimX * type->getElementSizeBytes();
250 alloc->mHal.drvState.lod[0].stride = rsRound(stride, 16);
Jason Sams709a0972012-11-15 18:18:04 -0800251 alloc->mHal.drvState.lodCount = type->getLODCount();
252 alloc->mHal.drvState.faceCount = type->getDimFaces();
Jason Sams807fdc42012-07-25 17:55:39 -0700253
254 size_t offsets[Allocation::MAX_LOD];
255 memset(offsets, 0, sizeof(offsets));
256
Jason Sams709a0972012-11-15 18:18:04 -0800257 size_t o = alloc->mHal.drvState.lod[0].stride * rsMax(alloc->mHal.drvState.lod[0].dimY, 1u) *
258 rsMax(alloc->mHal.drvState.lod[0].dimZ, 1u);
259 if(alloc->mHal.drvState.lodCount > 1) {
260 uint32_t tx = alloc->mHal.drvState.lod[0].dimX;
261 uint32_t ty = alloc->mHal.drvState.lod[0].dimY;
262 uint32_t tz = alloc->mHal.drvState.lod[0].dimZ;
263 for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
264 alloc->mHal.drvState.lod[lod].dimX = tx;
265 alloc->mHal.drvState.lod[lod].dimY = ty;
266 alloc->mHal.drvState.lod[lod].dimZ = tz;
Stephen Hines94999c32013-02-05 16:58:22 -0800267 alloc->mHal.drvState.lod[lod].stride =
268 rsRound(tx * type->getElementSizeBytes(), 16);
Jason Sams807fdc42012-07-25 17:55:39 -0700269 offsets[lod] = o;
Jason Sams709a0972012-11-15 18:18:04 -0800270 o += alloc->mHal.drvState.lod[lod].stride * rsMax(ty, 1u) * rsMax(tz, 1u);
Jason Sams807fdc42012-07-25 17:55:39 -0700271 if (tx > 1) tx >>= 1;
272 if (ty > 1) ty >>= 1;
273 if (tz > 1) tz >>= 1;
274 }
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800275 } else if (alloc->mHal.state.yuv) {
276 // YUV only supports basic 2d
277 // so we can stash the plane pointers in the mipmap levels.
278 switch(alloc->mHal.state.yuv) {
279 case HAL_PIXEL_FORMAT_YV12:
280 offsets[1] = o;
281 alloc->mHal.drvState.lod[1].dimX = alloc->mHal.drvState.lod[0].dimX / 2;
282 alloc->mHal.drvState.lod[1].dimY = alloc->mHal.drvState.lod[0].dimY / 2;
283 alloc->mHal.drvState.lod[1].stride = rsRound(alloc->mHal.drvState.lod[1].dimX *
284 type->getElementSizeBytes(), 16);
285 o += alloc->mHal.drvState.lod[1].stride * alloc->mHal.drvState.lod[1].dimY;
286 offsets[2] = o;
287 alloc->mHal.drvState.lod[2].dimX = alloc->mHal.drvState.lod[1].dimX;
288 alloc->mHal.drvState.lod[2].dimY = alloc->mHal.drvState.lod[1].dimY;
289 alloc->mHal.drvState.lod[2].stride = alloc->mHal.drvState.lod[1].stride;
290 o += alloc->mHal.drvState.lod[2].stride * alloc->mHal.drvState.lod[2].dimY;
291 alloc->mHal.drvState.lodCount = 3;
292 break;
293 case HAL_PIXEL_FORMAT_YCrCb_420_SP: // NV21
294 offsets[1] = o;
295 alloc->mHal.drvState.lod[1].dimX = alloc->mHal.drvState.lod[0].dimX;
296 alloc->mHal.drvState.lod[1].dimY = alloc->mHal.drvState.lod[0].dimY / 2;
297 alloc->mHal.drvState.lod[1].stride = rsRound(alloc->mHal.drvState.lod[1].dimX *
298 type->getElementSizeBytes(), 16);
299 o += alloc->mHal.drvState.lod[1].stride * alloc->mHal.drvState.lod[1].dimY;
300 alloc->mHal.drvState.lodCount = 2;
301 break;
302 default:
303 rsAssert(0);
304 }
Jason Sams807fdc42012-07-25 17:55:39 -0700305 }
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800306
Jason Sams709a0972012-11-15 18:18:04 -0800307 alloc->mHal.drvState.faceOffset = o;
Jason Sams807fdc42012-07-25 17:55:39 -0700308
Jason Sams709a0972012-11-15 18:18:04 -0800309 alloc->mHal.drvState.lod[0].mallocPtr = ptr;
310 for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
311 alloc->mHal.drvState.lod[lod].mallocPtr = ptr + offsets[lod];
Jason Sams463bfce2012-08-21 13:54:42 -0700312 }
Jason Sams463bfce2012-08-21 13:54:42 -0700313
Jason Sams709a0972012-11-15 18:18:04 -0800314 size_t allocSize = alloc->mHal.drvState.faceOffset;
315 if(alloc->mHal.drvState.faceCount) {
Jason Sams463bfce2012-08-21 13:54:42 -0700316 allocSize *= 6;
317 }
318
319 return allocSize;
320}
321
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800322static uint8_t* allocAlignedMemory(size_t allocSize, bool forceZero) {
323 // We align all allocations to a 16-byte boundary.
324 uint8_t* ptr = (uint8_t *)memalign(16, allocSize);
325 if (!ptr) {
326 return NULL;
327 }
328 if (forceZero) {
329 memset(ptr, 0, allocSize);
330 }
331 return ptr;
332}
333
Jason Sams463bfce2012-08-21 13:54:42 -0700334bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) {
335 DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation));
336 if (!drv) {
337 return false;
338 }
339 alloc->mHal.drv = drv;
340
341 // Calculate the object size.
342 size_t allocSize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), NULL);
343
Jason Sams807fdc42012-07-25 17:55:39 -0700344 uint8_t * ptr = NULL;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800345 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) {
Tim Murray2e1a94d2012-11-29 13:12:25 -0800346 } else if (alloc->mHal.state.userProvidedPtr != NULL) {
347 // user-provided allocation
348 // limitations: no faces, no LOD, USAGE_SCRIPT only
Tim Murray9e2bda52012-12-18 12:05:46 -0800349 if (alloc->mHal.state.usageFlags != (RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED)) {
350 ALOGE("Can't use user-allocated buffers if usage is not USAGE_SCRIPT and USAGE_SHARED");
Tim Murray2e1a94d2012-11-29 13:12:25 -0800351 return false;
352 }
353 if (alloc->getType()->getDimLOD() || alloc->getType()->getDimFaces()) {
354 ALOGE("User-allocated buffers must not have multiple faces or LODs");
355 return false;
356 }
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800357
358 // rows must be 16-byte aligned
359 // validate that here, otherwise fall back to not use the user-backed allocation
360 if (((alloc->getType()->getDimX() * alloc->getType()->getElement()->getSizeBytes()) % 16) != 0) {
361 ALOGV("User-backed allocation failed stride requirement, falling back to separate allocation");
362 drv->useUserProvidedPtr = false;
363
364 ptr = allocAlignedMemory(allocSize, forceZero);
365 if (!ptr) {
366 alloc->mHal.drv = NULL;
367 free(drv);
368 return false;
369 }
370
371 } else {
372 drv->useUserProvidedPtr = true;
373 ptr = (uint8_t*)alloc->mHal.state.userProvidedPtr;
374 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800375 } else {
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800376 ptr = allocAlignedMemory(allocSize, forceZero);
Jason Sams179e9a42011-11-23 15:02:15 -0800377 if (!ptr) {
You Kimed419f32012-12-17 03:42:57 +0900378 alloc->mHal.drv = NULL;
Jason Sams179e9a42011-11-23 15:02:15 -0800379 free(drv);
380 return false;
381 }
Jason Samseb4fe182011-05-26 16:33:01 -0700382 }
Jason Sams463bfce2012-08-21 13:54:42 -0700383 // Build the pointer tables
384 size_t verifySize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), ptr);
385 if(allocSize != verifySize) {
386 rsAssert(!"Size mismatch");
Jason Sams807fdc42012-07-25 17:55:39 -0700387 }
Jason Samseb4fe182011-05-26 16:33:01 -0700388
389 drv->glTarget = GL_NONE;
390 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
391 if (alloc->mHal.state.hasFaces) {
392 drv->glTarget = GL_TEXTURE_CUBE_MAP;
393 } else {
394 drv->glTarget = GL_TEXTURE_2D;
395 }
396 } else {
397 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
398 drv->glTarget = GL_ARRAY_BUFFER;
399 }
400 }
401
Jason Sams93eacc72012-12-18 14:26:57 -0800402#ifndef RS_COMPATIBILITY_LIB
Jason Samsa614ae12011-05-26 17:05:51 -0700403 drv->glType = rsdTypeToGLType(alloc->mHal.state.type->getElement()->getComponent().getType());
404 drv->glFormat = rsdKindToGLFormat(alloc->mHal.state.type->getElement()->getComponent().getKind());
Jason Sams93eacc72012-12-18 14:26:57 -0800405#else
406 drv->glType = 0;
407 drv->glFormat = 0;
408#endif
Jason Samsa614ae12011-05-26 17:05:51 -0700409
Jason Samseb4fe182011-05-26 16:33:01 -0700410 if (alloc->mHal.state.usageFlags & ~RS_ALLOCATION_USAGE_SCRIPT) {
411 drv->uploadDeferred = true;
412 }
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700413
Jason Samsb3220332012-04-02 14:41:54 -0700414
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700415 drv->readBackFBO = NULL;
416
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800417 // fill out the initial state of the buffer if we couldn't use the user-provided ptr and USAGE_SHARED was accepted
418 if ((alloc->mHal.state.userProvidedPtr != 0) && (drv->useUserProvidedPtr == false)) {
419 rsdAllocationData2D(rsc, alloc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, alloc->getType()->getDimX(), alloc->getType()->getDimY(), alloc->mHal.state.userProvidedPtr, allocSize, 0);
420 }
421
Jason Samseb4fe182011-05-26 16:33:01 -0700422 return true;
423}
424
425void rsdAllocationDestroy(const Context *rsc, Allocation *alloc) {
426 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
427
Jason Sams93eacc72012-12-18 14:26:57 -0800428#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700429 if (drv->bufferID) {
430 // Causes a SW crash....
Steve Block65982012011-10-20 11:56:00 +0100431 //ALOGV(" mBufferID %i", mBufferID);
Jason Samseb4fe182011-05-26 16:33:01 -0700432 //glDeleteBuffers(1, &mBufferID);
433 //mBufferID = 0;
434 }
435 if (drv->textureID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700436 RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID);
Jason Samseb4fe182011-05-26 16:33:01 -0700437 drv->textureID = 0;
438 }
439 if (drv->renderTargetID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700440 RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID);
Jason Samseb4fe182011-05-26 16:33:01 -0700441 drv->renderTargetID = 0;
442 }
Jason Sams93eacc72012-12-18 14:26:57 -0800443#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700444
Jason Sams709a0972012-11-15 18:18:04 -0800445 if (alloc->mHal.drvState.lod[0].mallocPtr) {
Tim Murray2e1a94d2012-11-29 13:12:25 -0800446 // don't free user-allocated ptrs
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800447 if (!(drv->useUserProvidedPtr)) {
Tim Murray2e1a94d2012-11-29 13:12:25 -0800448 free(alloc->mHal.drvState.lod[0].mallocPtr);
449 }
Jason Sams709a0972012-11-15 18:18:04 -0800450 alloc->mHal.drvState.lod[0].mallocPtr = NULL;
Jason Samseb4fe182011-05-26 16:33:01 -0700451 }
Jason Sams93eacc72012-12-18 14:26:57 -0800452
453#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700454 if (drv->readBackFBO != NULL) {
455 delete drv->readBackFBO;
456 drv->readBackFBO = NULL;
457 }
Jason Sams10c9dd72013-02-01 10:53:42 -0800458
459 if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) &&
460 (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
461
462 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
463 ANativeWindow *nw = alloc->mHal.state.wndSurface;
464
465 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
466 mapper.unlock(drv->wndBuffer->handle);
467 int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
468 }
Jason Sams93eacc72012-12-18 14:26:57 -0800469#endif
470
Jason Samseb4fe182011-05-26 16:33:01 -0700471 free(drv);
472 alloc->mHal.drv = NULL;
473}
474
475void rsdAllocationResize(const Context *rsc, const Allocation *alloc,
476 const Type *newType, bool zeroNew) {
Jason Samsa572aca2013-01-09 11:52:26 -0800477 const uint32_t oldDimX = alloc->mHal.drvState.lod[0].dimX;
478 const uint32_t dimX = newType->getDimX();
479
Tim Murray9e2bda52012-12-18 12:05:46 -0800480 // can't resize Allocations with user-allocated buffers
481 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) {
482 ALOGE("Resize cannot be called on a USAGE_SHARED allocation");
483 return;
484 }
Jason Sams709a0972012-11-15 18:18:04 -0800485 void * oldPtr = alloc->mHal.drvState.lod[0].mallocPtr;
Jason Sams463bfce2012-08-21 13:54:42 -0700486 // Calculate the object size
487 size_t s = AllocationBuildPointerTable(rsc, alloc, newType, NULL);
488 uint8_t *ptr = (uint8_t *)realloc(oldPtr, s);
489 // Build the relative pointer tables.
490 size_t verifySize = AllocationBuildPointerTable(rsc, alloc, newType, ptr);
491 if(s != verifySize) {
492 rsAssert(!"Size mismatch");
493 }
Jason Samseb4fe182011-05-26 16:33:01 -0700494
Jason Samseb4fe182011-05-26 16:33:01 -0700495
496 if (dimX > oldDimX) {
Jason Sams463bfce2012-08-21 13:54:42 -0700497 uint32_t stride = alloc->mHal.state.elementSizeBytes;
Jason Sams709a0972012-11-15 18:18:04 -0800498 memset(((uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr) + stride * oldDimX,
Jason Sams2275e9c2012-04-16 17:01:26 -0700499 0, stride * (dimX - oldDimX));
Jason Samseb4fe182011-05-26 16:33:01 -0700500 }
501}
502
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700503static void rsdAllocationSyncFromFBO(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800504#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700505 if (!alloc->getIsScript()) {
506 return; // nothing to sync
507 }
508
509 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
510 RsdFrameBufferObj *lastFbo = dc->gl.currentFrameBuffer;
511
512 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
513 if (!drv->textureID && !drv->renderTargetID) {
514 return; // nothing was rendered here yet, so nothing to sync
515 }
516 if (drv->readBackFBO == NULL) {
517 drv->readBackFBO = new RsdFrameBufferObj();
518 drv->readBackFBO->setColorTarget(drv, 0);
519 drv->readBackFBO->setDimensions(alloc->getType()->getDimX(),
520 alloc->getType()->getDimY());
521 }
522
523 // Bind the framebuffer object so we can read back from it
524 drv->readBackFBO->setActive(rsc);
525
526 // Do the readback
Jason Sams709a0972012-11-15 18:18:04 -0800527 RSD_CALL_GL(glReadPixels, 0, 0, alloc->mHal.drvState.lod[0].dimX,
528 alloc->mHal.drvState.lod[0].dimY,
529 drv->glFormat, drv->glType, alloc->mHal.drvState.lod[0].mallocPtr);
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700530
531 // Revert framebuffer to its original
532 lastFbo->setActive(rsc);
Jason Sams93eacc72012-12-18 14:26:57 -0800533#endif
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700534}
Jason Samseb4fe182011-05-26 16:33:01 -0700535
536
537void rsdAllocationSyncAll(const Context *rsc, const Allocation *alloc,
538 RsAllocationUsageType src) {
539 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
540
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700541 if (src == RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
542 if(!alloc->getIsRenderTarget()) {
543 rsc->setError(RS_ERROR_FATAL_DRIVER,
544 "Attempting to sync allocation from render target, "
545 "for non-render target allocation");
546 } else if (alloc->getType()->getElement()->getKind() != RS_KIND_PIXEL_RGBA) {
547 rsc->setError(RS_ERROR_FATAL_DRIVER, "Cannot only sync from RGBA"
548 "render target");
549 } else {
550 rsdAllocationSyncFromFBO(rsc, alloc);
551 }
Jason Samseb4fe182011-05-26 16:33:01 -0700552 return;
553 }
554
555 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
556
557 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
558 UploadToTexture(rsc, alloc);
559 } else {
Jason Samsb3220332012-04-02 14:41:54 -0700560 if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) &&
Jason Sams0dc66932012-04-10 17:05:49 -0700561 !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
Jason Samseb4fe182011-05-26 16:33:01 -0700562 AllocateRenderTarget(rsc, alloc);
563 }
564 }
565 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
566 UploadToBufferObject(rsc, alloc);
567 }
568
569 drv->uploadDeferred = false;
570}
571
572void rsdAllocationMarkDirty(const Context *rsc, const Allocation *alloc) {
573 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
574 drv->uploadDeferred = true;
575}
576
Jason Sams41e373d2012-01-13 14:01:20 -0800577int32_t rsdAllocationInitSurfaceTexture(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800578#ifndef RS_COMPATIBILITY_LIB
Jason Sams41e373d2012-01-13 14:01:20 -0800579 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
580 UploadToTexture(rsc, alloc);
581 return drv->textureID;
Jason Sams93eacc72012-12-18 14:26:57 -0800582#else
583 return 0;
584#endif
Jason Sams41e373d2012-01-13 14:01:20 -0800585}
586
Jason Sams93eacc72012-12-18 14:26:57 -0800587#ifndef RS_COMPATIBILITY_LIB
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800588static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
589 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
590
Jamie Genniscd919a12012-06-13 16:34:11 -0700591 int32_t r = native_window_dequeue_buffer_and_wait(nw, &drv->wndBuffer);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800592 if (r) {
593 rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer.");
594 return false;
595 }
596
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800597 // Must lock the whole surface
598 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
599 Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height);
600
601 void *dst = NULL;
602 mapper.lock(drv->wndBuffer->handle,
603 GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN,
604 bounds, &dst);
Jason Sams709a0972012-11-15 18:18:04 -0800605 alloc->mHal.drvState.lod[0].mallocPtr = dst;
606 alloc->mHal.drvState.lod[0].stride = drv->wndBuffer->stride * alloc->mHal.state.elementSizeBytes;
Stephen Hines94999c32013-02-05 16:58:22 -0800607 rsAssert((alloc->mHal.drvState.lod[0].stride & 0xf) == 0);
Jason Samsb3220332012-04-02 14:41:54 -0700608
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800609 return true;
610}
Jason Sams93eacc72012-12-18 14:26:57 -0800611#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800612
613void rsdAllocationSetSurfaceTexture(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
Jason Sams93eacc72012-12-18 14:26:57 -0800614#ifndef RS_COMPATIBILITY_LIB
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800615 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
616
617 //ALOGE("rsdAllocationSetSurfaceTexture %p %p", alloc, nw);
618
Jason Samsb3220332012-04-02 14:41:54 -0700619 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
620 //TODO finish support for render target + script
621 drv->wnd = nw;
622 return;
623 }
624
625
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800626 // Cleanup old surface if there is one.
627 if (alloc->mHal.state.wndSurface) {
628 ANativeWindow *old = alloc->mHal.state.wndSurface;
629 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
630 mapper.unlock(drv->wndBuffer->handle);
Jamie Genniscd919a12012-06-13 16:34:11 -0700631 old->queueBuffer(old, drv->wndBuffer, -1);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800632 }
633
634 if (nw != NULL) {
635 int32_t r;
Jason Samsb3220332012-04-02 14:41:54 -0700636 uint32_t flags = 0;
637 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
638 flags |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN;
639 }
640 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
641 flags |= GRALLOC_USAGE_HW_RENDER;
642 }
643
644 r = native_window_set_usage(nw, flags);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800645 if (r) {
646 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
647 return;
648 }
649
Jason Samsa572aca2013-01-09 11:52:26 -0800650 r = native_window_set_buffers_dimensions(nw, alloc->mHal.drvState.lod[0].dimX,
651 alloc->mHal.drvState.lod[0].dimY);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800652 if (r) {
653 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions.");
654 return;
655 }
656
657 r = native_window_set_buffer_count(nw, 3);
658 if (r) {
659 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer count.");
660 return;
661 }
662
663 IoGetBuffer(rsc, alloc, nw);
664 }
Jason Sams93eacc72012-12-18 14:26:57 -0800665#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800666}
667
668void rsdAllocationIoSend(const Context *rsc, Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800669#ifndef RS_COMPATIBILITY_LIB
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800670 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
671 ANativeWindow *nw = alloc->mHal.state.wndSurface;
672
Jason Samsb3220332012-04-02 14:41:54 -0700673 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
674 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
675 RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800676 return;
677 }
678
Jason Samsb3220332012-04-02 14:41:54 -0700679 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
680 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
681 mapper.unlock(drv->wndBuffer->handle);
Jamie Genniscd919a12012-06-13 16:34:11 -0700682 int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
Jason Samsb3220332012-04-02 14:41:54 -0700683 if (r) {
684 rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer.");
685 return;
686 }
687
688 IoGetBuffer(rsc, alloc, nw);
689 }
Jason Sams93eacc72012-12-18 14:26:57 -0800690#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800691}
692
693void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800694#ifndef RS_COMPATIBILITY_LIB
Jason Sams3522f402012-03-23 11:47:26 -0700695 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
696 alloc->mHal.state.surfaceTexture->updateTexImage();
Jason Sams93eacc72012-12-18 14:26:57 -0800697#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800698}
699
700
Jason Samseb4fe182011-05-26 16:33:01 -0700701void rsdAllocationData1D(const Context *rsc, const Allocation *alloc,
702 uint32_t xoff, uint32_t lod, uint32_t count,
Alex Sakhartchoukc794cd52012-02-13 11:57:32 -0800703 const void *data, size_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -0700704 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
705
706 const uint32_t eSize = alloc->mHal.state.type->getElementSizeBytes();
Jason Sams807fdc42012-07-25 17:55:39 -0700707 uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Jason Samseb4fe182011-05-26 16:33:01 -0700708 uint32_t size = count * eSize;
709
Stephen Hines20c535f2012-12-06 19:04:38 -0800710 if (ptr != data) {
711 // Skip the copy if we are the same allocation. This can arise from
712 // our Bitmap optimization, where we share the same storage.
713 if (alloc->mHal.state.hasReferences) {
714 alloc->incRefs(data, count);
715 alloc->decRefs(ptr, count);
716 }
717 memcpy(ptr, data, size);
Jason Samseb4fe182011-05-26 16:33:01 -0700718 }
Jason Samseb4fe182011-05-26 16:33:01 -0700719 drv->uploadDeferred = true;
720}
721
722void rsdAllocationData2D(const Context *rsc, const Allocation *alloc,
723 uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800724 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Jason Samseb4fe182011-05-26 16:33:01 -0700725 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
726
727 uint32_t eSize = alloc->mHal.state.elementSizeBytes;
728 uint32_t lineSize = eSize * w;
Tim Murray358747a2012-11-26 13:52:04 -0800729 if (!stride) {
730 stride = lineSize;
731 }
Jason Samseb4fe182011-05-26 16:33:01 -0700732
Jason Sams709a0972012-11-15 18:18:04 -0800733 if (alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Samseb4fe182011-05-26 16:33:01 -0700734 const uint8_t *src = static_cast<const uint8_t *>(data);
Jason Sams807fdc42012-07-25 17:55:39 -0700735 uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, lod, face);
Stephen Hines20c535f2012-12-06 19:04:38 -0800736 if (dst == src) {
737 // Skip the copy if we are the same allocation. This can arise from
738 // our Bitmap optimization, where we share the same storage.
739 drv->uploadDeferred = true;
740 return;
741 }
Jason Samseb4fe182011-05-26 16:33:01 -0700742
743 for (uint32_t line=yoff; line < (yoff+h); line++) {
744 if (alloc->mHal.state.hasReferences) {
745 alloc->incRefs(src, w);
746 alloc->decRefs(dst, w);
747 }
748 memcpy(dst, src, lineSize);
Tim Murray358747a2012-11-26 13:52:04 -0800749 src += stride;
Jason Sams709a0972012-11-15 18:18:04 -0800750 dst += alloc->mHal.drvState.lod[lod].stride;
Jason Samseb4fe182011-05-26 16:33:01 -0700751 }
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800752 if (alloc->mHal.state.yuv) {
753 int lod = 1;
754 while (alloc->mHal.drvState.lod[lod].mallocPtr) {
755 uint32_t lineSize = alloc->mHal.drvState.lod[lod].dimX;
756 uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, lod, face);
757
758 for (uint32_t line=(yoff >> 1); line < ((yoff+h)>>1); line++) {
759 memcpy(dst, src, lineSize);
760 src += lineSize;
761 dst += alloc->mHal.drvState.lod[lod].stride;
762 }
763 lod++;
764 }
765
766 }
Jason Samseb4fe182011-05-26 16:33:01 -0700767 drv->uploadDeferred = true;
768 } else {
Jason Sams2382aba2011-09-13 15:41:01 -0700769 Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h);
Jason Samseb4fe182011-05-26 16:33:01 -0700770 }
771}
772
773void rsdAllocationData3D(const Context *rsc, const Allocation *alloc,
774 uint32_t xoff, uint32_t yoff, uint32_t zoff,
775 uint32_t lod, RsAllocationCubemapFace face,
776 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
777
778}
779
Jason Sams807fdc42012-07-25 17:55:39 -0700780void rsdAllocationRead1D(const Context *rsc, const Allocation *alloc,
781 uint32_t xoff, uint32_t lod, uint32_t count,
782 void *data, size_t sizeBytes) {
Jason Sams807fdc42012-07-25 17:55:39 -0700783 const uint32_t eSize = alloc->mHal.state.type->getElementSizeBytes();
784 const uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Stephen Hines20c535f2012-12-06 19:04:38 -0800785 if (data != ptr) {
786 // Skip the copy if we are the same allocation. This can arise from
787 // our Bitmap optimization, where we share the same storage.
788 memcpy(data, ptr, count * eSize);
789 }
Jason Sams807fdc42012-07-25 17:55:39 -0700790}
791
792void rsdAllocationRead2D(const Context *rsc, const Allocation *alloc,
Tim Murray358747a2012-11-26 13:52:04 -0800793 uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
794 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
Jason Sams807fdc42012-07-25 17:55:39 -0700795 uint32_t eSize = alloc->mHal.state.elementSizeBytes;
796 uint32_t lineSize = eSize * w;
Tim Murray358747a2012-11-26 13:52:04 -0800797 if (!stride) {
798 stride = lineSize;
799 }
Jason Sams807fdc42012-07-25 17:55:39 -0700800
Jason Sams709a0972012-11-15 18:18:04 -0800801 if (alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Sams807fdc42012-07-25 17:55:39 -0700802 uint8_t *dst = static_cast<uint8_t *>(data);
803 const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, lod, face);
Stephen Hines20c535f2012-12-06 19:04:38 -0800804 if (dst == src) {
805 // Skip the copy if we are the same allocation. This can arise from
806 // our Bitmap optimization, where we share the same storage.
807 return;
808 }
Jason Sams807fdc42012-07-25 17:55:39 -0700809
810 for (uint32_t line=yoff; line < (yoff+h); line++) {
811 memcpy(dst, src, lineSize);
Tim Murray358747a2012-11-26 13:52:04 -0800812 dst += stride;
Jason Sams709a0972012-11-15 18:18:04 -0800813 src += alloc->mHal.drvState.lod[lod].stride;
Jason Sams807fdc42012-07-25 17:55:39 -0700814 }
815 } else {
816 ALOGE("Add code to readback from non-script memory");
817 }
818}
819
Tim Murray358747a2012-11-26 13:52:04 -0800820
Jason Sams807fdc42012-07-25 17:55:39 -0700821void rsdAllocationRead3D(const Context *rsc, const Allocation *alloc,
822 uint32_t xoff, uint32_t yoff, uint32_t zoff,
823 uint32_t lod, RsAllocationCubemapFace face,
824 uint32_t w, uint32_t h, uint32_t d, void *data, uint32_t sizeBytes) {
825
826}
827
828void * rsdAllocationLock1D(const android::renderscript::Context *rsc,
829 const android::renderscript::Allocation *alloc) {
Jason Sams709a0972012-11-15 18:18:04 -0800830 return alloc->mHal.drvState.lod[0].mallocPtr;
Jason Sams807fdc42012-07-25 17:55:39 -0700831}
832
833void rsdAllocationUnlock1D(const android::renderscript::Context *rsc,
834 const android::renderscript::Allocation *alloc) {
835
836}
837
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700838void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc,
839 const android::renderscript::Allocation *dstAlloc,
840 uint32_t dstXoff, uint32_t dstLod, uint32_t count,
841 const android::renderscript::Allocation *srcAlloc,
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700842 uint32_t srcXoff, uint32_t srcLod) {
843}
844
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700845
846void rsdAllocationData2D_alloc_script(const android::renderscript::Context *rsc,
847 const android::renderscript::Allocation *dstAlloc,
848 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
849 RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
850 const android::renderscript::Allocation *srcAlloc,
851 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
852 RsAllocationCubemapFace srcFace) {
853 uint32_t elementSize = dstAlloc->getType()->getElementSizeBytes();
854 for (uint32_t i = 0; i < h; i ++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700855 uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, dstLod, dstFace);
856 uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, srcLod, srcFace);
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700857 memcpy(dstPtr, srcPtr, w * elementSize);
858
Steve Blockaf12ac62012-01-06 19:20:56 +0000859 //ALOGE("COPIED dstXoff(%u), dstYoff(%u), dstLod(%u), dstFace(%u), w(%u), h(%u), srcXoff(%u), srcYoff(%u), srcLod(%u), srcFace(%u)",
Stephen Hines0a44ab42011-06-23 16:18:28 -0700860 // dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace);
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700861 }
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700862}
863
864void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc,
865 const android::renderscript::Allocation *dstAlloc,
866 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
867 RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
868 const android::renderscript::Allocation *srcAlloc,
869 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
870 RsAllocationCubemapFace srcFace) {
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700871 if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) {
872 rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not "
873 "yet implemented.");
874 return;
875 }
876 rsdAllocationData2D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff,
877 dstLod, dstFace, w, h, srcAlloc,
878 srcXoff, srcYoff, srcLod, srcFace);
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700879}
880
881void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc,
882 const android::renderscript::Allocation *dstAlloc,
883 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
884 uint32_t dstLod, RsAllocationCubemapFace dstFace,
885 uint32_t w, uint32_t h, uint32_t d,
886 const android::renderscript::Allocation *srcAlloc,
887 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
888 uint32_t srcLod, RsAllocationCubemapFace srcFace) {
889}
890
Jason Samseb4fe182011-05-26 16:33:01 -0700891void rsdAllocationElementData1D(const Context *rsc, const Allocation *alloc,
892 uint32_t x,
893 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
894 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
895
896 uint32_t eSize = alloc->mHal.state.elementSizeBytes;
Jason Sams807fdc42012-07-25 17:55:39 -0700897 uint8_t * ptr = GetOffsetPtr(alloc, x, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Jason Samseb4fe182011-05-26 16:33:01 -0700898
899 const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
900 ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
901
902 if (alloc->mHal.state.hasReferences) {
903 e->incRefs(data);
904 e->decRefs(ptr);
905 }
906
907 memcpy(ptr, data, sizeBytes);
908 drv->uploadDeferred = true;
909}
910
911void rsdAllocationElementData2D(const Context *rsc, const Allocation *alloc,
912 uint32_t x, uint32_t y,
913 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
914 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
915
916 uint32_t eSize = alloc->mHal.state.elementSizeBytes;
Jason Sams807fdc42012-07-25 17:55:39 -0700917 uint8_t * ptr = GetOffsetPtr(alloc, x, y, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Jason Samseb4fe182011-05-26 16:33:01 -0700918
919 const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
920 ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
921
922 if (alloc->mHal.state.hasReferences) {
923 e->incRefs(data);
924 e->decRefs(ptr);
925 }
926
927 memcpy(ptr, data, sizeBytes);
928 drv->uploadDeferred = true;
929}
930
Jason Sams61a4bb72012-07-25 19:33:43 -0700931static void mip565(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -0800932 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
933 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
Jason Sams61a4bb72012-07-25 19:33:43 -0700934
935 for (uint32_t y=0; y < h; y++) {
936 uint16_t *oPtr = (uint16_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face);
937 const uint16_t *i1 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2, lod, face);
938 const uint16_t *i2 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face);
939
940 for (uint32_t x=0; x < w; x++) {
941 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
942 oPtr ++;
943 i1 += 2;
944 i2 += 2;
945 }
946 }
947}
948
949static void mip8888(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -0800950 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
951 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
Jason Sams61a4bb72012-07-25 19:33:43 -0700952
953 for (uint32_t y=0; y < h; y++) {
954 uint32_t *oPtr = (uint32_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face);
955 const uint32_t *i1 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2, lod, face);
956 const uint32_t *i2 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face);
957
958 for (uint32_t x=0; x < w; x++) {
959 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
960 oPtr ++;
961 i1 += 2;
962 i2 += 2;
963 }
964 }
965}
966
967static void mip8(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -0800968 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
969 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
Jason Sams61a4bb72012-07-25 19:33:43 -0700970
971 for (uint32_t y=0; y < h; y++) {
972 uint8_t *oPtr = GetOffsetPtr(alloc, 0, y, lod + 1, face);
973 const uint8_t *i1 = GetOffsetPtr(alloc, 0, y*2, lod, face);
974 const uint8_t *i2 = GetOffsetPtr(alloc, 0, y*2+1, lod, face);
975
976 for (uint32_t x=0; x < w; x++) {
977 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
978 oPtr ++;
979 i1 += 2;
980 i2 += 2;
981 }
982 }
983}
984
985void rsdAllocationGenerateMipmaps(const Context *rsc, const Allocation *alloc) {
Jason Sams709a0972012-11-15 18:18:04 -0800986 if(!alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700987 return;
988 }
989 uint32_t numFaces = alloc->getType()->getDimFaces() ? 6 : 1;
990 for (uint32_t face = 0; face < numFaces; face ++) {
991 for (uint32_t lod=0; lod < (alloc->getType()->getLODCount() -1); lod++) {
992 switch (alloc->getType()->getElement()->getSizeBits()) {
993 case 32:
994 mip8888(alloc, lod, (RsAllocationCubemapFace)face);
995 break;
996 case 16:
997 mip565(alloc, lod, (RsAllocationCubemapFace)face);
998 break;
999 case 8:
1000 mip8(alloc, lod, (RsAllocationCubemapFace)face);
1001 break;
1002 }
1003 }
1004 }
1005}
1006
Jason Samseb4fe182011-05-26 16:33:01 -07001007