blob: b41880a240a209963ad87937a5e35efb86c1f29d [file] [log] [blame]
Jason Samseb4fe182011-05-26 16:33:01 -07001/*
Jason Sams3522f402012-03-23 11:47:26 -07002 * Copyright (C) 2011-2012 The Android Open Source Project
Jason Samseb4fe182011-05-26 16:33:01 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18#include "rsdCore.h"
Jason Samseb4fe182011-05-26 16:33:01 -070019#include "rsdAllocation.h"
Alex Sakhartchouka9495242011-06-16 11:05:13 -070020#include "rsdFrameBufferObj.h"
Jason Samseb4fe182011-05-26 16:33:01 -070021
22#include "rsAllocation.h"
23
Jason Sams7ac2a4d2012-02-15 12:04:24 -080024#include "system/window.h"
25#include "hardware/gralloc.h"
26#include "ui/Rect.h"
27#include "ui/GraphicBufferMapper.h"
Jason Sams3522f402012-03-23 11:47:26 -070028#include "gui/SurfaceTexture.h"
Jason Sams7ac2a4d2012-02-15 12:04:24 -080029
Jason Samseb4fe182011-05-26 16:33:01 -070030#include <GLES/gl.h>
31#include <GLES2/gl2.h>
32#include <GLES/glext.h>
33
34using namespace android;
35using namespace android::renderscript;
36
37
38
39const static GLenum gFaceOrder[] = {
40 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
41 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
42 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
43 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
44 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
45 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
46};
47
48
Jason Samsa614ae12011-05-26 17:05:51 -070049GLenum rsdTypeToGLType(RsDataType t) {
50 switch (t) {
51 case RS_TYPE_UNSIGNED_5_6_5: return GL_UNSIGNED_SHORT_5_6_5;
52 case RS_TYPE_UNSIGNED_5_5_5_1: return GL_UNSIGNED_SHORT_5_5_5_1;
53 case RS_TYPE_UNSIGNED_4_4_4_4: return GL_UNSIGNED_SHORT_4_4_4_4;
54
55 //case RS_TYPE_FLOAT_16: return GL_HALF_FLOAT;
56 case RS_TYPE_FLOAT_32: return GL_FLOAT;
57 case RS_TYPE_UNSIGNED_8: return GL_UNSIGNED_BYTE;
58 case RS_TYPE_UNSIGNED_16: return GL_UNSIGNED_SHORT;
59 case RS_TYPE_SIGNED_8: return GL_BYTE;
60 case RS_TYPE_SIGNED_16: return GL_SHORT;
61 default: break;
62 }
63 return 0;
64}
65
66GLenum rsdKindToGLFormat(RsDataKind k) {
67 switch (k) {
68 case RS_KIND_PIXEL_L: return GL_LUMINANCE;
69 case RS_KIND_PIXEL_A: return GL_ALPHA;
70 case RS_KIND_PIXEL_LA: return GL_LUMINANCE_ALPHA;
71 case RS_KIND_PIXEL_RGB: return GL_RGB;
72 case RS_KIND_PIXEL_RGBA: return GL_RGBA;
73 case RS_KIND_PIXEL_DEPTH: return GL_DEPTH_COMPONENT16;
74 default: break;
75 }
76 return 0;
77}
78
Jason Sams807fdc42012-07-25 17:55:39 -070079uint8_t *GetOffsetPtr(const android::renderscript::Allocation *alloc,
80 uint32_t xoff, uint32_t yoff, uint32_t lod,
81 RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -080082 uint8_t *ptr = (uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr;
83 ptr += face * alloc->mHal.drvState.faceOffset;
84 ptr += yoff * alloc->mHal.drvState.lod[lod].stride;
Jason Sams807fdc42012-07-25 17:55:39 -070085 ptr += xoff * alloc->mHal.state.elementSizeBytes;
86 return ptr;
87}
88
Jason Samsa614ae12011-05-26 17:05:51 -070089
Jason Sams2382aba2011-09-13 15:41:01 -070090static void Update2DTexture(const Context *rsc, const Allocation *alloc, const void *ptr,
91 uint32_t xoff, uint32_t yoff, uint32_t lod,
92 RsAllocationCubemapFace face, uint32_t w, uint32_t h) {
Jason Samseb4fe182011-05-26 16:33:01 -070093 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
94
Jason Samseb4fe182011-05-26 16:33:01 -070095 rsAssert(drv->textureID);
Jason Sams2382aba2011-09-13 15:41:01 -070096 RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID);
97 RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1);
Jason Samseb4fe182011-05-26 16:33:01 -070098 GLenum t = GL_TEXTURE_2D;
99 if (alloc->mHal.state.hasFaces) {
100 t = gFaceOrder[face];
101 }
Jason Sams2382aba2011-09-13 15:41:01 -0700102 RSD_CALL_GL(glTexSubImage2D, t, lod, xoff, yoff, w, h, drv->glFormat, drv->glType, ptr);
Jason Samseb4fe182011-05-26 16:33:01 -0700103}
104
105
106static void Upload2DTexture(const Context *rsc, const Allocation *alloc, bool isFirstUpload) {
107 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
108
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
112 uint32_t faceCount = 1;
113 if (alloc->mHal.state.hasFaces) {
114 faceCount = 6;
115 }
116
117 rsdGLCheckError(rsc, "Upload2DTexture 1 ");
118 for (uint32_t face = 0; face < faceCount; face ++) {
119 for (uint32_t lod = 0; lod < alloc->mHal.state.type->getLODCount(); lod++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700120 const uint8_t *p = GetOffsetPtr(alloc, 0, 0, lod, (RsAllocationCubemapFace)face);
Jason Samseb4fe182011-05-26 16:33:01 -0700121
122 GLenum t = GL_TEXTURE_2D;
123 if (alloc->mHal.state.hasFaces) {
124 t = gFaceOrder[face];
125 }
126
127 if (isFirstUpload) {
Jason Sams2382aba2011-09-13 15:41:01 -0700128 RSD_CALL_GL(glTexImage2D, t, lod, drv->glFormat,
Jason Samseb4fe182011-05-26 16:33:01 -0700129 alloc->mHal.state.type->getLODDimX(lod),
130 alloc->mHal.state.type->getLODDimY(lod),
Jason Samsa614ae12011-05-26 17:05:51 -0700131 0, drv->glFormat, drv->glType, p);
Jason Samseb4fe182011-05-26 16:33:01 -0700132 } else {
Jason Sams2382aba2011-09-13 15:41:01 -0700133 RSD_CALL_GL(glTexSubImage2D, t, lod, 0, 0,
Jason Samseb4fe182011-05-26 16:33:01 -0700134 alloc->mHal.state.type->getLODDimX(lod),
135 alloc->mHal.state.type->getLODDimY(lod),
Jason Samsa614ae12011-05-26 17:05:51 -0700136 drv->glFormat, drv->glType, p);
Jason Samseb4fe182011-05-26 16:33:01 -0700137 }
138 }
139 }
140
141 if (alloc->mHal.state.mipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) {
Jason Sams2382aba2011-09-13 15:41:01 -0700142 RSD_CALL_GL(glGenerateMipmap, drv->glTarget);
Jason Samseb4fe182011-05-26 16:33:01 -0700143 }
144 rsdGLCheckError(rsc, "Upload2DTexture");
145}
146
147static void UploadToTexture(const Context *rsc, const Allocation *alloc) {
148 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
149
Jason Sams3522f402012-03-23 11:47:26 -0700150 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) {
Jason Sams41e373d2012-01-13 14:01:20 -0800151 if (!drv->textureID) {
152 RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
153 }
154 return;
155 }
156
Jason Samsa614ae12011-05-26 17:05:51 -0700157 if (!drv->glType || !drv->glFormat) {
Jason Samseb4fe182011-05-26 16:33:01 -0700158 return;
159 }
160
Jason Sams709a0972012-11-15 18:18:04 -0800161 if (!alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Samseb4fe182011-05-26 16:33:01 -0700162 return;
163 }
164
165 bool isFirstUpload = false;
166
167 if (!drv->textureID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700168 RSD_CALL_GL(glGenTextures, 1, &drv->textureID);
Jason Samseb4fe182011-05-26 16:33:01 -0700169 isFirstUpload = true;
170 }
171
172 Upload2DTexture(rsc, alloc, isFirstUpload);
173
174 if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) {
Jason Sams709a0972012-11-15 18:18:04 -0800175 if (alloc->mHal.drvState.lod[0].mallocPtr) {
176 free(alloc->mHal.drvState.lod[0].mallocPtr);
177 alloc->mHal.drvState.lod[0].mallocPtr = NULL;
Jason Samseb4fe182011-05-26 16:33:01 -0700178 }
179 }
180 rsdGLCheckError(rsc, "UploadToTexture");
181}
182
183static void AllocateRenderTarget(const Context *rsc, const Allocation *alloc) {
184 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
185
Jason Samsa614ae12011-05-26 17:05:51 -0700186 if (!drv->glFormat) {
Jason Samseb4fe182011-05-26 16:33:01 -0700187 return;
188 }
189
190 if (!drv->renderTargetID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700191 RSD_CALL_GL(glGenRenderbuffers, 1, &drv->renderTargetID);
Jason Samseb4fe182011-05-26 16:33:01 -0700192
193 if (!drv->renderTargetID) {
194 // This should generally not happen
Steve Blockaf12ac62012-01-06 19:20:56 +0000195 ALOGE("allocateRenderTarget failed to gen mRenderTargetID");
Jason Samseb4fe182011-05-26 16:33:01 -0700196 rsc->dumpDebug();
197 return;
198 }
Jason Sams2382aba2011-09-13 15:41:01 -0700199 RSD_CALL_GL(glBindRenderbuffer, GL_RENDERBUFFER, drv->renderTargetID);
200 RSD_CALL_GL(glRenderbufferStorage, GL_RENDERBUFFER, drv->glFormat,
Jason Samseb4fe182011-05-26 16:33:01 -0700201 alloc->mHal.state.dimensionX, alloc->mHal.state.dimensionY);
202 }
203 rsdGLCheckError(rsc, "AllocateRenderTarget");
204}
205
206static void UploadToBufferObject(const Context *rsc, const Allocation *alloc) {
207 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
208
209 rsAssert(!alloc->mHal.state.type->getDimY());
210 rsAssert(!alloc->mHal.state.type->getDimZ());
211
212 //alloc->mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX;
213
214 if (!drv->bufferID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700215 RSD_CALL_GL(glGenBuffers, 1, &drv->bufferID);
Jason Samseb4fe182011-05-26 16:33:01 -0700216 }
217 if (!drv->bufferID) {
Steve Blockaf12ac62012-01-06 19:20:56 +0000218 ALOGE("Upload to buffer object failed");
Jason Samseb4fe182011-05-26 16:33:01 -0700219 drv->uploadDeferred = true;
220 return;
221 }
Jason Sams2382aba2011-09-13 15:41:01 -0700222 RSD_CALL_GL(glBindBuffer, drv->glTarget, drv->bufferID);
223 RSD_CALL_GL(glBufferData, drv->glTarget, alloc->mHal.state.type->getSizeBytes(),
Jason Sams709a0972012-11-15 18:18:04 -0800224 alloc->mHal.drvState.lod[0].mallocPtr, GL_DYNAMIC_DRAW);
Jason Sams2382aba2011-09-13 15:41:01 -0700225 RSD_CALL_GL(glBindBuffer, drv->glTarget, 0);
Jason Samseb4fe182011-05-26 16:33:01 -0700226 rsdGLCheckError(rsc, "UploadToBufferObject");
227}
228
Jason Sams463bfce2012-08-21 13:54:42 -0700229static size_t AllocationBuildPointerTable(const Context *rsc, const Allocation *alloc,
230 const Type *type, uint8_t *ptr) {
Jason Sams709a0972012-11-15 18:18:04 -0800231 alloc->mHal.drvState.lod[0].dimX = type->getDimX();
232 alloc->mHal.drvState.lod[0].dimY = type->getDimY();
233 alloc->mHal.drvState.lod[0].mallocPtr = 0;
234 alloc->mHal.drvState.lod[0].stride = alloc->mHal.drvState.lod[0].dimX * type->getElementSizeBytes();
235 alloc->mHal.drvState.lodCount = type->getLODCount();
236 alloc->mHal.drvState.faceCount = type->getDimFaces();
Jason Sams807fdc42012-07-25 17:55:39 -0700237
238 size_t offsets[Allocation::MAX_LOD];
239 memset(offsets, 0, sizeof(offsets));
240
Jason Sams709a0972012-11-15 18:18:04 -0800241 size_t o = alloc->mHal.drvState.lod[0].stride * rsMax(alloc->mHal.drvState.lod[0].dimY, 1u) *
242 rsMax(alloc->mHal.drvState.lod[0].dimZ, 1u);
243 if(alloc->mHal.drvState.lodCount > 1) {
244 uint32_t tx = alloc->mHal.drvState.lod[0].dimX;
245 uint32_t ty = alloc->mHal.drvState.lod[0].dimY;
246 uint32_t tz = alloc->mHal.drvState.lod[0].dimZ;
247 for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
248 alloc->mHal.drvState.lod[lod].dimX = tx;
249 alloc->mHal.drvState.lod[lod].dimY = ty;
250 alloc->mHal.drvState.lod[lod].dimZ = tz;
251 alloc->mHal.drvState.lod[lod].stride = tx * type->getElementSizeBytes();
Jason Sams807fdc42012-07-25 17:55:39 -0700252 offsets[lod] = o;
Jason Sams709a0972012-11-15 18:18:04 -0800253 o += alloc->mHal.drvState.lod[lod].stride * rsMax(ty, 1u) * rsMax(tz, 1u);
Jason Sams807fdc42012-07-25 17:55:39 -0700254 if (tx > 1) tx >>= 1;
255 if (ty > 1) ty >>= 1;
256 if (tz > 1) tz >>= 1;
257 }
258 }
Jason Sams709a0972012-11-15 18:18:04 -0800259 alloc->mHal.drvState.faceOffset = o;
Jason Sams807fdc42012-07-25 17:55:39 -0700260
Jason Sams709a0972012-11-15 18:18:04 -0800261 alloc->mHal.drvState.lod[0].mallocPtr = ptr;
262 for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) {
263 alloc->mHal.drvState.lod[lod].mallocPtr = ptr + offsets[lod];
Jason Sams463bfce2012-08-21 13:54:42 -0700264 }
Jason Sams463bfce2012-08-21 13:54:42 -0700265
Jason Sams709a0972012-11-15 18:18:04 -0800266 size_t allocSize = alloc->mHal.drvState.faceOffset;
267 if(alloc->mHal.drvState.faceCount) {
Jason Sams463bfce2012-08-21 13:54:42 -0700268 allocSize *= 6;
269 }
270
271 return allocSize;
272}
273
274bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) {
275 DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation));
276 if (!drv) {
277 return false;
278 }
279 alloc->mHal.drv = drv;
280
281 // Calculate the object size.
282 size_t allocSize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), NULL);
283
Jason Sams807fdc42012-07-25 17:55:39 -0700284 uint8_t * ptr = NULL;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800285 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) {
Tim Murray2e1a94d2012-11-29 13:12:25 -0800286 } else if (alloc->mHal.state.userProvidedPtr != NULL) {
287 // user-provided allocation
288 // limitations: no faces, no LOD, USAGE_SCRIPT only
Tim Murray9e2bda52012-12-18 12:05:46 -0800289 if (alloc->mHal.state.usageFlags != (RS_ALLOCATION_USAGE_SCRIPT | RS_ALLOCATION_USAGE_SHARED)) {
290 ALOGE("Can't use user-allocated buffers if usage is not USAGE_SCRIPT and USAGE_SHARED");
Tim Murray2e1a94d2012-11-29 13:12:25 -0800291 return false;
292 }
293 if (alloc->getType()->getDimLOD() || alloc->getType()->getDimFaces()) {
294 ALOGE("User-allocated buffers must not have multiple faces or LODs");
295 return false;
296 }
297 ptr = (uint8_t*)alloc->mHal.state.userProvidedPtr;
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800298 } else {
Tim Murray2e1a94d2012-11-29 13:12:25 -0800299 if (forceZero) {
300 ptr = (uint8_t *)calloc(1, allocSize);
301 } else {
302 ptr = (uint8_t *)malloc(allocSize);
303 }
Jason Sams179e9a42011-11-23 15:02:15 -0800304 if (!ptr) {
305 free(drv);
306 return false;
307 }
Jason Samseb4fe182011-05-26 16:33:01 -0700308 }
Jason Sams463bfce2012-08-21 13:54:42 -0700309 // Build the pointer tables
310 size_t verifySize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), ptr);
311 if(allocSize != verifySize) {
312 rsAssert(!"Size mismatch");
Jason Sams807fdc42012-07-25 17:55:39 -0700313 }
Jason Samseb4fe182011-05-26 16:33:01 -0700314
315 drv->glTarget = GL_NONE;
316 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
317 if (alloc->mHal.state.hasFaces) {
318 drv->glTarget = GL_TEXTURE_CUBE_MAP;
319 } else {
320 drv->glTarget = GL_TEXTURE_2D;
321 }
322 } else {
323 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
324 drv->glTarget = GL_ARRAY_BUFFER;
325 }
326 }
327
Jason Samsa614ae12011-05-26 17:05:51 -0700328 drv->glType = rsdTypeToGLType(alloc->mHal.state.type->getElement()->getComponent().getType());
329 drv->glFormat = rsdKindToGLFormat(alloc->mHal.state.type->getElement()->getComponent().getKind());
330
Jason Samseb4fe182011-05-26 16:33:01 -0700331 if (alloc->mHal.state.usageFlags & ~RS_ALLOCATION_USAGE_SCRIPT) {
332 drv->uploadDeferred = true;
333 }
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700334
Jason Samsb3220332012-04-02 14:41:54 -0700335
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700336 drv->readBackFBO = NULL;
337
Jason Samseb4fe182011-05-26 16:33:01 -0700338 return true;
339}
340
341void rsdAllocationDestroy(const Context *rsc, Allocation *alloc) {
342 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
343
344 if (drv->bufferID) {
345 // Causes a SW crash....
Steve Block65982012011-10-20 11:56:00 +0100346 //ALOGV(" mBufferID %i", mBufferID);
Jason Samseb4fe182011-05-26 16:33:01 -0700347 //glDeleteBuffers(1, &mBufferID);
348 //mBufferID = 0;
349 }
350 if (drv->textureID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700351 RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID);
Jason Samseb4fe182011-05-26 16:33:01 -0700352 drv->textureID = 0;
353 }
354 if (drv->renderTargetID) {
Jason Sams2382aba2011-09-13 15:41:01 -0700355 RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID);
Jason Samseb4fe182011-05-26 16:33:01 -0700356 drv->renderTargetID = 0;
357 }
358
Jason Sams709a0972012-11-15 18:18:04 -0800359 if (alloc->mHal.drvState.lod[0].mallocPtr) {
Tim Murray2e1a94d2012-11-29 13:12:25 -0800360 // don't free user-allocated ptrs
Tim Murray9e2bda52012-12-18 12:05:46 -0800361 if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED)) {
Tim Murray2e1a94d2012-11-29 13:12:25 -0800362 free(alloc->mHal.drvState.lod[0].mallocPtr);
363 }
Jason Sams709a0972012-11-15 18:18:04 -0800364 alloc->mHal.drvState.lod[0].mallocPtr = NULL;
Jason Samseb4fe182011-05-26 16:33:01 -0700365 }
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700366 if (drv->readBackFBO != NULL) {
367 delete drv->readBackFBO;
368 drv->readBackFBO = NULL;
369 }
Jason Samseb4fe182011-05-26 16:33:01 -0700370 free(drv);
371 alloc->mHal.drv = NULL;
372}
373
374void rsdAllocationResize(const Context *rsc, const Allocation *alloc,
375 const Type *newType, bool zeroNew) {
Tim Murray9e2bda52012-12-18 12:05:46 -0800376 // can't resize Allocations with user-allocated buffers
377 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SHARED) {
378 ALOGE("Resize cannot be called on a USAGE_SHARED allocation");
379 return;
380 }
Jason Sams709a0972012-11-15 18:18:04 -0800381 void * oldPtr = alloc->mHal.drvState.lod[0].mallocPtr;
Jason Sams463bfce2012-08-21 13:54:42 -0700382 // Calculate the object size
383 size_t s = AllocationBuildPointerTable(rsc, alloc, newType, NULL);
384 uint8_t *ptr = (uint8_t *)realloc(oldPtr, s);
385 // Build the relative pointer tables.
386 size_t verifySize = AllocationBuildPointerTable(rsc, alloc, newType, ptr);
387 if(s != verifySize) {
388 rsAssert(!"Size mismatch");
389 }
Jason Samseb4fe182011-05-26 16:33:01 -0700390
391 const uint32_t oldDimX = alloc->mHal.state.dimensionX;
392 const uint32_t dimX = newType->getDimX();
393
394 if (dimX > oldDimX) {
Jason Sams463bfce2012-08-21 13:54:42 -0700395 uint32_t stride = alloc->mHal.state.elementSizeBytes;
Jason Sams709a0972012-11-15 18:18:04 -0800396 memset(((uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr) + stride * oldDimX,
Jason Sams2275e9c2012-04-16 17:01:26 -0700397 0, stride * (dimX - oldDimX));
Jason Samseb4fe182011-05-26 16:33:01 -0700398 }
399}
400
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700401static void rsdAllocationSyncFromFBO(const Context *rsc, const Allocation *alloc) {
402 if (!alloc->getIsScript()) {
403 return; // nothing to sync
404 }
405
406 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
407 RsdFrameBufferObj *lastFbo = dc->gl.currentFrameBuffer;
408
409 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
410 if (!drv->textureID && !drv->renderTargetID) {
411 return; // nothing was rendered here yet, so nothing to sync
412 }
413 if (drv->readBackFBO == NULL) {
414 drv->readBackFBO = new RsdFrameBufferObj();
415 drv->readBackFBO->setColorTarget(drv, 0);
416 drv->readBackFBO->setDimensions(alloc->getType()->getDimX(),
417 alloc->getType()->getDimY());
418 }
419
420 // Bind the framebuffer object so we can read back from it
421 drv->readBackFBO->setActive(rsc);
422
423 // Do the readback
Jason Sams709a0972012-11-15 18:18:04 -0800424 RSD_CALL_GL(glReadPixels, 0, 0, alloc->mHal.drvState.lod[0].dimX,
425 alloc->mHal.drvState.lod[0].dimY,
426 drv->glFormat, drv->glType, alloc->mHal.drvState.lod[0].mallocPtr);
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700427
428 // Revert framebuffer to its original
429 lastFbo->setActive(rsc);
430}
Jason Samseb4fe182011-05-26 16:33:01 -0700431
432
433void rsdAllocationSyncAll(const Context *rsc, const Allocation *alloc,
434 RsAllocationUsageType src) {
435 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
436
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700437 if (src == RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
438 if(!alloc->getIsRenderTarget()) {
439 rsc->setError(RS_ERROR_FATAL_DRIVER,
440 "Attempting to sync allocation from render target, "
441 "for non-render target allocation");
442 } else if (alloc->getType()->getElement()->getKind() != RS_KIND_PIXEL_RGBA) {
443 rsc->setError(RS_ERROR_FATAL_DRIVER, "Cannot only sync from RGBA"
444 "render target");
445 } else {
446 rsdAllocationSyncFromFBO(rsc, alloc);
447 }
Jason Samseb4fe182011-05-26 16:33:01 -0700448 return;
449 }
450
451 rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT);
452
453 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) {
454 UploadToTexture(rsc, alloc);
455 } else {
Jason Samsb3220332012-04-02 14:41:54 -0700456 if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) &&
Jason Sams0dc66932012-04-10 17:05:49 -0700457 !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) {
Jason Samseb4fe182011-05-26 16:33:01 -0700458 AllocateRenderTarget(rsc, alloc);
459 }
460 }
461 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) {
462 UploadToBufferObject(rsc, alloc);
463 }
464
465 drv->uploadDeferred = false;
466}
467
468void rsdAllocationMarkDirty(const Context *rsc, const Allocation *alloc) {
469 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
470 drv->uploadDeferred = true;
471}
472
Jason Sams41e373d2012-01-13 14:01:20 -0800473int32_t rsdAllocationInitSurfaceTexture(const Context *rsc, const Allocation *alloc) {
474 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
475 UploadToTexture(rsc, alloc);
476 return drv->textureID;
477}
478
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800479static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
480 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
481
Jamie Genniscd919a12012-06-13 16:34:11 -0700482 int32_t r = native_window_dequeue_buffer_and_wait(nw, &drv->wndBuffer);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800483 if (r) {
484 rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer.");
485 return false;
486 }
487
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800488 // Must lock the whole surface
489 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
490 Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height);
491
492 void *dst = NULL;
493 mapper.lock(drv->wndBuffer->handle,
494 GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN,
495 bounds, &dst);
Jason Sams709a0972012-11-15 18:18:04 -0800496 alloc->mHal.drvState.lod[0].mallocPtr = dst;
497 alloc->mHal.drvState.lod[0].stride = drv->wndBuffer->stride * alloc->mHal.state.elementSizeBytes;
Jason Samsb3220332012-04-02 14:41:54 -0700498
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800499 return true;
500}
501
502void rsdAllocationSetSurfaceTexture(const Context *rsc, Allocation *alloc, ANativeWindow *nw) {
503 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
504
505 //ALOGE("rsdAllocationSetSurfaceTexture %p %p", alloc, nw);
506
Jason Samsb3220332012-04-02 14:41:54 -0700507 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
508 //TODO finish support for render target + script
509 drv->wnd = nw;
510 return;
511 }
512
513
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800514 // Cleanup old surface if there is one.
515 if (alloc->mHal.state.wndSurface) {
516 ANativeWindow *old = alloc->mHal.state.wndSurface;
517 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
518 mapper.unlock(drv->wndBuffer->handle);
Jamie Genniscd919a12012-06-13 16:34:11 -0700519 old->queueBuffer(old, drv->wndBuffer, -1);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800520 }
521
522 if (nw != NULL) {
523 int32_t r;
Jason Samsb3220332012-04-02 14:41:54 -0700524 uint32_t flags = 0;
525 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
526 flags |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN;
527 }
528 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
529 flags |= GRALLOC_USAGE_HW_RENDER;
530 }
531
532 r = native_window_set_usage(nw, flags);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800533 if (r) {
534 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage.");
535 return;
536 }
537
538 r = native_window_set_buffers_dimensions(nw, alloc->mHal.state.dimensionX,
539 alloc->mHal.state.dimensionY);
540 if (r) {
541 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions.");
542 return;
543 }
544
545 r = native_window_set_buffer_count(nw, 3);
546 if (r) {
547 rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer count.");
548 return;
549 }
550
551 IoGetBuffer(rsc, alloc, nw);
552 }
553}
554
555void rsdAllocationIoSend(const Context *rsc, Allocation *alloc) {
556 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
557 ANativeWindow *nw = alloc->mHal.state.wndSurface;
558
Jason Samsb3220332012-04-02 14:41:54 -0700559 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) {
560 RsdHal *dc = (RsdHal *)rsc->mHal.drv;
561 RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface);
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800562 return;
563 }
564
Jason Samsb3220332012-04-02 14:41:54 -0700565 if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) {
566 GraphicBufferMapper &mapper = GraphicBufferMapper::get();
567 mapper.unlock(drv->wndBuffer->handle);
Jamie Genniscd919a12012-06-13 16:34:11 -0700568 int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1);
Jason Samsb3220332012-04-02 14:41:54 -0700569 if (r) {
570 rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer.");
571 return;
572 }
573
574 IoGetBuffer(rsc, alloc, nw);
575 }
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800576}
577
578void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) {
Jason Sams3522f402012-03-23 11:47:26 -0700579 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
580 alloc->mHal.state.surfaceTexture->updateTexImage();
Jason Sams7ac2a4d2012-02-15 12:04:24 -0800581}
582
583
Jason Samseb4fe182011-05-26 16:33:01 -0700584void rsdAllocationData1D(const Context *rsc, const Allocation *alloc,
585 uint32_t xoff, uint32_t lod, uint32_t count,
Alex Sakhartchoukc794cd52012-02-13 11:57:32 -0800586 const void *data, size_t sizeBytes) {
Jason Samseb4fe182011-05-26 16:33:01 -0700587 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
588
589 const uint32_t eSize = alloc->mHal.state.type->getElementSizeBytes();
Jason Sams807fdc42012-07-25 17:55:39 -0700590 uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Jason Samseb4fe182011-05-26 16:33:01 -0700591 uint32_t size = count * eSize;
592
Stephen Hines20c535f2012-12-06 19:04:38 -0800593 if (ptr != data) {
594 // Skip the copy if we are the same allocation. This can arise from
595 // our Bitmap optimization, where we share the same storage.
596 if (alloc->mHal.state.hasReferences) {
597 alloc->incRefs(data, count);
598 alloc->decRefs(ptr, count);
599 }
600 memcpy(ptr, data, size);
Jason Samseb4fe182011-05-26 16:33:01 -0700601 }
Jason Samseb4fe182011-05-26 16:33:01 -0700602 drv->uploadDeferred = true;
603}
604
605void rsdAllocationData2D(const Context *rsc, const Allocation *alloc,
606 uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
Tim Murray358747a2012-11-26 13:52:04 -0800607 uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) {
Jason Samseb4fe182011-05-26 16:33:01 -0700608 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
609
610 uint32_t eSize = alloc->mHal.state.elementSizeBytes;
611 uint32_t lineSize = eSize * w;
Tim Murray358747a2012-11-26 13:52:04 -0800612 if (!stride) {
613 stride = lineSize;
614 }
Jason Samseb4fe182011-05-26 16:33:01 -0700615
Jason Sams709a0972012-11-15 18:18:04 -0800616 if (alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Samseb4fe182011-05-26 16:33:01 -0700617 const uint8_t *src = static_cast<const uint8_t *>(data);
Jason Sams807fdc42012-07-25 17:55:39 -0700618 uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, lod, face);
Stephen Hines20c535f2012-12-06 19:04:38 -0800619 if (dst == src) {
620 // Skip the copy if we are the same allocation. This can arise from
621 // our Bitmap optimization, where we share the same storage.
622 drv->uploadDeferred = true;
623 return;
624 }
Jason Samseb4fe182011-05-26 16:33:01 -0700625
626 for (uint32_t line=yoff; line < (yoff+h); line++) {
627 if (alloc->mHal.state.hasReferences) {
628 alloc->incRefs(src, w);
629 alloc->decRefs(dst, w);
630 }
631 memcpy(dst, src, lineSize);
Tim Murray358747a2012-11-26 13:52:04 -0800632 src += stride;
Jason Sams709a0972012-11-15 18:18:04 -0800633 dst += alloc->mHal.drvState.lod[lod].stride;
Jason Samseb4fe182011-05-26 16:33:01 -0700634 }
635 drv->uploadDeferred = true;
636 } else {
Jason Sams2382aba2011-09-13 15:41:01 -0700637 Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h);
Jason Samseb4fe182011-05-26 16:33:01 -0700638 }
639}
640
641void rsdAllocationData3D(const Context *rsc, const Allocation *alloc,
642 uint32_t xoff, uint32_t yoff, uint32_t zoff,
643 uint32_t lod, RsAllocationCubemapFace face,
644 uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) {
645
646}
647
Jason Sams807fdc42012-07-25 17:55:39 -0700648void rsdAllocationRead1D(const Context *rsc, const Allocation *alloc,
649 uint32_t xoff, uint32_t lod, uint32_t count,
650 void *data, size_t sizeBytes) {
Jason Sams807fdc42012-07-25 17:55:39 -0700651 const uint32_t eSize = alloc->mHal.state.type->getElementSizeBytes();
652 const uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Stephen Hines20c535f2012-12-06 19:04:38 -0800653 if (data != ptr) {
654 // Skip the copy if we are the same allocation. This can arise from
655 // our Bitmap optimization, where we share the same storage.
656 memcpy(data, ptr, count * eSize);
657 }
Jason Sams807fdc42012-07-25 17:55:39 -0700658}
659
660void rsdAllocationRead2D(const Context *rsc, const Allocation *alloc,
Tim Murray358747a2012-11-26 13:52:04 -0800661 uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
662 uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) {
Jason Sams807fdc42012-07-25 17:55:39 -0700663 uint32_t eSize = alloc->mHal.state.elementSizeBytes;
664 uint32_t lineSize = eSize * w;
Tim Murray358747a2012-11-26 13:52:04 -0800665 if (!stride) {
666 stride = lineSize;
667 }
Jason Sams807fdc42012-07-25 17:55:39 -0700668
Jason Sams709a0972012-11-15 18:18:04 -0800669 if (alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Sams807fdc42012-07-25 17:55:39 -0700670 uint8_t *dst = static_cast<uint8_t *>(data);
671 const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, lod, face);
Stephen Hines20c535f2012-12-06 19:04:38 -0800672 if (dst == src) {
673 // Skip the copy if we are the same allocation. This can arise from
674 // our Bitmap optimization, where we share the same storage.
675 return;
676 }
Jason Sams807fdc42012-07-25 17:55:39 -0700677
678 for (uint32_t line=yoff; line < (yoff+h); line++) {
679 memcpy(dst, src, lineSize);
Tim Murray358747a2012-11-26 13:52:04 -0800680 dst += stride;
Jason Sams709a0972012-11-15 18:18:04 -0800681 src += alloc->mHal.drvState.lod[lod].stride;
Jason Sams807fdc42012-07-25 17:55:39 -0700682 }
683 } else {
684 ALOGE("Add code to readback from non-script memory");
685 }
686}
687
Tim Murray358747a2012-11-26 13:52:04 -0800688
Jason Sams807fdc42012-07-25 17:55:39 -0700689void rsdAllocationRead3D(const Context *rsc, const Allocation *alloc,
690 uint32_t xoff, uint32_t yoff, uint32_t zoff,
691 uint32_t lod, RsAllocationCubemapFace face,
692 uint32_t w, uint32_t h, uint32_t d, void *data, uint32_t sizeBytes) {
693
694}
695
696void * rsdAllocationLock1D(const android::renderscript::Context *rsc,
697 const android::renderscript::Allocation *alloc) {
Jason Sams709a0972012-11-15 18:18:04 -0800698 return alloc->mHal.drvState.lod[0].mallocPtr;
Jason Sams807fdc42012-07-25 17:55:39 -0700699}
700
701void rsdAllocationUnlock1D(const android::renderscript::Context *rsc,
702 const android::renderscript::Allocation *alloc) {
703
704}
705
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700706void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc,
707 const android::renderscript::Allocation *dstAlloc,
708 uint32_t dstXoff, uint32_t dstLod, uint32_t count,
709 const android::renderscript::Allocation *srcAlloc,
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700710 uint32_t srcXoff, uint32_t srcLod) {
711}
712
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700713
714void rsdAllocationData2D_alloc_script(const android::renderscript::Context *rsc,
715 const android::renderscript::Allocation *dstAlloc,
716 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
717 RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
718 const android::renderscript::Allocation *srcAlloc,
719 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
720 RsAllocationCubemapFace srcFace) {
721 uint32_t elementSize = dstAlloc->getType()->getElementSizeBytes();
722 for (uint32_t i = 0; i < h; i ++) {
Jason Sams807fdc42012-07-25 17:55:39 -0700723 uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, dstLod, dstFace);
724 uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, srcLod, srcFace);
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700725 memcpy(dstPtr, srcPtr, w * elementSize);
726
Steve Blockaf12ac62012-01-06 19:20:56 +0000727 //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 -0700728 // dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace);
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700729 }
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700730}
731
732void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc,
733 const android::renderscript::Allocation *dstAlloc,
734 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod,
735 RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h,
736 const android::renderscript::Allocation *srcAlloc,
737 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod,
738 RsAllocationCubemapFace srcFace) {
Alex Sakhartchouka9495242011-06-16 11:05:13 -0700739 if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) {
740 rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not "
741 "yet implemented.");
742 return;
743 }
744 rsdAllocationData2D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff,
745 dstLod, dstFace, w, h, srcAlloc,
746 srcXoff, srcYoff, srcLod, srcFace);
Alex Sakhartchouk74a82792011-06-14 11:13:19 -0700747}
748
749void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc,
750 const android::renderscript::Allocation *dstAlloc,
751 uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff,
752 uint32_t dstLod, RsAllocationCubemapFace dstFace,
753 uint32_t w, uint32_t h, uint32_t d,
754 const android::renderscript::Allocation *srcAlloc,
755 uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff,
756 uint32_t srcLod, RsAllocationCubemapFace srcFace) {
757}
758
Jason Samseb4fe182011-05-26 16:33:01 -0700759void rsdAllocationElementData1D(const Context *rsc, const Allocation *alloc,
760 uint32_t x,
761 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
762 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
763
764 uint32_t eSize = alloc->mHal.state.elementSizeBytes;
Jason Sams807fdc42012-07-25 17:55:39 -0700765 uint8_t * ptr = GetOffsetPtr(alloc, x, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Jason Samseb4fe182011-05-26 16:33:01 -0700766
767 const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
768 ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
769
770 if (alloc->mHal.state.hasReferences) {
771 e->incRefs(data);
772 e->decRefs(ptr);
773 }
774
775 memcpy(ptr, data, sizeBytes);
776 drv->uploadDeferred = true;
777}
778
779void rsdAllocationElementData2D(const Context *rsc, const Allocation *alloc,
780 uint32_t x, uint32_t y,
781 const void *data, uint32_t cIdx, uint32_t sizeBytes) {
782 DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv;
783
784 uint32_t eSize = alloc->mHal.state.elementSizeBytes;
Jason Sams807fdc42012-07-25 17:55:39 -0700785 uint8_t * ptr = GetOffsetPtr(alloc, x, y, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X);
Jason Samseb4fe182011-05-26 16:33:01 -0700786
787 const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx);
788 ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx);
789
790 if (alloc->mHal.state.hasReferences) {
791 e->incRefs(data);
792 e->decRefs(ptr);
793 }
794
795 memcpy(ptr, data, sizeBytes);
796 drv->uploadDeferred = true;
797}
798
Jason Sams61a4bb72012-07-25 19:33:43 -0700799static void mip565(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -0800800 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
801 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
Jason Sams61a4bb72012-07-25 19:33:43 -0700802
803 for (uint32_t y=0; y < h; y++) {
804 uint16_t *oPtr = (uint16_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face);
805 const uint16_t *i1 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2, lod, face);
806 const uint16_t *i2 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face);
807
808 for (uint32_t x=0; x < w; x++) {
809 *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]);
810 oPtr ++;
811 i1 += 2;
812 i2 += 2;
813 }
814 }
815}
816
817static void mip8888(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -0800818 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
819 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
Jason Sams61a4bb72012-07-25 19:33:43 -0700820
821 for (uint32_t y=0; y < h; y++) {
822 uint32_t *oPtr = (uint32_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face);
823 const uint32_t *i1 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2, lod, face);
824 const uint32_t *i2 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face);
825
826 for (uint32_t x=0; x < w; x++) {
827 *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]);
828 oPtr ++;
829 i1 += 2;
830 i2 += 2;
831 }
832 }
833}
834
835static void mip8(const Allocation *alloc, int lod, RsAllocationCubemapFace face) {
Jason Sams709a0972012-11-15 18:18:04 -0800836 uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX;
837 uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY;
Jason Sams61a4bb72012-07-25 19:33:43 -0700838
839 for (uint32_t y=0; y < h; y++) {
840 uint8_t *oPtr = GetOffsetPtr(alloc, 0, y, lod + 1, face);
841 const uint8_t *i1 = GetOffsetPtr(alloc, 0, y*2, lod, face);
842 const uint8_t *i2 = GetOffsetPtr(alloc, 0, y*2+1, lod, face);
843
844 for (uint32_t x=0; x < w; x++) {
845 *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f);
846 oPtr ++;
847 i1 += 2;
848 i2 += 2;
849 }
850 }
851}
852
853void rsdAllocationGenerateMipmaps(const Context *rsc, const Allocation *alloc) {
Jason Sams709a0972012-11-15 18:18:04 -0800854 if(!alloc->mHal.drvState.lod[0].mallocPtr) {
Jason Sams61a4bb72012-07-25 19:33:43 -0700855 return;
856 }
857 uint32_t numFaces = alloc->getType()->getDimFaces() ? 6 : 1;
858 for (uint32_t face = 0; face < numFaces; face ++) {
859 for (uint32_t lod=0; lod < (alloc->getType()->getLODCount() -1); lod++) {
860 switch (alloc->getType()->getElement()->getSizeBits()) {
861 case 32:
862 mip8888(alloc, lod, (RsAllocationCubemapFace)face);
863 break;
864 case 16:
865 mip565(alloc, lod, (RsAllocationCubemapFace)face);
866 break;
867 case 8:
868 mip8(alloc, lod, (RsAllocationCubemapFace)face);
869 break;
870 }
871 }
872 }
873}
874
Jason Samseb4fe182011-05-26 16:33:01 -0700875