blob: 246e67c3e109129c1b1fa589c6b4d618759e8e02 [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
Jason Samseb4fe182011-05-26 16:33:01 -070017#include "rsdCore.h"
Jason Samseb4fe182011-05-26 16:33:01 -070018#include "rsdAllocation.h"
19
20#include "rsAllocation.h"
21
Tim Murray0b575de2013-03-15 15:56:43 -070022#ifndef RS_SERVER
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"
Tim Murray0b575de2013-03-15 15:56:43 -070026#endif
Jason Sams93eacc72012-12-18 14:26:57 -080027
28#ifndef RS_COMPATIBILITY_LIB
29#include "rsdFrameBufferObj.h"
Andy McFadden58fd6a52012-12-18 09:50:03 -080030#include "gui/GLConsumer.h"
Jason Sams733396b2013-02-22 12:46:18 -080031#include "gui/CpuConsumer.h"
32#include "gui/Surface.h"
Jason Sams93eacc72012-12-18 14:26:57 -080033#include "hardware/gralloc.h"
Jason Sams7ac2a4d2012-02-15 12:04:24 -080034
Jason Samseb4fe182011-05-26 16:33:01 -070035#include <GLES/gl.h>
36#include <GLES2/gl2.h>
37#include <GLES/glext.h>
Jason Sams93eacc72012-12-18 14:26:57 -080038#endif
Jason Samseb4fe182011-05-26 16:33:01 -070039
Tim Murray0b575de2013-03-15 15:56:43 -070040#ifdef RS_SERVER
41// server requires malloc.h for memalign
42#include <malloc.h>
43#endif
44
Jason Samseb4fe182011-05-26 16:33:01 -070045using namespace android;
46using namespace android::renderscript;
47
48
Jason Sams93eacc72012-12-18 14:26:57 -080049#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -070050const static GLenum gFaceOrder[] = {
51 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
52 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
53 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
54 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
55 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
56 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
57};
58
Jason Samsa614ae12011-05-26 17:05:51 -070059GLenum rsdTypeToGLType(RsDataType t) {
60 switch (t) {
61 case RS_TYPE_UNSIGNED_5_6_5: return GL_UNSIGNED_SHORT_5_6_5;
62 case RS_TYPE_UNSIGNED_5_5_5_1: return GL_UNSIGNED_SHORT_5_5_5_1;
63 case RS_TYPE_UNSIGNED_4_4_4_4: return GL_UNSIGNED_SHORT_4_4_4_4;
64
65 //case RS_TYPE_FLOAT_16: return GL_HALF_FLOAT;
66 case RS_TYPE_FLOAT_32: return GL_FLOAT;
67 case RS_TYPE_UNSIGNED_8: return GL_UNSIGNED_BYTE;
68 case RS_TYPE_UNSIGNED_16: return GL_UNSIGNED_SHORT;
69 case RS_TYPE_SIGNED_8: return GL_BYTE;
70 case RS_TYPE_SIGNED_16: return GL_SHORT;
71 default: break;
72 }
73 return 0;
74}
75
76GLenum rsdKindToGLFormat(RsDataKind k) {
77 switch (k) {
78 case RS_KIND_PIXEL_L: return GL_LUMINANCE;
79 case RS_KIND_PIXEL_A: return GL_ALPHA;
80 case RS_KIND_PIXEL_LA: return GL_LUMINANCE_ALPHA;
81 case RS_KIND_PIXEL_RGB: return GL_RGB;
82 case RS_KIND_PIXEL_RGBA: return GL_RGBA;
83 case RS_KIND_PIXEL_DEPTH: return GL_DEPTH_COMPONENT16;
84 default: break;
85 }
86 return 0;
87}
Jason Sams93eacc72012-12-18 14:26:57 -080088#endif
Jason Samsa614ae12011-05-26 17:05:51 -070089
Jason Sams807fdc42012-07-25 17:55:39 -070090uint8_t *GetOffsetPtr(const android::renderscript::Allocation *alloc,
91 uint32_t xoff, uint32_t yoff, uint32_t lod,
92 RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -080093 uint8_t *ptr = (uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr;
94 ptr += face * alloc->mHal.drvState.faceOffset;
95 ptr += yoff * alloc->mHal.drvState.lod[lod].stride;
Jason Sams807fdc42012-07-25 17:55:39 -070096 ptr += xoff * alloc->mHal.state.elementSizeBytes;
97 return ptr;
98}
99
Jason Samsa614ae12011-05-26 17:05:51 -0700100
Jason Sams2382aba2011-09-13 15:41:01 -0700101static void Update2DTexture(const Context *rsc, const Allocation *alloc, const void *ptr,
102 uint32_t xoff, uint32_t yoff, uint32_t lod,
103 RsAllocationCubemapFace face, uint32_t w, uint32_t h) {
Jason Sams93eacc72012-12-18 14:26:57 -0800104#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700105 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
106
Jason Samseb4fe182011-05-26 16:33:01 -0700107 rsAssert(drv->textureID);
Jason Sams2382aba2011-09-13 15:41:01 -0700108 RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
109 RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
Jason Samseb4fe182011-05-26 16:33:01 -0700110 GLenum t = GL_TEXTURE_2D;
111 if (alloc->mHal.state.hasFaces) {
112 t = gFaceOrder[face];
113 }
Jason Sams2382aba2011-09-13 15:41:01 -0700114 RSD_CALL_GL(glTexSubImage2D, t, lod, xoff, yoff, w, h, drv->glFormat, drv->glType, ptr);
Jason Sams93eacc72012-12-18 14:26:57 -0800115#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700116}
117
118
Jason Sams93eacc72012-12-18 14:26:57 -0800119#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700120static void Upload2DTexture(const Context *rsc, const Allocation *alloc, bool isFirstUpload) {
121 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
122
Jason Sams2382aba2011-09-13 15:41:01 -0700123 RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
124 RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
Jason Samseb4fe182011-05-26 16:33:01 -0700125
126 uint32_t faceCount = 1;
127 if (alloc->mHal.state.hasFaces) {
128 faceCount = 6;
129 }
130
131 rsdGLCheckError(rsc, "Upload2DTexture 1 ");
132 for (uint32_t face = 0; face < faceCount; face ++) {
133 for (uint32_t lod = 0; lod < alloc->mHal.state.type->getLODCount(); lod++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700134 const uint8_t *p = GetOffsetPtr(alloc, 0, 0, lod, (RsAllocationCubemapFace)face);
Jason Samseb4fe182011-05-26 16:33:01 -0700135
136 GLenum t = GL_TEXTURE_2D;
137 if (alloc->mHal.state.hasFaces) {
138 t = gFaceOrder[face];
139 }
140
141 if (isFirstUpload) {
Jason Sams2382aba2011-09-13 15:41:01 -0700142 RSD_CALL_GL(glTexImage2D, t, lod, drv->glFormat,
Jason Samseb4fe182011-05-26 16:33:01 -0700143 alloc->mHal.state.type->getLODDimX(lod),
144 alloc->mHal.state.type->getLODDimY(lod),
Jason Samsa614ae12011-05-26 17:05:51 -0700145 0, drv->glFormat, drv->glType, p);
Jason Samseb4fe182011-05-26 16:33:01 -0700146 } else {
Jason Sams2382aba2011-09-13 15:41:01 -0700147 RSD_CALL_GL(glTexSubImage2D, t, lod, 0, 0,
Jason Samseb4fe182011-05-26 16:33:01 -0700148 alloc->mHal.state.type->getLODDimX(lod),
149 alloc->mHal.state.type->getLODDimY(lod),
Jason Samsa614ae12011-05-26 17:05:51 -0700150 drv->glFormat, drv->glType, p);
Jason Samseb4fe182011-05-26 16:33:01 -0700151 }
152 }
153 }
154
155 if (alloc->mHal.state.mipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
Jason Sams2382aba2011-09-13 15:41:01 -0700156 RSD_CALL_GL(glGenerateMipmap, drv->glTarget);
Jason Samseb4fe182011-05-26 16:33:01 -0700157 }
158 rsdGLCheckError(rsc, "Upload2DTexture");
159}
Jason Sams93eacc72012-12-18 14:26:57 -0800160#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700161
162static void UploadToTexture(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800163#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700164 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
165
Jason Sams3522f402012-03-23 11:47:26 -0700166 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
Jason Sams41e373d2012-01-13 14:01:20 -0800167 if (!drv->textureID) {
168 RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
169 }
170 return;
171 }
172
Jason Samsa614ae12011-05-26 17:05:51 -0700173 if (!drv->glType || !drv->glFormat) {
Jason Samseb4fe182011-05-26 16:33:01 -0700174 return;
175 }
176
Jason Sams709a0972012-11-15 18:18:04 -0800177 if (!alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Samseb4fe182011-05-26 16:33:01 -0700178 return;
179 }
180
181 bool isFirstUpload = false;
182
183 if (!drv->textureID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700184 RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
Jason Samseb4fe182011-05-26 16:33:01 -0700185 isFirstUpload = true;
186 }
187
188 Upload2DTexture(rsc, alloc, isFirstUpload);
189
190 if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
Jason Sams709a0972012-11-15 18:18:04 -0800191 if (alloc->mHal.drvState.lod[0].mallocPtr) {
192 free(alloc->mHal.drvState.lod[0].mallocPtr);
193 alloc->mHal.drvState.lod[0].mallocPtr = NULL;
Jason Samseb4fe182011-05-26 16:33:01 -0700194 }
195 }
196 rsdGLCheckError(rsc, "UploadToTexture");
Jason Sams93eacc72012-12-18 14:26:57 -0800197#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700198}
199
200static void AllocateRenderTarget(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800201#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700202 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
203
Jason Samsa614ae12011-05-26 17:05:51 -0700204 if (!drv->glFormat) {
Jason Samseb4fe182011-05-26 16:33:01 -0700205 return;
206 }
207
208 if (!drv->renderTargetID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700209 RSD_CALL_GL(glGenRenderbuffers, 1, &drv->renderTargetID);
Jason Samseb4fe182011-05-26 16:33:01 -0700210
211 if (!drv->renderTargetID) {
212 // This should generally not happen
Steve Blockaf12ac62012-01-06 19:20:56 +0000213 ALOGE("allocateRenderTarget failed to gen mRenderTargetID");
Jason Samseb4fe182011-05-26 16:33:01 -0700214 rsc->dumpDebug();
215 return;
216 }
Jason Sams2382aba2011-09-13 15:41:01 -0700217 RSD_CALL_GL(glBindRenderbuffer, GL_RENDERBUFFER, drv->renderTargetID);
218 RSD_CALL_GL(glRenderbufferStorage, GL_RENDERBUFFER, drv->glFormat,
Jason Samsa572aca2013-01-09 11:52:26 -0800219 alloc->mHal.drvState.lod[0].dimX, alloc->mHal.drvState.lod[0].dimY);
Jason Samseb4fe182011-05-26 16:33:01 -0700220 }
221 rsdGLCheckError(rsc, "AllocateRenderTarget");
Jason Sams93eacc72012-12-18 14:26:57 -0800222#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700223}
224
225static void UploadToBufferObject(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800226#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700227 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
228
229 rsAssert(!alloc->mHal.state.type->getDimY());
230 rsAssert(!alloc->mHal.state.type->getDimZ());
231
232 //alloc->mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
233
234 if (!drv->bufferID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700235 RSD_CALL_GL(glGenBuffers, 1, &drv->bufferID);
Jason Samseb4fe182011-05-26 16:33:01 -0700236 }
237 if (!drv->bufferID) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000238 ALOGE("Upload to buffer object failed");
Jason Samseb4fe182011-05-26 16:33:01 -0700239 drv->uploadDeferred = true;
240 return;
241 }
Jason Sams2382aba2011-09-13 15:41:01 -0700242 RSD_CALL_GL(glBindBuffer, drv->glTarget, drv->bufferID);
243 RSD_CALL_GL(glBufferData, drv->glTarget, alloc->mHal.state.type->getSizeBytes(),
Jason Sams709a0972012-11-15 18:18:04 -0800244 alloc->mHal.drvState.lod[0].mallocPtr, GL_DYNAMIC_DRAW);
Jason Sams2382aba2011-09-13 15:41:01 -0700245 RSD_CALL_GL(glBindBuffer, drv->glTarget, 0);
Jason Samseb4fe182011-05-26 16:33:01 -0700246 rsdGLCheckError(rsc, "UploadToBufferObject");
Jason Sams93eacc72012-12-18 14:26:57 -0800247#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700248}
249
Tim Murray0b575de2013-03-15 15:56:43 -0700250
Jason Sams9d8e5af2013-02-28 14:00:08 -0800251static size_t DeriveYUVLayout(int yuv, Allocation::Hal::DrvState *state) {
Jason Sams733396b2013-02-22 12:46:18 -0800252 // YUV only supports basic 2d
253 // so we can stash the plane pointers in the mipmap levels.
Jason Sams9d8e5af2013-02-28 14:00:08 -0800254 size_t uvSize = 0;
Tim Murray0b575de2013-03-15 15:56:43 -0700255#ifndef RS_SERVER
Jason Sams733396b2013-02-22 12:46:18 -0800256 switch(yuv) {
257 case HAL_PIXEL_FORMAT_YV12:
258 state->lod[1].dimX = state->lod[0].dimX / 2;
259 state->lod[1].dimY = state->lod[0].dimY / 2;
260 state->lod[1].stride = rsRound(state->lod[0].stride >> 1, 16);
261 state->lod[1].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) +
262 (state->lod[0].stride * state->lod[0].dimY);
Jason Sams9d8e5af2013-02-28 14:00:08 -0800263 uvSize += state->lod[1].stride * state->lod[1].dimY;
Jason Sams733396b2013-02-22 12:46:18 -0800264
265 state->lod[2].dimX = state->lod[1].dimX;
266 state->lod[2].dimY = state->lod[1].dimY;
267 state->lod[2].stride = state->lod[1].stride;
268 state->lod[2].mallocPtr = ((uint8_t *)state->lod[1].mallocPtr) +
269 (state->lod[1].stride * state->lod[1].dimY);
Jason Sams9d8e5af2013-02-28 14:00:08 -0800270 uvSize += state->lod[2].stride * state->lod[2].dimY;
Jason Sams733396b2013-02-22 12:46:18 -0800271
272 state->lodCount = 3;
273 break;
274 case HAL_PIXEL_FORMAT_YCrCb_420_SP: // NV21
275 state->lod[1].dimX = state->lod[0].dimX;
276 state->lod[1].dimY = state->lod[0].dimY / 2;
277 state->lod[1].stride = state->lod[0].stride;
278 state->lod[1].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) +
279 (state->lod[0].stride * state->lod[0].dimY);
Jason Sams9d8e5af2013-02-28 14:00:08 -0800280 uvSize += state->lod[1].stride * state->lod[1].dimY;
Jason Sams733396b2013-02-22 12:46:18 -0800281 state->lodCount = 2;
282 break;
283 default:
284 rsAssert(0);
285 }
Tim Murray0b575de2013-03-15 15:56:43 -0700286#endif
Jason Sams9d8e5af2013-02-28 14:00:08 -0800287 return uvSize;
Jason Sams733396b2013-02-22 12:46:18 -0800288}
289
290
Jason Sams463bfce2012-08-21 13:54:42 -0700291static size_t AllocationBuildPointerTable(const Context *rsc, const Allocation *alloc,
292 const Type *type, uint8_t *ptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800293 alloc->mHal.drvState.lod[0].dimX = type->getDimX();
294 alloc->mHal.drvState.lod[0].dimY = type->getDimY();
Jason Samsf2b611e2012-12-27 19:05:22 -0800295 alloc->mHal.drvState.lod[0].dimZ = type->getDimZ();
Jason Sams709a0972012-11-15 18:18:04 -0800296 alloc->mHal.drvState.lod[0].mallocPtr = 0;
Stephen Hines94999c32013-02-05 16:58:22 -0800297 // Stride needs to be 16-byte aligned too!
298 size_t stride = alloc->mHal.drvState.lod[0].dimX * type->getElementSizeBytes();
299 alloc->mHal.drvState.lod[0].stride = rsRound(stride, 16);
Jason Sams709a0972012-11-15 18:18:04 -0800300 alloc->mHal.drvState.lodCount = type->getLODCount();
301 alloc->mHal.drvState.faceCount = type->getDimFaces();
Jason Sams807fdc42012-07-25 17:55:39 -0700302
303 size_t offsets[Allocation::MAX_LOD];
304 memset(offsets, 0, sizeof(offsets));
305
Jason Sams709a0972012-11-15 18:18:04 -0800306 size_t o = alloc->mHal.drvState.lod[0].stride * rsMax(alloc->mHal.drvState.lod[0].dimY, 1u) *
307 rsMax(alloc->mHal.drvState.lod[0].dimZ, 1u);
308 if(alloc->mHal.drvState.lodCount > 1) {
309 uint32_t tx = alloc->mHal.drvState.lod[0].dimX;
310 uint32_t ty = alloc->mHal.drvState.lod[0].dimY;
311 uint32_t tz = alloc->mHal.drvState.lod[0].dimZ;
312 for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
313 alloc->mHal.drvState.lod[lod].dimX = tx;
314 alloc->mHal.drvState.lod[lod].dimY = ty;
315 alloc->mHal.drvState.lod[lod].dimZ = tz;
Stephen Hines94999c32013-02-05 16:58:22 -0800316 alloc->mHal.drvState.lod[lod].stride =
317 rsRound(tx * type->getElementSizeBytes(), 16);
Jason Sams807fdc42012-07-25 17:55:39 -0700318 offsets[lod] = o;
Jason Sams709a0972012-11-15 18:18:04 -0800319 o += alloc->mHal.drvState.lod[lod].stride * rsMax(ty, 1u) * rsMax(tz, 1u);
Jason Sams807fdc42012-07-25 17:55:39 -0700320 if (tx > 1) tx >>= 1;
321 if (ty > 1) ty >>= 1;
322 if (tz > 1) tz >>= 1;
323 }
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800324 } else if (alloc->mHal.state.yuv) {
Jason Sams9d8e5af2013-02-28 14:00:08 -0800325 o += DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState);
Jason Sams733396b2013-02-22 12:46:18 -0800326
327 for (uint32_t ct = 1; ct < alloc->mHal.drvState.lodCount; ct++) {
328 offsets[ct] = (size_t)alloc->mHal.drvState.lod[ct].mallocPtr;
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800329 }
Jason Sams807fdc42012-07-25 17:55:39 -0700330 }
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800331
Jason Sams709a0972012-11-15 18:18:04 -0800332 alloc->mHal.drvState.faceOffset = o;
Jason Sams807fdc42012-07-25 17:55:39 -0700333
Jason Sams709a0972012-11-15 18:18:04 -0800334 alloc->mHal.drvState.lod[0].mallocPtr = ptr;
335 for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
336 alloc->mHal.drvState.lod[lod].mallocPtr = ptr + offsets[lod];
Jason Sams463bfce2012-08-21 13:54:42 -0700337 }
Jason Sams463bfce2012-08-21 13:54:42 -0700338
Jason Sams709a0972012-11-15 18:18:04 -0800339 size_t allocSize = alloc->mHal.drvState.faceOffset;
340 if(alloc->mHal.drvState.faceCount) {
Jason Sams463bfce2012-08-21 13:54:42 -0700341 allocSize *= 6;
342 }
343
344 return allocSize;
345}
346
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800347static uint8_t* allocAlignedMemory(size_t allocSize, bool forceZero) {
348 // We align all allocations to a 16-byte boundary.
349 uint8_t* ptr = (uint8_t *)memalign(16, allocSize);
350 if (!ptr) {
351 return NULL;
352 }
353 if (forceZero) {
354 memset(ptr, 0, allocSize);
355 }
356 return ptr;
357}
358
Jason Sams463bfce2012-08-21 13:54:42 -0700359bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) {
360 DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation));
361 if (!drv) {
362 return false;
363 }
364 alloc->mHal.drv = drv;
365
366 // Calculate the object size.
367 size_t allocSize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), NULL);
368
Jason Sams807fdc42012-07-25 17:55:39 -0700369 uint8_t * ptr = NULL;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800370 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) {
Jason Sams733396b2013-02-22 12:46:18 -0800371
372 } else if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
373 // Allocation is allocated when the surface is created
374 // in getSurface
Tim Murray2e1a94d2012-11-29 13:12:25 -0800375 } else if (alloc->mHal.state.userProvidedPtr != NULL) {
376 // user-provided allocation
377 // limitations: no faces, no LOD, USAGE_SCRIPT only
Tim Murray9e2bda52012-12-18 12:05:46 -0800378 if (alloc->mHal.state.usageFlags != (RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED)) {
379 ALOGE("Can't use user-allocated buffers if usage is not USAGE_SCRIPT and USAGE_SHARED");
Tim Murray2e1a94d2012-11-29 13:12:25 -0800380 return false;
381 }
382 if (alloc->getType()->getDimLOD() || alloc->getType()->getDimFaces()) {
383 ALOGE("User-allocated buffers must not have multiple faces or LODs");
384 return false;
385 }
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800386
387 // rows must be 16-byte aligned
388 // validate that here, otherwise fall back to not use the user-backed allocation
389 if (((alloc->getType()->getDimX() * alloc->getType()->getElement()->getSizeBytes()) % 16) != 0) {
390 ALOGV("User-backed allocation failed stride requirement, falling back to separate allocation");
391 drv->useUserProvidedPtr = false;
392
393 ptr = allocAlignedMemory(allocSize, forceZero);
394 if (!ptr) {
395 alloc->mHal.drv = NULL;
396 free(drv);
397 return false;
398 }
399
400 } else {
401 drv->useUserProvidedPtr = true;
402 ptr = (uint8_t*)alloc->mHal.state.userProvidedPtr;
403 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800404 } else {
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800405 ptr = allocAlignedMemory(allocSize, forceZero);
Jason Sams179e9a42011-11-23 15:02:15 -0800406 if (!ptr) {
You Kimed419f32012-12-17 03:42:57 +0900407 alloc->mHal.drv = NULL;
Jason Sams179e9a42011-11-23 15:02:15 -0800408 free(drv);
409 return false;
410 }
Jason Samseb4fe182011-05-26 16:33:01 -0700411 }
Jason Sams463bfce2012-08-21 13:54:42 -0700412 // Build the pointer tables
413 size_t verifySize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), ptr);
414 if(allocSize != verifySize) {
415 rsAssert(!"Size mismatch");
Jason Sams807fdc42012-07-25 17:55:39 -0700416 }
Jason Samseb4fe182011-05-26 16:33:01 -0700417
Tim Murray0b575de2013-03-15 15:56:43 -0700418#ifndef RS_SERVER
Jason Samseb4fe182011-05-26 16:33:01 -0700419 drv->glTarget = GL_NONE;
420 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
421 if (alloc->mHal.state.hasFaces) {
422 drv->glTarget = GL_TEXTURE_CUBE_MAP;
423 } else {
424 drv->glTarget = GL_TEXTURE_2D;
425 }
426 } else {
427 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
428 drv->glTarget = GL_ARRAY_BUFFER;
429 }
430 }
Tim Murray0b575de2013-03-15 15:56:43 -0700431#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700432
Jason Sams93eacc72012-12-18 14:26:57 -0800433#ifndef RS_COMPATIBILITY_LIB
Jason Samsa614ae12011-05-26 17:05:51 -0700434 drv->glType = rsdTypeToGLType(alloc->mHal.state.type->getElement()->getComponent().getType());
435 drv->glFormat = rsdKindToGLFormat(alloc->mHal.state.type->getElement()->getComponent().getKind());
Jason Sams93eacc72012-12-18 14:26:57 -0800436#else
437 drv->glType = 0;
438 drv->glFormat = 0;
439#endif
Jason Samsa614ae12011-05-26 17:05:51 -0700440
Jason Samseb4fe182011-05-26 16:33:01 -0700441 if (alloc->mHal.state.usageFlags & ~RS_ALLOCATION_USAGE_SCRIPT) {
442 drv->uploadDeferred = true;
443 }
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700444
Jason Samsb3220332012-04-02 14:41:54 -0700445
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700446 drv->readBackFBO = NULL;
447
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800448 // fill out the initial state of the buffer if we couldn't use the user-provided ptr and USAGE_SHARED was accepted
449 if ((alloc->mHal.state.userProvidedPtr != 0) && (drv->useUserProvidedPtr == false)) {
450 rsdAllocationData2D(rsc, alloc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, alloc->getType()->getDimX(), alloc->getType()->getDimY(), alloc->mHal.state.userProvidedPtr, allocSize, 0);
451 }
452
Jason Samseb4fe182011-05-26 16:33:01 -0700453 return true;
454}
455
456void rsdAllocationDestroy(const Context *rsc, Allocation *alloc) {
457 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
458
Jason Sams93eacc72012-12-18 14:26:57 -0800459#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700460 if (drv->bufferID) {
461 // Causes a SW crash....
Steve Block65982012011-10-20 11:56:00 +0100462 //ALOGV(" mBufferID %i", mBufferID);
Jason Samseb4fe182011-05-26 16:33:01 -0700463 //glDeleteBuffers(1, &mBufferID);
464 //mBufferID = 0;
465 }
466 if (drv->textureID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700467 RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID);
Jason Samseb4fe182011-05-26 16:33:01 -0700468 drv->textureID = 0;
469 }
470 if (drv->renderTargetID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700471 RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID);
Jason Samseb4fe182011-05-26 16:33:01 -0700472 drv->renderTargetID = 0;
473 }
Jason Sams93eacc72012-12-18 14:26:57 -0800474#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700475
Jason Sams709a0972012-11-15 18:18:04 -0800476 if (alloc->mHal.drvState.lod[0].mallocPtr) {
Tim Murray2e1a94d2012-11-29 13:12:25 -0800477 // don't free user-allocated ptrs
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800478 if (!(drv->useUserProvidedPtr)) {
Tim Murray2e1a94d2012-11-29 13:12:25 -0800479 free(alloc->mHal.drvState.lod[0].mallocPtr);
480 }
Jason Sams709a0972012-11-15 18:18:04 -0800481 alloc->mHal.drvState.lod[0].mallocPtr = NULL;
Jason Samseb4fe182011-05-26 16:33:01 -0700482 }
Jason Sams93eacc72012-12-18 14:26:57 -0800483
484#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700485 if (drv->readBackFBO != NULL) {
486 delete drv->readBackFBO;
487 drv->readBackFBO = NULL;
488 }
Jason Sams10c9dd72013-02-01 10:53:42 -0800489
490 if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) &&
491 (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
492
493 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Tim Murray3a25fdd2013-02-22 17:56:56 -0800494 ANativeWindow *nw = drv->wndSurface;
495 if (nw) {
496 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
497 mapper.unlock(drv->wndBuffer->handle);
498 int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
499 }
Jason Sams10c9dd72013-02-01 10:53:42 -0800500 }
Jason Sams93eacc72012-12-18 14:26:57 -0800501#endif
502
Jason Samseb4fe182011-05-26 16:33:01 -0700503 free(drv);
504 alloc->mHal.drv = NULL;
505}
506
507void rsdAllocationResize(const Context *rsc, const Allocation *alloc,
508 const Type *newType, bool zeroNew) {
Jason Samsa572aca2013-01-09 11:52:26 -0800509 const uint32_t oldDimX = alloc->mHal.drvState.lod[0].dimX;
510 const uint32_t dimX = newType->getDimX();
511
Tim Murray9e2bda52012-12-18 12:05:46 -0800512 // can't resize Allocations with user-allocated buffers
513 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) {
514 ALOGE("Resize cannot be called on a USAGE_SHARED allocation");
515 return;
516 }
Jason Sams709a0972012-11-15 18:18:04 -0800517 void * oldPtr = alloc->mHal.drvState.lod[0].mallocPtr;
Jason Sams463bfce2012-08-21 13:54:42 -0700518 // Calculate the object size
519 size_t s = AllocationBuildPointerTable(rsc, alloc, newType, NULL);
520 uint8_t *ptr = (uint8_t *)realloc(oldPtr, s);
521 // Build the relative pointer tables.
522 size_t verifySize = AllocationBuildPointerTable(rsc, alloc, newType, ptr);
523 if(s != verifySize) {
524 rsAssert(!"Size mismatch");
525 }
Jason Samseb4fe182011-05-26 16:33:01 -0700526
Jason Samseb4fe182011-05-26 16:33:01 -0700527
528 if (dimX > oldDimX) {
Tim Murray099bc262013-03-20 16:54:03 -0700529 size_t stride = alloc->mHal.state.elementSizeBytes;
Jason Sams709a0972012-11-15 18:18:04 -0800530 memset(((uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr) + stride * oldDimX,
Jason Sams2275e9c2012-04-16 17:01:26 -0700531 0, stride * (dimX - oldDimX));
Jason Samseb4fe182011-05-26 16:33:01 -0700532 }
533}
534
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700535static void rsdAllocationSyncFromFBO(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800536#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700537 if (!alloc->getIsScript()) {
538 return; // nothing to sync
539 }
540
541 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
542 RsdFrameBufferObj *lastFbo = dc->gl.currentFrameBuffer;
543
544 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
545 if (!drv->textureID && !drv->renderTargetID) {
546 return; // nothing was rendered here yet, so nothing to sync
547 }
548 if (drv->readBackFBO == NULL) {
549 drv->readBackFBO = new RsdFrameBufferObj();
550 drv->readBackFBO->setColorTarget(drv, 0);
551 drv->readBackFBO->setDimensions(alloc->getType()->getDimX(),
552 alloc->getType()->getDimY());
553 }
554
555 // Bind the framebuffer object so we can read back from it
556 drv->readBackFBO->setActive(rsc);
557
558 // Do the readback
Jason Sams709a0972012-11-15 18:18:04 -0800559 RSD_CALL_GL(glReadPixels, 0, 0, alloc->mHal.drvState.lod[0].dimX,
560 alloc->mHal.drvState.lod[0].dimY,
561 drv->glFormat, drv->glType, alloc->mHal.drvState.lod[0].mallocPtr);
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700562
563 // Revert framebuffer to its original
564 lastFbo->setActive(rsc);
Jason Sams93eacc72012-12-18 14:26:57 -0800565#endif
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700566}
Jason Samseb4fe182011-05-26 16:33:01 -0700567
568
569void rsdAllocationSyncAll(const Context *rsc, const Allocation *alloc,
570 RsAllocationUsageType src) {
571 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
572
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700573 if (src == RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
574 if(!alloc->getIsRenderTarget()) {
575 rsc->setError(RS_ERROR_FATAL_DRIVER,
576 "Attempting to sync allocation from render target, "
577 "for non-render target allocation");
578 } else if (alloc->getType()->getElement()->getKind() != RS_KIND_PIXEL_RGBA) {
579 rsc->setError(RS_ERROR_FATAL_DRIVER, "Cannot only sync from RGBA"
580 "render target");
581 } else {
582 rsdAllocationSyncFromFBO(rsc, alloc);
583 }
Jason Samseb4fe182011-05-26 16:33:01 -0700584 return;
585 }
586
587 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
588
589 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
590 UploadToTexture(rsc, alloc);
591 } else {
Jason Samsb3220332012-04-02 14:41:54 -0700592 if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) &&
Jason Sams0dc66932012-04-10 17:05:49 -0700593 !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
Jason Samseb4fe182011-05-26 16:33:01 -0700594 AllocateRenderTarget(rsc, alloc);
595 }
596 }
597 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
598 UploadToBufferObject(rsc, alloc);
599 }
600
601 drv->uploadDeferred = false;
602}
603
604void rsdAllocationMarkDirty(const Context *rsc, const Allocation *alloc) {
605 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
606 drv->uploadDeferred = true;
607}
608
Jason Sams733396b2013-02-22 12:46:18 -0800609void* rsdAllocationGetSurface(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800610#ifndef RS_COMPATIBILITY_LIB
Jason Sams41e373d2012-01-13 14:01:20 -0800611 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Jason Sams733396b2013-02-22 12:46:18 -0800612
Eino-Ville Talvala0d44f122013-04-03 16:42:35 -0700613 // Configure CpuConsumer to be in asynchronous mode
614 drv->cpuConsumer = new CpuConsumer(2, false);
Jason Sams733396b2013-02-22 12:46:18 -0800615 sp<IGraphicBufferProducer> bp = drv->cpuConsumer->getProducerInterface();
616 bp->incStrong(NULL);
617 return bp.get();
Jason Sams93eacc72012-12-18 14:26:57 -0800618#else
Jason Sams733396b2013-02-22 12:46:18 -0800619 return NULL;
Jason Sams93eacc72012-12-18 14:26:57 -0800620#endif
Jason Sams41e373d2012-01-13 14:01:20 -0800621}
622
Jason Sams93eacc72012-12-18 14:26:57 -0800623#ifndef RS_COMPATIBILITY_LIB
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800624static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
625 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
626
Jamie Genniscd919a12012-06-13 16:34:11 -0700627 int32_t r = native_window_dequeue_buffer_and_wait(nw, &drv->wndBuffer);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800628 if (r) {
629 rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer.");
630 return false;
631 }
632
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800633 // Must lock the whole surface
634 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
635 Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height);
636
637 void *dst = NULL;
638 mapper.lock(drv->wndBuffer->handle,
639 GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN,
640 bounds, &dst);
Jason Sams709a0972012-11-15 18:18:04 -0800641 alloc->mHal.drvState.lod[0].mallocPtr = dst;
642 alloc->mHal.drvState.lod[0].stride = drv->wndBuffer->stride * alloc->mHal.state.elementSizeBytes;
Stephen Hines94999c32013-02-05 16:58:22 -0800643 rsAssert((alloc->mHal.drvState.lod[0].stride & 0xf) == 0);
Jason Samsb3220332012-04-02 14:41:54 -0700644
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800645 return true;
646}
Jason Sams93eacc72012-12-18 14:26:57 -0800647#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800648
Jason Sams733396b2013-02-22 12:46:18 -0800649void rsdAllocationSetSurface(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
Jason Sams93eacc72012-12-18 14:26:57 -0800650#ifndef RS_COMPATIBILITY_LIB
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800651 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Tim Murray3a25fdd2013-02-22 17:56:56 -0800652 ANativeWindow *old = drv->wndSurface;
653
654 if (nw) {
655 nw->incStrong(NULL);
656 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800657
Jason Samsb3220332012-04-02 14:41:54 -0700658 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
659 //TODO finish support for render target + script
660 drv->wnd = nw;
661 return;
662 }
663
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800664 // Cleanup old surface if there is one.
Tim Murray3a25fdd2013-02-22 17:56:56 -0800665 if (drv->wndSurface) {
666 ANativeWindow *old = drv->wndSurface;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800667 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
668 mapper.unlock(drv->wndBuffer->handle);
Tim Murray3a25fdd2013-02-22 17:56:56 -0800669 old->cancelBuffer(old, drv->wndBuffer, -1);
670 drv->wndSurface = NULL;
671 old->decStrong(NULL);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800672 }
673
674 if (nw != NULL) {
675 int32_t r;
Jason Samsb3220332012-04-02 14:41:54 -0700676 uint32_t flags = 0;
Tim Murray3a25fdd2013-02-22 17:56:56 -0800677 r = native_window_set_buffer_count(nw, 3);
678 if (r) {
679 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer count.");
680 goto error;
681 }
682
Jason Samsb3220332012-04-02 14:41:54 -0700683 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
684 flags |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN;
685 }
686 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
687 flags |= GRALLOC_USAGE_HW_RENDER;
688 }
689
690 r = native_window_set_usage(nw, flags);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800691 if (r) {
692 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
Tim Murray3a25fdd2013-02-22 17:56:56 -0800693 goto error;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800694 }
695
Jason Samsa572aca2013-01-09 11:52:26 -0800696 r = native_window_set_buffers_dimensions(nw, alloc->mHal.drvState.lod[0].dimX,
697 alloc->mHal.drvState.lod[0].dimY);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800698 if (r) {
699 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions.");
Tim Murray3a25fdd2013-02-22 17:56:56 -0800700 goto error;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800701 }
702
Jason Sams733396b2013-02-22 12:46:18 -0800703 int format = 0;
704 const Element *e = alloc->mHal.state.type->getElement();
705 switch(e->getType()) {
706 case RS_TYPE_UNSIGNED_8:
707 switch (e->getVectorSize()) {
708 case 1:
709 rsAssert(e->getKind() == RS_KIND_PIXEL_A);
710 format = PIXEL_FORMAT_A_8;
711 break;
712 case 4:
713 rsAssert(e->getKind() == RS_KIND_PIXEL_RGBA);
714 format = PIXEL_FORMAT_RGBA_8888;
715 break;
716 default:
717 rsAssert(0);
718 }
719 break;
720 default:
721 rsAssert(0);
722 }
723
724 r = native_window_set_buffers_format(nw, format);
725 if (r) {
726 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer format.");
Tim Murray3a25fdd2013-02-22 17:56:56 -0800727 goto error;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800728 }
729
730 IoGetBuffer(rsc, alloc, nw);
Tim Murray3a25fdd2013-02-22 17:56:56 -0800731 drv->wndSurface = nw;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800732 }
Tim Murray3a25fdd2013-02-22 17:56:56 -0800733
734 return;
735
736 error:
737
738 if (nw) {
739 nw->decStrong(NULL);
740 }
741
742
Jason Sams93eacc72012-12-18 14:26:57 -0800743#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800744}
745
746void rsdAllocationIoSend(const Context *rsc, Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800747#ifndef RS_COMPATIBILITY_LIB
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800748 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Tim Murray3a25fdd2013-02-22 17:56:56 -0800749 ANativeWindow *nw = drv->wndSurface;
Jason Samsb3220332012-04-02 14:41:54 -0700750 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
751 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
752 RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800753 return;
754 }
Tim Murray3a25fdd2013-02-22 17:56:56 -0800755 if (nw) {
756 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
757 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
758 mapper.unlock(drv->wndBuffer->handle);
759 int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
760 if (r) {
761 rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer.");
762 return;
763 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800764
Tim Murray3a25fdd2013-02-22 17:56:56 -0800765 IoGetBuffer(rsc, alloc, nw);
Jason Samsb3220332012-04-02 14:41:54 -0700766 }
Tim Murray3a25fdd2013-02-22 17:56:56 -0800767 } else {
768 rsc->setError(RS_ERROR_DRIVER, "Sent IO buffer with no attached surface.");
769 return;
Jason Samsb3220332012-04-02 14:41:54 -0700770 }
Jason Sams93eacc72012-12-18 14:26:57 -0800771#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800772}
773
774void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800775#ifndef RS_COMPATIBILITY_LIB
Jason Sams3522f402012-03-23 11:47:26 -0700776 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Jason Sams733396b2013-02-22 12:46:18 -0800777
778 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
Eino-Ville Talvala0d44f122013-04-03 16:42:35 -0700779 CpuConsumer::LockedBuffer lb;
780 status_t ret = drv->cpuConsumer->lockNextBuffer(&lb);
781 if (ret == OK) {
782 if (drv->lb.data != NULL) {
783 drv->cpuConsumer->unlockBuffer(drv->lb);
784 }
785 drv->lb = lb;
786 alloc->mHal.drvState.lod[0].mallocPtr = drv->lb.data;
787 alloc->mHal.drvState.lod[0].stride = drv->lb.stride *
788 alloc->mHal.state.elementSizeBytes;
Jason Sams733396b2013-02-22 12:46:18 -0800789
Eino-Ville Talvala0d44f122013-04-03 16:42:35 -0700790 if (alloc->mHal.state.yuv) {
791 DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState);
792 }
793 } else if (ret == BAD_VALUE) {
794 // No new frame, don't do anything
795 } else {
796 rsc->setError(RS_ERROR_DRIVER, "Error receiving IO input buffer.");
Jason Sams733396b2013-02-22 12:46:18 -0800797 }
798
799 } else {
Tim Murray3a25fdd2013-02-22 17:56:56 -0800800 drv->surfaceTexture->updateTexImage();
Jason Sams733396b2013-02-22 12:46:18 -0800801 }
802
803
Jason Sams93eacc72012-12-18 14:26:57 -0800804#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800805}
806
807
Jason Samseb4fe182011-05-26 16:33:01 -0700808void rsdAllocationData1D(const Context *rsc, const Allocation *alloc,
Tim Murray099bc262013-03-20 16:54:03 -0700809 uint32_t xoff, uint32_t lod, size_t count,
Alex Sakhartchoukc794cd52012-02-13 11:57:32 -0800810 const void *data, size_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -0700811 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
812
Tim Murray099bc262013-03-20 16:54:03 -0700813 const size_t eSize = alloc->mHal.state.type->getElementSizeBytes();
Jason Sams807fdc42012-07-25 17:55:39 -0700814 uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Tim Murray099bc262013-03-20 16:54:03 -0700815 size_t size = count * eSize;
Jason Samseb4fe182011-05-26 16:33:01 -0700816
Stephen Hines20c535f2012-12-06 19:04:38 -0800817 if (ptr != data) {
818 // Skip the copy if we are the same allocation. This can arise from
819 // our Bitmap optimization, where we share the same storage.
820 if (alloc->mHal.state.hasReferences) {
821 alloc->incRefs(data, count);
822 alloc->decRefs(ptr, count);
823 }
824 memcpy(ptr, data, size);
Jason Samseb4fe182011-05-26 16:33:01 -0700825 }
Jason Samseb4fe182011-05-26 16:33:01 -0700826 drv->uploadDeferred = true;
827}
828
829void rsdAllocationData2D(const Context *rsc, const Allocation *alloc,
830 uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800831 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Jason Samseb4fe182011-05-26 16:33:01 -0700832 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
833
Tim Murray099bc262013-03-20 16:54:03 -0700834 size_t eSize = alloc->mHal.state.elementSizeBytes;
835 size_t lineSize = eSize * w;
Tim Murray358747a2012-11-26 13:52:04 -0800836 if (!stride) {
837 stride = lineSize;
838 }
Jason Samseb4fe182011-05-26 16:33:01 -0700839
Jason Sams709a0972012-11-15 18:18:04 -0800840 if (alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Samseb4fe182011-05-26 16:33:01 -0700841 const uint8_t *src = static_cast<const uint8_t *>(data);
Jason Sams807fdc42012-07-25 17:55:39 -0700842 uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, lod, face);
Stephen Hines20c535f2012-12-06 19:04:38 -0800843 if (dst == src) {
844 // Skip the copy if we are the same allocation. This can arise from
845 // our Bitmap optimization, where we share the same storage.
846 drv->uploadDeferred = true;
847 return;
848 }
Jason Samseb4fe182011-05-26 16:33:01 -0700849
850 for (uint32_t line=yoff; line < (yoff+h); line++) {
851 if (alloc->mHal.state.hasReferences) {
852 alloc->incRefs(src, w);
853 alloc->decRefs(dst, w);
854 }
855 memcpy(dst, src, lineSize);
Tim Murray358747a2012-11-26 13:52:04 -0800856 src += stride;
Jason Sams709a0972012-11-15 18:18:04 -0800857 dst += alloc->mHal.drvState.lod[lod].stride;
Jason Samseb4fe182011-05-26 16:33:01 -0700858 }
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800859 if (alloc->mHal.state.yuv) {
860 int lod = 1;
861 while (alloc->mHal.drvState.lod[lod].mallocPtr) {
Tim Murray099bc262013-03-20 16:54:03 -0700862 size_t lineSize = alloc->mHal.drvState.lod[lod].dimX;
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800863 uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, lod, face);
864
865 for (uint32_t line=(yoff >> 1); line < ((yoff+h)>>1); line++) {
866 memcpy(dst, src, lineSize);
867 src += lineSize;
868 dst += alloc->mHal.drvState.lod[lod].stride;
869 }
870 lod++;
871 }
872
873 }
Jason Samseb4fe182011-05-26 16:33:01 -0700874 drv->uploadDeferred = true;
875 } else {
Jason Sams2382aba2011-09-13 15:41:01 -0700876 Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h);
Jason Samseb4fe182011-05-26 16:33:01 -0700877 }
878}
879
880void rsdAllocationData3D(const Context *rsc, const Allocation *alloc,
881 uint32_t xoff, uint32_t yoff, uint32_t zoff,
882 uint32_t lod, RsAllocationCubemapFace face,
Tim Murray099bc262013-03-20 16:54:03 -0700883 uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -0700884
885}
886
Jason Sams807fdc42012-07-25 17:55:39 -0700887void rsdAllocationRead1D(const Context *rsc, const Allocation *alloc,
Tim Murray099bc262013-03-20 16:54:03 -0700888 uint32_t xoff, uint32_t lod, size_t count,
Jason Sams807fdc42012-07-25 17:55:39 -0700889 void *data, size_t sizeBytes) {
Tim Murray099bc262013-03-20 16:54:03 -0700890 const size_t eSize = alloc->mHal.state.type->getElementSizeBytes();
Jason Sams807fdc42012-07-25 17:55:39 -0700891 const uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Stephen Hines20c535f2012-12-06 19:04:38 -0800892 if (data != ptr) {
893 // Skip the copy if we are the same allocation. This can arise from
894 // our Bitmap optimization, where we share the same storage.
895 memcpy(data, ptr, count * eSize);
896 }
Jason Sams807fdc42012-07-25 17:55:39 -0700897}
898
899void rsdAllocationRead2D(const Context *rsc, const Allocation *alloc,
Tim Murray358747a2012-11-26 13:52:04 -0800900 uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
901 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
Tim Murray099bc262013-03-20 16:54:03 -0700902 size_t eSize = alloc->mHal.state.elementSizeBytes;
903 size_t lineSize = eSize * w;
Tim Murray358747a2012-11-26 13:52:04 -0800904 if (!stride) {
905 stride = lineSize;
906 }
Jason Sams807fdc42012-07-25 17:55:39 -0700907
Jason Sams709a0972012-11-15 18:18:04 -0800908 if (alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Sams807fdc42012-07-25 17:55:39 -0700909 uint8_t *dst = static_cast<uint8_t *>(data);
910 const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, lod, face);
Stephen Hines20c535f2012-12-06 19:04:38 -0800911 if (dst == src) {
912 // Skip the copy if we are the same allocation. This can arise from
913 // our Bitmap optimization, where we share the same storage.
914 return;
915 }
Jason Sams807fdc42012-07-25 17:55:39 -0700916
917 for (uint32_t line=yoff; line < (yoff+h); line++) {
918 memcpy(dst, src, lineSize);
Tim Murray358747a2012-11-26 13:52:04 -0800919 dst += stride;
Jason Sams709a0972012-11-15 18:18:04 -0800920 src += alloc->mHal.drvState.lod[lod].stride;
Jason Sams807fdc42012-07-25 17:55:39 -0700921 }
922 } else {
923 ALOGE("Add code to readback from non-script memory");
924 }
925}
926
Tim Murray358747a2012-11-26 13:52:04 -0800927
Jason Sams807fdc42012-07-25 17:55:39 -0700928void rsdAllocationRead3D(const Context *rsc, const Allocation *alloc,
929 uint32_t xoff, uint32_t yoff, uint32_t zoff,
930 uint32_t lod, RsAllocationCubemapFace face,
Tim Murray099bc262013-03-20 16:54:03 -0700931 uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes) {
Jason Sams807fdc42012-07-25 17:55:39 -0700932
933}
934
935void * rsdAllocationLock1D(const android::renderscript::Context *rsc,
936 const android::renderscript::Allocation *alloc) {
Jason Sams709a0972012-11-15 18:18:04 -0800937 return alloc->mHal.drvState.lod[0].mallocPtr;
Jason Sams807fdc42012-07-25 17:55:39 -0700938}
939
940void rsdAllocationUnlock1D(const android::renderscript::Context *rsc,
941 const android::renderscript::Allocation *alloc) {
942
943}
944
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700945void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc,
946 const android::renderscript::Allocation *dstAlloc,
Tim Murray099bc262013-03-20 16:54:03 -0700947 uint32_t dstXoff, uint32_t dstLod, size_t count,
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700948 const android::renderscript::Allocation *srcAlloc,
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700949 uint32_t srcXoff, uint32_t srcLod) {
950}
951
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700952
953void rsdAllocationData2D_alloc_script(const android::renderscript::Context *rsc,
954 const android::renderscript::Allocation *dstAlloc,
955 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
956 RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
957 const android::renderscript::Allocation *srcAlloc,
958 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
959 RsAllocationCubemapFace srcFace) {
Tim Murray099bc262013-03-20 16:54:03 -0700960 size_t elementSize = dstAlloc->getType()->getElementSizeBytes();
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700961 for (uint32_t i = 0; i < h; i ++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700962 uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, dstLod, dstFace);
963 uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, srcLod, srcFace);
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700964 memcpy(dstPtr, srcPtr, w * elementSize);
965
Steve Blockaf12ac62012-01-06 19:20:56 +0000966 //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 -0700967 // dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace);
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700968 }
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700969}
970
971void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc,
972 const android::renderscript::Allocation *dstAlloc,
973 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
974 RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
975 const android::renderscript::Allocation *srcAlloc,
976 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
977 RsAllocationCubemapFace srcFace) {
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700978 if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) {
979 rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not "
980 "yet implemented.");
981 return;
982 }
983 rsdAllocationData2D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff,
984 dstLod, dstFace, w, h, srcAlloc,
985 srcXoff, srcYoff, srcLod, srcFace);
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700986}
987
988void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc,
989 const android::renderscript::Allocation *dstAlloc,
990 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
991 uint32_t dstLod, RsAllocationCubemapFace dstFace,
992 uint32_t w, uint32_t h, uint32_t d,
993 const android::renderscript::Allocation *srcAlloc,
994 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
995 uint32_t srcLod, RsAllocationCubemapFace srcFace) {
996}
997
Jason Samseb4fe182011-05-26 16:33:01 -0700998void rsdAllocationElementData1D(const Context *rsc, const Allocation *alloc,
999 uint32_t x,
Tim Murray099bc262013-03-20 16:54:03 -07001000 const void *data, uint32_t cIdx, size_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -07001001 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
1002
Tim Murray099bc262013-03-20 16:54:03 -07001003 size_t eSize = alloc->mHal.state.elementSizeBytes;
Jason Sams807fdc42012-07-25 17:55:39 -07001004 uint8_t * ptr = GetOffsetPtr(alloc, x, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Jason Samseb4fe182011-05-26 16:33:01 -07001005
1006 const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
1007 ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
1008
1009 if (alloc->mHal.state.hasReferences) {
1010 e->incRefs(data);
1011 e->decRefs(ptr);
1012 }
1013
1014 memcpy(ptr, data, sizeBytes);
1015 drv->uploadDeferred = true;
1016}
1017
1018void rsdAllocationElementData2D(const Context *rsc, const Allocation *alloc,
1019 uint32_t x, uint32_t y,
Tim Murray099bc262013-03-20 16:54:03 -07001020 const void *data, uint32_t cIdx, size_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -07001021 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
1022
Tim Murray099bc262013-03-20 16:54:03 -07001023 size_t eSize = alloc->mHal.state.elementSizeBytes;
Jason Sams807fdc42012-07-25 17:55:39 -07001024 uint8_t * ptr = GetOffsetPtr(alloc, x, y, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Jason Samseb4fe182011-05-26 16:33:01 -07001025
1026 const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
1027 ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
1028
1029 if (alloc->mHal.state.hasReferences) {
1030 e->incRefs(data);
1031 e->decRefs(ptr);
1032 }
1033
1034 memcpy(ptr, data, sizeBytes);
1035 drv->uploadDeferred = true;
1036}
1037
Jason Sams61a4bb72012-07-25 19:33:43 -07001038static void mip565(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -08001039 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1040 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
Jason Sams61a4bb72012-07-25 19:33:43 -07001041
1042 for (uint32_t y=0; y < h; y++) {
1043 uint16_t *oPtr = (uint16_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face);
1044 const uint16_t *i1 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2, lod, face);
1045 const uint16_t *i2 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face);
1046
1047 for (uint32_t x=0; x < w; x++) {
1048 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
1049 oPtr ++;
1050 i1 += 2;
1051 i2 += 2;
1052 }
1053 }
1054}
1055
1056static void mip8888(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -08001057 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1058 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
Jason Sams61a4bb72012-07-25 19:33:43 -07001059
1060 for (uint32_t y=0; y < h; y++) {
1061 uint32_t *oPtr = (uint32_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face);
1062 const uint32_t *i1 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2, lod, face);
1063 const uint32_t *i2 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face);
1064
1065 for (uint32_t x=0; x < w; x++) {
1066 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
1067 oPtr ++;
1068 i1 += 2;
1069 i2 += 2;
1070 }
1071 }
1072}
1073
1074static void mip8(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -08001075 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1076 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
Jason Sams61a4bb72012-07-25 19:33:43 -07001077
1078 for (uint32_t y=0; y < h; y++) {
1079 uint8_t *oPtr = GetOffsetPtr(alloc, 0, y, lod + 1, face);
1080 const uint8_t *i1 = GetOffsetPtr(alloc, 0, y*2, lod, face);
1081 const uint8_t *i2 = GetOffsetPtr(alloc, 0, y*2+1, lod, face);
1082
1083 for (uint32_t x=0; x < w; x++) {
1084 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
1085 oPtr ++;
1086 i1 += 2;
1087 i2 += 2;
1088 }
1089 }
1090}
1091
1092void rsdAllocationGenerateMipmaps(const Context *rsc, const Allocation *alloc) {
Jason Sams709a0972012-11-15 18:18:04 -08001093 if(!alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Sams61a4bb72012-07-25 19:33:43 -07001094 return;
1095 }
1096 uint32_t numFaces = alloc->getType()->getDimFaces() ? 6 : 1;
1097 for (uint32_t face = 0; face < numFaces; face ++) {
1098 for (uint32_t lod=0; lod < (alloc->getType()->getLODCount() -1); lod++) {
1099 switch (alloc->getType()->getElement()->getSizeBits()) {
1100 case 32:
1101 mip8888(alloc, lod, (RsAllocationCubemapFace)face);
1102 break;
1103 case 16:
1104 mip565(alloc, lod, (RsAllocationCubemapFace)face);
1105 break;
1106 case 8:
1107 mip8(alloc, lod, (RsAllocationCubemapFace)face);
1108 break;
1109 }
1110 }
1111 }
1112}