blob: 28d8aad15d4dda99b725cb31fc5cc93ed3216718 [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 Sams733396b2013-02-22 12:46:18 -080030#include "gui/CpuConsumer.h"
31#include "gui/Surface.h"
Jason Sams93eacc72012-12-18 14:26:57 -080032#include "hardware/gralloc.h"
Jason Sams7ac2a4d2012-02-15 12:04:24 -080033
Jason Samseb4fe182011-05-26 16:33:01 -070034#include <GLES/gl.h>
35#include <GLES2/gl2.h>
36#include <GLES/glext.h>
Jason Sams93eacc72012-12-18 14:26:57 -080037#endif
Jason Samseb4fe182011-05-26 16:33:01 -070038
39using namespace android;
40using namespace android::renderscript;
41
42
Jason Sams93eacc72012-12-18 14:26:57 -080043#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -070044const static GLenum gFaceOrder[] = {
45 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
46 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
47 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
48 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
49 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
50 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
51};
52
Jason Samsa614ae12011-05-26 17:05:51 -070053GLenum rsdTypeToGLType(RsDataType t) {
54 switch (t) {
55 case RS_TYPE_UNSIGNED_5_6_5: return GL_UNSIGNED_SHORT_5_6_5;
56 case RS_TYPE_UNSIGNED_5_5_5_1: return GL_UNSIGNED_SHORT_5_5_5_1;
57 case RS_TYPE_UNSIGNED_4_4_4_4: return GL_UNSIGNED_SHORT_4_4_4_4;
58
59 //case RS_TYPE_FLOAT_16: return GL_HALF_FLOAT;
60 case RS_TYPE_FLOAT_32: return GL_FLOAT;
61 case RS_TYPE_UNSIGNED_8: return GL_UNSIGNED_BYTE;
62 case RS_TYPE_UNSIGNED_16: return GL_UNSIGNED_SHORT;
63 case RS_TYPE_SIGNED_8: return GL_BYTE;
64 case RS_TYPE_SIGNED_16: return GL_SHORT;
65 default: break;
66 }
67 return 0;
68}
69
70GLenum rsdKindToGLFormat(RsDataKind k) {
71 switch (k) {
72 case RS_KIND_PIXEL_L: return GL_LUMINANCE;
73 case RS_KIND_PIXEL_A: return GL_ALPHA;
74 case RS_KIND_PIXEL_LA: return GL_LUMINANCE_ALPHA;
75 case RS_KIND_PIXEL_RGB: return GL_RGB;
76 case RS_KIND_PIXEL_RGBA: return GL_RGBA;
77 case RS_KIND_PIXEL_DEPTH: return GL_DEPTH_COMPONENT16;
78 default: break;
79 }
80 return 0;
81}
Jason Sams93eacc72012-12-18 14:26:57 -080082#endif
Jason Samsa614ae12011-05-26 17:05:51 -070083
Jason Sams807fdc42012-07-25 17:55:39 -070084uint8_t *GetOffsetPtr(const android::renderscript::Allocation *alloc,
85 uint32_t xoff, uint32_t yoff, uint32_t lod,
86 RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -080087 uint8_t *ptr = (uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr;
88 ptr += face * alloc->mHal.drvState.faceOffset;
89 ptr += yoff * alloc->mHal.drvState.lod[lod].stride;
Jason Sams807fdc42012-07-25 17:55:39 -070090 ptr += xoff * alloc->mHal.state.elementSizeBytes;
91 return ptr;
92}
93
Jason Samsa614ae12011-05-26 17:05:51 -070094
Jason Sams2382aba2011-09-13 15:41:01 -070095static void Update2DTexture(const Context *rsc, const Allocation *alloc, const void *ptr,
96 uint32_t xoff, uint32_t yoff, uint32_t lod,
97 RsAllocationCubemapFace face, uint32_t w, uint32_t h) {
Jason Sams93eacc72012-12-18 14:26:57 -080098#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -070099 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
100
Jason Samseb4fe182011-05-26 16:33:01 -0700101 rsAssert(drv->textureID);
Jason Sams2382aba2011-09-13 15:41:01 -0700102 RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
103 RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
Jason Samseb4fe182011-05-26 16:33:01 -0700104 GLenum t = GL_TEXTURE_2D;
105 if (alloc->mHal.state.hasFaces) {
106 t = gFaceOrder[face];
107 }
Jason Sams2382aba2011-09-13 15:41:01 -0700108 RSD_CALL_GL(glTexSubImage2D, t, lod, xoff, yoff, w, h, drv->glFormat, drv->glType, ptr);
Jason Sams93eacc72012-12-18 14:26:57 -0800109#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700110}
111
112
Jason Sams93eacc72012-12-18 14:26:57 -0800113#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700114static void Upload2DTexture(const Context *rsc, const Allocation *alloc, bool isFirstUpload) {
115 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
116
Jason Sams2382aba2011-09-13 15:41:01 -0700117 RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
118 RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
Jason Samseb4fe182011-05-26 16:33:01 -0700119
120 uint32_t faceCount = 1;
121 if (alloc->mHal.state.hasFaces) {
122 faceCount = 6;
123 }
124
125 rsdGLCheckError(rsc, "Upload2DTexture 1 ");
126 for (uint32_t face = 0; face < faceCount; face ++) {
127 for (uint32_t lod = 0; lod < alloc->mHal.state.type->getLODCount(); lod++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700128 const uint8_t *p = GetOffsetPtr(alloc, 0, 0, lod, (RsAllocationCubemapFace)face);
Jason Samseb4fe182011-05-26 16:33:01 -0700129
130 GLenum t = GL_TEXTURE_2D;
131 if (alloc->mHal.state.hasFaces) {
132 t = gFaceOrder[face];
133 }
134
135 if (isFirstUpload) {
Jason Sams2382aba2011-09-13 15:41:01 -0700136 RSD_CALL_GL(glTexImage2D, t, lod, drv->glFormat,
Jason Samseb4fe182011-05-26 16:33:01 -0700137 alloc->mHal.state.type->getLODDimX(lod),
138 alloc->mHal.state.type->getLODDimY(lod),
Jason Samsa614ae12011-05-26 17:05:51 -0700139 0, drv->glFormat, drv->glType, p);
Jason Samseb4fe182011-05-26 16:33:01 -0700140 } else {
Jason Sams2382aba2011-09-13 15:41:01 -0700141 RSD_CALL_GL(glTexSubImage2D, t, lod, 0, 0,
Jason Samseb4fe182011-05-26 16:33:01 -0700142 alloc->mHal.state.type->getLODDimX(lod),
143 alloc->mHal.state.type->getLODDimY(lod),
Jason Samsa614ae12011-05-26 17:05:51 -0700144 drv->glFormat, drv->glType, p);
Jason Samseb4fe182011-05-26 16:33:01 -0700145 }
146 }
147 }
148
149 if (alloc->mHal.state.mipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
Jason Sams2382aba2011-09-13 15:41:01 -0700150 RSD_CALL_GL(glGenerateMipmap, drv->glTarget);
Jason Samseb4fe182011-05-26 16:33:01 -0700151 }
152 rsdGLCheckError(rsc, "Upload2DTexture");
153}
Jason Sams93eacc72012-12-18 14:26:57 -0800154#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700155
156static void UploadToTexture(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800157#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700158 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
159
Jason Sams3522f402012-03-23 11:47:26 -0700160 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
Jason Sams41e373d2012-01-13 14:01:20 -0800161 if (!drv->textureID) {
162 RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
163 }
164 return;
165 }
166
Jason Samsa614ae12011-05-26 17:05:51 -0700167 if (!drv->glType || !drv->glFormat) {
Jason Samseb4fe182011-05-26 16:33:01 -0700168 return;
169 }
170
Jason Sams709a0972012-11-15 18:18:04 -0800171 if (!alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Samseb4fe182011-05-26 16:33:01 -0700172 return;
173 }
174
175 bool isFirstUpload = false;
176
177 if (!drv->textureID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700178 RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
Jason Samseb4fe182011-05-26 16:33:01 -0700179 isFirstUpload = true;
180 }
181
182 Upload2DTexture(rsc, alloc, isFirstUpload);
183
184 if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
Jason Sams709a0972012-11-15 18:18:04 -0800185 if (alloc->mHal.drvState.lod[0].mallocPtr) {
186 free(alloc->mHal.drvState.lod[0].mallocPtr);
187 alloc->mHal.drvState.lod[0].mallocPtr = NULL;
Jason Samseb4fe182011-05-26 16:33:01 -0700188 }
189 }
190 rsdGLCheckError(rsc, "UploadToTexture");
Jason Sams93eacc72012-12-18 14:26:57 -0800191#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700192}
193
194static void AllocateRenderTarget(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800195#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700196 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
197
Jason Samsa614ae12011-05-26 17:05:51 -0700198 if (!drv->glFormat) {
Jason Samseb4fe182011-05-26 16:33:01 -0700199 return;
200 }
201
202 if (!drv->renderTargetID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700203 RSD_CALL_GL(glGenRenderbuffers, 1, &drv->renderTargetID);
Jason Samseb4fe182011-05-26 16:33:01 -0700204
205 if (!drv->renderTargetID) {
206 // This should generally not happen
Steve Blockaf12ac62012-01-06 19:20:56 +0000207 ALOGE("allocateRenderTarget failed to gen mRenderTargetID");
Jason Samseb4fe182011-05-26 16:33:01 -0700208 rsc->dumpDebug();
209 return;
210 }
Jason Sams2382aba2011-09-13 15:41:01 -0700211 RSD_CALL_GL(glBindRenderbuffer, GL_RENDERBUFFER, drv->renderTargetID);
212 RSD_CALL_GL(glRenderbufferStorage, GL_RENDERBUFFER, drv->glFormat,
Jason Samsa572aca2013-01-09 11:52:26 -0800213 alloc->mHal.drvState.lod[0].dimX, alloc->mHal.drvState.lod[0].dimY);
Jason Samseb4fe182011-05-26 16:33:01 -0700214 }
215 rsdGLCheckError(rsc, "AllocateRenderTarget");
Jason Sams93eacc72012-12-18 14:26:57 -0800216#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700217}
218
219static void UploadToBufferObject(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800220#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700221 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
222
223 rsAssert(!alloc->mHal.state.type->getDimY());
224 rsAssert(!alloc->mHal.state.type->getDimZ());
225
226 //alloc->mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
227
228 if (!drv->bufferID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700229 RSD_CALL_GL(glGenBuffers, 1, &drv->bufferID);
Jason Samseb4fe182011-05-26 16:33:01 -0700230 }
231 if (!drv->bufferID) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000232 ALOGE("Upload to buffer object failed");
Jason Samseb4fe182011-05-26 16:33:01 -0700233 drv->uploadDeferred = true;
234 return;
235 }
Jason Sams2382aba2011-09-13 15:41:01 -0700236 RSD_CALL_GL(glBindBuffer, drv->glTarget, drv->bufferID);
237 RSD_CALL_GL(glBufferData, drv->glTarget, alloc->mHal.state.type->getSizeBytes(),
Jason Sams709a0972012-11-15 18:18:04 -0800238 alloc->mHal.drvState.lod[0].mallocPtr, GL_DYNAMIC_DRAW);
Jason Sams2382aba2011-09-13 15:41:01 -0700239 RSD_CALL_GL(glBindBuffer, drv->glTarget, 0);
Jason Samseb4fe182011-05-26 16:33:01 -0700240 rsdGLCheckError(rsc, "UploadToBufferObject");
Jason Sams93eacc72012-12-18 14:26:57 -0800241#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700242}
243
Jason Sams733396b2013-02-22 12:46:18 -0800244static void DeriveYUVLayout(int yuv, Allocation::Hal::DrvState *state) {
245 // YUV only supports basic 2d
246 // so we can stash the plane pointers in the mipmap levels.
247 switch(yuv) {
248 case HAL_PIXEL_FORMAT_YV12:
249 state->lod[1].dimX = state->lod[0].dimX / 2;
250 state->lod[1].dimY = state->lod[0].dimY / 2;
251 state->lod[1].stride = rsRound(state->lod[0].stride >> 1, 16);
252 state->lod[1].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) +
253 (state->lod[0].stride * state->lod[0].dimY);
254
255 state->lod[2].dimX = state->lod[1].dimX;
256 state->lod[2].dimY = state->lod[1].dimY;
257 state->lod[2].stride = state->lod[1].stride;
258 state->lod[2].mallocPtr = ((uint8_t *)state->lod[1].mallocPtr) +
259 (state->lod[1].stride * state->lod[1].dimY);
260
261 state->lodCount = 3;
262 break;
263 case HAL_PIXEL_FORMAT_YCrCb_420_SP: // NV21
264 state->lod[1].dimX = state->lod[0].dimX;
265 state->lod[1].dimY = state->lod[0].dimY / 2;
266 state->lod[1].stride = state->lod[0].stride;
267 state->lod[1].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) +
268 (state->lod[0].stride * state->lod[0].dimY);
269 state->lodCount = 2;
270 break;
271 default:
272 rsAssert(0);
273 }
274}
275
276
Jason Sams463bfce2012-08-21 13:54:42 -0700277static size_t AllocationBuildPointerTable(const Context *rsc, const Allocation *alloc,
278 const Type *type, uint8_t *ptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800279 alloc->mHal.drvState.lod[0].dimX = type->getDimX();
280 alloc->mHal.drvState.lod[0].dimY = type->getDimY();
Jason Samsf2b611e2012-12-27 19:05:22 -0800281 alloc->mHal.drvState.lod[0].dimZ = type->getDimZ();
Jason Sams709a0972012-11-15 18:18:04 -0800282 alloc->mHal.drvState.lod[0].mallocPtr = 0;
Stephen Hines94999c32013-02-05 16:58:22 -0800283 // Stride needs to be 16-byte aligned too!
284 size_t stride = alloc->mHal.drvState.lod[0].dimX * type->getElementSizeBytes();
285 alloc->mHal.drvState.lod[0].stride = rsRound(stride, 16);
Jason Sams709a0972012-11-15 18:18:04 -0800286 alloc->mHal.drvState.lodCount = type->getLODCount();
287 alloc->mHal.drvState.faceCount = type->getDimFaces();
Jason Sams807fdc42012-07-25 17:55:39 -0700288
289 size_t offsets[Allocation::MAX_LOD];
290 memset(offsets, 0, sizeof(offsets));
291
Jason Sams709a0972012-11-15 18:18:04 -0800292 size_t o = alloc->mHal.drvState.lod[0].stride * rsMax(alloc->mHal.drvState.lod[0].dimY, 1u) *
293 rsMax(alloc->mHal.drvState.lod[0].dimZ, 1u);
294 if(alloc->mHal.drvState.lodCount > 1) {
295 uint32_t tx = alloc->mHal.drvState.lod[0].dimX;
296 uint32_t ty = alloc->mHal.drvState.lod[0].dimY;
297 uint32_t tz = alloc->mHal.drvState.lod[0].dimZ;
298 for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
299 alloc->mHal.drvState.lod[lod].dimX = tx;
300 alloc->mHal.drvState.lod[lod].dimY = ty;
301 alloc->mHal.drvState.lod[lod].dimZ = tz;
Stephen Hines94999c32013-02-05 16:58:22 -0800302 alloc->mHal.drvState.lod[lod].stride =
303 rsRound(tx * type->getElementSizeBytes(), 16);
Jason Sams807fdc42012-07-25 17:55:39 -0700304 offsets[lod] = o;
Jason Sams709a0972012-11-15 18:18:04 -0800305 o += alloc->mHal.drvState.lod[lod].stride * rsMax(ty, 1u) * rsMax(tz, 1u);
Jason Sams807fdc42012-07-25 17:55:39 -0700306 if (tx > 1) tx >>= 1;
307 if (ty > 1) ty >>= 1;
308 if (tz > 1) tz >>= 1;
309 }
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800310 } else if (alloc->mHal.state.yuv) {
Jason Sams733396b2013-02-22 12:46:18 -0800311 DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState);
312
313 for (uint32_t ct = 1; ct < alloc->mHal.drvState.lodCount; ct++) {
314 offsets[ct] = (size_t)alloc->mHal.drvState.lod[ct].mallocPtr;
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800315 }
Jason Sams807fdc42012-07-25 17:55:39 -0700316 }
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800317
Jason Sams709a0972012-11-15 18:18:04 -0800318 alloc->mHal.drvState.faceOffset = o;
Jason Sams807fdc42012-07-25 17:55:39 -0700319
Jason Sams709a0972012-11-15 18:18:04 -0800320 alloc->mHal.drvState.lod[0].mallocPtr = ptr;
321 for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
322 alloc->mHal.drvState.lod[lod].mallocPtr = ptr + offsets[lod];
Jason Sams463bfce2012-08-21 13:54:42 -0700323 }
Jason Sams463bfce2012-08-21 13:54:42 -0700324
Jason Sams709a0972012-11-15 18:18:04 -0800325 size_t allocSize = alloc->mHal.drvState.faceOffset;
326 if(alloc->mHal.drvState.faceCount) {
Jason Sams463bfce2012-08-21 13:54:42 -0700327 allocSize *= 6;
328 }
329
330 return allocSize;
331}
332
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800333static uint8_t* allocAlignedMemory(size_t allocSize, bool forceZero) {
334 // We align all allocations to a 16-byte boundary.
335 uint8_t* ptr = (uint8_t *)memalign(16, allocSize);
336 if (!ptr) {
337 return NULL;
338 }
339 if (forceZero) {
340 memset(ptr, 0, allocSize);
341 }
342 return ptr;
343}
344
Jason Sams463bfce2012-08-21 13:54:42 -0700345bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) {
346 DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation));
347 if (!drv) {
348 return false;
349 }
350 alloc->mHal.drv = drv;
351
352 // Calculate the object size.
353 size_t allocSize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), NULL);
354
Jason Sams807fdc42012-07-25 17:55:39 -0700355 uint8_t * ptr = NULL;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800356 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) {
Jason Sams733396b2013-02-22 12:46:18 -0800357
358 } else if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
359 // Allocation is allocated when the surface is created
360 // in getSurface
Tim Murray2e1a94d2012-11-29 13:12:25 -0800361 } else if (alloc->mHal.state.userProvidedPtr != NULL) {
362 // user-provided allocation
363 // limitations: no faces, no LOD, USAGE_SCRIPT only
Tim Murray9e2bda52012-12-18 12:05:46 -0800364 if (alloc->mHal.state.usageFlags != (RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED)) {
365 ALOGE("Can't use user-allocated buffers if usage is not USAGE_SCRIPT and USAGE_SHARED");
Tim Murray2e1a94d2012-11-29 13:12:25 -0800366 return false;
367 }
368 if (alloc->getType()->getDimLOD() || alloc->getType()->getDimFaces()) {
369 ALOGE("User-allocated buffers must not have multiple faces or LODs");
370 return false;
371 }
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800372
373 // rows must be 16-byte aligned
374 // validate that here, otherwise fall back to not use the user-backed allocation
375 if (((alloc->getType()->getDimX() * alloc->getType()->getElement()->getSizeBytes()) % 16) != 0) {
376 ALOGV("User-backed allocation failed stride requirement, falling back to separate allocation");
377 drv->useUserProvidedPtr = false;
378
379 ptr = allocAlignedMemory(allocSize, forceZero);
380 if (!ptr) {
381 alloc->mHal.drv = NULL;
382 free(drv);
383 return false;
384 }
385
386 } else {
387 drv->useUserProvidedPtr = true;
388 ptr = (uint8_t*)alloc->mHal.state.userProvidedPtr;
389 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800390 } else {
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800391 ptr = allocAlignedMemory(allocSize, forceZero);
Jason Sams179e9a42011-11-23 15:02:15 -0800392 if (!ptr) {
You Kimed419f32012-12-17 03:42:57 +0900393 alloc->mHal.drv = NULL;
Jason Sams179e9a42011-11-23 15:02:15 -0800394 free(drv);
395 return false;
396 }
Jason Samseb4fe182011-05-26 16:33:01 -0700397 }
Jason Sams463bfce2012-08-21 13:54:42 -0700398 // Build the pointer tables
399 size_t verifySize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), ptr);
400 if(allocSize != verifySize) {
401 rsAssert(!"Size mismatch");
Jason Sams807fdc42012-07-25 17:55:39 -0700402 }
Jason Samseb4fe182011-05-26 16:33:01 -0700403
404 drv->glTarget = GL_NONE;
405 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
406 if (alloc->mHal.state.hasFaces) {
407 drv->glTarget = GL_TEXTURE_CUBE_MAP;
408 } else {
409 drv->glTarget = GL_TEXTURE_2D;
410 }
411 } else {
412 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
413 drv->glTarget = GL_ARRAY_BUFFER;
414 }
415 }
416
Jason Sams93eacc72012-12-18 14:26:57 -0800417#ifndef RS_COMPATIBILITY_LIB
Jason Samsa614ae12011-05-26 17:05:51 -0700418 drv->glType = rsdTypeToGLType(alloc->mHal.state.type->getElement()->getComponent().getType());
419 drv->glFormat = rsdKindToGLFormat(alloc->mHal.state.type->getElement()->getComponent().getKind());
Jason Sams93eacc72012-12-18 14:26:57 -0800420#else
421 drv->glType = 0;
422 drv->glFormat = 0;
423#endif
Jason Samsa614ae12011-05-26 17:05:51 -0700424
Jason Samseb4fe182011-05-26 16:33:01 -0700425 if (alloc->mHal.state.usageFlags & ~RS_ALLOCATION_USAGE_SCRIPT) {
426 drv->uploadDeferred = true;
427 }
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700428
Jason Samsb3220332012-04-02 14:41:54 -0700429
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700430 drv->readBackFBO = NULL;
431
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800432 // fill out the initial state of the buffer if we couldn't use the user-provided ptr and USAGE_SHARED was accepted
433 if ((alloc->mHal.state.userProvidedPtr != 0) && (drv->useUserProvidedPtr == false)) {
434 rsdAllocationData2D(rsc, alloc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, alloc->getType()->getDimX(), alloc->getType()->getDimY(), alloc->mHal.state.userProvidedPtr, allocSize, 0);
435 }
436
Jason Samseb4fe182011-05-26 16:33:01 -0700437 return true;
438}
439
440void rsdAllocationDestroy(const Context *rsc, Allocation *alloc) {
441 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
442
Jason Sams93eacc72012-12-18 14:26:57 -0800443#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700444 if (drv->bufferID) {
445 // Causes a SW crash....
Steve Block65982012011-10-20 11:56:00 +0100446 //ALOGV(" mBufferID %i", mBufferID);
Jason Samseb4fe182011-05-26 16:33:01 -0700447 //glDeleteBuffers(1, &mBufferID);
448 //mBufferID = 0;
449 }
450 if (drv->textureID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700451 RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID);
Jason Samseb4fe182011-05-26 16:33:01 -0700452 drv->textureID = 0;
453 }
454 if (drv->renderTargetID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700455 RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID);
Jason Samseb4fe182011-05-26 16:33:01 -0700456 drv->renderTargetID = 0;
457 }
Jason Sams93eacc72012-12-18 14:26:57 -0800458#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700459
Jason Sams709a0972012-11-15 18:18:04 -0800460 if (alloc->mHal.drvState.lod[0].mallocPtr) {
Tim Murray2e1a94d2012-11-29 13:12:25 -0800461 // don't free user-allocated ptrs
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800462 if (!(drv->useUserProvidedPtr)) {
Tim Murray2e1a94d2012-11-29 13:12:25 -0800463 free(alloc->mHal.drvState.lod[0].mallocPtr);
464 }
Jason Sams709a0972012-11-15 18:18:04 -0800465 alloc->mHal.drvState.lod[0].mallocPtr = NULL;
Jason Samseb4fe182011-05-26 16:33:01 -0700466 }
Jason Sams93eacc72012-12-18 14:26:57 -0800467
468#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700469 if (drv->readBackFBO != NULL) {
470 delete drv->readBackFBO;
471 drv->readBackFBO = NULL;
472 }
Jason Sams10c9dd72013-02-01 10:53:42 -0800473
474 if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) &&
475 (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
476
477 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Tim Murray3a25fdd2013-02-22 17:56:56 -0800478 ANativeWindow *nw = drv->wndSurface;
479 if (nw) {
480 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
481 mapper.unlock(drv->wndBuffer->handle);
482 int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
483 }
Jason Sams10c9dd72013-02-01 10:53:42 -0800484 }
Jason Sams93eacc72012-12-18 14:26:57 -0800485#endif
486
Jason Samseb4fe182011-05-26 16:33:01 -0700487 free(drv);
488 alloc->mHal.drv = NULL;
489}
490
491void rsdAllocationResize(const Context *rsc, const Allocation *alloc,
492 const Type *newType, bool zeroNew) {
Jason Samsa572aca2013-01-09 11:52:26 -0800493 const uint32_t oldDimX = alloc->mHal.drvState.lod[0].dimX;
494 const uint32_t dimX = newType->getDimX();
495
Tim Murray9e2bda52012-12-18 12:05:46 -0800496 // can't resize Allocations with user-allocated buffers
497 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) {
498 ALOGE("Resize cannot be called on a USAGE_SHARED allocation");
499 return;
500 }
Jason Sams709a0972012-11-15 18:18:04 -0800501 void * oldPtr = alloc->mHal.drvState.lod[0].mallocPtr;
Jason Sams463bfce2012-08-21 13:54:42 -0700502 // Calculate the object size
503 size_t s = AllocationBuildPointerTable(rsc, alloc, newType, NULL);
504 uint8_t *ptr = (uint8_t *)realloc(oldPtr, s);
505 // Build the relative pointer tables.
506 size_t verifySize = AllocationBuildPointerTable(rsc, alloc, newType, ptr);
507 if(s != verifySize) {
508 rsAssert(!"Size mismatch");
509 }
Jason Samseb4fe182011-05-26 16:33:01 -0700510
Jason Samseb4fe182011-05-26 16:33:01 -0700511
512 if (dimX > oldDimX) {
Jason Sams463bfce2012-08-21 13:54:42 -0700513 uint32_t stride = alloc->mHal.state.elementSizeBytes;
Jason Sams709a0972012-11-15 18:18:04 -0800514 memset(((uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr) + stride * oldDimX,
Jason Sams2275e9c2012-04-16 17:01:26 -0700515 0, stride * (dimX - oldDimX));
Jason Samseb4fe182011-05-26 16:33:01 -0700516 }
517}
518
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700519static void rsdAllocationSyncFromFBO(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800520#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700521 if (!alloc->getIsScript()) {
522 return; // nothing to sync
523 }
524
525 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
526 RsdFrameBufferObj *lastFbo = dc->gl.currentFrameBuffer;
527
528 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
529 if (!drv->textureID && !drv->renderTargetID) {
530 return; // nothing was rendered here yet, so nothing to sync
531 }
532 if (drv->readBackFBO == NULL) {
533 drv->readBackFBO = new RsdFrameBufferObj();
534 drv->readBackFBO->setColorTarget(drv, 0);
535 drv->readBackFBO->setDimensions(alloc->getType()->getDimX(),
536 alloc->getType()->getDimY());
537 }
538
539 // Bind the framebuffer object so we can read back from it
540 drv->readBackFBO->setActive(rsc);
541
542 // Do the readback
Jason Sams709a0972012-11-15 18:18:04 -0800543 RSD_CALL_GL(glReadPixels, 0, 0, alloc->mHal.drvState.lod[0].dimX,
544 alloc->mHal.drvState.lod[0].dimY,
545 drv->glFormat, drv->glType, alloc->mHal.drvState.lod[0].mallocPtr);
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700546
547 // Revert framebuffer to its original
548 lastFbo->setActive(rsc);
Jason Sams93eacc72012-12-18 14:26:57 -0800549#endif
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700550}
Jason Samseb4fe182011-05-26 16:33:01 -0700551
552
553void rsdAllocationSyncAll(const Context *rsc, const Allocation *alloc,
554 RsAllocationUsageType src) {
555 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
556
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700557 if (src == RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
558 if(!alloc->getIsRenderTarget()) {
559 rsc->setError(RS_ERROR_FATAL_DRIVER,
560 "Attempting to sync allocation from render target, "
561 "for non-render target allocation");
562 } else if (alloc->getType()->getElement()->getKind() != RS_KIND_PIXEL_RGBA) {
563 rsc->setError(RS_ERROR_FATAL_DRIVER, "Cannot only sync from RGBA"
564 "render target");
565 } else {
566 rsdAllocationSyncFromFBO(rsc, alloc);
567 }
Jason Samseb4fe182011-05-26 16:33:01 -0700568 return;
569 }
570
571 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
572
573 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
574 UploadToTexture(rsc, alloc);
575 } else {
Jason Samsb3220332012-04-02 14:41:54 -0700576 if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) &&
Jason Sams0dc66932012-04-10 17:05:49 -0700577 !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
Jason Samseb4fe182011-05-26 16:33:01 -0700578 AllocateRenderTarget(rsc, alloc);
579 }
580 }
581 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
582 UploadToBufferObject(rsc, alloc);
583 }
584
585 drv->uploadDeferred = false;
586}
587
588void rsdAllocationMarkDirty(const Context *rsc, const Allocation *alloc) {
589 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
590 drv->uploadDeferred = true;
591}
592
Jason Sams733396b2013-02-22 12:46:18 -0800593void* rsdAllocationGetSurface(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800594#ifndef RS_COMPATIBILITY_LIB
Jason Sams41e373d2012-01-13 14:01:20 -0800595 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Jason Sams733396b2013-02-22 12:46:18 -0800596
597 drv->cpuConsumer = new CpuConsumer(2);
598 sp<IGraphicBufferProducer> bp = drv->cpuConsumer->getProducerInterface();
599 bp->incStrong(NULL);
600 return bp.get();
Jason Sams93eacc72012-12-18 14:26:57 -0800601#else
Jason Sams733396b2013-02-22 12:46:18 -0800602 return NULL;
Jason Sams93eacc72012-12-18 14:26:57 -0800603#endif
Jason Sams41e373d2012-01-13 14:01:20 -0800604}
605
Jason Sams93eacc72012-12-18 14:26:57 -0800606#ifndef RS_COMPATIBILITY_LIB
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800607static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
608 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
609
Jamie Genniscd919a12012-06-13 16:34:11 -0700610 int32_t r = native_window_dequeue_buffer_and_wait(nw, &drv->wndBuffer);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800611 if (r) {
612 rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer.");
613 return false;
614 }
615
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800616 // Must lock the whole surface
617 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
618 Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height);
619
620 void *dst = NULL;
621 mapper.lock(drv->wndBuffer->handle,
622 GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN,
623 bounds, &dst);
Jason Sams709a0972012-11-15 18:18:04 -0800624 alloc->mHal.drvState.lod[0].mallocPtr = dst;
625 alloc->mHal.drvState.lod[0].stride = drv->wndBuffer->stride * alloc->mHal.state.elementSizeBytes;
Stephen Hines94999c32013-02-05 16:58:22 -0800626 rsAssert((alloc->mHal.drvState.lod[0].stride & 0xf) == 0);
Jason Samsb3220332012-04-02 14:41:54 -0700627
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800628 return true;
629}
Jason Sams93eacc72012-12-18 14:26:57 -0800630#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800631
Jason Sams733396b2013-02-22 12:46:18 -0800632void rsdAllocationSetSurface(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
Jason Sams93eacc72012-12-18 14:26:57 -0800633#ifndef RS_COMPATIBILITY_LIB
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800634 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Tim Murray3a25fdd2013-02-22 17:56:56 -0800635 ANativeWindow *old = drv->wndSurface;
636
637 if (nw) {
638 nw->incStrong(NULL);
639 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800640
Jason Samsb3220332012-04-02 14:41:54 -0700641 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
642 //TODO finish support for render target + script
643 drv->wnd = nw;
644 return;
645 }
646
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800647 // Cleanup old surface if there is one.
Tim Murray3a25fdd2013-02-22 17:56:56 -0800648 if (drv->wndSurface) {
649 ANativeWindow *old = drv->wndSurface;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800650 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
651 mapper.unlock(drv->wndBuffer->handle);
Tim Murray3a25fdd2013-02-22 17:56:56 -0800652 old->cancelBuffer(old, drv->wndBuffer, -1);
653 drv->wndSurface = NULL;
654 old->decStrong(NULL);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800655 }
656
657 if (nw != NULL) {
658 int32_t r;
Jason Samsb3220332012-04-02 14:41:54 -0700659 uint32_t flags = 0;
Tim Murray3a25fdd2013-02-22 17:56:56 -0800660 r = native_window_set_buffer_count(nw, 3);
661 if (r) {
662 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer count.");
663 goto error;
664 }
665
Jason Samsb3220332012-04-02 14:41:54 -0700666 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
667 flags |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN;
668 }
669 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
670 flags |= GRALLOC_USAGE_HW_RENDER;
671 }
672
673 r = native_window_set_usage(nw, flags);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800674 if (r) {
675 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
Tim Murray3a25fdd2013-02-22 17:56:56 -0800676 goto error;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800677 }
678
Jason Samsa572aca2013-01-09 11:52:26 -0800679 r = native_window_set_buffers_dimensions(nw, alloc->mHal.drvState.lod[0].dimX,
680 alloc->mHal.drvState.lod[0].dimY);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800681 if (r) {
682 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions.");
Tim Murray3a25fdd2013-02-22 17:56:56 -0800683 goto error;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800684 }
685
Jason Sams733396b2013-02-22 12:46:18 -0800686 int format = 0;
687 const Element *e = alloc->mHal.state.type->getElement();
688 switch(e->getType()) {
689 case RS_TYPE_UNSIGNED_8:
690 switch (e->getVectorSize()) {
691 case 1:
692 rsAssert(e->getKind() == RS_KIND_PIXEL_A);
693 format = PIXEL_FORMAT_A_8;
694 break;
695 case 4:
696 rsAssert(e->getKind() == RS_KIND_PIXEL_RGBA);
697 format = PIXEL_FORMAT_RGBA_8888;
698 break;
699 default:
700 rsAssert(0);
701 }
702 break;
703 default:
704 rsAssert(0);
705 }
706
707 r = native_window_set_buffers_format(nw, format);
708 if (r) {
709 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer format.");
Tim Murray3a25fdd2013-02-22 17:56:56 -0800710 goto error;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800711 }
712
713 IoGetBuffer(rsc, alloc, nw);
Tim Murray3a25fdd2013-02-22 17:56:56 -0800714 drv->wndSurface = nw;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800715 }
Tim Murray3a25fdd2013-02-22 17:56:56 -0800716
717 return;
718
719 error:
720
721 if (nw) {
722 nw->decStrong(NULL);
723 }
724
725
Jason Sams93eacc72012-12-18 14:26:57 -0800726#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800727}
728
729void rsdAllocationIoSend(const Context *rsc, Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800730#ifndef RS_COMPATIBILITY_LIB
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800731 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Tim Murray3a25fdd2013-02-22 17:56:56 -0800732 ANativeWindow *nw = drv->wndSurface;
Jason Samsb3220332012-04-02 14:41:54 -0700733 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
734 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
735 RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800736 return;
737 }
Tim Murray3a25fdd2013-02-22 17:56:56 -0800738 if (nw) {
739 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
740 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
741 mapper.unlock(drv->wndBuffer->handle);
742 int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
743 if (r) {
744 rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer.");
745 return;
746 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800747
Tim Murray3a25fdd2013-02-22 17:56:56 -0800748 IoGetBuffer(rsc, alloc, nw);
Jason Samsb3220332012-04-02 14:41:54 -0700749 }
Tim Murray3a25fdd2013-02-22 17:56:56 -0800750 } else {
751 rsc->setError(RS_ERROR_DRIVER, "Sent IO buffer with no attached surface.");
752 return;
Jason Samsb3220332012-04-02 14:41:54 -0700753 }
Jason Sams93eacc72012-12-18 14:26:57 -0800754#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800755}
756
757void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800758#ifndef RS_COMPATIBILITY_LIB
Jason Sams3522f402012-03-23 11:47:26 -0700759 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Jason Sams733396b2013-02-22 12:46:18 -0800760
761 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
762 if (drv->lb.data != NULL) {
763 drv->cpuConsumer->unlockBuffer(drv->lb);
764 }
765
766 status_t ret = drv->cpuConsumer->lockNextBuffer(&drv->lb);
767 alloc->mHal.drvState.lod[0].mallocPtr = drv->lb.data;
768 alloc->mHal.drvState.lod[0].stride = drv->lb.stride * alloc->mHal.state.elementSizeBytes;
769
770 if (alloc->mHal.state.yuv) {
771 DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState);
772 }
773
774 } else {
Tim Murray3a25fdd2013-02-22 17:56:56 -0800775 drv->surfaceTexture->updateTexImage();
Jason Sams733396b2013-02-22 12:46:18 -0800776 }
777
778
Jason Sams93eacc72012-12-18 14:26:57 -0800779#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800780}
781
782
Jason Samseb4fe182011-05-26 16:33:01 -0700783void rsdAllocationData1D(const Context *rsc, const Allocation *alloc,
784 uint32_t xoff, uint32_t lod, uint32_t count,
Alex Sakhartchoukc794cd52012-02-13 11:57:32 -0800785 const void *data, size_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -0700786 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
787
788 const uint32_t eSize = alloc->mHal.state.type->getElementSizeBytes();
Jason Sams807fdc42012-07-25 17:55:39 -0700789 uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Jason Samseb4fe182011-05-26 16:33:01 -0700790 uint32_t size = count * eSize;
791
Stephen Hines20c535f2012-12-06 19:04:38 -0800792 if (ptr != data) {
793 // Skip the copy if we are the same allocation. This can arise from
794 // our Bitmap optimization, where we share the same storage.
795 if (alloc->mHal.state.hasReferences) {
796 alloc->incRefs(data, count);
797 alloc->decRefs(ptr, count);
798 }
799 memcpy(ptr, data, size);
Jason Samseb4fe182011-05-26 16:33:01 -0700800 }
Jason Samseb4fe182011-05-26 16:33:01 -0700801 drv->uploadDeferred = true;
802}
803
804void rsdAllocationData2D(const Context *rsc, const Allocation *alloc,
805 uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800806 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Jason Samseb4fe182011-05-26 16:33:01 -0700807 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
808
809 uint32_t eSize = alloc->mHal.state.elementSizeBytes;
810 uint32_t lineSize = eSize * w;
Tim Murray358747a2012-11-26 13:52:04 -0800811 if (!stride) {
812 stride = lineSize;
813 }
Jason Samseb4fe182011-05-26 16:33:01 -0700814
Jason Sams709a0972012-11-15 18:18:04 -0800815 if (alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Samseb4fe182011-05-26 16:33:01 -0700816 const uint8_t *src = static_cast<const uint8_t *>(data);
Jason Sams807fdc42012-07-25 17:55:39 -0700817 uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, lod, face);
Stephen Hines20c535f2012-12-06 19:04:38 -0800818 if (dst == src) {
819 // Skip the copy if we are the same allocation. This can arise from
820 // our Bitmap optimization, where we share the same storage.
821 drv->uploadDeferred = true;
822 return;
823 }
Jason Samseb4fe182011-05-26 16:33:01 -0700824
825 for (uint32_t line=yoff; line < (yoff+h); line++) {
826 if (alloc->mHal.state.hasReferences) {
827 alloc->incRefs(src, w);
828 alloc->decRefs(dst, w);
829 }
830 memcpy(dst, src, lineSize);
Tim Murray358747a2012-11-26 13:52:04 -0800831 src += stride;
Jason Sams709a0972012-11-15 18:18:04 -0800832 dst += alloc->mHal.drvState.lod[lod].stride;
Jason Samseb4fe182011-05-26 16:33:01 -0700833 }
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800834 if (alloc->mHal.state.yuv) {
835 int lod = 1;
836 while (alloc->mHal.drvState.lod[lod].mallocPtr) {
837 uint32_t lineSize = alloc->mHal.drvState.lod[lod].dimX;
838 uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, lod, face);
839
840 for (uint32_t line=(yoff >> 1); line < ((yoff+h)>>1); line++) {
841 memcpy(dst, src, lineSize);
842 src += lineSize;
843 dst += alloc->mHal.drvState.lod[lod].stride;
844 }
845 lod++;
846 }
847
848 }
Jason Samseb4fe182011-05-26 16:33:01 -0700849 drv->uploadDeferred = true;
850 } else {
Jason Sams2382aba2011-09-13 15:41:01 -0700851 Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h);
Jason Samseb4fe182011-05-26 16:33:01 -0700852 }
853}
854
855void rsdAllocationData3D(const Context *rsc, const Allocation *alloc,
856 uint32_t xoff, uint32_t yoff, uint32_t zoff,
857 uint32_t lod, RsAllocationCubemapFace face,
858 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
859
860}
861
Jason Sams807fdc42012-07-25 17:55:39 -0700862void rsdAllocationRead1D(const Context *rsc, const Allocation *alloc,
863 uint32_t xoff, uint32_t lod, uint32_t count,
864 void *data, size_t sizeBytes) {
Jason Sams807fdc42012-07-25 17:55:39 -0700865 const uint32_t eSize = alloc->mHal.state.type->getElementSizeBytes();
866 const uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Stephen Hines20c535f2012-12-06 19:04:38 -0800867 if (data != ptr) {
868 // Skip the copy if we are the same allocation. This can arise from
869 // our Bitmap optimization, where we share the same storage.
870 memcpy(data, ptr, count * eSize);
871 }
Jason Sams807fdc42012-07-25 17:55:39 -0700872}
873
874void rsdAllocationRead2D(const Context *rsc, const Allocation *alloc,
Tim Murray358747a2012-11-26 13:52:04 -0800875 uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
876 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
Jason Sams807fdc42012-07-25 17:55:39 -0700877 uint32_t eSize = alloc->mHal.state.elementSizeBytes;
878 uint32_t lineSize = eSize * w;
Tim Murray358747a2012-11-26 13:52:04 -0800879 if (!stride) {
880 stride = lineSize;
881 }
Jason Sams807fdc42012-07-25 17:55:39 -0700882
Jason Sams709a0972012-11-15 18:18:04 -0800883 if (alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Sams807fdc42012-07-25 17:55:39 -0700884 uint8_t *dst = static_cast<uint8_t *>(data);
885 const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, lod, face);
Stephen Hines20c535f2012-12-06 19:04:38 -0800886 if (dst == src) {
887 // Skip the copy if we are the same allocation. This can arise from
888 // our Bitmap optimization, where we share the same storage.
889 return;
890 }
Jason Sams807fdc42012-07-25 17:55:39 -0700891
892 for (uint32_t line=yoff; line < (yoff+h); line++) {
893 memcpy(dst, src, lineSize);
Tim Murray358747a2012-11-26 13:52:04 -0800894 dst += stride;
Jason Sams709a0972012-11-15 18:18:04 -0800895 src += alloc->mHal.drvState.lod[lod].stride;
Jason Sams807fdc42012-07-25 17:55:39 -0700896 }
897 } else {
898 ALOGE("Add code to readback from non-script memory");
899 }
900}
901
Tim Murray358747a2012-11-26 13:52:04 -0800902
Jason Sams807fdc42012-07-25 17:55:39 -0700903void rsdAllocationRead3D(const Context *rsc, const Allocation *alloc,
904 uint32_t xoff, uint32_t yoff, uint32_t zoff,
905 uint32_t lod, RsAllocationCubemapFace face,
906 uint32_t w, uint32_t h, uint32_t d, void *data, uint32_t sizeBytes) {
907
908}
909
910void * rsdAllocationLock1D(const android::renderscript::Context *rsc,
911 const android::renderscript::Allocation *alloc) {
Jason Sams709a0972012-11-15 18:18:04 -0800912 return alloc->mHal.drvState.lod[0].mallocPtr;
Jason Sams807fdc42012-07-25 17:55:39 -0700913}
914
915void rsdAllocationUnlock1D(const android::renderscript::Context *rsc,
916 const android::renderscript::Allocation *alloc) {
917
918}
919
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700920void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc,
921 const android::renderscript::Allocation *dstAlloc,
922 uint32_t dstXoff, uint32_t dstLod, uint32_t count,
923 const android::renderscript::Allocation *srcAlloc,
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700924 uint32_t srcXoff, uint32_t srcLod) {
925}
926
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700927
928void rsdAllocationData2D_alloc_script(const android::renderscript::Context *rsc,
929 const android::renderscript::Allocation *dstAlloc,
930 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
931 RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
932 const android::renderscript::Allocation *srcAlloc,
933 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
934 RsAllocationCubemapFace srcFace) {
935 uint32_t elementSize = dstAlloc->getType()->getElementSizeBytes();
936 for (uint32_t i = 0; i < h; i ++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700937 uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, dstLod, dstFace);
938 uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, srcLod, srcFace);
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700939 memcpy(dstPtr, srcPtr, w * elementSize);
940
Steve Blockaf12ac62012-01-06 19:20:56 +0000941 //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 -0700942 // dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace);
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700943 }
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700944}
945
946void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc,
947 const android::renderscript::Allocation *dstAlloc,
948 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
949 RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
950 const android::renderscript::Allocation *srcAlloc,
951 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
952 RsAllocationCubemapFace srcFace) {
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700953 if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) {
954 rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not "
955 "yet implemented.");
956 return;
957 }
958 rsdAllocationData2D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff,
959 dstLod, dstFace, w, h, srcAlloc,
960 srcXoff, srcYoff, srcLod, srcFace);
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700961}
962
963void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc,
964 const android::renderscript::Allocation *dstAlloc,
965 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
966 uint32_t dstLod, RsAllocationCubemapFace dstFace,
967 uint32_t w, uint32_t h, uint32_t d,
968 const android::renderscript::Allocation *srcAlloc,
969 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
970 uint32_t srcLod, RsAllocationCubemapFace srcFace) {
971}
972
Jason Samseb4fe182011-05-26 16:33:01 -0700973void rsdAllocationElementData1D(const Context *rsc, const Allocation *alloc,
974 uint32_t x,
975 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
976 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
977
978 uint32_t eSize = alloc->mHal.state.elementSizeBytes;
Jason Sams807fdc42012-07-25 17:55:39 -0700979 uint8_t * ptr = GetOffsetPtr(alloc, x, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Jason Samseb4fe182011-05-26 16:33:01 -0700980
981 const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
982 ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
983
984 if (alloc->mHal.state.hasReferences) {
985 e->incRefs(data);
986 e->decRefs(ptr);
987 }
988
989 memcpy(ptr, data, sizeBytes);
990 drv->uploadDeferred = true;
991}
992
993void rsdAllocationElementData2D(const Context *rsc, const Allocation *alloc,
994 uint32_t x, uint32_t y,
995 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
996 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
997
998 uint32_t eSize = alloc->mHal.state.elementSizeBytes;
Jason Sams807fdc42012-07-25 17:55:39 -0700999 uint8_t * ptr = GetOffsetPtr(alloc, x, y, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Jason Samseb4fe182011-05-26 16:33:01 -07001000
1001 const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
1002 ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
1003
1004 if (alloc->mHal.state.hasReferences) {
1005 e->incRefs(data);
1006 e->decRefs(ptr);
1007 }
1008
1009 memcpy(ptr, data, sizeBytes);
1010 drv->uploadDeferred = true;
1011}
1012
Jason Sams61a4bb72012-07-25 19:33:43 -07001013static void mip565(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -08001014 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1015 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
Jason Sams61a4bb72012-07-25 19:33:43 -07001016
1017 for (uint32_t y=0; y < h; y++) {
1018 uint16_t *oPtr = (uint16_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face);
1019 const uint16_t *i1 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2, lod, face);
1020 const uint16_t *i2 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face);
1021
1022 for (uint32_t x=0; x < w; x++) {
1023 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
1024 oPtr ++;
1025 i1 += 2;
1026 i2 += 2;
1027 }
1028 }
1029}
1030
1031static void mip8888(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -08001032 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1033 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
Jason Sams61a4bb72012-07-25 19:33:43 -07001034
1035 for (uint32_t y=0; y < h; y++) {
1036 uint32_t *oPtr = (uint32_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face);
1037 const uint32_t *i1 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2, lod, face);
1038 const uint32_t *i2 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face);
1039
1040 for (uint32_t x=0; x < w; x++) {
1041 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
1042 oPtr ++;
1043 i1 += 2;
1044 i2 += 2;
1045 }
1046 }
1047}
1048
1049static void mip8(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -08001050 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1051 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
Jason Sams61a4bb72012-07-25 19:33:43 -07001052
1053 for (uint32_t y=0; y < h; y++) {
1054 uint8_t *oPtr = GetOffsetPtr(alloc, 0, y, lod + 1, face);
1055 const uint8_t *i1 = GetOffsetPtr(alloc, 0, y*2, lod, face);
1056 const uint8_t *i2 = GetOffsetPtr(alloc, 0, y*2+1, lod, face);
1057
1058 for (uint32_t x=0; x < w; x++) {
1059 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
1060 oPtr ++;
1061 i1 += 2;
1062 i2 += 2;
1063 }
1064 }
1065}
1066
1067void rsdAllocationGenerateMipmaps(const Context *rsc, const Allocation *alloc) {
Jason Sams709a0972012-11-15 18:18:04 -08001068 if(!alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Sams61a4bb72012-07-25 19:33:43 -07001069 return;
1070 }
1071 uint32_t numFaces = alloc->getType()->getDimFaces() ? 6 : 1;
1072 for (uint32_t face = 0; face < numFaces; face ++) {
1073 for (uint32_t lod=0; lod < (alloc->getType()->getLODCount() -1); lod++) {
1074 switch (alloc->getType()->getElement()->getSizeBits()) {
1075 case 32:
1076 mip8888(alloc, lod, (RsAllocationCubemapFace)face);
1077 break;
1078 case 16:
1079 mip565(alloc, lod, (RsAllocationCubemapFace)face);
1080 break;
1081 case 8:
1082 mip8(alloc, lod, (RsAllocationCubemapFace)face);
1083 break;
1084 }
1085 }
1086 }
1087}
1088
Jason Samseb4fe182011-05-26 16:33:01 -07001089