blob: f502f32efb4fb79369a42cc8727147f3abacd1f0 [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,
Jason Sams3bbc0fd2013-04-09 14:16:13 -070091 uint32_t xoff, uint32_t yoff, uint32_t zoff,
92 uint32_t lod, 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;
Jason Sams3bbc0fd2013-04-09 14:16:13 -070095 ptr += zoff * alloc->mHal.drvState.lod[lod].dimY * alloc->mHal.drvState.lod[lod].stride;
Jason Sams709a0972012-11-15 18:18:04 -080096 ptr += yoff * alloc->mHal.drvState.lod[lod].stride;
Jason Sams807fdc42012-07-25 17:55:39 -070097 ptr += xoff * alloc->mHal.state.elementSizeBytes;
98 return ptr;
99}
100
Jason Samsa614ae12011-05-26 17:05:51 -0700101
Jason Sams2382aba2011-09-13 15:41:01 -0700102static void Update2DTexture(const Context *rsc, const Allocation *alloc, const void *ptr,
103 uint32_t xoff, uint32_t yoff, uint32_t lod,
104 RsAllocationCubemapFace face, uint32_t w, uint32_t h) {
Jason Sams93eacc72012-12-18 14:26:57 -0800105#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700106 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
107
Jason Samseb4fe182011-05-26 16:33:01 -0700108 rsAssert(drv->textureID);
Jason Sams2382aba2011-09-13 15:41:01 -0700109 RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
110 RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
Jason Samseb4fe182011-05-26 16:33:01 -0700111 GLenum t = GL_TEXTURE_2D;
112 if (alloc->mHal.state.hasFaces) {
113 t = gFaceOrder[face];
114 }
Jason Sams2382aba2011-09-13 15:41:01 -0700115 RSD_CALL_GL(glTexSubImage2D, t, lod, xoff, yoff, w, h, drv->glFormat, drv->glType, ptr);
Jason Sams93eacc72012-12-18 14:26:57 -0800116#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700117}
118
119
Jason Sams93eacc72012-12-18 14:26:57 -0800120#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700121static void Upload2DTexture(const Context *rsc, const Allocation *alloc, bool isFirstUpload) {
122 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
123
Jason Sams2382aba2011-09-13 15:41:01 -0700124 RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
125 RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
Jason Samseb4fe182011-05-26 16:33:01 -0700126
127 uint32_t faceCount = 1;
128 if (alloc->mHal.state.hasFaces) {
129 faceCount = 6;
130 }
131
132 rsdGLCheckError(rsc, "Upload2DTexture 1 ");
133 for (uint32_t face = 0; face < faceCount; face ++) {
134 for (uint32_t lod = 0; lod < alloc->mHal.state.type->getLODCount(); lod++) {
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700135 const uint8_t *p = GetOffsetPtr(alloc, 0, 0, 0, lod, (RsAllocationCubemapFace)face);
Jason Samseb4fe182011-05-26 16:33:01 -0700136
137 GLenum t = GL_TEXTURE_2D;
138 if (alloc->mHal.state.hasFaces) {
139 t = gFaceOrder[face];
140 }
141
142 if (isFirstUpload) {
Jason Sams2382aba2011-09-13 15:41:01 -0700143 RSD_CALL_GL(glTexImage2D, t, lod, drv->glFormat,
Jason Samseb4fe182011-05-26 16:33:01 -0700144 alloc->mHal.state.type->getLODDimX(lod),
145 alloc->mHal.state.type->getLODDimY(lod),
Jason Samsa614ae12011-05-26 17:05:51 -0700146 0, drv->glFormat, drv->glType, p);
Jason Samseb4fe182011-05-26 16:33:01 -0700147 } else {
Jason Sams2382aba2011-09-13 15:41:01 -0700148 RSD_CALL_GL(glTexSubImage2D, t, lod, 0, 0,
Jason Samseb4fe182011-05-26 16:33:01 -0700149 alloc->mHal.state.type->getLODDimX(lod),
150 alloc->mHal.state.type->getLODDimY(lod),
Jason Samsa614ae12011-05-26 17:05:51 -0700151 drv->glFormat, drv->glType, p);
Jason Samseb4fe182011-05-26 16:33:01 -0700152 }
153 }
154 }
155
156 if (alloc->mHal.state.mipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
Jason Sams2382aba2011-09-13 15:41:01 -0700157 RSD_CALL_GL(glGenerateMipmap, drv->glTarget);
Jason Samseb4fe182011-05-26 16:33:01 -0700158 }
159 rsdGLCheckError(rsc, "Upload2DTexture");
160}
Jason Sams93eacc72012-12-18 14:26:57 -0800161#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700162
163static void UploadToTexture(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800164#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700165 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
166
Jason Sams3522f402012-03-23 11:47:26 -0700167 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
Jason Sams41e373d2012-01-13 14:01:20 -0800168 if (!drv->textureID) {
169 RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
170 }
171 return;
172 }
173
Jason Samsa614ae12011-05-26 17:05:51 -0700174 if (!drv->glType || !drv->glFormat) {
Jason Samseb4fe182011-05-26 16:33:01 -0700175 return;
176 }
177
Jason Sams709a0972012-11-15 18:18:04 -0800178 if (!alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Samseb4fe182011-05-26 16:33:01 -0700179 return;
180 }
181
182 bool isFirstUpload = false;
183
184 if (!drv->textureID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700185 RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
Jason Samseb4fe182011-05-26 16:33:01 -0700186 isFirstUpload = true;
187 }
188
189 Upload2DTexture(rsc, alloc, isFirstUpload);
190
191 if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
Jason Sams709a0972012-11-15 18:18:04 -0800192 if (alloc->mHal.drvState.lod[0].mallocPtr) {
193 free(alloc->mHal.drvState.lod[0].mallocPtr);
194 alloc->mHal.drvState.lod[0].mallocPtr = NULL;
Jason Samseb4fe182011-05-26 16:33:01 -0700195 }
196 }
197 rsdGLCheckError(rsc, "UploadToTexture");
Jason Sams93eacc72012-12-18 14:26:57 -0800198#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700199}
200
201static void AllocateRenderTarget(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800202#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700203 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
204
Jason Samsa614ae12011-05-26 17:05:51 -0700205 if (!drv->glFormat) {
Jason Samseb4fe182011-05-26 16:33:01 -0700206 return;
207 }
208
209 if (!drv->renderTargetID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700210 RSD_CALL_GL(glGenRenderbuffers, 1, &drv->renderTargetID);
Jason Samseb4fe182011-05-26 16:33:01 -0700211
212 if (!drv->renderTargetID) {
213 // This should generally not happen
Steve Blockaf12ac62012-01-06 19:20:56 +0000214 ALOGE("allocateRenderTarget failed to gen mRenderTargetID");
Jason Samseb4fe182011-05-26 16:33:01 -0700215 rsc->dumpDebug();
216 return;
217 }
Jason Sams2382aba2011-09-13 15:41:01 -0700218 RSD_CALL_GL(glBindRenderbuffer, GL_RENDERBUFFER, drv->renderTargetID);
219 RSD_CALL_GL(glRenderbufferStorage, GL_RENDERBUFFER, drv->glFormat,
Jason Samsa572aca2013-01-09 11:52:26 -0800220 alloc->mHal.drvState.lod[0].dimX, alloc->mHal.drvState.lod[0].dimY);
Jason Samseb4fe182011-05-26 16:33:01 -0700221 }
222 rsdGLCheckError(rsc, "AllocateRenderTarget");
Jason Sams93eacc72012-12-18 14:26:57 -0800223#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700224}
225
226static void UploadToBufferObject(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800227#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700228 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
229
230 rsAssert(!alloc->mHal.state.type->getDimY());
231 rsAssert(!alloc->mHal.state.type->getDimZ());
232
233 //alloc->mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
234
235 if (!drv->bufferID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700236 RSD_CALL_GL(glGenBuffers, 1, &drv->bufferID);
Jason Samseb4fe182011-05-26 16:33:01 -0700237 }
238 if (!drv->bufferID) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000239 ALOGE("Upload to buffer object failed");
Jason Samseb4fe182011-05-26 16:33:01 -0700240 drv->uploadDeferred = true;
241 return;
242 }
Jason Sams2382aba2011-09-13 15:41:01 -0700243 RSD_CALL_GL(glBindBuffer, drv->glTarget, drv->bufferID);
244 RSD_CALL_GL(glBufferData, drv->glTarget, alloc->mHal.state.type->getSizeBytes(),
Jason Sams709a0972012-11-15 18:18:04 -0800245 alloc->mHal.drvState.lod[0].mallocPtr, GL_DYNAMIC_DRAW);
Jason Sams2382aba2011-09-13 15:41:01 -0700246 RSD_CALL_GL(glBindBuffer, drv->glTarget, 0);
Jason Samseb4fe182011-05-26 16:33:01 -0700247 rsdGLCheckError(rsc, "UploadToBufferObject");
Jason Sams93eacc72012-12-18 14:26:57 -0800248#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700249}
250
Tim Murray0b575de2013-03-15 15:56:43 -0700251
Jason Sams9d8e5af2013-02-28 14:00:08 -0800252static size_t DeriveYUVLayout(int yuv, Allocation::Hal::DrvState *state) {
Jason Sams733396b2013-02-22 12:46:18 -0800253 // YUV only supports basic 2d
254 // so we can stash the plane pointers in the mipmap levels.
Jason Sams9d8e5af2013-02-28 14:00:08 -0800255 size_t uvSize = 0;
Tim Murray0b575de2013-03-15 15:56:43 -0700256#ifndef RS_SERVER
Jason Sams733396b2013-02-22 12:46:18 -0800257 switch(yuv) {
258 case HAL_PIXEL_FORMAT_YV12:
Jason Sams4961cce2013-04-11 16:11:46 -0700259 state->lod[2].dimX = state->lod[0].dimX / 2;
260 state->lod[2].dimY = state->lod[0].dimY / 2;
261 state->lod[2].stride = rsRound(state->lod[0].stride >> 1, 16);
262 state->lod[2].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) +
Jason Sams733396b2013-02-22 12:46:18 -0800263 (state->lod[0].stride * state->lod[0].dimY);
Jason Sams9d8e5af2013-02-28 14:00:08 -0800264 uvSize += state->lod[2].stride * state->lod[2].dimY;
Jason Sams733396b2013-02-22 12:46:18 -0800265
Jason Sams4961cce2013-04-11 16:11:46 -0700266 state->lod[1].dimX = state->lod[2].dimX;
267 state->lod[1].dimY = state->lod[2].dimY;
268 state->lod[1].stride = state->lod[2].stride;
269 state->lod[1].mallocPtr = ((uint8_t *)state->lod[2].mallocPtr) +
270 (state->lod[2].stride * state->lod[2].dimY);
271 uvSize += state->lod[1].stride * state->lod[2].dimY;
272
Jason Sams733396b2013-02-22 12:46:18 -0800273 state->lodCount = 3;
274 break;
275 case HAL_PIXEL_FORMAT_YCrCb_420_SP: // NV21
276 state->lod[1].dimX = state->lod[0].dimX;
277 state->lod[1].dimY = state->lod[0].dimY / 2;
278 state->lod[1].stride = state->lod[0].stride;
279 state->lod[1].mallocPtr = ((uint8_t *)state->lod[0].mallocPtr) +
280 (state->lod[0].stride * state->lod[0].dimY);
Jason Sams9d8e5af2013-02-28 14:00:08 -0800281 uvSize += state->lod[1].stride * state->lod[1].dimY;
Jason Sams733396b2013-02-22 12:46:18 -0800282 state->lodCount = 2;
283 break;
284 default:
285 rsAssert(0);
286 }
Tim Murray0b575de2013-03-15 15:56:43 -0700287#endif
Jason Sams9d8e5af2013-02-28 14:00:08 -0800288 return uvSize;
Jason Sams733396b2013-02-22 12:46:18 -0800289}
290
291
Jason Sams463bfce2012-08-21 13:54:42 -0700292static size_t AllocationBuildPointerTable(const Context *rsc, const Allocation *alloc,
293 const Type *type, uint8_t *ptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800294 alloc->mHal.drvState.lod[0].dimX = type->getDimX();
295 alloc->mHal.drvState.lod[0].dimY = type->getDimY();
Jason Samsf2b611e2012-12-27 19:05:22 -0800296 alloc->mHal.drvState.lod[0].dimZ = type->getDimZ();
Jason Sams709a0972012-11-15 18:18:04 -0800297 alloc->mHal.drvState.lod[0].mallocPtr = 0;
Stephen Hines94999c32013-02-05 16:58:22 -0800298 // Stride needs to be 16-byte aligned too!
299 size_t stride = alloc->mHal.drvState.lod[0].dimX * type->getElementSizeBytes();
300 alloc->mHal.drvState.lod[0].stride = rsRound(stride, 16);
Jason Sams709a0972012-11-15 18:18:04 -0800301 alloc->mHal.drvState.lodCount = type->getLODCount();
302 alloc->mHal.drvState.faceCount = type->getDimFaces();
Jason Sams807fdc42012-07-25 17:55:39 -0700303
304 size_t offsets[Allocation::MAX_LOD];
305 memset(offsets, 0, sizeof(offsets));
306
Jason Sams709a0972012-11-15 18:18:04 -0800307 size_t o = alloc->mHal.drvState.lod[0].stride * rsMax(alloc->mHal.drvState.lod[0].dimY, 1u) *
308 rsMax(alloc->mHal.drvState.lod[0].dimZ, 1u);
309 if(alloc->mHal.drvState.lodCount > 1) {
310 uint32_t tx = alloc->mHal.drvState.lod[0].dimX;
311 uint32_t ty = alloc->mHal.drvState.lod[0].dimY;
312 uint32_t tz = alloc->mHal.drvState.lod[0].dimZ;
313 for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
314 alloc->mHal.drvState.lod[lod].dimX = tx;
315 alloc->mHal.drvState.lod[lod].dimY = ty;
316 alloc->mHal.drvState.lod[lod].dimZ = tz;
Stephen Hines94999c32013-02-05 16:58:22 -0800317 alloc->mHal.drvState.lod[lod].stride =
318 rsRound(tx * type->getElementSizeBytes(), 16);
Jason Sams807fdc42012-07-25 17:55:39 -0700319 offsets[lod] = o;
Jason Sams709a0972012-11-15 18:18:04 -0800320 o += alloc->mHal.drvState.lod[lod].stride * rsMax(ty, 1u) * rsMax(tz, 1u);
Jason Sams807fdc42012-07-25 17:55:39 -0700321 if (tx > 1) tx >>= 1;
322 if (ty > 1) ty >>= 1;
323 if (tz > 1) tz >>= 1;
324 }
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800325 } else if (alloc->mHal.state.yuv) {
Jason Sams9d8e5af2013-02-28 14:00:08 -0800326 o += DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState);
Jason Sams733396b2013-02-22 12:46:18 -0800327
328 for (uint32_t ct = 1; ct < alloc->mHal.drvState.lodCount; ct++) {
329 offsets[ct] = (size_t)alloc->mHal.drvState.lod[ct].mallocPtr;
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800330 }
Jason Sams807fdc42012-07-25 17:55:39 -0700331 }
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800332
Jason Sams709a0972012-11-15 18:18:04 -0800333 alloc->mHal.drvState.faceOffset = o;
Jason Sams807fdc42012-07-25 17:55:39 -0700334
Jason Sams709a0972012-11-15 18:18:04 -0800335 alloc->mHal.drvState.lod[0].mallocPtr = ptr;
336 for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
337 alloc->mHal.drvState.lod[lod].mallocPtr = ptr + offsets[lod];
Jason Sams463bfce2012-08-21 13:54:42 -0700338 }
Jason Sams463bfce2012-08-21 13:54:42 -0700339
Jason Sams709a0972012-11-15 18:18:04 -0800340 size_t allocSize = alloc->mHal.drvState.faceOffset;
341 if(alloc->mHal.drvState.faceCount) {
Jason Sams463bfce2012-08-21 13:54:42 -0700342 allocSize *= 6;
343 }
344
345 return allocSize;
346}
347
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800348static uint8_t* allocAlignedMemory(size_t allocSize, bool forceZero) {
349 // We align all allocations to a 16-byte boundary.
350 uint8_t* ptr = (uint8_t *)memalign(16, allocSize);
351 if (!ptr) {
352 return NULL;
353 }
354 if (forceZero) {
355 memset(ptr, 0, allocSize);
356 }
357 return ptr;
358}
359
Jason Sams463bfce2012-08-21 13:54:42 -0700360bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) {
361 DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation));
362 if (!drv) {
363 return false;
364 }
365 alloc->mHal.drv = drv;
366
367 // Calculate the object size.
368 size_t allocSize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), NULL);
369
Jason Sams807fdc42012-07-25 17:55:39 -0700370 uint8_t * ptr = NULL;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800371 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) {
Jason Sams733396b2013-02-22 12:46:18 -0800372
373 } else if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
374 // Allocation is allocated when the surface is created
375 // in getSurface
Tim Murray2e1a94d2012-11-29 13:12:25 -0800376 } else if (alloc->mHal.state.userProvidedPtr != NULL) {
377 // user-provided allocation
Tim Murrayba24d082013-04-09 17:31:11 -0700378 // limitations: no faces, no LOD, USAGE_SCRIPT or SCRIPT+TEXTURE only
379 if (!(alloc->mHal.state.usageFlags == (RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED) ||
380 alloc->mHal.state.usageFlags == (RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED | RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE))) {
381 ALOGE("Can't use user-allocated buffers if usage is not USAGE_SCRIPT | USAGE_SHARED or USAGE_SCRIPT | USAGE_SHARED | USAGE_GRAPHICS_TEXTURE");
Tim Murray2e1a94d2012-11-29 13:12:25 -0800382 return false;
383 }
384 if (alloc->getType()->getDimLOD() || alloc->getType()->getDimFaces()) {
385 ALOGE("User-allocated buffers must not have multiple faces or LODs");
386 return false;
387 }
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800388
389 // rows must be 16-byte aligned
390 // validate that here, otherwise fall back to not use the user-backed allocation
391 if (((alloc->getType()->getDimX() * alloc->getType()->getElement()->getSizeBytes()) % 16) != 0) {
392 ALOGV("User-backed allocation failed stride requirement, falling back to separate allocation");
393 drv->useUserProvidedPtr = false;
394
395 ptr = allocAlignedMemory(allocSize, forceZero);
396 if (!ptr) {
397 alloc->mHal.drv = NULL;
398 free(drv);
399 return false;
400 }
401
402 } else {
403 drv->useUserProvidedPtr = true;
404 ptr = (uint8_t*)alloc->mHal.state.userProvidedPtr;
405 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800406 } else {
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800407 ptr = allocAlignedMemory(allocSize, forceZero);
Jason Sams179e9a42011-11-23 15:02:15 -0800408 if (!ptr) {
You Kimed419f32012-12-17 03:42:57 +0900409 alloc->mHal.drv = NULL;
Jason Sams179e9a42011-11-23 15:02:15 -0800410 free(drv);
411 return false;
412 }
Jason Samseb4fe182011-05-26 16:33:01 -0700413 }
Jason Sams463bfce2012-08-21 13:54:42 -0700414 // Build the pointer tables
415 size_t verifySize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), ptr);
416 if(allocSize != verifySize) {
417 rsAssert(!"Size mismatch");
Jason Sams807fdc42012-07-25 17:55:39 -0700418 }
Jason Samseb4fe182011-05-26 16:33:01 -0700419
Tim Murray0b575de2013-03-15 15:56:43 -0700420#ifndef RS_SERVER
Jason Samseb4fe182011-05-26 16:33:01 -0700421 drv->glTarget = GL_NONE;
422 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
423 if (alloc->mHal.state.hasFaces) {
424 drv->glTarget = GL_TEXTURE_CUBE_MAP;
425 } else {
426 drv->glTarget = GL_TEXTURE_2D;
427 }
428 } else {
429 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
430 drv->glTarget = GL_ARRAY_BUFFER;
431 }
432 }
Tim Murray0b575de2013-03-15 15:56:43 -0700433#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700434
Jason Sams93eacc72012-12-18 14:26:57 -0800435#ifndef RS_COMPATIBILITY_LIB
Jason Samsa614ae12011-05-26 17:05:51 -0700436 drv->glType = rsdTypeToGLType(alloc->mHal.state.type->getElement()->getComponent().getType());
437 drv->glFormat = rsdKindToGLFormat(alloc->mHal.state.type->getElement()->getComponent().getKind());
Jason Sams93eacc72012-12-18 14:26:57 -0800438#else
439 drv->glType = 0;
440 drv->glFormat = 0;
441#endif
Jason Samsa614ae12011-05-26 17:05:51 -0700442
Jason Samseb4fe182011-05-26 16:33:01 -0700443 if (alloc->mHal.state.usageFlags & ~RS_ALLOCATION_USAGE_SCRIPT) {
444 drv->uploadDeferred = true;
445 }
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700446
Jason Samsb3220332012-04-02 14:41:54 -0700447
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700448 drv->readBackFBO = NULL;
449
Tim Murrayc2cfe6a2013-02-14 16:21:33 -0800450 // fill out the initial state of the buffer if we couldn't use the user-provided ptr and USAGE_SHARED was accepted
451 if ((alloc->mHal.state.userProvidedPtr != 0) && (drv->useUserProvidedPtr == false)) {
452 rsdAllocationData2D(rsc, alloc, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X, alloc->getType()->getDimX(), alloc->getType()->getDimY(), alloc->mHal.state.userProvidedPtr, allocSize, 0);
453 }
454
Jason Samseb4fe182011-05-26 16:33:01 -0700455 return true;
456}
457
458void rsdAllocationDestroy(const Context *rsc, Allocation *alloc) {
459 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
460
Jason Sams93eacc72012-12-18 14:26:57 -0800461#ifndef RS_COMPATIBILITY_LIB
Jason Samseb4fe182011-05-26 16:33:01 -0700462 if (drv->bufferID) {
463 // Causes a SW crash....
Steve Block65982012011-10-20 11:56:00 +0100464 //ALOGV(" mBufferID %i", mBufferID);
Jason Samseb4fe182011-05-26 16:33:01 -0700465 //glDeleteBuffers(1, &mBufferID);
466 //mBufferID = 0;
467 }
468 if (drv->textureID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700469 RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID);
Jason Samseb4fe182011-05-26 16:33:01 -0700470 drv->textureID = 0;
471 }
472 if (drv->renderTargetID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700473 RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID);
Jason Samseb4fe182011-05-26 16:33:01 -0700474 drv->renderTargetID = 0;
475 }
Jason Sams93eacc72012-12-18 14:26:57 -0800476#endif
Jason Samseb4fe182011-05-26 16:33:01 -0700477
Jason Sams709a0972012-11-15 18:18:04 -0800478 if (alloc->mHal.drvState.lod[0].mallocPtr) {
Tim Murrayaafa7a82013-05-09 13:22:53 -0700479 // don't free user-allocated ptrs or IO_OUTPUT buffers
480 if (!(drv->useUserProvidedPtr) &&
Sakshi Agrawalc88bcef2013-05-13 16:46:09 -0700481 !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) &&
Tim Murrayaafa7a82013-05-09 13:22:53 -0700482 !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
Sakshi Agrawalc88bcef2013-05-13 16:46:09 -0700483 free(alloc->mHal.drvState.lod[0].mallocPtr);
Tim Murray2e1a94d2012-11-29 13:12:25 -0800484 }
Jason Sams709a0972012-11-15 18:18:04 -0800485 alloc->mHal.drvState.lod[0].mallocPtr = NULL;
Jason Samseb4fe182011-05-26 16:33:01 -0700486 }
Jason Sams93eacc72012-12-18 14:26:57 -0800487
488#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700489 if (drv->readBackFBO != NULL) {
490 delete drv->readBackFBO;
491 drv->readBackFBO = NULL;
492 }
Jason Sams10c9dd72013-02-01 10:53:42 -0800493
494 if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) &&
495 (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
496
497 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Tim Murray3a25fdd2013-02-22 17:56:56 -0800498 ANativeWindow *nw = drv->wndSurface;
499 if (nw) {
500 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
501 mapper.unlock(drv->wndBuffer->handle);
502 int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
503 }
Jason Sams10c9dd72013-02-01 10:53:42 -0800504 }
Jason Sams93eacc72012-12-18 14:26:57 -0800505#endif
506
Jason Samseb4fe182011-05-26 16:33:01 -0700507 free(drv);
508 alloc->mHal.drv = NULL;
509}
510
511void rsdAllocationResize(const Context *rsc, const Allocation *alloc,
512 const Type *newType, bool zeroNew) {
Jason Samsa572aca2013-01-09 11:52:26 -0800513 const uint32_t oldDimX = alloc->mHal.drvState.lod[0].dimX;
514 const uint32_t dimX = newType->getDimX();
515
Tim Murray9e2bda52012-12-18 12:05:46 -0800516 // can't resize Allocations with user-allocated buffers
517 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) {
518 ALOGE("Resize cannot be called on a USAGE_SHARED allocation");
519 return;
520 }
Jason Sams709a0972012-11-15 18:18:04 -0800521 void * oldPtr = alloc->mHal.drvState.lod[0].mallocPtr;
Jason Sams463bfce2012-08-21 13:54:42 -0700522 // Calculate the object size
523 size_t s = AllocationBuildPointerTable(rsc, alloc, newType, NULL);
524 uint8_t *ptr = (uint8_t *)realloc(oldPtr, s);
525 // Build the relative pointer tables.
526 size_t verifySize = AllocationBuildPointerTable(rsc, alloc, newType, ptr);
527 if(s != verifySize) {
528 rsAssert(!"Size mismatch");
529 }
Jason Samseb4fe182011-05-26 16:33:01 -0700530
Jason Samseb4fe182011-05-26 16:33:01 -0700531
532 if (dimX > oldDimX) {
Tim Murray099bc262013-03-20 16:54:03 -0700533 size_t stride = alloc->mHal.state.elementSizeBytes;
Jason Sams709a0972012-11-15 18:18:04 -0800534 memset(((uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr) + stride * oldDimX,
Jason Sams2275e9c2012-04-16 17:01:26 -0700535 0, stride * (dimX - oldDimX));
Jason Samseb4fe182011-05-26 16:33:01 -0700536 }
537}
538
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700539static void rsdAllocationSyncFromFBO(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800540#ifndef RS_COMPATIBILITY_LIB
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700541 if (!alloc->getIsScript()) {
542 return; // nothing to sync
543 }
544
545 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
546 RsdFrameBufferObj *lastFbo = dc->gl.currentFrameBuffer;
547
548 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
549 if (!drv->textureID && !drv->renderTargetID) {
550 return; // nothing was rendered here yet, so nothing to sync
551 }
552 if (drv->readBackFBO == NULL) {
553 drv->readBackFBO = new RsdFrameBufferObj();
554 drv->readBackFBO->setColorTarget(drv, 0);
555 drv->readBackFBO->setDimensions(alloc->getType()->getDimX(),
556 alloc->getType()->getDimY());
557 }
558
559 // Bind the framebuffer object so we can read back from it
560 drv->readBackFBO->setActive(rsc);
561
562 // Do the readback
Jason Sams709a0972012-11-15 18:18:04 -0800563 RSD_CALL_GL(glReadPixels, 0, 0, alloc->mHal.drvState.lod[0].dimX,
564 alloc->mHal.drvState.lod[0].dimY,
565 drv->glFormat, drv->glType, alloc->mHal.drvState.lod[0].mallocPtr);
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700566
567 // Revert framebuffer to its original
568 lastFbo->setActive(rsc);
Jason Sams93eacc72012-12-18 14:26:57 -0800569#endif
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700570}
Jason Samseb4fe182011-05-26 16:33:01 -0700571
572
573void rsdAllocationSyncAll(const Context *rsc, const Allocation *alloc,
574 RsAllocationUsageType src) {
575 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
576
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700577 if (src == RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
578 if(!alloc->getIsRenderTarget()) {
579 rsc->setError(RS_ERROR_FATAL_DRIVER,
580 "Attempting to sync allocation from render target, "
581 "for non-render target allocation");
582 } else if (alloc->getType()->getElement()->getKind() != RS_KIND_PIXEL_RGBA) {
583 rsc->setError(RS_ERROR_FATAL_DRIVER, "Cannot only sync from RGBA"
584 "render target");
585 } else {
586 rsdAllocationSyncFromFBO(rsc, alloc);
587 }
Jason Samseb4fe182011-05-26 16:33:01 -0700588 return;
589 }
590
591 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
592
593 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
594 UploadToTexture(rsc, alloc);
595 } else {
Jason Samsb3220332012-04-02 14:41:54 -0700596 if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) &&
Jason Sams0dc66932012-04-10 17:05:49 -0700597 !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
Jason Samseb4fe182011-05-26 16:33:01 -0700598 AllocateRenderTarget(rsc, alloc);
599 }
600 }
601 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
602 UploadToBufferObject(rsc, alloc);
603 }
604
Tim Murrayba24d082013-04-09 17:31:11 -0700605 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) {
606 // NOP in CPU driver for now
607 }
608
Jason Samseb4fe182011-05-26 16:33:01 -0700609 drv->uploadDeferred = false;
610}
611
612void rsdAllocationMarkDirty(const Context *rsc, const Allocation *alloc) {
613 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
614 drv->uploadDeferred = true;
615}
616
Jason Sams4961cce2013-04-11 16:11:46 -0700617#ifndef RS_COMPATIBILITY_LIB
618void DrvAllocation::NewBufferListener::onFrameAvailable() {
619 intptr_t ip = (intptr_t)alloc;
620 rsc->sendMessageToClient(NULL, RS_MESSAGE_TO_CLIENT_NEW_BUFFER, ip, 0, true);
621}
622#endif
623
Jason Sams733396b2013-02-22 12:46:18 -0800624void* rsdAllocationGetSurface(const Context *rsc, const Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800625#ifndef RS_COMPATIBILITY_LIB
Jason Sams41e373d2012-01-13 14:01:20 -0800626 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Jason Sams733396b2013-02-22 12:46:18 -0800627
Eino-Ville Talvala0d44f122013-04-03 16:42:35 -0700628 // Configure CpuConsumer to be in asynchronous mode
629 drv->cpuConsumer = new CpuConsumer(2, false);
Jason Sams733396b2013-02-22 12:46:18 -0800630 sp<IGraphicBufferProducer> bp = drv->cpuConsumer->getProducerInterface();
631 bp->incStrong(NULL);
Jason Sams4961cce2013-04-11 16:11:46 -0700632
633 drv->mBufferListener = new DrvAllocation::NewBufferListener();
634 drv->mBufferListener->rsc = rsc;
635 drv->mBufferListener->alloc = alloc;
636
637 drv->cpuConsumer->setFrameAvailableListener(drv->mBufferListener);
638
Jason Sams733396b2013-02-22 12:46:18 -0800639 return bp.get();
Jason Sams93eacc72012-12-18 14:26:57 -0800640#else
Jason Sams733396b2013-02-22 12:46:18 -0800641 return NULL;
Jason Sams93eacc72012-12-18 14:26:57 -0800642#endif
Jason Sams41e373d2012-01-13 14:01:20 -0800643}
644
Jason Sams93eacc72012-12-18 14:26:57 -0800645#ifndef RS_COMPATIBILITY_LIB
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800646static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
647 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
648
Jamie Genniscd919a12012-06-13 16:34:11 -0700649 int32_t r = native_window_dequeue_buffer_and_wait(nw, &drv->wndBuffer);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800650 if (r) {
651 rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer.");
652 return false;
653 }
654
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800655 // Must lock the whole surface
656 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
657 Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height);
658
659 void *dst = NULL;
660 mapper.lock(drv->wndBuffer->handle,
661 GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN,
662 bounds, &dst);
Jason Sams709a0972012-11-15 18:18:04 -0800663 alloc->mHal.drvState.lod[0].mallocPtr = dst;
664 alloc->mHal.drvState.lod[0].stride = drv->wndBuffer->stride * alloc->mHal.state.elementSizeBytes;
Stephen Hines94999c32013-02-05 16:58:22 -0800665 rsAssert((alloc->mHal.drvState.lod[0].stride & 0xf) == 0);
Jason Samsb3220332012-04-02 14:41:54 -0700666
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800667 return true;
668}
Jason Sams93eacc72012-12-18 14:26:57 -0800669#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800670
Jason Sams733396b2013-02-22 12:46:18 -0800671void rsdAllocationSetSurface(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
Jason Sams93eacc72012-12-18 14:26:57 -0800672#ifndef RS_COMPATIBILITY_LIB
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800673 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Tim Murray3a25fdd2013-02-22 17:56:56 -0800674 ANativeWindow *old = drv->wndSurface;
675
676 if (nw) {
677 nw->incStrong(NULL);
678 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800679
Jason Samsb3220332012-04-02 14:41:54 -0700680 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
681 //TODO finish support for render target + script
682 drv->wnd = nw;
683 return;
684 }
685
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800686 // Cleanup old surface if there is one.
Tim Murray3a25fdd2013-02-22 17:56:56 -0800687 if (drv->wndSurface) {
688 ANativeWindow *old = drv->wndSurface;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800689 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
690 mapper.unlock(drv->wndBuffer->handle);
Tim Murray3a25fdd2013-02-22 17:56:56 -0800691 old->cancelBuffer(old, drv->wndBuffer, -1);
692 drv->wndSurface = NULL;
693 old->decStrong(NULL);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800694 }
695
696 if (nw != NULL) {
697 int32_t r;
Jason Samsb3220332012-04-02 14:41:54 -0700698 uint32_t flags = 0;
Tim Murray3a25fdd2013-02-22 17:56:56 -0800699
Jason Samsb3220332012-04-02 14:41:54 -0700700 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
701 flags |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN;
702 }
703 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
704 flags |= GRALLOC_USAGE_HW_RENDER;
705 }
706
707 r = native_window_set_usage(nw, flags);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800708 if (r) {
709 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
Tim Murray3a25fdd2013-02-22 17:56:56 -0800710 goto error;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800711 }
712
Jason Samsa572aca2013-01-09 11:52:26 -0800713 r = native_window_set_buffers_dimensions(nw, alloc->mHal.drvState.lod[0].dimX,
714 alloc->mHal.drvState.lod[0].dimY);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800715 if (r) {
716 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions.");
Tim Murray3a25fdd2013-02-22 17:56:56 -0800717 goto error;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800718 }
719
Jason Sams733396b2013-02-22 12:46:18 -0800720 int format = 0;
721 const Element *e = alloc->mHal.state.type->getElement();
722 switch(e->getType()) {
723 case RS_TYPE_UNSIGNED_8:
724 switch (e->getVectorSize()) {
725 case 1:
726 rsAssert(e->getKind() == RS_KIND_PIXEL_A);
727 format = PIXEL_FORMAT_A_8;
728 break;
729 case 4:
730 rsAssert(e->getKind() == RS_KIND_PIXEL_RGBA);
731 format = PIXEL_FORMAT_RGBA_8888;
732 break;
733 default:
734 rsAssert(0);
735 }
736 break;
737 default:
738 rsAssert(0);
739 }
740
741 r = native_window_set_buffers_format(nw, format);
742 if (r) {
743 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer format.");
Tim Murray3a25fdd2013-02-22 17:56:56 -0800744 goto error;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800745 }
746
747 IoGetBuffer(rsc, alloc, nw);
Tim Murray3a25fdd2013-02-22 17:56:56 -0800748 drv->wndSurface = nw;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800749 }
Tim Murray3a25fdd2013-02-22 17:56:56 -0800750
751 return;
752
753 error:
754
755 if (nw) {
756 nw->decStrong(NULL);
757 }
758
759
Jason Sams93eacc72012-12-18 14:26:57 -0800760#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800761}
762
763void rsdAllocationIoSend(const Context *rsc, Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800764#ifndef RS_COMPATIBILITY_LIB
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800765 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Tim Murray3a25fdd2013-02-22 17:56:56 -0800766 ANativeWindow *nw = drv->wndSurface;
Jason Samsb3220332012-04-02 14:41:54 -0700767 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
768 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
769 RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800770 return;
771 }
Tim Murray3a25fdd2013-02-22 17:56:56 -0800772 if (nw) {
773 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
774 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
775 mapper.unlock(drv->wndBuffer->handle);
776 int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
777 if (r) {
778 rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer.");
779 return;
780 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800781
Tim Murray3a25fdd2013-02-22 17:56:56 -0800782 IoGetBuffer(rsc, alloc, nw);
Jason Samsb3220332012-04-02 14:41:54 -0700783 }
Tim Murray3a25fdd2013-02-22 17:56:56 -0800784 } else {
785 rsc->setError(RS_ERROR_DRIVER, "Sent IO buffer with no attached surface.");
786 return;
Jason Samsb3220332012-04-02 14:41:54 -0700787 }
Jason Sams93eacc72012-12-18 14:26:57 -0800788#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800789}
790
791void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) {
Jason Sams93eacc72012-12-18 14:26:57 -0800792#ifndef RS_COMPATIBILITY_LIB
Jason Sams3522f402012-03-23 11:47:26 -0700793 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Jason Sams733396b2013-02-22 12:46:18 -0800794
795 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
Eino-Ville Talvala0d44f122013-04-03 16:42:35 -0700796 CpuConsumer::LockedBuffer lb;
797 status_t ret = drv->cpuConsumer->lockNextBuffer(&lb);
798 if (ret == OK) {
799 if (drv->lb.data != NULL) {
800 drv->cpuConsumer->unlockBuffer(drv->lb);
801 }
802 drv->lb = lb;
803 alloc->mHal.drvState.lod[0].mallocPtr = drv->lb.data;
804 alloc->mHal.drvState.lod[0].stride = drv->lb.stride *
805 alloc->mHal.state.elementSizeBytes;
Jason Sams733396b2013-02-22 12:46:18 -0800806
Eino-Ville Talvala0d44f122013-04-03 16:42:35 -0700807 if (alloc->mHal.state.yuv) {
808 DeriveYUVLayout(alloc->mHal.state.yuv, &alloc->mHal.drvState);
809 }
810 } else if (ret == BAD_VALUE) {
811 // No new frame, don't do anything
812 } else {
813 rsc->setError(RS_ERROR_DRIVER, "Error receiving IO input buffer.");
Jason Sams733396b2013-02-22 12:46:18 -0800814 }
815
816 } else {
Tim Murray3a25fdd2013-02-22 17:56:56 -0800817 drv->surfaceTexture->updateTexImage();
Jason Sams733396b2013-02-22 12:46:18 -0800818 }
819
820
Jason Sams93eacc72012-12-18 14:26:57 -0800821#endif
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800822}
823
824
Jason Samseb4fe182011-05-26 16:33:01 -0700825void rsdAllocationData1D(const Context *rsc, const Allocation *alloc,
Tim Murray099bc262013-03-20 16:54:03 -0700826 uint32_t xoff, uint32_t lod, size_t count,
Alex Sakhartchoukc794cd52012-02-13 11:57:32 -0800827 const void *data, size_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -0700828 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
829
Tim Murray099bc262013-03-20 16:54:03 -0700830 const size_t eSize = alloc->mHal.state.type->getElementSizeBytes();
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700831 uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Tim Murray099bc262013-03-20 16:54:03 -0700832 size_t size = count * eSize;
Jason Samseb4fe182011-05-26 16:33:01 -0700833
Stephen Hines20c535f2012-12-06 19:04:38 -0800834 if (ptr != data) {
835 // Skip the copy if we are the same allocation. This can arise from
836 // our Bitmap optimization, where we share the same storage.
837 if (alloc->mHal.state.hasReferences) {
838 alloc->incRefs(data, count);
839 alloc->decRefs(ptr, count);
840 }
841 memcpy(ptr, data, size);
Jason Samseb4fe182011-05-26 16:33:01 -0700842 }
Jason Samseb4fe182011-05-26 16:33:01 -0700843 drv->uploadDeferred = true;
844}
845
846void rsdAllocationData2D(const Context *rsc, const Allocation *alloc,
847 uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800848 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Jason Samseb4fe182011-05-26 16:33:01 -0700849 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
850
Tim Murray099bc262013-03-20 16:54:03 -0700851 size_t eSize = alloc->mHal.state.elementSizeBytes;
852 size_t lineSize = eSize * w;
Tim Murray358747a2012-11-26 13:52:04 -0800853 if (!stride) {
854 stride = lineSize;
855 }
Jason Samseb4fe182011-05-26 16:33:01 -0700856
Jason Sams709a0972012-11-15 18:18:04 -0800857 if (alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Samseb4fe182011-05-26 16:33:01 -0700858 const uint8_t *src = static_cast<const uint8_t *>(data);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700859 uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face);
Stephen Hines20c535f2012-12-06 19:04:38 -0800860 if (dst == src) {
861 // Skip the copy if we are the same allocation. This can arise from
862 // our Bitmap optimization, where we share the same storage.
863 drv->uploadDeferred = true;
864 return;
865 }
Jason Samseb4fe182011-05-26 16:33:01 -0700866
867 for (uint32_t line=yoff; line < (yoff+h); line++) {
868 if (alloc->mHal.state.hasReferences) {
869 alloc->incRefs(src, w);
870 alloc->decRefs(dst, w);
871 }
872 memcpy(dst, src, lineSize);
Tim Murray358747a2012-11-26 13:52:04 -0800873 src += stride;
Jason Sams709a0972012-11-15 18:18:04 -0800874 dst += alloc->mHal.drvState.lod[lod].stride;
Jason Samseb4fe182011-05-26 16:33:01 -0700875 }
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800876 if (alloc->mHal.state.yuv) {
877 int lod = 1;
878 while (alloc->mHal.drvState.lod[lod].mallocPtr) {
Tim Murray099bc262013-03-20 16:54:03 -0700879 size_t lineSize = alloc->mHal.drvState.lod[lod].dimX;
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700880 uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face);
Jason Samsbc0ca6b2013-02-15 18:13:43 -0800881
882 for (uint32_t line=(yoff >> 1); line < ((yoff+h)>>1); line++) {
883 memcpy(dst, src, lineSize);
884 src += lineSize;
885 dst += alloc->mHal.drvState.lod[lod].stride;
886 }
887 lod++;
888 }
889
890 }
Jason Samseb4fe182011-05-26 16:33:01 -0700891 drv->uploadDeferred = true;
892 } else {
Jason Sams2382aba2011-09-13 15:41:01 -0700893 Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h);
Jason Samseb4fe182011-05-26 16:33:01 -0700894 }
895}
896
897void rsdAllocationData3D(const Context *rsc, const Allocation *alloc,
898 uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700899 uint32_t lod,
900 uint32_t w, uint32_t h, uint32_t d, const void *data,
901 size_t sizeBytes, size_t stride) {
902 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
Jason Samseb4fe182011-05-26 16:33:01 -0700903
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700904 uint32_t eSize = alloc->mHal.state.elementSizeBytes;
905 uint32_t lineSize = eSize * w;
906 if (!stride) {
907 stride = lineSize;
908 }
909
910 if (alloc->mHal.drvState.lod[0].mallocPtr) {
911 const uint8_t *src = static_cast<const uint8_t *>(data);
912 for (uint32_t z = zoff; z < d; z++) {
913 uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, z, lod,
914 RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
915 if (dst == src) {
916 // Skip the copy if we are the same allocation. This can arise from
917 // our Bitmap optimization, where we share the same storage.
918 drv->uploadDeferred = true;
919 return;
920 }
921
922 for (uint32_t line=yoff; line < (yoff+h); line++) {
923 if (alloc->mHal.state.hasReferences) {
924 alloc->incRefs(src, w);
925 alloc->decRefs(dst, w);
926 }
927 memcpy(dst, src, lineSize);
928 src += stride;
929 dst += alloc->mHal.drvState.lod[lod].stride;
930 }
931 }
932 drv->uploadDeferred = true;
933 }
Jason Samseb4fe182011-05-26 16:33:01 -0700934}
935
Jason Sams807fdc42012-07-25 17:55:39 -0700936void rsdAllocationRead1D(const Context *rsc, const Allocation *alloc,
Tim Murray099bc262013-03-20 16:54:03 -0700937 uint32_t xoff, uint32_t lod, size_t count,
Jason Sams807fdc42012-07-25 17:55:39 -0700938 void *data, size_t sizeBytes) {
Tim Murray099bc262013-03-20 16:54:03 -0700939 const size_t eSize = alloc->mHal.state.type->getElementSizeBytes();
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700940 const uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Stephen Hines20c535f2012-12-06 19:04:38 -0800941 if (data != ptr) {
942 // Skip the copy if we are the same allocation. This can arise from
943 // our Bitmap optimization, where we share the same storage.
944 memcpy(data, ptr, count * eSize);
945 }
Jason Sams807fdc42012-07-25 17:55:39 -0700946}
947
948void rsdAllocationRead2D(const Context *rsc, const Allocation *alloc,
Tim Murray358747a2012-11-26 13:52:04 -0800949 uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
950 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
Tim Murray099bc262013-03-20 16:54:03 -0700951 size_t eSize = alloc->mHal.state.elementSizeBytes;
952 size_t lineSize = eSize * w;
Tim Murray358747a2012-11-26 13:52:04 -0800953 if (!stride) {
954 stride = lineSize;
955 }
Jason Sams807fdc42012-07-25 17:55:39 -0700956
Jason Sams709a0972012-11-15 18:18:04 -0800957 if (alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Sams807fdc42012-07-25 17:55:39 -0700958 uint8_t *dst = static_cast<uint8_t *>(data);
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700959 const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, 0, lod, face);
Stephen Hines20c535f2012-12-06 19:04:38 -0800960 if (dst == src) {
961 // Skip the copy if we are the same allocation. This can arise from
962 // our Bitmap optimization, where we share the same storage.
963 return;
964 }
Jason Sams807fdc42012-07-25 17:55:39 -0700965
966 for (uint32_t line=yoff; line < (yoff+h); line++) {
967 memcpy(dst, src, lineSize);
Tim Murray358747a2012-11-26 13:52:04 -0800968 dst += stride;
Jason Sams709a0972012-11-15 18:18:04 -0800969 src += alloc->mHal.drvState.lod[lod].stride;
Jason Sams807fdc42012-07-25 17:55:39 -0700970 }
971 } else {
972 ALOGE("Add code to readback from non-script memory");
973 }
974}
975
Tim Murray358747a2012-11-26 13:52:04 -0800976
Jason Sams807fdc42012-07-25 17:55:39 -0700977void rsdAllocationRead3D(const Context *rsc, const Allocation *alloc,
978 uint32_t xoff, uint32_t yoff, uint32_t zoff,
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700979 uint32_t lod,
980 uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride) {
981 uint32_t eSize = alloc->mHal.state.elementSizeBytes;
982 uint32_t lineSize = eSize * w;
983 if (!stride) {
984 stride = lineSize;
985 }
Jason Sams807fdc42012-07-25 17:55:39 -0700986
Jason Sams3bbc0fd2013-04-09 14:16:13 -0700987 if (alloc->mHal.drvState.lod[0].mallocPtr) {
988 uint8_t *dst = static_cast<uint8_t *>(data);
989 for (uint32_t z = zoff; z < d; z++) {
990 const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, z, lod,
991 RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
992 if (dst == src) {
993 // Skip the copy if we are the same allocation. This can arise from
994 // our Bitmap optimization, where we share the same storage.
995 return;
996 }
997
998 for (uint32_t line=yoff; line < (yoff+h); line++) {
999 memcpy(dst, src, lineSize);
1000 dst += stride;
1001 src += alloc->mHal.drvState.lod[lod].stride;
1002 }
1003 }
1004 }
Jason Sams807fdc42012-07-25 17:55:39 -07001005}
1006
1007void * rsdAllocationLock1D(const android::renderscript::Context *rsc,
1008 const android::renderscript::Allocation *alloc) {
Jason Sams709a0972012-11-15 18:18:04 -08001009 return alloc->mHal.drvState.lod[0].mallocPtr;
Jason Sams807fdc42012-07-25 17:55:39 -07001010}
1011
1012void rsdAllocationUnlock1D(const android::renderscript::Context *rsc,
1013 const android::renderscript::Allocation *alloc) {
1014
1015}
1016
Alex Sakhartchouk74a82792011-06-14 11:13:19 -07001017void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc,
1018 const android::renderscript::Allocation *dstAlloc,
Tim Murray099bc262013-03-20 16:54:03 -07001019 uint32_t dstXoff, uint32_t dstLod, size_t count,
Alex Sakhartchouk74a82792011-06-14 11:13:19 -07001020 const android::renderscript::Allocation *srcAlloc,
Alex Sakhartchouka9495242011-06-16 11:05:13 -07001021 uint32_t srcXoff, uint32_t srcLod) {
1022}
1023
Alex Sakhartchouka9495242011-06-16 11:05:13 -07001024
1025void rsdAllocationData2D_alloc_script(const android::renderscript::Context *rsc,
1026 const android::renderscript::Allocation *dstAlloc,
1027 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
1028 RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
1029 const android::renderscript::Allocation *srcAlloc,
1030 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
1031 RsAllocationCubemapFace srcFace) {
Tim Murray099bc262013-03-20 16:54:03 -07001032 size_t elementSize = dstAlloc->getType()->getElementSizeBytes();
Alex Sakhartchouka9495242011-06-16 11:05:13 -07001033 for (uint32_t i = 0; i < h; i ++) {
Jason Sams3bbc0fd2013-04-09 14:16:13 -07001034 uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, 0, dstLod, dstFace);
1035 uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, 0, srcLod, srcFace);
Alex Sakhartchouka9495242011-06-16 11:05:13 -07001036 memcpy(dstPtr, srcPtr, w * elementSize);
1037
Steve Blockaf12ac62012-01-06 19:20:56 +00001038 //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 -07001039 // dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace);
Alex Sakhartchouka9495242011-06-16 11:05:13 -07001040 }
Alex Sakhartchouk74a82792011-06-14 11:13:19 -07001041}
1042
Jason Sams3bbc0fd2013-04-09 14:16:13 -07001043void rsdAllocationData3D_alloc_script(const android::renderscript::Context *rsc,
1044 const android::renderscript::Allocation *dstAlloc,
1045 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff, uint32_t dstLod,
1046 uint32_t w, uint32_t h, uint32_t d,
1047 const android::renderscript::Allocation *srcAlloc,
1048 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff, uint32_t srcLod) {
1049 uint32_t elementSize = dstAlloc->getType()->getElementSizeBytes();
1050 for (uint32_t j = 0; j < d; j++) {
1051 for (uint32_t i = 0; i < h; i ++) {
1052 uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, dstZoff + j,
1053 dstLod, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1054 uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, srcZoff + j,
1055 srcLod, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
1056 memcpy(dstPtr, srcPtr, w * elementSize);
1057
1058 //ALOGE("COPIED dstXoff(%u), dstYoff(%u), dstLod(%u), dstFace(%u), w(%u), h(%u), srcXoff(%u), srcYoff(%u), srcLod(%u), srcFace(%u)",
1059 // dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace);
1060 }
1061 }
1062}
1063
Alex Sakhartchouk74a82792011-06-14 11:13:19 -07001064void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc,
1065 const android::renderscript::Allocation *dstAlloc,
1066 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
1067 RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
1068 const android::renderscript::Allocation *srcAlloc,
1069 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
1070 RsAllocationCubemapFace srcFace) {
Alex Sakhartchouka9495242011-06-16 11:05:13 -07001071 if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) {
1072 rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not "
1073 "yet implemented.");
1074 return;
1075 }
1076 rsdAllocationData2D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff,
1077 dstLod, dstFace, w, h, srcAlloc,
1078 srcXoff, srcYoff, srcLod, srcFace);
Alex Sakhartchouk74a82792011-06-14 11:13:19 -07001079}
1080
1081void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc,
1082 const android::renderscript::Allocation *dstAlloc,
1083 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
Jason Sams3bbc0fd2013-04-09 14:16:13 -07001084 uint32_t dstLod,
Alex Sakhartchouk74a82792011-06-14 11:13:19 -07001085 uint32_t w, uint32_t h, uint32_t d,
1086 const android::renderscript::Allocation *srcAlloc,
1087 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
Jason Sams3bbc0fd2013-04-09 14:16:13 -07001088 uint32_t srcLod) {
1089 if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) {
1090 rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not "
1091 "yet implemented.");
1092 return;
1093 }
1094 rsdAllocationData3D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff, dstZoff,
1095 dstLod, w, h, d, srcAlloc,
1096 srcXoff, srcYoff, srcZoff, srcLod);
Alex Sakhartchouk74a82792011-06-14 11:13:19 -07001097}
1098
Jason Samseb4fe182011-05-26 16:33:01 -07001099void rsdAllocationElementData1D(const Context *rsc, const Allocation *alloc,
1100 uint32_t x,
Tim Murray099bc262013-03-20 16:54:03 -07001101 const void *data, uint32_t cIdx, size_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -07001102 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
1103
Tim Murray099bc262013-03-20 16:54:03 -07001104 size_t eSize = alloc->mHal.state.elementSizeBytes;
Jason Sams3bbc0fd2013-04-09 14:16:13 -07001105 uint8_t * ptr = GetOffsetPtr(alloc, x, 0, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Jason Samseb4fe182011-05-26 16:33:01 -07001106
1107 const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
1108 ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
1109
1110 if (alloc->mHal.state.hasReferences) {
1111 e->incRefs(data);
1112 e->decRefs(ptr);
1113 }
1114
1115 memcpy(ptr, data, sizeBytes);
1116 drv->uploadDeferred = true;
1117}
1118
1119void rsdAllocationElementData2D(const Context *rsc, const Allocation *alloc,
1120 uint32_t x, uint32_t y,
Tim Murray099bc262013-03-20 16:54:03 -07001121 const void *data, uint32_t cIdx, size_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -07001122 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
1123
Tim Murray099bc262013-03-20 16:54:03 -07001124 size_t eSize = alloc->mHal.state.elementSizeBytes;
Jason Sams3bbc0fd2013-04-09 14:16:13 -07001125 uint8_t * ptr = GetOffsetPtr(alloc, x, y, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Jason Samseb4fe182011-05-26 16:33:01 -07001126
1127 const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
1128 ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
1129
1130 if (alloc->mHal.state.hasReferences) {
1131 e->incRefs(data);
1132 e->decRefs(ptr);
1133 }
1134
1135 memcpy(ptr, data, sizeBytes);
1136 drv->uploadDeferred = true;
1137}
1138
Jason Sams61a4bb72012-07-25 19:33:43 -07001139static void mip565(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -08001140 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1141 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
Jason Sams61a4bb72012-07-25 19:33:43 -07001142
1143 for (uint32_t y=0; y < h; y++) {
Jason Sams3bbc0fd2013-04-09 14:16:13 -07001144 uint16_t *oPtr = (uint16_t *)GetOffsetPtr(alloc, 0, y, 0, lod + 1, face);
1145 const uint16_t *i1 = (uint16_t *)GetOffsetPtr(alloc, 0, 0, y*2, lod, face);
1146 const uint16_t *i2 = (uint16_t *)GetOffsetPtr(alloc, 0, 0, y*2+1, lod, face);
Jason Sams61a4bb72012-07-25 19:33:43 -07001147
1148 for (uint32_t x=0; x < w; x++) {
1149 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
1150 oPtr ++;
1151 i1 += 2;
1152 i2 += 2;
1153 }
1154 }
1155}
1156
1157static void mip8888(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -08001158 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1159 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
Jason Sams61a4bb72012-07-25 19:33:43 -07001160
1161 for (uint32_t y=0; y < h; y++) {
Jason Sams3bbc0fd2013-04-09 14:16:13 -07001162 uint32_t *oPtr = (uint32_t *)GetOffsetPtr(alloc, 0, y, 0, lod + 1, face);
1163 const uint32_t *i1 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2, 0, lod, face);
1164 const uint32_t *i2 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2+1, 0, lod, face);
Jason Sams61a4bb72012-07-25 19:33:43 -07001165
1166 for (uint32_t x=0; x < w; x++) {
1167 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
1168 oPtr ++;
1169 i1 += 2;
1170 i2 += 2;
1171 }
1172 }
1173}
1174
1175static void mip8(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -08001176 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
1177 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
Jason Sams61a4bb72012-07-25 19:33:43 -07001178
1179 for (uint32_t y=0; y < h; y++) {
Jason Sams3bbc0fd2013-04-09 14:16:13 -07001180 uint8_t *oPtr = GetOffsetPtr(alloc, 0, y, 0, lod + 1, face);
1181 const uint8_t *i1 = GetOffsetPtr(alloc, 0, y*2, 0, lod, face);
1182 const uint8_t *i2 = GetOffsetPtr(alloc, 0, y*2+1, 0, lod, face);
Jason Sams61a4bb72012-07-25 19:33:43 -07001183
1184 for (uint32_t x=0; x < w; x++) {
1185 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
1186 oPtr ++;
1187 i1 += 2;
1188 i2 += 2;
1189 }
1190 }
1191}
1192
1193void rsdAllocationGenerateMipmaps(const Context *rsc, const Allocation *alloc) {
Jason Sams709a0972012-11-15 18:18:04 -08001194 if(!alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Sams61a4bb72012-07-25 19:33:43 -07001195 return;
1196 }
1197 uint32_t numFaces = alloc->getType()->getDimFaces() ? 6 : 1;
1198 for (uint32_t face = 0; face < numFaces; face ++) {
1199 for (uint32_t lod=0; lod < (alloc->getType()->getLODCount() -1); lod++) {
1200 switch (alloc->getType()->getElement()->getSizeBits()) {
1201 case 32:
1202 mip8888(alloc, lod, (RsAllocationCubemapFace)face);
1203 break;
1204 case 16:
1205 mip565(alloc, lod, (RsAllocationCubemapFace)face);
1206 break;
1207 case 8:
1208 mip8(alloc, lod, (RsAllocationCubemapFace)face);
1209 break;
1210 }
1211 }
1212 }
1213}